annotate libinterp/corefcn/oct-stream.cc @ 21552:102b33b53ea4

textscan: Use correct delimiters for file buffering (bug #47539) * oct-stream.cc (textscan::do_scan): Pass only value of delimiters or whitespace, not both.
author Lachlan Andrew <lachlanbis@gmail.com>
date Mon, 28 Mar 2016 20:00:37 +1100
parents 990c6c31a684
children 6c2fd62db1f7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1 /*
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
2
19697
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 19346
diff changeset
3 Copyright (C) 1996-2015 John W. Eaton
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
4 Copyright (C) 2015-2016 Lachlan Andrew, Monash University
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6 This file is part of Octave.
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
8 Octave is free software; you can redistribute it and/or modify it
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
9 under the terms of the GNU General Public License as published by the
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6970
diff changeset
10 Free Software Foundation; either version 3 of the License, or (at your
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6970
diff changeset
11 option) any later version.
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
12
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
13 Octave is distributed in the hope that it will be useful, but WITHOUT
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
16 for more details.
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
17
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6970
diff changeset
19 along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6970
diff changeset
20 <http://www.gnu.org/licenses/>.
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
21
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
22 */
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
23
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
24 #ifdef HAVE_CONFIG_H
21301
40de9f8f23a6 Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents: 21297
diff changeset
25 # include "config.h"
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
26 #endif
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
27
3268
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
28 #include <cassert>
7709
fa41af732801 octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents: 7538
diff changeset
29 #include <cctype>
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
30 #include <cstring>
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
31
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
32 #include <deque>
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
33 #include <fstream>
3503
d14c483b3c12 [project @ 2000-02-01 04:06:07 by jwe]
jwe
parents: 3491
diff changeset
34 #include <iomanip>
9202
4b2147b25e8d clean up Array instantiation mess in oct-stream.cc
Jaroslav Hajek <highegg@gmail.com>
parents: 9017
diff changeset
35 #include <iostream>
5765
7ba9ad1fec11 [project @ 2006-04-17 05:05:15 by jwe]
jwe
parents: 5760
diff changeset
36 #include <sstream>
3535
c5ebcd5d25a9 [project @ 2000-02-02 11:55:49 by jwe]
jwe
parents: 3534
diff changeset
37 #include <string>
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
38
19269
65554f5847ac don't include oct-locbuf.h in header files unnecessarily
John W. Eaton <jwe@octave.org>
parents: 19123
diff changeset
39 #include "Array.h"
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
40 #include "Cell.h"
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
41 #include "byte-swap.h"
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
42 #include "lo-ieee.h"
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
43 #include "lo-mappers.h"
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
44 #include "lo-utils.h"
19269
65554f5847ac don't include oct-locbuf.h in header files unnecessarily
John W. Eaton <jwe@octave.org>
parents: 19123
diff changeset
45 #include "oct-locbuf.h"
13983
7dd7cccf0757 clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents: 13271
diff changeset
46 #include "quit.h"
7dd7cccf0757 clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents: 13271
diff changeset
47 #include "singleton-cleanup.h"
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
48 #include "str-vec.h"
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
49
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
50 #include "error.h"
21100
e39e05d90788 Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents: 21040
diff changeset
51 #include "errwarn.h"
3342
d8d3700fb4ab [project @ 1999-11-05 16:55:17 by jwe]
jwe
parents: 3341
diff changeset
52 #include "input.h"
3775
13905c3a24af [project @ 2001-02-06 01:56:59 by jwe]
jwe
parents: 3716
diff changeset
53 #include "oct-stdstrm.h"
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
54 #include "oct-stream.h"
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
55 #include "ov.h"
20940
48b2ad5ee801 maint: Rename oct-obj.[cc|h] to ovl.[cc|h] for clarity.
Rik <rik@octave.org>
parents: 20938
diff changeset
56 #include "ovl.h"
20756
200ae1d650b7 propagate octave_execution_exception objects through try/catch blocks
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
57 #include "toplev.h"
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
58 #include "utils.h"
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
59
20938
aac911d8847b choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents: 20931
diff changeset
60 // Programming Note: There are two very different error functions used
aac911d8847b choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents: 20931
diff changeset
61 // in the stream code. When invoked with "error (...)" the member
aac911d8847b choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents: 20931
diff changeset
62 // function from octave_stream or octave_base_stream is called. This
aac911d8847b choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents: 20931
diff changeset
63 // function sets the error state on the stream AND returns control to
aac911d8847b choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents: 20931
diff changeset
64 // the caller. The caller must then return a value at the end of the
aac911d8847b choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents: 20931
diff changeset
65 // function. When invoked with "::error (...)" the exception-based
aac911d8847b choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents: 20931
diff changeset
66 // error function from error.h is used. This function will throw an
aac911d8847b choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents: 20931
diff changeset
67 // exception and not return control to the caller. BE CAREFUL and
aac911d8847b choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents: 20931
diff changeset
68 // invoke the correct error function!
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
69
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
70 // Possible values for conv_err:
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
71 //
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
72 // 1 : not a real scalar
2902
c5b7a019b9ed [project @ 1997-04-30 03:46:53 by jwe]
jwe
parents: 2877
diff changeset
73 // 2 : value is NaN
c5b7a019b9ed [project @ 1997-04-30 03:46:53 by jwe]
jwe
parents: 2877
diff changeset
74 // 3 : value is not an integer
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
75
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
76 static int
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
77 convert_to_valid_int (const octave_value& tc, int& conv_err)
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
78 {
20747
ee2743bd07a8 eliminate various compiler warnings
John W. Eaton <jwe@octave.org>
parents: 20741
diff changeset
79 conv_err = 0;
ee2743bd07a8 eliminate various compiler warnings
John W. Eaton <jwe@octave.org>
parents: 20741
diff changeset
80
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
81 int retval = 0;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
82
20747
ee2743bd07a8 eliminate various compiler warnings
John W. Eaton <jwe@octave.org>
parents: 20741
diff changeset
83 double dval = 0.0;
20741
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
84
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
85 try
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
86 {
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
87 dval = tc.double_value ();
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
88 }
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
89 catch (const octave_execution_exception&)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
90 {
20756
200ae1d650b7 propagate octave_execution_exception objects through try/catch blocks
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
91 recover_from_exception ();
200ae1d650b7 propagate octave_execution_exception objects through try/catch blocks
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
92
20741
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
93 conv_err = 1;
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
94 }
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
95
20747
ee2743bd07a8 eliminate various compiler warnings
John W. Eaton <jwe@octave.org>
parents: 20741
diff changeset
96 if (! conv_err)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
97 {
5389
25c8956d2204 [project @ 2005-06-15 03:45:46 by jwe]
jwe
parents: 5353
diff changeset
98 if (! lo_ieee_isnan (dval))
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
99 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
100 int ival = NINT (dval);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
101
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
102 if (ival == dval)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
103 retval = ival;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
104 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
105 conv_err = 3;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
106 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
107 else
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
108 conv_err = 2;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
109 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
110
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
111 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
112 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
113
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
114 static int
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
115 get_size (double d, const std::string& who)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
116 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
117 int retval = -1;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
118
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
119 if (lo_ieee_isnan (d))
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
120 ::error ("%s: NaN is invalid as size specification", who.c_str ());
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
121
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
122 if (xisinf (d))
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
123 retval = -1;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
124 else
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
125 {
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
126 if (d < 0.0)
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
127 ::error ("%s: negative value invalid as size specification",
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
128 who.c_str ());
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
129
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
130 retval = NINT (d);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
131 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
132
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
133 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
134 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
135
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
136 static void
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
137 get_size (const Array<double>& size, octave_idx_type& nr, octave_idx_type& nc,
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
138 bool& one_elt_size_spec, const std::string& who)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
139 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
140 nr = -1;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
141 nc = -1;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
142
3268
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
143 one_elt_size_spec = false;
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
144
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
145 double dnr = -1.0;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
146 double dnc = -1.0;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
147
20232
a9574e3c6e9e Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents: 20230
diff changeset
148 octave_idx_type sz_len = size.numel ();
3810
f19f00339363 [project @ 2001-03-29 21:03:01 by jwe]
jwe
parents: 3779
diff changeset
149
f19f00339363 [project @ 2001-03-29 21:03:01 by jwe]
jwe
parents: 3779
diff changeset
150 if (sz_len == 1)
2601
3723512a827a [project @ 1997-01-06 05:43:16 by jwe]
jwe
parents: 2600
diff changeset
151 {
3268
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
152 one_elt_size_spec = true;
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
153
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
154 dnr = size(0);
4293
977f977fb2c3 [project @ 2003-01-06 18:18:14 by jwe]
jwe
parents: 4257
diff changeset
155
977f977fb2c3 [project @ 2003-01-06 18:18:14 by jwe]
jwe
parents: 4257
diff changeset
156 dnc = (dnr == 0.0) ? 0.0 : 1.0;
2601
3723512a827a [project @ 1997-01-06 05:43:16 by jwe]
jwe
parents: 2600
diff changeset
157 }
3810
f19f00339363 [project @ 2001-03-29 21:03:01 by jwe]
jwe
parents: 3779
diff changeset
158 else if (sz_len == 2)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
159 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
160 dnr = size(0);
3810
f19f00339363 [project @ 2001-03-29 21:03:01 by jwe]
jwe
parents: 3779
diff changeset
161
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
162 if (xisinf (dnr))
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
163 ::error ("%s: invalid size specification", who.c_str ());
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
164
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
165 dnc = size(1);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
166 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
167 else
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
168 ::error ("%s: invalid size specification", who.c_str ());
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
169
20558
1a0a433c8263 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20465
diff changeset
170 nr = get_size (dnr, who);
1a0a433c8263 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20465
diff changeset
171
1a0a433c8263 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20465
diff changeset
172 if (dnc >= 0.0)
1a0a433c8263 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20465
diff changeset
173 nc = get_size (dnc, who);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
174 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
175
21477
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
176 class
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
177 scanf_format_elt
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
178 {
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
179 public:
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
180
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
181 enum special_conversion
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
182 {
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
183 whitespace_conversion = 1,
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
184 literal_conversion = 2,
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
185 null = 3
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
186 };
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
187
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
188 scanf_format_elt (const char *txt = 0, int w = 0, bool d = false,
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
189 char typ = '\0', char mod = '\0',
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
190 const std::string& ch_class = "")
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
191 : text (strsave (txt)), width (w), discard (d), type (typ),
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
192 modifier (mod), char_class (ch_class)
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
193 { }
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
194
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
195 scanf_format_elt (const scanf_format_elt& e)
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
196 : text (strsave (e.text)), width (e.width), discard (e.discard),
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
197 type (e.type), modifier (e.modifier), char_class (e.char_class)
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
198 { }
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
199
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
200 scanf_format_elt& operator = (const scanf_format_elt& e)
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
201 {
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
202 if (this != &e)
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
203 {
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
204 text = strsave (e.text);
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
205 width = e.width;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
206 discard = e.discard;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
207 type = e.type;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
208 modifier = e.modifier;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
209 char_class = e.char_class;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
210 }
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
211
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
212 return *this;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
213 }
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
214
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
215 ~scanf_format_elt (void) { delete [] text; }
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
216
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
217 // The C-style format string.
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
218 const char *text;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
219
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
220 // The maximum field width.
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
221 int width;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
222
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
223 // TRUE if we are not storing the result of this conversion.
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
224 bool discard;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
225
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
226 // Type of conversion -- 'd', 'i', 'o', 'u', 'x', 'e', 'f', 'g',
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
227 // 'c', 's', 'p', '%', or '['.
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
228 char type;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
229
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
230 // A length modifier -- 'h', 'l', or 'L'.
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
231 char modifier;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
232
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
233 // The class of characters in a '[' format.
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
234 std::string char_class;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
235 };
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
236
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
237 class
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
238 scanf_format_list
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
239 {
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
240 public:
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
241
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
242 scanf_format_list (const std::string& fmt = "");
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
243
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
244 ~scanf_format_list (void);
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
245
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
246 octave_idx_type num_conversions (void) { return nconv; }
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
247
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
248 // The length can be different than the number of conversions.
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
249 // For example, "x %d y %d z" has 2 conversions but the length of
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
250 // the list is 3 because of the characters that appear after the
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
251 // last conversion.
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
252
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
253 size_t length (void) const { return fmt_elts.size (); }
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
254
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
255 const scanf_format_elt *first (void)
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
256 {
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
257 curr_idx = 0;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
258 return current ();
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
259 }
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
260
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
261 const scanf_format_elt *current (void) const
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
262 {
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
263 return length () > 0 ? fmt_elts[curr_idx] : 0;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
264 }
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
265
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
266 const scanf_format_elt *next (bool cycle = true)
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
267 {
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
268 static scanf_format_elt dummy
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
269 (0, 0, false, scanf_format_elt::null, '\0', "");
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
270
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
271 curr_idx++;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
272
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
273 if (curr_idx >= length ())
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
274 {
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
275 if (cycle)
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
276 curr_idx = 0;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
277 else
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
278 return &dummy;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
279 }
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
280
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
281 return current ();
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
282 }
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
283
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
284 void printme (void) const;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
285
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
286 bool ok (void) const { return (nconv >= 0); }
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
287
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
288 operator bool () const { return ok (); }
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
289
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
290 bool all_character_conversions (void);
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
291
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
292 bool all_numeric_conversions (void);
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
293
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
294 private:
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
295
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
296 // Number of conversions specified by this format string, or -1 if
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
297 // invalid conversions have been found.
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
298 octave_idx_type nconv;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
299
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
300 // Index to current element;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
301 size_t curr_idx;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
302
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
303 // List of format elements.
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
304 std::deque<scanf_format_elt*> fmt_elts;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
305
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
306 // Temporary buffer.
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
307 std::ostringstream buf;
21477
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
308
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
309 void add_elt_to_list (int width, bool discard, char type, char modifier,
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
310 const std::string& char_class = "");
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
311
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
312 void process_conversion (const std::string& s, size_t& i, size_t n,
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
313 int& width, bool& discard, char& type,
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
314 char& modifier);
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
315
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
316 int finish_conversion (const std::string& s, size_t& i, size_t n,
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
317 int& width, bool discard, char& type,
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
318 char modifier);
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
319 // No copying!
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
320
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
321 scanf_format_list (const scanf_format_list&);
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
322
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
323 scanf_format_list& operator = (const scanf_format_list&);
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
324 };
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
325
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
326 scanf_format_list::scanf_format_list (const std::string& s)
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
327 : nconv (0), curr_idx (0), fmt_elts (), buf ()
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
328 {
10187
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
329 size_t n = s.length ();
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
330
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
331 size_t i = 0;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
332
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
333 int width = 0;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
334 bool discard = false;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
335 char modifier = '\0';
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
336 char type = '\0';
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
337
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
338 bool have_more = true;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
339
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
340 while (i < n)
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
341 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
342 have_more = true;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
343
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
344 if (s[i] == '%')
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
345 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
346 // Process percent-escape conversion type.
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
347
21475
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
348 process_conversion (s, i, n, width, discard, type, modifier);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
349
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
350 have_more = (buf.tellp () != 0);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
351 }
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
352 else if (isspace (s[i]))
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
353 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
354 type = scanf_format_elt::whitespace_conversion;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
355
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
356 width = 0;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
357 discard = false;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
358 modifier = '\0';
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
359 buf << " ";
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
360
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
361 while (++i < n && isspace (s[i]))
20946
6eff66fb8a02 style fixes for comments
John W. Eaton <jwe@octave.org>
parents: 20940
diff changeset
362 ; // skip whitespace
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
363
21475
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
364 add_elt_to_list (width, discard, type, modifier);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
365
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
366 have_more = false;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
367 }
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
368 else
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
369 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
370 type = scanf_format_elt::literal_conversion;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
371
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
372 width = 0;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
373 discard = false;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
374 modifier = '\0';
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
375
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
376 while (i < n && ! isspace (s[i]) && s[i] != '%')
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
377 buf << s[i++];
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
378
21475
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
379 add_elt_to_list (width, discard, type, modifier);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
380
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
381 have_more = false;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
382 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
383
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
384 if (nconv < 0)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
385 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
386 have_more = false;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
387 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
388 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
389 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
390
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
391 if (have_more)
21475
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
392 add_elt_to_list (width, discard, type, modifier);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
393
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
394 buf.clear ();
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
395 buf.str ("");
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
396 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
397
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
398 scanf_format_list::~scanf_format_list (void)
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
399 {
21475
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
400 size_t n = fmt_elts.size ();
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
401
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
402 for (size_t i = 0; i < n; i++)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
403 {
21475
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
404 scanf_format_elt *elt = fmt_elts[i];
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
405 delete elt;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
406 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
407 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
408
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
409 void
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
410 scanf_format_list::add_elt_to_list (int width, bool discard, char type,
21475
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
411 char modifier,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
412 const std::string& char_class)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
413 {
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
414 std::string text = buf.str ();
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
415
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
416 if (! text.empty ())
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
417 {
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
418 scanf_format_elt *elt
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
419 = new scanf_format_elt (text.c_str (), width, discard, type,
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
420 modifier, char_class);
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
421
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
422 fmt_elts.push_back (elt);
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
423 }
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
424
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
425 buf.clear ();
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
426 buf.str ("");
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
427 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
428
3535
c5ebcd5d25a9 [project @ 2000-02-02 11:55:49 by jwe]
jwe
parents: 3534
diff changeset
429 static std::string
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
430 expand_char_class (const std::string& s)
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
431 {
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
432 std::string retval;
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
433
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
434 size_t len = s.length ();
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
435
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
436 size_t i = 0;
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
437
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
438 while (i < len)
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
439 {
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
440 unsigned char c = s[i++];
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
441
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
442 if (c == '-' && i > 1 && i < len
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
443 && ( static_cast<unsigned char> (s[i-2])
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
444 <= static_cast<unsigned char> (s[i])))
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
445 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
446 // Add all characters from the range except the first (we
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
447 // already added it below).
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
448
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
449 for (c = s[i-2]+1; c < s[i]; c++)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
450 retval += c;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
451 }
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
452 else
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
453 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
454 // Add the character to the class. Only add '-' if it is
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
455 // the last character in the class.
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
456
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
457 if (c != '-' || i == len)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
458 retval += c;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
459 }
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
460 }
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
461
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
462 return retval;
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
463 }
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
464
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
465 void
10187
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
466 scanf_format_list::process_conversion (const std::string& s, size_t& i,
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
467 size_t n, int& width, bool& discard,
21475
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
468 char& type, char& modifier)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
469 {
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
470 width = 0;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
471 discard = false;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
472 modifier = '\0';
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
473 type = '\0';
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
474
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
475 buf << s[i++];
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
476
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
477 bool have_width = false;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
478
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
479 while (i < n)
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
480 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
481 switch (s[i])
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
482 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
483 case '*':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
484 if (discard)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
485 nconv = -1;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
486 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
487 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
488 discard = true;
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
489 buf << s[i++];
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
490 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
491 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
492
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
493 case '0': case '1': case '2': case '3': case '4':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
494 case '5': case '6': case '7': case '8': case '9':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
495 if (have_width)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
496 nconv = -1;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
497 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
498 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
499 char c = s[i++];
20230
e914b5399c67 Use in-place operators in C++ code where possible.
Rik <rik@octave.org>
parents: 19864
diff changeset
500 width = 10 * width + c - '0';
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
501 have_width = true;
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
502 buf << c;
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
503 while (i < n && isdigit (s[i]))
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
504 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
505 c = s[i++];
20230
e914b5399c67 Use in-place operators in C++ code where possible.
Rik <rik@octave.org>
parents: 19864
diff changeset
506 width = 10 * width + c - '0';
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
507 buf << c;
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
508 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
509 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
510 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
511
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
512 case 'h': case 'l': case 'L':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
513 if (modifier != '\0')
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
514 nconv = -1;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
515 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
516 modifier = s[i++];
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
517 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
518
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
519 case 'd': case 'i': case 'o': case 'u': case 'x':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
520 if (modifier == 'L')
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
521 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
522 nconv = -1;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
523 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
524 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
525 goto fini;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
526
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
527 case 'e': case 'f': case 'g':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
528 if (modifier == 'h')
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
529 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
530 nconv = -1;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
531 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
532 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
533
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
534 // No float or long double conversions, thanks.
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
535 buf << 'l';
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
536
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
537 goto fini;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
538
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
539 case 'c': case 's': case 'p': case '%': case '[':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
540 if (modifier != '\0')
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
541 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
542 nconv = -1;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
543 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
544 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
545 goto fini;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
546
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
547 fini:
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
548 {
21475
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
549 if (finish_conversion (s, i, n, width, discard,
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
550 type, modifier) == 0)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
551 return;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
552 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
553 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
554
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
555 default:
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
556 nconv = -1;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
557 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
558 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
559
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
560 if (nconv < 0)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
561 break;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
562 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
563
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
564 nconv = -1;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
565 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
566
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
567 int
10187
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
568 scanf_format_list::finish_conversion (const std::string& s, size_t& i,
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
569 size_t n, int& width, bool discard,
21475
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
570 char& type, char modifier)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
571 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
572 int retval = 0;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
573
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
574 std::string char_class;
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
575
10187
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
576 size_t beg_idx = std::string::npos;
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
577 size_t end_idx = std::string::npos;
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
578
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
579 if (s[i] == '%')
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
580 {
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
581 type = '%';
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
582 buf << s[i++];
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
583 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
584 else
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
585 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
586 type = s[i];
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
587
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
588 if (s[i] == '[')
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
589 {
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
590 buf << s[i++];
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
591
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
592 if (i < n)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
593 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
594 beg_idx = i;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
595
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
596 if (s[i] == '^')
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
597 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
598 type = '^';
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
599 buf << s[i++];
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
600
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
601 if (i < n)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
602 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
603 beg_idx = i;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
604
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
605 if (s[i] == ']')
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
606 buf << s[i++];
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
607 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
608 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
609 else if (s[i] == ']')
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
610 buf << s[i++];
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
611 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
612
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
613 while (i < n && s[i] != ']')
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
614 buf << s[i++];
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
615
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
616 if (i < n && s[i] == ']')
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
617 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
618 end_idx = i-1;
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
619 buf << s[i++];
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
620 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
621
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
622 if (s[i-1] != ']')
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
623 retval = nconv = -1;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
624 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
625 else
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
626 buf << s[i++];
10187
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
627
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
628 nconv++;
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
629 }
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
630
10187
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
631 if (nconv >= 0)
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
632 {
10187
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
633 if (beg_idx != std::string::npos && end_idx != std::string::npos)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
634 char_class = expand_char_class (s.substr (beg_idx,
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
635 end_idx - beg_idx + 1));
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
636
21475
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
637 add_elt_to_list (width, discard, type, modifier, char_class);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
638 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
639
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
640 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
641 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
642
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
643 void
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
644 scanf_format_list::printme (void) const
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
645 {
21475
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
646 size_t n = fmt_elts.size ();
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
647
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
648 for (size_t i = 0; i < n; i++)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
649 {
21475
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
650 scanf_format_elt *elt = fmt_elts[i];
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
651
3531
97cf542676e1 [project @ 2000-02-02 11:30:40 by jwe]
jwe
parents: 3523
diff changeset
652 std::cerr
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
653 << "width: " << elt->width << "\n"
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
654 << "discard: " << elt->discard << "\n"
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
655 << "type: ";
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
656
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
657 if (elt->type == scanf_format_elt::literal_conversion)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
658 std::cerr << "literal text\n";
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
659 else if (elt->type == scanf_format_elt::whitespace_conversion)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
660 std::cerr << "whitespace\n";
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
661 else
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
662 std::cerr << elt->type << "\n";
3531
97cf542676e1 [project @ 2000-02-02 11:30:40 by jwe]
jwe
parents: 3523
diff changeset
663
97cf542676e1 [project @ 2000-02-02 11:30:40 by jwe]
jwe
parents: 3523
diff changeset
664 std::cerr
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
665 << "modifier: " << elt->modifier << "\n"
15466
d174210ce1ec use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
666 << "char_class: '" << undo_string_escapes (elt->char_class) << "'\n"
d174210ce1ec use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
667 << "text: '" << undo_string_escapes (elt->text) << "'\n\n";
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
668 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
669 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
670
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
671 bool
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
672 scanf_format_list::all_character_conversions (void)
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
673 {
21475
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
674 size_t n = fmt_elts.size ();
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
675
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
676 if (n > 0)
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
677 {
21475
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
678 for (size_t i = 0; i < n; i++)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
679 {
21475
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
680 scanf_format_elt *elt = fmt_elts[i];
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
681
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
682 switch (elt->type)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
683 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
684 case 'c': case 's': case '%': case '[': case '^':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
685 case scanf_format_elt::literal_conversion:
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
686 case scanf_format_elt::whitespace_conversion:
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
687 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
688
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
689 default:
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
690 return false;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
691 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
692 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
693 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
694
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
695 return true;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
696 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
697 else
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
698 return false;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
699 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
700
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
701 bool
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
702 scanf_format_list::all_numeric_conversions (void)
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
703 {
21475
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
704 size_t n = fmt_elts.size ();
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
705
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
706 if (n > 0)
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
707 {
21475
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
708 for (size_t i = 0; i < n; i++)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
709 {
21475
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
710 scanf_format_elt *elt = fmt_elts[i];
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
711
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
712 switch (elt->type)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
713 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
714 case 'd': case 'i': case 'o': case 'u': case 'x':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
715 case 'e': case 'f': case 'g':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
716 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
717
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
718 default:
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
719 return false;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
720 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
721 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
722 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
723
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
724 return true;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
725 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
726 else
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
727 return false;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
728 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
729
21477
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
730 class
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
731 printf_format_elt
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
732 {
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
733 public:
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
734
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
735 printf_format_elt (const char *txt = 0, int n = 0, int w = -1,
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
736 int p = -1, const std::string& f = "",
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
737 char typ = '\0', char mod = '\0')
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
738 : text (strsave (txt)), args (n), fw (w), prec (p), flags (f),
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
739 type (typ), modifier (mod)
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
740 { }
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
741
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
742 printf_format_elt (const printf_format_elt& e)
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
743 : text (strsave (e.text)), args (e.args), fw (e.fw), prec (e.prec),
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
744 flags (e.flags), type (e.type), modifier (e.modifier)
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
745 { }
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
746
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
747 printf_format_elt& operator = (const printf_format_elt& e)
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
748 {
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
749 if (this != &e)
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
750 {
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
751 text = strsave (e.text);
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
752 args = e.args;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
753 fw = e.fw;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
754 prec = e.prec;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
755 flags = e.flags;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
756 type = e.type;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
757 modifier = e.modifier;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
758 }
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
759
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
760 return *this;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
761 }
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
762
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
763 ~printf_format_elt (void) { delete [] text; }
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
764
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
765 // The C-style format string.
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
766 const char *text;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
767
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
768 // How many args do we expect to consume?
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
769 int args;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
770
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
771 // Field width.
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
772 int fw;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
773
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
774 // Precision.
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
775 int prec;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
776
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
777 // Flags -- '-', '+', ' ', '0', or '#'.
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
778 std::string flags;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
779
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
780 // Type of conversion -- 'd', 'i', 'o', 'x', 'X', 'u', 'c', 's',
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
781 // 'f', 'e', 'E', 'g', 'G', 'p', or '%'
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
782 char type;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
783
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
784 // A length modifier -- 'h', 'l', or 'L'.
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
785 char modifier;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
786 };
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
787
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
788 class
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
789 printf_format_list
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
790 {
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
791 public:
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
792
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
793 printf_format_list (const std::string& fmt = "");
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
794
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
795 ~printf_format_list (void);
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
796
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
797 octave_idx_type num_conversions (void) { return nconv; }
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
798
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
799 const printf_format_elt *first (void)
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
800 {
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
801 curr_idx = 0;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
802 return current ();
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
803 }
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
804
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
805 const printf_format_elt *current (void) const
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
806 {
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
807 return length () > 0 ? fmt_elts[curr_idx] : 0;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
808 }
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
809
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
810 size_t length (void) const { return fmt_elts.size (); }
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
811
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
812 const printf_format_elt *next (bool cycle = true)
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
813 {
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
814 curr_idx++;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
815
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
816 if (curr_idx >= length ())
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
817 {
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
818 if (cycle)
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
819 curr_idx = 0;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
820 else
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
821 return 0;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
822 }
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
823
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
824 return current ();
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
825 }
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
826
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
827 bool last_elt_p (void) { return (curr_idx + 1 == length ()); }
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
828
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
829 void printme (void) const;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
830
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
831 bool ok (void) const { return (nconv >= 0); }
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
832
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
833 operator bool () const { return ok (); }
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
834
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
835 private:
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
836
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
837 // Number of conversions specified by this format string, or -1 if
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
838 // invalid conversions have been found.
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
839 octave_idx_type nconv;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
840
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
841 // Index to current element;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
842 size_t curr_idx;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
843
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
844 // List of format elements.
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
845 std::deque<printf_format_elt*> fmt_elts;
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
846
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
847 // Temporary buffer.
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
848 std::ostringstream buf;
21477
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
849
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
850 void add_elt_to_list (int args, const std::string& flags, int fw,
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
851 int prec, char type, char modifier);
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
852
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
853 void process_conversion (const std::string& s, size_t& i, size_t n,
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
854 int& args, std::string& flags, int& fw,
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
855 int& prec, char& modifier, char& type);
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
856
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
857 void finish_conversion (const std::string& s, size_t& i, int args,
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
858 const std::string& flags, int fw, int prec,
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
859 char modifier, char& type);
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
860
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
861 // No copying!
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
862
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
863 printf_format_list (const printf_format_list&);
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
864
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
865 printf_format_list& operator = (const printf_format_list&);
4fc04d04dd9c make printf and scanf format element and list objects private
John W. Eaton <jwe@octave.org>
parents: 21475
diff changeset
866 };
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
867
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
868 printf_format_list::printf_format_list (const std::string& s)
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
869 : nconv (0), curr_idx (0), fmt_elts (), buf ()
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
870 {
10187
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
871 size_t n = s.length ();
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
872
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
873 size_t i = 0;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
874
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
875 int args = 0;
3643
3af6d00b82ed [project @ 2000-03-24 10:53:21 by jwe]
jwe
parents: 3640
diff changeset
876 std::string flags;
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
877 int fw = -1;
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
878 int prec = -1;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
879 char modifier = '\0';
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
880 char type = '\0';
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
881
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
882 bool have_more = true;
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
883 bool empty_buf = true;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
884
4223
5ebaf7eee36e [project @ 2002-12-17 23:54:34 by jwe]
jwe
parents: 4153
diff changeset
885 if (n == 0)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
886 {
4223
5ebaf7eee36e [project @ 2002-12-17 23:54:34 by jwe]
jwe
parents: 4153
diff changeset
887 printf_format_elt *elt
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
888 = new printf_format_elt ("", args, fw, prec, flags, type, modifier);
4223
5ebaf7eee36e [project @ 2002-12-17 23:54:34 by jwe]
jwe
parents: 4153
diff changeset
889
21475
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
890 fmt_elts.push_back (elt);
4223
5ebaf7eee36e [project @ 2002-12-17 23:54:34 by jwe]
jwe
parents: 4153
diff changeset
891 }
5ebaf7eee36e [project @ 2002-12-17 23:54:34 by jwe]
jwe
parents: 4153
diff changeset
892 else
5ebaf7eee36e [project @ 2002-12-17 23:54:34 by jwe]
jwe
parents: 4153
diff changeset
893 {
5ebaf7eee36e [project @ 2002-12-17 23:54:34 by jwe]
jwe
parents: 4153
diff changeset
894 while (i < n)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
895 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
896 have_more = true;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
897
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
898 empty_buf = (buf.tellp () == 0);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
899
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
900 switch (s[i])
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
901 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
902 case '%':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
903 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
904 if (empty_buf)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
905 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
906 process_conversion (s, i, n, args, flags, fw, prec,
21475
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
907 type, modifier);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
908
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
909 // If there is nothing in the buffer, then
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
910 // add_elt_to_list must have just been called, so we
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
911 // are already done with the current element and we
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
912 // don't need to call add_elt_to_list if this is our
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
913 // last trip through the loop.
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
914
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
915 have_more = (buf.tellp () != 0);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
916 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
917 else
21475
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
918 add_elt_to_list (args, flags, fw, prec, type, modifier);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
919 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
920 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
921
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
922 default:
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
923 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
924 args = 0;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
925 flags = "";
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
926 fw = -1;
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
927 prec = -1;
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
928 modifier = '\0';
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
929 type = '\0';
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
930 buf << s[i++];
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
931 empty_buf = false;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
932 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
933 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
934 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
935
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
936 if (nconv < 0)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
937 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
938 have_more = false;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
939 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
940 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
941 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
942
4223
5ebaf7eee36e [project @ 2002-12-17 23:54:34 by jwe]
jwe
parents: 4153
diff changeset
943 if (have_more)
21475
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
944 add_elt_to_list (args, flags, fw, prec, type, modifier);
4223
5ebaf7eee36e [project @ 2002-12-17 23:54:34 by jwe]
jwe
parents: 4153
diff changeset
945
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
946 buf.clear ();
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
947 buf.str ("");
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
948 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
949 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
950
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
951 printf_format_list::~printf_format_list (void)
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
952 {
21475
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
953 size_t n = fmt_elts.size ();
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
954
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
955 for (size_t i = 0; i < n; i++)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
956 {
21475
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
957 printf_format_elt *elt = fmt_elts[i];
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
958 delete elt;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
959 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
960 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
961
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
962 void
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
963 printf_format_list::add_elt_to_list (int args, const std::string& flags,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
964 int fw, int prec, char type,
21475
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
965 char modifier)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
966 {
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
967 std::string text = buf.str ();
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
968
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
969 if (! text.empty ())
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
970 {
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
971 printf_format_elt *elt
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
972 = new printf_format_elt (text.c_str (), args, fw, prec, flags,
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
973 type, modifier);
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
974
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
975 fmt_elts.push_back (elt);
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
976 }
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
977
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
978 buf.clear ();
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
979 buf.str ("");
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
980 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
981
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
982 void
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
983 printf_format_list::process_conversion (const std::string& s, size_t& i,
21475
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
984 size_t n, int& args,
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
985 std::string& flags, int& fw,
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
986 int& prec, char& modifier,
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
987 char& type)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
988 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
989 args = 0;
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
990 flags = "";
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
991 fw = -1;
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
992 prec = -1;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
993 modifier = '\0';
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
994 type = '\0';
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
995
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
996 buf << s[i++];
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
997
4587
7b957b442818 [project @ 2003-11-10 15:50:39 by jwe]
jwe
parents: 4574
diff changeset
998 bool nxt = false;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
999
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1000 while (i < n)
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1001 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1002 switch (s[i])
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1003 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1004 case '-': case '+': case ' ': case '0': case '#':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1005 flags += s[i];
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1006 buf << s[i++];
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1007 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1008
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1009 default:
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1010 nxt = true;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1011 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1012 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1013
4587
7b957b442818 [project @ 2003-11-10 15:50:39 by jwe]
jwe
parents: 4574
diff changeset
1014 if (nxt)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1015 break;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1016 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1017
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1018 if (i < n)
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1019 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1020 if (s[i] == '*')
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1021 {
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
1022 fw = -2;
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1023 args++;
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1024 buf << s[i++];
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1025 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1026 else
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1027 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1028 if (isdigit (s[i]))
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1029 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1030 int nn = 0;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1031 std::string tmp = s.substr (i);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1032 sscanf (tmp.c_str (), "%d%n", &fw, &nn);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1033 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1034
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1035 while (i < n && isdigit (s[i]))
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1036 buf << s[i++];
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1037 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1038 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1039
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1040 if (i < n && s[i] == '.')
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1041 {
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
1042 // nothing before the . means 0.
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
1043 if (fw == -1)
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
1044 fw = 0;
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
1045
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
1046 // . followed by nothing is 0.
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
1047 prec = 0;
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
1048
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1049 buf << s[i++];
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1050
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1051 if (i < n)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1052 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1053 if (s[i] == '*')
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1054 {
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
1055 prec = -2;
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1056 args++;
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1057 buf << s[i++];
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1058 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1059 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1060 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1061 if (isdigit (s[i]))
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1062 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1063 int nn = 0;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1064 std::string tmp = s.substr (i);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1065 sscanf (tmp.c_str (), "%d%n", &prec, &nn);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1066 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1067
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1068 while (i < n && isdigit (s[i]))
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1069 buf << s[i++];
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1070 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1071 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1072 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1073
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1074 if (i < n)
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1075 {
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
1076 // Accept and record modifier, but don't place it in the format
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
1077 // item text. All integer conversions are handled as 64-bit
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
1078 // integers.
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
1079
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1080 switch (s[i])
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1081 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1082 case 'h': case 'l': case 'L':
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
1083 modifier = s[i++];
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1084 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1085
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1086 default:
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1087 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1088 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1089 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1090
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1091 if (i < n)
21475
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
1092 finish_conversion (s, i, args, flags, fw, prec, modifier, type);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1093 else
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1094 nconv = -1;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1095 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1096
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1097 void
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
1098 printf_format_list::finish_conversion (const std::string& s, size_t& i,
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
1099 int args, const std::string& flags,
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
1100 int fw, int prec, char modifier,
21475
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
1101 char& type)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1102 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1103 switch (s[i])
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1104 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1105 case 'd': case 'i': case 'o': case 'x': case 'X':
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1106 case 'u': case 'c':
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1107 if (modifier == 'L')
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1108 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1109 nconv = -1;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1110 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1111 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1112 goto fini;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1113
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1114 case 'f': case 'e': case 'E': case 'g': case 'G':
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1115 if (modifier == 'h' || modifier == 'l')
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1116 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1117 nconv = -1;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1118 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1119 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1120 goto fini;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1121
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1122 case 's': case 'p': case '%':
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1123 if (modifier != '\0')
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1124 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1125 nconv = -1;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1126 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1127 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1128 goto fini;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1129
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1130 fini:
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1131
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
1132 type = s[i];
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
1133
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1134 buf << s[i++];
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
1135
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
1136 if (type != '%' || args != 0)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1137 nconv++;
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
1138
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
1139 if (type != '%')
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1140 args++;
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
1141
21475
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
1142 add_elt_to_list (args, flags, fw, prec, type, modifier);
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
1143
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1144 break;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1145
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1146 default:
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1147 nconv = -1;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1148 break;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1149 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1150 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1151
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1152 void
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1153 printf_format_list::printme (void) const
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1154 {
21475
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
1155 size_t n = fmt_elts.size ();
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
1156
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
1157 for (size_t i = 0; i < n; i++)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1158 {
21475
4f3e63d75f33 use std::deque instead of Array to hold scanf and printf format elements
John W. Eaton <jwe@octave.org>
parents: 21354
diff changeset
1159 printf_format_elt *elt = fmt_elts[i];
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1160
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
1161 std::cerr
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1162 << "args: " << elt->args << "\n"
15466
d174210ce1ec use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
1163 << "flags: '" << elt->flags << "'\n"
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1164 << "width: " << elt->fw << "\n"
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
1165 << "prec: " << elt->prec << "\n"
15466
d174210ce1ec use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
1166 << "type: '" << elt->type << "'\n"
d174210ce1ec use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
1167 << "modifier: '" << elt->modifier << "'\n"
d174210ce1ec use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
1168 << "text: '" << undo_string_escapes (elt->text) << "'\n\n";
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1169 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1170 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
1171
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1172 // Delimited stream, optimised to read strings of characters separated
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1173 // by single-character delimiters.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1174 //
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1175 // The reason behind this class is that octstream doesn't provide
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1176 // seek/tell, but the opportunity has been taken to optimise for the
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1177 // textscan workload.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1178 //
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1179 // The function reads chunks into a 4kiB buffer, and marks where the
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1180 // last delimiter occurs. Reads up to this delimiter can be fast.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1181 // After that last delimiter, the remaining text is moved to the front
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1182 // of the buffer and the buffer is refilled. This also allows cheap
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1183 // seek and tell operations within a "fast read" block.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1184
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1185 class
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1186 delimited_stream
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1187 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1188 public:
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1189
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1190 delimited_stream (std::istream& is, const std::string& delimiters,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1191 int longest_lookahead, octave_idx_type bsize = 4096);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1192
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1193 delimited_stream (std::istream& is, const delimited_stream& ds);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1194
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1195 ~delimited_stream (void);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1196
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1197 // Called when optimised sequence of get is finished. Ensures that
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1198 // there is a remaining delimiter in buf, or loads more data in.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1199 void field_done (void)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1200 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1201 if (idx >= last)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1202 refresh_buf ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1203 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1204
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1205 // Load new data into buffer, and set eob, last, idx.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1206 // Return EOF at end of file, 0 otherwise.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1207 int refresh_buf (void);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1208
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1209 // Get a character, relying on caller to call field_done if
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1210 // a delimiter has been reached.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1211 int get (void)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1212 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1213 if (delimited)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1214 return eof () ? std::istream::traits_type::eof () : *idx++;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1215 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1216 return get_undelim ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1217 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1218
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1219 // Get a character, checking for underrun of the buffer.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1220 int get_undelim (void);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1221
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1222 // Read character that will be got by the next get.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1223 int peek (void) { return eof () ? std::istream::traits_type::eof () : *idx; }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1224
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1225 // Read character that will be got by the next get.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1226 int peek_undelim (void);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1227
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1228 // Undo a 'get' or 'get_undelim'. It is the caller's responsibility
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1229 // to avoid overflow by calling putbacks only for a character got by
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1230 // get() or get_undelim(), with no intervening
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1231 // get, get_delim, field_done, refresh_buf, getline, read or seekg.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1232 void putback (char /*ch*/ = 0) { if (! eof ()) --idx; }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1233
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1234 int getline (std::string& dest, char delim);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1235
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1236 // int skipline (char delim);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1237
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1238 char *read (char *buffer, int size, char* &new_start);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1239
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1240 // Return a position suitable to "seekg", valid only within this
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1241 // block between calls to field_done.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1242 char *tellg (void) { return idx; }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1243
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1244 void seekg (char *old_idx) { idx = old_idx; }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1245
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1246 bool eof (void)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1247 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1248 return (eob == buf && i_stream.eof ()) || (flags & std::ios_base::eofbit);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1249 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1250
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1251 operator const void* (void) { return (! eof () && ! flags) ? this : 0; }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1252
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1253 bool fail (void) { return flags & std::ios_base::failbit; }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1254
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1255 std::ios_base::iostate rdstate (void) { return flags; }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1256
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1257 void setstate (std::ios_base::iostate m) { flags = flags | m; }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1258
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1259 void clear (std::ios_base::iostate m
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1260 = (std::ios_base::eofbit & ~std::ios_base::eofbit))
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1261 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1262 flags = flags & m;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1263 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1264
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1265 // Report if any characters have been consumed.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1266 // (get, read etc. not cancelled by putback or seekg)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1267
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1268 void progress_benchmark (void) { progress_marker = idx; }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1269
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1270 bool no_progress (void) { return progress_marker == idx; }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1271
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1272 private:
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1273
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1274 // Number of characters to read from the file at once.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1275 int bufsize;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1276
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1277 // Stream to read from.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1278 std::istream& i_stream;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1279
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1280 // Temporary storage for a "chunk" of data.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1281 char *buf;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1282
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1283 // Current read pointer.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1284 char *idx;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1285
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1286 // Location of last delimiter in the buffer at buf (undefined if
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1287 // delimited is false).
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1288 char *last;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1289
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1290 // Position after last character in buffer.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1291 char *eob;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1292
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1293 // True if there is delimiter in the bufer after idx.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1294 bool delimited;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1295
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1296 // Longest lookahead required.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1297 int longest;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1298
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1299 // Sequence of single-character delimiters.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1300 const std::string delims;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1301
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1302 // Position of start of buf in original stream.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1303 std::streampos buf_in_file;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1304
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1305 // Marker to see if a read consumes any characters.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1306 char *progress_marker;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1307
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1308 std::ios_base::iostate flags;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1309
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1310 // No copying!
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1311
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1312 delimited_stream (const delimited_stream&);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1313
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1314 delimited_stream& operator = (const delimited_stream&);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1315 };
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1316
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1317 // Create a delimited stream, reading from is, with delimiters delims,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1318 // and allowing reading of up to tellg + longest_lookeahead. When is
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1319 // is at EOF, lookahead may be padded by ASCII nuls.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1320
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1321 delimited_stream::delimited_stream (std::istream& is,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1322 const std::string& delimiters,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1323 int longest_lookahead,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1324 octave_idx_type bsize)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1325 : bufsize (bsize), i_stream (is), longest (longest_lookahead),
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1326 delims (delimiters),
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1327 flags (std::ios::failbit & ~std::ios::failbit) // can't cast 0
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1328 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1329 buf = new char[bufsize];
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1330 eob = buf + bufsize;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1331 idx = eob; // refresh_buf shouldn't try to copy old data
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1332 progress_marker = idx;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1333 refresh_buf (); // load the first batch of data
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1334 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1335
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1336 // Used to create a stream from a strstream from data read from a dstr.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1337 // FIXME: Find a more efficient approach. Perhaps derived dstr
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1338 delimited_stream::delimited_stream (std::istream& is,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1339 const delimited_stream& ds)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1340 : bufsize (ds.bufsize), i_stream (is), longest (ds.longest),
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1341 delims (ds.delims),
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1342 flags (std::ios::failbit & ~std::ios::failbit) // can't cast 0
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1343 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1344 buf = new char[bufsize];
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1345 eob = buf + bufsize;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1346 idx = eob; // refresh_buf shouldn't try to copy old data
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1347 progress_marker = idx;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1348 refresh_buf (); // load the first batch of data
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1349 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1350
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1351 delimited_stream::~delimited_stream (void)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1352 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1353 // Seek to the correct position in i_stream.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1354 if (! eof ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1355 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1356 i_stream.clear ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1357 i_stream.seekg (buf_in_file);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1358 i_stream.read (buf, idx - buf);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1359 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1360
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1361 delete [] buf;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1362 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1363
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1364 // Read a character from the buffer, refilling the buffer from the file
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1365 // if necessary.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1366
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1367 int
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1368 delimited_stream::get_undelim (void)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1369 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1370 int retval;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1371 if (eof ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1372 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1373 setstate (std::ios_base::failbit);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1374 return std::istream::traits_type::eof ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1375 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1376
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1377 if (idx < eob)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1378 retval = *idx++;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1379 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1380 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1381 refresh_buf ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1382
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1383 if (eof ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1384 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1385 setstate (std::ios_base::eofbit);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1386 retval = std::istream::traits_type::eof ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1387 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1388 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1389 retval = *idx++;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1390 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1391
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1392 if (idx >= last)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1393 delimited = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1394
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1395 return retval;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1396 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1397
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1398 // Return the next character to be read without incrementing the
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1399 // pointer, refilling the buffer from the file if necessary.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1400
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1401 int
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1402 delimited_stream::peek_undelim (void)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1403 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1404 int retval = get_undelim ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1405 putback ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1406
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1407 return retval;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1408 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1409
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1410 // Copy remaining unprocessed data to the start of the buffer and load
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1411 // new data to fill it. Return EOF if the file is at EOF before
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1412 // reading any data and all of the data that has been read has been
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1413 // processed.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1414
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1415 int
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1416 delimited_stream::refresh_buf (void)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1417 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1418 if (eof ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1419 return std::istream::traits_type::eof ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1420
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1421 int retval;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1422
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1423 if (eob < idx)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1424 idx = eob;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1425
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1426 size_t old_remaining = eob - idx;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1427
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1428 octave_quit (); // allow ctrl-C
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1429
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1430 if (old_remaining > 0)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1431 memmove (buf, idx, old_remaining);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1432
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1433 progress_marker -= idx - buf; // where original idx would have been
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1434 idx = buf;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1435
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1436 int gcount; // chars read
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1437 if (! i_stream.eof ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1438 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1439 buf_in_file = i_stream.tellg (); // record for destructor
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1440 i_stream.read (buf + old_remaining, bufsize - old_remaining);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1441 gcount = i_stream.gcount ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1442 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1443 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1444 gcount = 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1445
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1446 eob = buf + old_remaining + gcount;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1447 last = eob;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1448 if (gcount == 0)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1449 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1450 delimited = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1451
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1452 if (eob != buf) // no more data in file, but still some to go
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1453 retval = 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1454 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1455 // file and buffer are both done.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1456 retval = std::istream::traits_type::eof ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1457 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1458 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1459 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1460 delimited = true;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1461
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1462 for (last = eob - longest; last - buf >= 0; last--)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1463 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1464 if (delims.find (*last) != std::string::npos)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1465 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1466 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1467
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1468 if (last < buf)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1469 delimited = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1470
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1471 retval = 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1472 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1473
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1474 // Ensure fast peek doesn't give valid char
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1475 if (retval == std::istream::traits_type::eof ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1476 *idx = '\0'; // FIXME - check that no TreatAsEmpty etc starts w. \0?
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1477
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1478 return retval;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1479 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1480
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1481 // Return a pointer to a block of data of size size, assuming that a
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1482 // sufficiently large buffer is available in buffer, if required.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1483 // If called when delimited == true, and size is no greater than
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1484 // longest_lookahead then this will not call refresh_buf, so seekg
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1485 // still works. Otherwise, seekg may be invalidated.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1486
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1487 char *
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1488 delimited_stream::read (char *buffer, int size, char* &prior_tell)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1489 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1490 char *retval;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1491
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1492 if (eob - idx > size)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1493 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1494 retval = idx;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1495 idx += size;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1496 if (idx > last)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1497 delimited = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1498 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1499 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1500 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1501 // If there was a tellg pointing to an earlier point than the current
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1502 // read position, try to keep it in the active buffer.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1503 // In the current code, prior_tell==idx for each call,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1504 // so this is not necessary, just a precaution.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1505
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1506 if (eob - prior_tell + size < bufsize)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1507 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1508 octave_idx_type gap = idx - prior_tell;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1509 idx = prior_tell;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1510 refresh_buf ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1511 idx += gap;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1512 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1513 else // can't keep the tellg in range. May skip some data.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1514 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1515 refresh_buf ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1516 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1517
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1518 prior_tell = buf;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1519
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1520 if (eob - idx > size)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1521 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1522 retval = idx;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1523 idx += size;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1524 if (idx > last)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1525 delimited = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1526 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1527 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1528 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1529 if (size <= bufsize) // small read, but reached EOF
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1530 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1531 retval = idx;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1532 memset (eob, 0, size + (idx - buf));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1533 idx += size;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1534 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1535 else // Reading more than the whole buf; return it in buffer
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1536 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1537 retval = buffer;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1538 // FIXME -- read bufsize at a time
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1539 int i;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1540 for (i = 0; i < size && ! eof (); i++)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1541 *buffer++ = get_undelim ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1542 if (eof ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1543 memset (buffer, 0, size - i);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1544 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1545 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1546 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1547
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1548 return retval;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1549 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1550
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1551 // Return in OUT an entire line, terminated by delim. On input, OUT
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1552 // must have length at least 1.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1553
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1554 int
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1555 delimited_stream::getline (std::string& out, char delim)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1556 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1557 int len = out.length (), used = 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1558 int ch;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1559 while ((ch = get_undelim ()) != delim
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1560 && ch != std::istream::traits_type::eof ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1561 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1562 out[used++] = ch;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1563 if (used == len)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1564 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1565 len <<= 1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1566 out.resize (len);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1567 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1568 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1569 out.resize (used);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1570 field_done ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1571
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1572 return ch;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1573 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1574
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1575 // A single conversion specifier, such as %f or %c.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1576
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1577 class
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1578 textscan_format_elt
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1579 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1580 public:
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1581
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1582 enum special_conversion
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1583 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1584 whitespace_conversion = 1,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1585 literal_conversion = 2
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1586 };
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1587
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1588 textscan_format_elt (const std::string& txt, int w = 0, int p = -1,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1589 int bw = 0, bool dis = false, char typ = '\0',
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1590 const std::string& ch_class = std::string ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1591 : text (txt), width (w), prec (p), bitwidth (bw),
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1592 char_class (ch_class), type (typ), discard (dis),
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1593 numeric (typ == 'd' || typ == 'u' || type == 'f' || type == 'n')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1594 { }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1595
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1596 textscan_format_elt (const textscan_format_elt& e)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1597 : text (e.text), width (e.width), prec (e.prec),
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1598 bitwidth (e.bitwidth), char_class (e.char_class), type (e.type),
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1599 discard (e.discard), numeric (e.numeric)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1600 { }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1601
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1602 textscan_format_elt& operator = (const textscan_format_elt& e)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1603 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1604 if (this != &e)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1605 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1606 text = e.text;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1607 width = e.width;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1608 prec = e.prec;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1609 bitwidth = e.bitwidth;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1610 discard = e.discard;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1611 type = e.type;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1612 numeric = e.numeric;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1613 char_class = e.char_class;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1614 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1615
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1616 return *this;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1617 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1618
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1619 // The C-style format string.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1620 std::string text;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1621
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1622 // The maximum field width.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1623 unsigned int width;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1624
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1625 // The maximum number of digits to read after the decimal in a
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1626 // floating point conversion.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1627 int prec;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1628
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1629 // The size of the result. For integers, bitwidth may be 8, 16, 34,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1630 // or 64. For floating point values, bitwidth may be 32 or 64.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1631 int bitwidth;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1632
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1633 // The class of characters in a `[' or `^' format.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1634 std::string char_class;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1635
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1636 // Type of conversion
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1637 // -- `d', `u', `f', `n', `s', `q', `c', `%', `C', `D', `[' or `^'.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1638 char type;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1639
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1640 // TRUE if we are not storing the result of this conversion.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1641 bool discard;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1642
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1643 // TRUE if the type is 'd', 'u', 'f', 'n'
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1644 bool numeric;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1645 };
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1646
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1647 // The (parsed) sequence of format specifiers.
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1648
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1649 class textscan;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1650
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1651 class
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1652 textscan_format_list
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1653 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1654 public:
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1655
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1656 textscan_format_list (const std::string& fmt = std::string (),
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1657 const std::string& who = "textscan");
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1658
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1659 ~textscan_format_list (void);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1660
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1661 octave_idx_type num_conversions (void) const { return nconv; }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1662
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1663 // The length can be different than the number of conversions.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1664 // For example, "x %d y %d z" has 2 conversions but the length of
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1665 // the list is 3 because of the characters that appear after the
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1666 // last conversion.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1667
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1668 size_t numel (void) const { return fmt_elts.size (); }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1669
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1670 const textscan_format_elt *first (void)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1671 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1672 curr_idx = 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1673 return current ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1674 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1675
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1676 const textscan_format_elt *current (void) const
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1677 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1678 return numel () > 0 ? fmt_elts[curr_idx] : 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1679 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1680
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1681 const textscan_format_elt *next (bool cycle = true)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1682 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1683 curr_idx++;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1684
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1685 if (curr_idx >= numel ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1686 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1687 if (cycle)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1688 curr_idx = 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1689 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1690 return 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1691 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1692
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1693 return current ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1694 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1695
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1696 void printme (void) const;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1697
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1698 bool ok (void) const { return (nconv >= 0); }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1699
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1700 operator const void* (void) const { return ok () ? this : 0; }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1701
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1702 // What function name should be shown when reporting errors.
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1703 std::string who;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1704
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1705 // True if number of %f to be set from data file.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1706 bool set_from_first;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1707
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1708 // At least one conversion specifier is s,q,c, or [...].
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1709 bool has_string;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1710
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1711 int read_first_row (delimited_stream& is, textscan& ts);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1712
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1713 std::list<octave_value> out_buf (void) const { return (output_container); }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1714
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1715 private:
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1716
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1717 // Number of conversions specified by this format string, or -1 if
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1718 // invalid conversions have been found.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1719 octave_idx_type nconv;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1720
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1721 // Index to current element;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1722 size_t curr_idx;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1723
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1724 // List of format elements.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1725 std::deque<textscan_format_elt*> fmt_elts;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1726
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1727 // list holding column arrays of types specified by conversions
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1728 std::list<octave_value> output_container;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1729
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1730 // Temporary buffer.
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1731 std::ostringstream buf;
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1732
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1733 void add_elt_to_list (unsigned int width, int prec, int bitwidth,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1734 octave_value val_type, bool discard,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1735 char type,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1736 const std::string& char_class = std::string ());
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1737
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1738 void process_conversion (const std::string& s, size_t& i, size_t n);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1739
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1740 std::string parse_char_class (const std::string& pattern) const;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1741
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1742 int finish_conversion (const std::string& s, size_t& i, size_t n,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1743 unsigned int& width, int& prec, int& bitwidth,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1744 octave_value& val_type,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1745 bool discard, char& type);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1746 // No copying!
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1747
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1748 textscan_format_list (const textscan_format_list&);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1749
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1750 textscan_format_list& operator = (const textscan_format_list&);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1751 };
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1752
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1753 // Main class to implement textscan. Read data and parse it
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1754 // according to a format.
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1755 //
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1756 // The calling sequence is
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1757 //
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1758 // textscan scanner ();
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1759 // scanner.scan (...);
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1760
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1761 class
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1762 OCTINTERP_API
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1763 textscan
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1764 {
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1765 public:
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1766
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1767 textscan (const std::string& who_arg = "textscan");
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1768
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1769 ~textscan (void) { }
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1770
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1771 octave_value scan (std::istream& isp, const std::string& fmt,
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1772 octave_idx_type ntimes,
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1773 const octave_value_list& options,
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1774 octave_idx_type& read_count);
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1775
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1776 private:
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1777
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1778 friend class textscan_format_list;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1779
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1780 // What function name should be shown when reporting errors.
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1781 std::string who;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1782
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1783 std::string buf;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1784
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1785 // Three cases for delim_table and delim_list
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1786 // 1. delim_table empty, delim_list empty: whitespace delimiters
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1787 // 2. delim_table = look-up table of delim chars, delim_list empty.
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1788 // 3. delim_table non-empty, delim_list = Cell array of delim strings
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1789
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1790 std::string whitespace_table;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1791
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1792 // delim_table[i] == '\0' if i is not a delimiter.
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1793 std::string delim_table;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1794
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1795 // String of delimiter characters.
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1796 std::string delims;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1797
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1798 Cell comment_style;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1799
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1800 // How far ahead to look to detect an open comment.
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1801 int comment_len;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1802
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1803 // First character of open comment.
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1804 int comment_char;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1805
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1806 octave_idx_type buffer_size;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1807
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1808 std::string date_locale;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1809
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1810 // 'inf' and 'nan' for formatted_double.
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1811 Cell inf_nan;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1812
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1813 // Array of strings of delimiters.
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1814 Cell delim_list;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1815
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1816 // Longest delimiter.
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1817 int delim_len;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1818
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1819 octave_value empty_value;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1820 std::string exp_chars;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1821 int header_lines;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1822 Cell treat_as_empty;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1823
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1824 // Longest string to treat as "N/A".
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1825 int treat_as_empty_len;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1826
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1827 std::string whitespace;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1828
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1829 short eol1;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1830 short eol2;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1831 short return_on_error;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1832
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1833 bool collect_output;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1834 bool multiple_delims_as_one;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1835 bool default_exp;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1836 bool numeric_delim;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1837
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1838 octave_idx_type lines;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1839
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1840 octave_value do_scan (std::istream& isp, textscan_format_list& fmt_list,
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1841 octave_idx_type ntimes);
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1842
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1843 void parse_options (const octave_value_list& args,
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1844 textscan_format_list& fmt_list);
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1845
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1846 int read_format_once (delimited_stream& isp, textscan_format_list& fmt_list,
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1847 std::list<octave_value>& retval,
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1848 Array<octave_idx_type> row, int& done_after);
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1849
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1850 void scan_one (delimited_stream& is, const textscan_format_elt& fmt,
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1851 octave_value& ov, Array<octave_idx_type> row);
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1852
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1853 // Methods to process a particular conversion specifier.
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1854 double read_double (delimited_stream& is,
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1855 const textscan_format_elt& fmt) const;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1856
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1857 void scan_complex (delimited_stream& is, const textscan_format_elt& fmt,
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1858 Complex& val) const;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1859
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1860 int scan_bracket (delimited_stream& is, const std::string& pattern,
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1861 std::string& val) const;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1862
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1863 int scan_caret (delimited_stream& is, const std::string& pattern,
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1864 std::string& val) const;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1865
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1866 void scan_string (delimited_stream& is, const textscan_format_elt& fmt,
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1867 std::string& val) const;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1868
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1869 void scan_cstring (delimited_stream& is, const textscan_format_elt& fmt,
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1870 std::string& val) const;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1871
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1872 void scan_qstring (delimited_stream& is, const textscan_format_elt& fmt,
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1873 std::string& val);
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1874
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1875 // Helper methods.
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1876 std::string read_until (delimited_stream& is, const Cell& delimiters,
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1877 const std::string& ends) const;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1878
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1879 int lookahead (delimited_stream& is, const Cell& targets, int max_len,
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1880 bool case_sensitive = true) const;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1881
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1882 bool match_literal (delimited_stream& isp, const textscan_format_elt& elem);
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1883
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1884 int skip_whitespace (delimited_stream& is, bool EOLstop = false);
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1885
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1886 int skip_delim (delimited_stream& is);
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1887
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1888 bool is_delim (unsigned char ch) const
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1889 {
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1890 return ((delim_table.empty () && (isspace (ch) || ch == eol1 || ch == eol2))
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1891 || delim_table[ch] != '\0');
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1892 }
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1893
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1894 bool isspace (unsigned int ch) const { return whitespace_table[ch & 0xff]; }
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1895
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1896 // True if the only delimiter is whitespace.
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1897 bool whitespace_delim (void) const { return delim_table.empty (); }
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1898
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1899 // No copying!
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1900
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1901 textscan (const textscan&);
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1902
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1903 textscan& operator = (const textscan&);
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1904 };
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1905
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1906 textscan_format_list::textscan_format_list (const std::string& s,
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1907 const std::string& who_arg)
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
1908 : who (who_arg), set_from_first (false), has_string (false),
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1909 nconv (0), curr_idx (0), fmt_elts (), buf ()
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1910 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1911 size_t n = s.length ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1912
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1913 size_t i = 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1914
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1915 unsigned int width = -1; // Unspecified width = max (except %c)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1916 int prec = -1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1917 int bitwidth = 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1918 bool discard = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1919 char type = '\0';
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1920
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1921 bool have_more = true;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1922
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1923 if (s.empty ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1924 {
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1925 buf.clear ();
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1926 buf.str ("");
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1927
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1928 buf << "%f";
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1929
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1930 bitwidth = 64;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1931 type = 'f';
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1932 add_elt_to_list (width, prec, bitwidth, octave_value (NDArray ()),
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1933 discard, type);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1934 have_more = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1935 set_from_first = true;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1936 nconv = 1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1937 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1938 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1939 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1940 set_from_first = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1941
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1942 while (i < n)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1943 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1944 have_more = true;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1945
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1946 if (s[i] == '%' && (i+1 == n || s[i+1] != '%'))
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1947 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1948 // Process percent-escape conversion type.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1949
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1950 process_conversion (s, i, n);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1951
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1952 // If there is nothing in the buffer, then add_elt_to_list
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1953 // must have just been called, so we are already done with
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1954 // the current element and we don't need to call
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1955 // add_elt_to_list if this is our last trip through the
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1956 // loop.
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1957
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1958 have_more = (buf.tellp () != 0);
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1959 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1960 else if (isspace (s[i]))
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1961 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1962 while (++i < n && isspace (s[i]))
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1963 /* skip whitespace */;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1964
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1965 have_more = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1966 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1967 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1968 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1969 type = textscan_format_elt::literal_conversion;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1970
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1971 width = 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1972 prec = -1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1973 bitwidth = 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1974 discard = true;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1975
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1976 while (i < n && ! isspace (s[i])
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1977 && (s[i] != '%' || (i+1 < n && s[i+1] == '%')))
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1978 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1979 if (s[i] == '%') // if double %, skip one
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1980 i++;
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1981 buf << s[i++];
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1982 width++;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1983 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1984
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1985 add_elt_to_list (width, prec, bitwidth, octave_value (),
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1986 discard, type);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1987
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1988 have_more = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1989 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1990
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1991 if (nconv < 0)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1992 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1993 have_more = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1994 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1995 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1996 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1997 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1998
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
1999 if (have_more)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2000 add_elt_to_list (width, prec, bitwidth, octave_value (), discard, type);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2001
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2002 buf.clear ();
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2003 buf.str ("");
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2004 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2005
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2006 textscan_format_list::~textscan_format_list (void)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2007 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2008 size_t n = numel ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2009
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2010 for (size_t i = 0; i < n; i++)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2011 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2012 textscan_format_elt *elt = fmt_elts[i];
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2013 delete elt;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2014 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2015 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2016
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2017 void
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2018 textscan_format_list::add_elt_to_list (unsigned int width, int prec,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2019 int bitwidth, octave_value val_type,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2020 bool discard, char type,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2021 const std::string& char_class)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2022 {
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2023 std::string text = buf.str ();
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2024
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2025 if (! text.empty ())
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2026 {
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2027 textscan_format_elt *elt
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2028 = new textscan_format_elt (text, width, prec, bitwidth, discard, type, char_class);
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2029
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2030 if (! discard)
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2031 output_container.push_back (val_type);
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2032
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2033 fmt_elts.push_back (elt);
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2034 }
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2035
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2036 buf.clear ();
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2037 buf.str ("");
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2038 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2039
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2040 void
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2041 textscan_format_list::process_conversion (const std::string& s, size_t& i,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2042 size_t n)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2043 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2044 unsigned width = 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2045 int prec = -1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2046 int bitwidth = 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2047 bool discard = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2048 octave_value val_type;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2049 char type = '\0';
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2050
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2051 buf << s[i++];
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2052
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2053 bool have_width = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2054
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2055 while (i < n)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2056 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2057 switch (s[i])
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2058 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2059 case '*':
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2060 if (discard)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2061 nconv = -1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2062 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2063 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2064 discard = true;
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2065 buf << s[i++];
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2066 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2067 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2068
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2069 case '0': case '1': case '2': case '3': case '4':
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2070 case '5': case '6': case '7': case '8': case '9':
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2071 if (have_width)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2072 nconv = -1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2073 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2074 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2075 char c = s[i++];
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2076 width = width * 10 + c - '0';
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2077 have_width = true;
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2078 buf << c;
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2079 while (i < n && isdigit (s[i]))
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2080 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2081 c = s[i++];
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2082 width = width * 10 + c - '0';
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2083 buf << c;
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2084 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2085
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2086 if (i < n && s[i] == '.')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2087 {
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2088 buf << s[i++];
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2089 prec = 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2090 while (i < n && isdigit (s[i]))
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2091 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2092 c = s[i++];
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2093 prec = prec * 10 + c - '0';
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2094 buf << c;
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2095 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2096 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2097 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2098 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2099
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2100 case 'd': case 'u':
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2101 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2102 bool done = true;
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2103 buf << (type = s[i++]);
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2104 if (i < n)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2105 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2106 if (s[i] == '8')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2107 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2108 bitwidth = 8;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2109 if (type == 'd')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2110 val_type = octave_value (int8NDArray ());
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2111 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2112 val_type = octave_value (uint8NDArray ());
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2113 buf << s[i++];
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2114 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2115 else if (s[i] == '1' && i+1 < n && s[i+1] == '6')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2116 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2117 bitwidth = 16;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2118 if (type == 'd')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2119 val_type = octave_value (int16NDArray ());
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2120 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2121 val_type = octave_value (uint16NDArray ());
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2122 buf << s[i++];
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2123 buf << s[i++];
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2124 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2125 else if (s[i] == '3' && i+1 < n && s[i+1] == '2')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2126 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2127 done = false; // use default size below
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2128 buf << s[i++];
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2129 buf << s[i++];
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2130 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2131 else if (s[i] == '6' && i+1 < n && s[i+1] == '4')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2132 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2133 bitwidth = 64;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2134 if (type == 'd')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2135 val_type = octave_value (int64NDArray ());
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2136 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2137 val_type = octave_value (uint64NDArray ());
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2138 buf << s[i++];
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2139 buf << s[i++];
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2140 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2141 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2142 done = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2143 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2144 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2145 done = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2146
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2147 if (! done)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2148 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2149 bitwidth = 32;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2150 if (type == 'd')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2151 val_type = octave_value (int32NDArray ());
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2152 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2153 val_type = octave_value (uint32NDArray ());
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2154 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2155 goto fini;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2156 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2157
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2158 case 'f':
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2159 buf << (type = s[i++]);
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2160 bitwidth = 64;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2161 if (i < n)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2162 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2163 if (s[i] == '3' && i+1 < n && s[i+1] == '2')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2164 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2165 bitwidth = 32;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2166 val_type = octave_value (FloatNDArray ());
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2167 buf << s[i++];
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2168 buf << s[i++];
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2169 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2170 else if (s[i] == '6' && i+1 < n && s[i+1] == '4')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2171 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2172 val_type = octave_value (NDArray ());
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2173 buf << s[i++];
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2174 buf << s[i++];
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2175 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2176 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2177 val_type = octave_value (NDArray ());
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2178 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2179 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2180 val_type = octave_value (NDArray ());
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2181 goto fini;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2182
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2183 case 'n':
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2184 buf << (type = s[i++]);
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2185 bitwidth = 64;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2186 val_type = octave_value (NDArray ());
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2187 goto fini;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2188
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2189 case 's': case 'q': case '[': case 'c':
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2190 if (! discard)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2191 val_type = octave_value (Cell ());
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2192 buf << (type = s[i++]);
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2193 has_string = true;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2194 goto fini;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2195
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2196 fini:
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2197 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2198 if (! have_width)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2199 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2200 if (type == 'c') // %c defaults to one character
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2201 width = 1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2202 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2203 width = static_cast<unsigned int> (-1); // others: unlimited
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2204 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2205
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2206 if (finish_conversion (s, i, n, width, prec, bitwidth, val_type,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2207 discard, type) == 0)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2208 return;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2209 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2210 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2211
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2212 default:
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
2213 error ("%s: '%%%c' is not a valid format specifier",
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
2214 who.c_str (), s[i]);
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2215 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2216
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2217 if (nconv < 0)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2218 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2219 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2220
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2221 nconv = -1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2222 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2223
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2224 // Parse [...] and [^...]
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2225 //
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2226 // Matlab does not expand expressions like A-Z, but they are useful, and
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2227 // so we parse them "carefully". We treat '-' as a usual character
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2228 // unless both start and end characters are from the same class (upper
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2229 // case, lower case, numeric), or this is not the first '-' in the
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2230 // pattern.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2231 //
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2232 // Keep both a running list of characters and a mask of which chars have
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2233 // occurred. The first is efficient for patterns with few characters.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2234 // The latter is efficient for [^...] patterns.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2235
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
2236 std::string
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
2237 textscan_format_list::parse_char_class (const std::string& pattern) const
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2238 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2239 int len = pattern.length ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2240 if (len == 0)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2241 return "";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2242
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2243 std::string retval (256, '\0');
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2244 std::string mask (256, '\0'); // number of times chr has been seen
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2245
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2246 int in = 0, out = 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2247 unsigned char ch, prev = 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2248 bool flip = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2249
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2250 ch = pattern[in];
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2251 if (ch == '^')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2252 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2253 in++;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2254 flip = true;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2255 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2256 mask[pattern[in]] = '\1';
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2257 retval[out++] = pattern[in++]; // even copy ']' if it is first
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2258
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2259 bool prev_was_range = false; // disallow "a-m-z" as a pattern
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2260 bool prev_prev_was_range = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2261 for (; in < len; in++)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2262 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2263 bool was_range = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2264 ch = pattern[in];
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2265 if (ch == ']')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2266 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2267
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2268 if (prev == '-' && in > 1 && isalnum (ch) && ! prev_prev_was_range)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2269 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2270 unsigned char start_of_range = pattern[in-2];
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2271 if (start_of_range < ch
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2272 && ((isupper (ch) && isupper (start_of_range))
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2273 || (islower (ch) && islower (start_of_range))
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2274 || (isdigit (ch) && isdigit (start_of_range))
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2275 || mask['-'] > 1)) // not the first '-'
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2276 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2277 was_range = true;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2278 out--;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2279 mask['-']--;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2280 for (int i = start_of_range; i <= ch; i++)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2281 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2282 if (mask[i] == '\0')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2283 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2284 mask[i] = '\1';
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2285 retval[out++] = i;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2286 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2287 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2288 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2289 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2290 if (! was_range)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2291 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2292 if (mask[ch]++ == 0)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2293 retval[out++] = ch;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2294 else if (ch != '-')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2295 warning_with_id ("octave:textscan-pattern",
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
2296 "%s: [...] contains two '%c's",
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
2297 who.c_str (), ch);
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2298
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2299 if (prev == '-' && mask['-'] >= 2)
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
2300 warning_with_id
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
2301 ("octave:textscan-pattern",
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
2302 "%s: [...] contains two '-'s outside range expressions",
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
2303 who.c_str ());
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2304 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2305 prev = ch;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2306 prev_prev_was_range = prev_was_range;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2307 prev_was_range = was_range;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2308 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2309
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2310 if (flip) // [^...]
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2311 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2312 out = 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2313 for (int i = 0; i < 256; i++)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2314 if (! mask[i])
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2315 retval[out++] = i;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2316 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2317
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2318 retval.resize (out);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2319
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2320 return retval;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2321 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2322
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2323 int
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2324 textscan_format_list::finish_conversion (const std::string& s, size_t& i,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2325 size_t n, unsigned int& width,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2326 int& prec, int& bitwidth,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2327 octave_value& val_type, bool discard,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2328 char& type)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2329 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2330 int retval = 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2331
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2332 std::string char_class;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2333
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2334 size_t beg_idx = std::string::npos;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2335 size_t end_idx = std::string::npos;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2336
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2337 if (type != '%')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2338 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2339 nconv++;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2340 if (type == '[')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2341 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2342 if (i < n)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2343 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2344 beg_idx = i;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2345
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2346 if (s[i] == '^')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2347 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2348 type = '^';
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2349 buf << s[i++];
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2350
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2351 if (i < n)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2352 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2353 beg_idx = i;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2354
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2355 if (s[i] == ']')
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2356 buf << s[i++];
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2357 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2358 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2359 else if (s[i] == ']')
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2360 buf << s[i++];
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2361 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2362
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2363 while (i < n && s[i] != ']')
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2364 buf << s[i++];
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2365
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2366 if (i < n && s[i] == ']')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2367 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2368 end_idx = i-1;
21517
e626d8a3427e avoid possible memory leak related to ostringstream objects
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
2369 buf << s[i++];
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2370 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2371
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2372 if (s[i-1] != ']')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2373 retval = nconv = -1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2374 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2375 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2376
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2377 if (nconv >= 0)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2378 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2379 if (beg_idx != std::string::npos && end_idx != std::string::npos)
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
2380 char_class = parse_char_class (s.substr (beg_idx,
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
2381 end_idx - beg_idx + 1));
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2382
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2383 add_elt_to_list (width, prec, bitwidth, val_type, discard, type,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2384 char_class);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2385 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2386
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2387 return retval;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2388 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2389
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2390 void
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2391 textscan_format_list::printme (void) const
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2392 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2393 size_t n = numel ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2394
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2395 for (size_t i = 0; i < n; i++)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2396 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2397 textscan_format_elt *elt = fmt_elts[i];
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2398
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2399 std::cerr
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2400 << "width: " << elt->width << "\n"
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2401 << "digits " << elt->prec << "\n"
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2402 << "bitwidth: " << elt->bitwidth << "\n"
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2403 << "discard: " << elt->discard << "\n"
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2404 << "type: ";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2405
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2406 if (elt->type == textscan_format_elt::literal_conversion)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2407 std::cerr << "literal text\n";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2408 else if (elt->type == textscan_format_elt::whitespace_conversion)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2409 std::cerr << "whitespace\n";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2410 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2411 std::cerr << elt->type << "\n";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2412
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2413 std::cerr
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2414 << "char_class: `" << undo_string_escapes (elt->char_class) << "'\n"
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2415 << "text: `" << undo_string_escapes (elt->text) << "'\n\n";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2416 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2417 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2418
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2419 // If FORMAT is explicitly "", it is assumed to be "%f" repeated enough
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2420 // times to read the first row of the file. Set it now.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2421
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2422 int
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2423 textscan_format_list::read_first_row (delimited_stream& is, textscan& ts)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2424 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2425 // Read first line and strip end-of-line, which may be two characters
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2426 std::string first_line (20, ' ');
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2427
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2428 is.getline (first_line, static_cast<char> (ts.eol2));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2429
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2430 if (! first_line.empty ()
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2431 && first_line[first_line.length () - 1] == ts.eol1)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2432 first_line.resize (first_line.length () - 1);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2433
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2434 std::istringstream strstr (first_line);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2435 delimited_stream ds (strstr, is);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2436
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2437 dim_vector dv (1,1); // initial size of each output_container
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2438 Complex val;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2439 octave_value val_type;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2440 nconv = 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2441 int max_empty = 1000; // failsafe, if ds fails but not with eof
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2442 int retval = 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2443
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2444 // read line, creating output_container as we go
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2445 while (! ds.eof ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2446 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2447 bool already_skipped_delim = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2448 ts.skip_whitespace (ds);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2449 ds.progress_benchmark ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2450 bool progress = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2451 ts.scan_complex (ds, *fmt_elts[0], val);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2452 if (ds.fail ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2453 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2454 ds.clear (ds.rdstate () & ~std::ios::failbit);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2455
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2456 if (ds.eof ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2457 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2458
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2459 // If we don't continue after a conversion error, then
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2460 // unless this was a missing value (i.e., followed by a delimiter),
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2461 // return with an error status.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2462 if (ts.return_on_error < 2)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2463 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2464 ts.skip_delim (ds);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2465 if (ds.no_progress ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2466 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2467 retval = 4;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2468 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2469 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2470 already_skipped_delim = true;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2471 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2472 else // skip offending field
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2473 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2474 std::ios::iostate state = ds.rdstate ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2475 ds.clear (); // clear to allow read pointer to advance
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2476
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2477 std::string dummy;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2478 textscan_format_elt fe ("", first_line.length ());
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2479 ts.scan_string (ds, fe, dummy);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2480
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2481 progress = (dummy.length ());
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2482 ds.setstate (state);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2483 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2484
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2485 val = ts.empty_value.scalar_value ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2486
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2487 if (! --max_empty)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2488 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2489 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2490
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2491 if (val.imag () == 0)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2492 val_type = octave_value (NDArray (dv, val.real ()));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2493 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2494 val_type = octave_value (ComplexNDArray (dv, val));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2495
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2496 output_container.push_back (val_type);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2497
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2498 if (! already_skipped_delim)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2499 ts.skip_delim (ds);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2500
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2501 if (! progress && ds.no_progress ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2502 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2503
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2504 nconv++;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2505 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2506
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2507 output_container.pop_front (); // discard empty element from constructor
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2508
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2509 // Create fmt_list now that the size is known
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2510 for (octave_idx_type i = 1; i < nconv; i++)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2511 fmt_elts.push_back (new textscan_format_elt (*fmt_elts[0]));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2512
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2513 return retval; // May have returned 4 above.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2514 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2515
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2516 static Cell
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2517 init_inf_nan (void)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2518 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2519 Cell retval (dim_vector (1, 2));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2520
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2521 retval(0) = Cell (octave_value ("inf"));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2522 retval(1) = Cell (octave_value ("nan"));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2523
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2524 return retval;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2525 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2526
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
2527 textscan::textscan (const std::string& who_arg)
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
2528 : who (who_arg), buf (), whitespace_table (), delim_table (),
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
2529 delims (), comment_style (), comment_len (0), comment_char (-2),
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2530 buffer_size (0), date_locale (), inf_nan (init_inf_nan ()),
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2531 empty_value (octave_NaN), exp_chars ("edED"),
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2532 header_lines (0), treat_as_empty (), treat_as_empty_len (0),
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2533 whitespace (" \b\t"), eol1 ('\r'), eol2 ('\n'),
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2534 return_on_error (2), collect_output (false),
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2535 multiple_delims_as_one (false), default_exp (true),
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2536 numeric_delim (false), lines (0)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2537 { }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2538
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2539 octave_value
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
2540 textscan::scan (std::istream& isp, const std::string& fmt,
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
2541 octave_idx_type ntimes, const octave_value_list& options,
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2542 octave_idx_type& count)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2543 {
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
2544 textscan_format_list fmt_list (fmt);
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
2545
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
2546 parse_options (options, fmt_list);
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2547
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2548 octave_value result = do_scan (isp, fmt_list, ntimes);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2549
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2550 // FIXME: this is probably not the best way to get count. The
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2551 // position could easily be larger than octave_idx_type when using
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2552 // 32-bit indexing.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2553
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2554 std::ios::iostate state = isp.rdstate ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2555 isp.clear ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2556 count = static_cast<octave_idx_type> (isp.tellg ());
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2557 isp.setstate (state);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2558
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2559 return result;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2560 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2561
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2562 octave_value
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2563 textscan::do_scan (std::istream& isp, textscan_format_list& fmt_list,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2564 octave_idx_type ntimes)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2565 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2566 octave_value retval;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2567
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2568 if (fmt_list.num_conversions () == -1)
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
2569 error ("%s: invalid format specified", who.c_str ());
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2570
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2571 if (fmt_list.num_conversions () == 0)
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
2572 error ("%s: no valid format conversion specifiers", who.c_str ());
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2573
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2574 // skip the first header_lines
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2575 std::string dummy;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2576 for (int i = 0; i < header_lines && isp; i++)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2577 getline (isp, dummy, static_cast<char> (eol2));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2578
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2579 // Create our own buffered stream, for fast get/putback/tell/seek.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2580
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2581 // First, see how far ahead it should let us look.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2582 int max_lookahead = std::max (std::max (comment_len, treat_as_empty_len),
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2583 std::max (delim_len, 3)); // 3 for NaN and Inf
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2584
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2585 // Next, choose a buffer size to avoid reading too much, or too often.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2586 octave_idx_type buf_size = 4096;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2587 if (buffer_size)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2588 buf_size = buffer_size;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2589 else if (ntimes > 0)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2590 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2591 // Avoid overflow of 80*ntimes...
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2592 buf_size = std::min (buf_size, std::max (ntimes, 80 * ntimes));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2593 buf_size = std::max (buf_size, ntimes);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2594 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2595 // Finally, create the stream.
21552
102b33b53ea4 textscan: Use correct delimiters for file buffering (bug #47539)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21551
diff changeset
2596 delimited_stream is (isp,
102b33b53ea4 textscan: Use correct delimiters for file buffering (bug #47539)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21551
diff changeset
2597 (delim_table.empty () ? whitespace + "\r\n" : delims),
102b33b53ea4 textscan: Use correct delimiters for file buffering (bug #47539)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21551
diff changeset
2598 max_lookahead, buf_size);
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2599
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2600 // Grow retval dynamically. "size" is half the initial size
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2601 // (FIXME -- Should we start smaller if ntimes is large?)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2602 octave_idx_type size = ((ntimes < 8 && ntimes >= 0) ? ntimes : 1);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2603 Array<octave_idx_type> row_idx (dim_vector (1,2));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2604 row_idx(1) = 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2605
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2606 int err = 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2607 octave_idx_type row = 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2608
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2609 if (multiple_delims_as_one) // bug #44750?
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2610 skip_delim (is);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2611
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2612 int done_after; // Number of columns read when EOF seen.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2613
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2614 // If FORMAT explicitly "", read first line and see how many "%f" match
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2615 if (fmt_list.set_from_first)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2616 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2617 err = fmt_list.read_first_row (is, *this);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2618 lines = 1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2619
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2620 done_after = fmt_list.numel () + 1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2621 if (! err)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2622 row = 1; // the above puts the first line into fmt_list.out_buf ()
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2623 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2624 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2625 done_after = fmt_list.out_buf ().size () + 1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2626
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2627 std::list<octave_value> out = fmt_list.out_buf ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2628
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2629 // We will later merge adjacent columns of the same type.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2630 // Check now which columns to merge.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2631 // Reals may become complex, and so we can't trust types
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2632 // after reading in data.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2633 // If the format was "", that conversion may already have happened,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2634 // so force all to be merged (as all are %f).
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2635 bool merge_with_prev[fmt_list.numel ()];
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2636 int conv = 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2637 if (collect_output)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2638 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2639 int prev_type = -1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2640 for (std::list<octave_value>::iterator col = out.begin ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2641 col != out.end (); col++)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2642 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2643 if (col->type_id () == prev_type
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2644 || (fmt_list.set_from_first && prev_type != -1))
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2645 merge_with_prev [conv++] = true;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2646 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2647 merge_with_prev [conv++] = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2648
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2649 prev_type = col->type_id ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2650 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2651 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2652
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2653 // This should be caught by earlier code, but this avoids a possible
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2654 // infinite loop below.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2655 if (fmt_list.num_conversions () == 0)
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
2656 error ("%s: No conversions specified", who.c_str ());
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2657
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2658 // Read the data. This is the main loop.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2659 if (! err)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2660 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2661 for (/* row set ~30 lines above */; row < ntimes || ntimes == -1; row++)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2662 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2663 if (row == 0 || row >= size)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2664 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2665 size += size+1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2666 for (std::list<octave_value>::iterator col = out.begin ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2667 col != out.end (); col++)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2668 *col = (*col).resize (dim_vector (size, 1), 0);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2669 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2670
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2671 row_idx(0) = row;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2672 err = read_format_once (is, fmt_list, out, row_idx, done_after);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2673
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2674 if (err > 0 || ! is || (lines >= ntimes && ntimes > -1))
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2675 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2676 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2677 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2678
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2679 if ((err & 4) && ! return_on_error)
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
2680 error ("%s: Read error in field %d of row %d", who.c_str (),
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2681 done_after + 1, row + 1);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2682
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2683 // If file does not end in EOL, do not pad columns with NaN.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2684 bool uneven_columns = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2685 if (err & 4)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2686 uneven_columns = true;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2687 else if (isp.eof ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2688 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2689 isp.clear ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2690 isp.seekg (-1, std::ios_base::end);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2691 int last_char = isp.get ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2692 isp.setstate (isp.eofbit);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2693 uneven_columns = (last_char != eol1 && last_char != eol2);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2694 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2695
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2696 // convert return value to Cell array
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2697 Array<octave_idx_type> ra_idx (dim_vector (1,2));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2698
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2699 // (err & 1) means "error, and no columns read this row
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2700 // FIXME -- This may redundant now that done_after=0 says the same
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2701 if (err & 1)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2702 done_after = out.size () + 1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2703
21532
936ab0fca2f7 Textscan returns emptyVal for rows with literal but no conversion (bug #47458)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21517
diff changeset
2704 int valid_rows = (row == ntimes) ? ntimes
936ab0fca2f7 Textscan returns emptyVal for rows with literal but no conversion (bug #47458)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21517
diff changeset
2705 : (((err & 1) && (err & 8)) ? row : row+1);
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2706 dim_vector dv (valid_rows, 1);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2707
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2708 ra_idx(0) = 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2709 int i = 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2710 if (! collect_output)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2711 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2712 retval = Cell (dim_vector (1, out.size ()));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2713 for (std::list<octave_value>::iterator col = out.begin ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2714 col != out.end (); col++, i++)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2715 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2716 // trim last columns if that was requested
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2717 if (i == done_after && uneven_columns)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2718 dv = dim_vector (std::max (valid_rows - 1, 0), 1);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2719
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2720 ra_idx(1) = i;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2721 retval = do_cat_op (retval, octave_value (Cell (col->resize (dv,0))),
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2722 ra_idx);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2723 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2724 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2725 else // group adjacent cells of the same type into a single cell
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2726 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2727 octave_value cur; // current cell, accumulating columns
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2728 octave_idx_type group_size = 0; // columns in this cell
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2729 int prev_type = -1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2730
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2731 conv = 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2732 retval = Cell ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2733 for (std::list<octave_value>::iterator col = out.begin ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2734 col != out.end (); col++)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2735 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2736 if (! merge_with_prev [conv++]) // including first time
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2737 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2738 if (prev_type != -1)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2739 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2740 ra_idx(1) = i++;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2741 retval = do_cat_op (retval, octave_value (Cell (cur)),
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2742 ra_idx);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2743 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2744 cur = octave_value (col->resize (dv,0));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2745 group_size = 1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2746 prev_type = col->type_id ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2747 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2748 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2749 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2750 ra_idx(1) = group_size++;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2751 cur = do_cat_op (cur, octave_value (col->resize (dv,0)),
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2752 ra_idx);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2753 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2754 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2755 ra_idx(1) = i;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2756 retval = do_cat_op (retval, octave_value (Cell (cur)), ra_idx);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2757 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2758
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2759 return retval;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2760 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2761
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2762 // Calculate x^n. Used for ...e+nn so that, for example, 1e2 is
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2763 // exactly 100 and 5e-1 is 1/2
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2764
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2765 static double
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2766 pown (double x, unsigned int n)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2767 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2768 double retval = 1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2769
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2770 for (unsigned int d = n; d; d >>= 1)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2771 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2772 if (d & 1)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2773 retval *= x;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2774 x *= x;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2775 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2776
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2777 return retval;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2778 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2779
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2780 // Read a double considering the "precision" field of FMT and the
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2781 // EXP_CHARS option of OPTIONS.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2782
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2783 double
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2784 textscan::read_double (delimited_stream& is,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2785 const textscan_format_elt& fmt) const
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2786 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2787 int sign = 1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2788 unsigned int width_left = fmt.width;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2789 double retval = 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2790 bool valid = false; // syntactically correct double?
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2791
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2792 int ch = is.peek ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2793
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2794 if (ch == '+')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2795 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2796 is.get ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2797 ch = is.peek ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2798 if (width_left)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2799 width_left--;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2800 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2801 else if (ch == '-')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2802 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2803 sign = -1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2804 is.get ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2805 ch = is.peek ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2806 if (width_left)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2807 width_left--;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2808 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2809
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2810 // Read integer part
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2811 if (ch != '.')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2812 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2813 if (ch >= '0' && ch <= '9') // valid if at least one digit
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2814 valid = true;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2815 while (width_left-- && is && (ch = is.get ()) >= '0' && ch <= '9')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2816 retval = retval * 10 + (ch - '0');
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2817 width_left++;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2818 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2819
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2820 // Read fractional part, up to specified precision
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2821 if (ch == '.' && width_left)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2822 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2823 double multiplier = 1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2824 int precision = fmt.prec;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2825 int i;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2826
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2827 if (width_left)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2828 width_left--; // Consider width of '.'
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2829
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2830 if (precision == -1)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2831 precision = 1<<30; // FIXME Should be MAXINT
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2832
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2833 if (! valid) // if there was nothing before '.'...
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2834 is.get (); // ...ch was a "peek", not "get".
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2835
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2836 for (i = 0; i < precision; i++)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2837 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2838 if (width_left-- && is && (ch = is.get ()) >= '0' && ch <= '9')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2839 retval += (ch - '0') * (multiplier *= 0.1);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2840 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2841 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2842 width_left++;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2843 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2844 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2845 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2846
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2847 // round up if we truncated and the next digit is >= 5
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2848 if ((i == precision || ! width_left) && (ch = is.get ()) >= '5'
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2849 && ch <= '9')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2850 retval += multiplier;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2851
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2852 if (i > 0)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2853 valid = true; // valid if at least one digit after '.'
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2854
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2855 // skip remainder after '.', to field width, to look for exponent
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2856 if (i == precision)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2857 while (width_left-- && is && (ch = is.get ()) >= '0' && ch <= '9')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2858 ; // discard
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2859
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2860 width_left++;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2861 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2862
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2863 // look for exponent part in, e.g., 6.023E+23
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2864 bool used_exp = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2865 if (valid && width_left > 1 && exp_chars.find (ch) != std::string::npos)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2866 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2867 int ch1 = is.peek ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2868 if (ch1 == '-' || ch1 == '+' || (ch1 >= '0' && ch1 <= '9'))
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2869 { // if 1.0e+$ or some such, this will set failbit, as we want
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2870 width_left--; // count "E"
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2871 int exp = 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2872 int exp_sign = 1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2873 if (ch1 == '+')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2874 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2875 if (width_left)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2876 width_left--;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2877 is.get ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2878 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2879 else if (ch1 == '-')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2880 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2881 exp_sign = -1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2882 is.get ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2883 if (width_left)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2884 width_left--;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2885 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2886 valid = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2887 while (width_left-- && is && (ch = is.get ()) >= '0' && ch1 <= '9')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2888 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2889 exp = exp*10 + ch - '0';
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2890 valid = true;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2891 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2892 width_left++;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2893 if (ch != std::istream::traits_type::eof () && width_left)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2894 is.putback (ch);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2895
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2896 double multiplier = pown (10, exp);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2897 if (exp_sign > 0)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2898 retval *= multiplier;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2899 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2900 retval /= multiplier;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2901
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2902 used_exp = true;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2903 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2904 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2905 is.clear ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2906 if (! used_exp && ch != std::istream::traits_type::eof () && width_left)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2907 is.putback (ch);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2908
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2909 // Check for +/- inf and NaN
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2910 if (! valid && width_left >= 3)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2911 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2912 int i = lookahead (is, inf_nan, 3, false); // false -> case insensitive
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2913 if (i == 0)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2914 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2915 retval = octave_Inf;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2916 valid = true;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2917 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2918 else if (i == 1)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2919 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2920 retval = octave_NaN;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2921 valid = true;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2922 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2923 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2924
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2925 // Check for +/- inf and NaN
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2926 if (! valid && width_left >= 3)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2927 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2928 int i = lookahead (is, inf_nan, 3, false); // false -> case insensitive
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2929 if (i == 0)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2930 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2931 retval = octave_Inf;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2932 valid = true;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2933 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2934 else if (i == 1)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2935 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2936 retval = octave_NaN;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2937 valid = true;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2938 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2939 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2940
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2941 if (! valid)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2942 is.setstate (std::ios::failbit);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2943 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2944 is.setstate (is.rdstate () & ~std::ios::failbit);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2945
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2946 return retval * sign;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2947 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2948
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2949 // Read a single number: real, complex, inf, NaN, possibly with limited
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2950 // precision. Calls to this should be preceded by skip_whitespace.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2951 // Calling that inside scan_complex would violate its const declaration.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2952
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2953 void
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2954 textscan::scan_complex (delimited_stream& is, const textscan_format_elt& fmt,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2955 Complex& val) const
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2956 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2957 double im = 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2958 double re = 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2959 bool as_empty = false; // did we fail but match a "treat_as_empty" string?
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2960 bool inf = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2961
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2962 int ch = is.peek ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2963 if (ch == '+' || ch == '-') // check for [+-][ij] with no coefficients
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2964 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2965 ch = is.get ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2966 int ch2 = is.peek ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2967 if (ch2 == 'i' || ch2 == 'j')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2968 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2969 double value = 1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2970 is.get ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2971 // Check not -inf
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2972 if (is.peek () == 'n')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2973 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2974 char *pos = is.tellg ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2975 std::ios::iostate state = is.rdstate ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2976
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2977 is.get ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2978 ch2 = is.get ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2979 if (ch2 == 'f')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2980 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2981 inf = true;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2982 re = (ch == '+') ? octave_Inf : -octave_Inf;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2983 value = 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2984 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2985 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2986 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2987 is.clear (state);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2988 is.seekg (pos); // reset to position before look-ahead
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2989 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2990 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2991
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2992 im = (ch == '+') ? value : -value;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2993 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2994 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2995 is.putback (ch);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2996 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2997
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2998 if (! im && ! inf) // if not [+-][ij] or [+-]inf, read real normally
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2999 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3000 char *pos = is.tellg ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3001 std::ios::iostate state = is.rdstate ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3002 //re = octave_read_value<double> (is);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3003 re = read_double (is, fmt);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3004
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3005 // check for "treat as empty" string
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3006 if (treat_as_empty.numel ()
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3007 && (is.fail () || octave_is_NaN_or_NA (Complex (re))
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3008 || re == octave_Inf))
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3009 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3010
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3011 for (int i = 0; i < treat_as_empty.numel (); i++)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3012 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3013 if (ch == treat_as_empty (i).string_value ()[0])
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3014 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3015 as_empty = true; // first char matches, so read the lot
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3016 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3017 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3018 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3019 if (as_empty) // if first char matched...
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3020 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3021 as_empty = false; // ...look for the whole string
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3022
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3023 is.clear (state); // treat_as_empty "-" causes partial read
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3024 is.seekg (pos); // reset to position before failed read
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3025
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3026 // treat_as_empty strings may be different sizes.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3027 // Read ahead longest, put it all back, then re-read the string
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3028 // that matches.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3029 std::string look_buf (treat_as_empty_len, '\0');
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3030 char *look = is.read (&look_buf[0], look_buf.size (), pos);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3031
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3032 is.clear (state);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3033 is.seekg (pos); // reset to position before look-ahead
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3034 // FIXME -- is.read could invalidate pos
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3035
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3036 for (int i = 0; i < treat_as_empty.numel (); i++)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3037 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3038 std::string s = treat_as_empty (i).string_value ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3039 if (! strncmp (s.c_str (), look, s.size ()))
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3040 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3041 as_empty = true;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3042 // read just the right amount
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3043 is.read (&look_buf[0], s.size (), pos);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3044 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3045 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3046 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3047 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3048 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3049
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3050 if (! is.eof () && ! as_empty)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3051 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3052 state = is.rdstate (); // before tellg, since that fails at EOF
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3053 pos = is.tellg ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3054 ch = is.peek (); // ch == EOF if read failed; no need to chk fail
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3055 if (ch == 'i' || ch == 'j') // pure imaginary
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3056 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3057 is.get ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3058 im = re;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3059 re = 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3060 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3061 else if (ch == '+' || ch == '-') // see if it is real+imag[ij]
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3062 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3063 // save stream state in case we have to restore it
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3064 pos = is.tellg ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3065 state = is.rdstate ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3066
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3067 //im = octave_read_value<double> (is);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3068 im = read_double (is, fmt);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3069 if (is.fail ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3070 im = 1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3071
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3072 if (is.peek () == 'i' || is.peek () == 'j')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3073 is.get ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3074 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3075 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3076 im = 0; // no valid imaginary part. Restore state
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3077 is.clear (state); // eof shouldn't cause fail.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3078 is.seekg (pos);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3079 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3080 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3081 else if (is.eof ()) // we've read enough to be a "valid" read
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3082 is.clear (state); // failed peek shouldn't cause fail
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3083 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3084 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3085 if (as_empty)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3086 val = empty_value.scalar_value ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3087 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3088 val = Complex (re, im);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3089 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3090
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3091 // Return in VAL the run of characters from IS NOT contained in PATTERN.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3092
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3093 int
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3094 textscan::scan_caret (delimited_stream& is, const std::string& pattern,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3095 std::string& val) const
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3096 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3097 int c1 = std::istream::traits_type::eof ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3098 std::ostringstream obuf; // Is this optimised for growing?
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3099
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3100 while (is && ((c1 = (is && ! is.eof ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3101 ? is.get_undelim ()
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3102 : std::istream::traits_type::eof ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3103 != std::istream::traits_type::eof ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3104 && pattern.find (c1) == std::string::npos)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3105 obuf << static_cast<char> (c1);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3106
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3107 val = obuf.str ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3108
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3109 if (c1 != std::istream::traits_type::eof ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3110 is.putback (c1);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3111
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3112 return c1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3113 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3114
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3115 // Read until one of the strings in DELIMITERS is found. For
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3116 // efficiency, ENDS is a list of the last character of each delimiter.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3117
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3118 std::string
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3119 textscan::read_until (delimited_stream& is, const Cell& delimiters,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3120 const std::string& ends) const
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3121 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3122 std::string retval ("");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3123 bool done = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3124 do
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3125 { // find sequence ending with an ending char
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3126 std::string next;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3127 scan_caret (is, ends.c_str (), next);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3128 retval = retval + next; // FIXME -- could use repeated doubling of size
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3129
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3130 int last = (! is.eof ()
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3131 ? is.get_undelim () : std::istream::traits_type::eof ());
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3132
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3133 if (last != std::istream::traits_type::eof ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3134 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3135 retval = retval + static_cast<char> (last);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3136 for (int i = 0; i < delimiters.numel (); i++)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3137 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3138 std::string delim = delimiters(i).string_value ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3139 size_t start = (retval.length () > delim.length ()
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3140 ? retval.length () - delim.length ()
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3141 : 0);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3142 std::string may_match = retval.substr (start);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3143 if (may_match == delim)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3144 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3145 done = true;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3146 retval = retval.substr (0, start);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3147 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3148 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3149 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3150 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3151 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3152 while (! done && is && ! is.eof ());
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3153
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3154 return retval;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3155 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3156
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3157
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3158 // Read stream until either fmt.width chars have been read, or
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3159 // options.delimiter has been found. Does *not* rely on fmt being 's'.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3160 // Used by formats like %6f to limit to 6.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3161
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3162 void
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3163 textscan::scan_string (delimited_stream& is, const textscan_format_elt& fmt,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3164 std::string& val) const
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3165 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3166 if (delim_list.numel () == 0)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3167 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3168 unsigned int i = 0;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3169 unsigned int width = fmt.width;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3170
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3171 for (i = 0; i < width; i++)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3172 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3173 if (i+1 > val.length ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3174 val = val + val + ' '; // grow even if empty
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3175 int ch = is.get ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3176 if (is_delim (ch) || ch == std::istream::traits_type::eof ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3177 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3178 is.putback (ch);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3179 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3180 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3181 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3182 val[i] = ch;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3183 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3184 val = val.substr (0, i); // trim pre-allocation
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3185 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3186 else // Cell array of multi-character delimiters
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3187 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3188 std::string ends ("");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3189 for (int i = 0; i < delim_list.numel (); i++)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3190 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3191 std::string tmp = delim_list(i).string_value ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3192 ends += tmp.substr (tmp.length () - 1);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3193 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3194 val = textscan::read_until (is, delim_list, ends);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3195 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3196 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3197
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3198 // Return in VAL the run of characters from IS contained in PATTERN.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3199
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3200 int
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3201 textscan::scan_bracket (delimited_stream& is, const std::string& pattern,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3202 std::string& val) const
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3203 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3204 int c1 = std::istream::traits_type::eof ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3205 std::ostringstream obuf; // Is this optimised for growing?
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3206
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3207 while (is && pattern.find (c1 = is.get_undelim ()) != std::string::npos)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3208 obuf << static_cast<char> (c1);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3209
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3210 val = obuf.str ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3211 if (c1 != std::istream::traits_type::eof ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3212 is.putback (c1);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3213 return c1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3214 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3215
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3216 // Return in VAL a string, either delimited by whitespace/delimiters, or
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3217 // enclosed in a pair of double quotes ("..."). Enclosing quotes are
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3218 // removed. A consecutive pair "" is inserted into VAL as a single ".
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3219
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3220 void
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3221 textscan::scan_qstring (delimited_stream& is, const textscan_format_elt& fmt,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3222 std::string& val)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3223 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3224 skip_whitespace (is);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3225
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3226 if (is.peek () != '\"')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3227 scan_string (is, fmt, val);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3228 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3229 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3230 is.get ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3231 scan_caret (is, "\"", val); // read everything until "
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3232 is.get (); // swallow "
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3233
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3234 while (is && is.peek () == '\"') // if double ", insert one in stream,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3235 { // and keep looking for single "
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3236 is.get ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3237 std::string val1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3238 scan_caret (is, "\"", val1);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3239 val = val + "\"" + val1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3240 is.get_undelim ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3241 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3242 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3243 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3244
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3245 // Read from IS into VAL a string of the next fmt.width characters,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3246 // including any whitespace or delimiters.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3247
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3248 void
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3249 textscan::scan_cstring (delimited_stream& is, const textscan_format_elt& fmt,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3250 std::string& val) const
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3251 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3252 val.resize (fmt.width);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3253
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3254 for (unsigned int i = 0; is && i < fmt.width; i++)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3255 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3256 int ch = is.get_undelim ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3257 if (ch != std::istream::traits_type::eof ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3258 val[i] = ch;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3259 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3260 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3261 val.resize (i);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3262 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3263 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3264 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3265 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3266
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3267
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3268 // Read a single '%...' conversion and place it in position ROW of OV.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3269
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3270 void
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3271 textscan::scan_one (delimited_stream& is, const textscan_format_elt& fmt,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3272 octave_value& ov, Array<octave_idx_type> row)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3273 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3274 skip_whitespace (is);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3275
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3276 is.clear ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3277
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3278 octave_value val;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3279 if (fmt.numeric)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3280 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3281 if (fmt.type == 'f' || fmt.type == 'n')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3282 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3283 Complex v;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3284 skip_whitespace (is);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3285 scan_complex (is, fmt, v);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3286
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3287 if (! fmt.discard && ! is.fail ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3288 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3289 if (fmt.bitwidth == 64)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3290 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3291 if (ov.is_real_type () && v.imag () == 0)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3292 ov.internal_rep ()->fast_elem_insert (row(0), v.real ());
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3293 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3294 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3295 if (ov.is_real_type ()) // cat does type conversion
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3296 ov = do_cat_op (ov, octave_value (v), row);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3297 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3298 ov.internal_rep ()->fast_elem_insert (row(0), v);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3299 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3300 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3301 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3302 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3303 if (ov.is_real_type () && v.imag () == 0)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3304 ov.internal_rep ()->fast_elem_insert (row(0),
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3305 float (v.real ()));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3306 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3307 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3308 if (ov.is_real_type ()) // cat does type conversion
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3309 ov = do_cat_op (ov, octave_value (v), row);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3310 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3311 ov.internal_rep ()->fast_elem_insert (row(0),
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3312 FloatComplex (v));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3313 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3314 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3315 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3316 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3317 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3318 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3319 double v; // Matlab docs say 1e30 etc should be valid for %d and
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3320 // 1000 as a %d8 should be 127, so read as double.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3321 // Some loss of precision for d64 and u64.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3322 skip_whitespace (is);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3323 v = read_double (is, fmt);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3324 if (! fmt.discard && ! is.fail ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3325 switch (fmt.bitwidth)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3326 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3327 case 64:
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3328 switch (fmt.type)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3329 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3330 case 'd':
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3331 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3332 octave_int64 vv = v;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3333 ov.internal_rep ()->fast_elem_insert (row(0), vv);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3334 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3335 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3336
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3337 case 'u':
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3338 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3339 octave_uint64 vv = v;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3340 ov.internal_rep ()->fast_elem_insert (row(0), vv);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3341 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3342 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3343 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3344 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3345
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3346 case 32:
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3347 switch (fmt.type)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3348 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3349 case 'd':
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3350 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3351 octave_int32 vv = v;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3352 ov.internal_rep ()->fast_elem_insert (row(0), vv);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3353 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3354 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3355
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3356 case 'u':
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3357 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3358 octave_uint32 vv = v;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3359 ov.internal_rep ()->fast_elem_insert (row(0), vv);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3360 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3361 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3362 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3363 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3364
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3365 case 16:
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3366 if (fmt.type == 'd')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3367 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3368 octave_int16 vv = v;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3369 ov.internal_rep ()->fast_elem_insert (row(0), vv);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3370 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3371 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3372 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3373 octave_uint16 vv = v;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3374 ov.internal_rep ()->fast_elem_insert (row(0), vv);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3375 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3376 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3377
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3378 case 8:
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3379 if (fmt.type == 'd')
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3380 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3381 octave_int8 vv = v;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3382 ov.internal_rep ()->fast_elem_insert (row(0), vv);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3383 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3384 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3385 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3386 octave_uint8 vv = v;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3387 ov.internal_rep ()->fast_elem_insert (row(0), vv);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3388 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3389 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3390 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3391 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3392
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3393 if (is.fail ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3394 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3395 if (! fmt.discard)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3396 ov = do_cat_op (ov, empty_value, row);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3397
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3398 // If we are continuing after errors, skip over this field
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3399 if (return_on_error == 2)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3400 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3401 std::ios::iostate state = is.rdstate ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3402 is.clear (); // clear to allow read pointer to advance
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3403
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3404 std::string dummy;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3405 scan_string (is, fmt, dummy);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3406
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3407 is.setstate (state);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3408 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3409 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3410
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3411 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3412 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3413 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3414 std::string vv (" "); // initial buffer. Grows as needed
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3415 switch (fmt.type)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3416 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3417 case 's':
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3418 scan_string (is, fmt, vv);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3419 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3420
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3421 case 'q':
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3422 scan_qstring (is, fmt, vv);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3423 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3424
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3425 case 'c':
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3426 scan_cstring (is, fmt, vv);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3427 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3428
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3429 case '[':
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3430 scan_bracket (is, fmt.char_class.c_str (), vv);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3431 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3432
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3433 case '^':
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3434 scan_caret (is, fmt.char_class.c_str (), vv);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3435 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3436 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3437
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3438 if (! fmt.discard)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3439 ov.internal_rep ()->fast_elem_insert (row (0),
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3440 Cell (octave_value (vv)));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3441
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3442 // FIXME -- why does failbit get set at EOF, instead of eofbit?
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3443 if (! vv.empty ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3444 is.clear (is.rdstate () & ~std::ios_base::failbit);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3445 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3446
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3447 is.field_done ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3448 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3449
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3450 // Read data corresponding to the entire format string once, placing the
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3451 // values in row ROW of retval.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3452
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3453 int
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3454 textscan::read_format_once (delimited_stream& is,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3455 textscan_format_list& fmt_list,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3456 std::list<octave_value>& retval,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3457 Array<octave_idx_type> row, int& done_after)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3458 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3459 const textscan_format_elt *elem = fmt_list.first ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3460 std::list<octave_value>::iterator out = retval.begin ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3461 bool no_conversions = true;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3462 bool done = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3463 bool conversion_failed = false; // Record for ReturnOnError
21532
936ab0fca2f7 Textscan returns emptyVal for rows with literal but no conversion (bug #47458)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21517
diff changeset
3464 bool nothing_worked = true;
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3465
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3466 octave_quit ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3467
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3468 for (size_t i = 0; i < fmt_list.numel (); i++)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3469 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3470 bool this_conversion_failed = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3471
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3472 // Clear fail of previous numeric conversions.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3473 is.clear ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3474
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3475 switch (elem->type)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3476 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3477 case 'C':
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3478 case 'D':
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3479 warning ("%s: conversion %c not yet implemented",
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3480 who.c_str (), elem->type);
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3481 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3482
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3483 case 'u':
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3484 case 'd':
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3485 case 'f':
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3486 case 'n':
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3487 case 's':
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3488 case '[':
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3489 case '^':
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3490 case 'q':
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3491 case 'c':
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3492 scan_one (is, *elem, *out, row);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3493 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3494
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3495 case textscan_format_elt::literal_conversion :
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3496 match_literal (is, *elem);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3497 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3498
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3499 default:
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3500 error ("Unknown format element '%c'", elem->type);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3501 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3503 if (! is.fail ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3504 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3505 if (! elem->discard)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3506 no_conversions = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3507 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3508 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3509 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3510 if (return_on_error < 2)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3511 this_conversion_failed = true;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3512
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3513 is.clear (is.rdstate () & ~std::ios::failbit);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3514 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3515
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3516 if (! elem->discard)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3517 out++;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3518
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3519 elem = fmt_list.next ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3520 char *pos = is.tellg ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3521
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3522 // FIXME -- these conversions "ignore delimiters". Should they include
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3523 // delimiters at the start of the conversion, or can those be skipped?
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3524 if (elem->type != textscan_format_elt::literal_conversion
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3525 // && elem->type != '[' && elem->type != '^' && elem->type != 'c'
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3526 )
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3527 skip_delim (is);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3528
21532
936ab0fca2f7 Textscan returns emptyVal for rows with literal but no conversion (bug #47458)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21517
diff changeset
3529 if (is.eof ())
936ab0fca2f7 Textscan returns emptyVal for rows with literal but no conversion (bug #47458)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21517
diff changeset
3530 {
936ab0fca2f7 Textscan returns emptyVal for rows with literal but no conversion (bug #47458)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21517
diff changeset
3531 if (! done)
936ab0fca2f7 Textscan returns emptyVal for rows with literal but no conversion (bug #47458)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21517
diff changeset
3532 done_after = i+1;
936ab0fca2f7 Textscan returns emptyVal for rows with literal but no conversion (bug #47458)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21517
diff changeset
3533
936ab0fca2f7 Textscan returns emptyVal for rows with literal but no conversion (bug #47458)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21517
diff changeset
3534 // note EOF, but process others to get empty_val.
936ab0fca2f7 Textscan returns emptyVal for rows with literal but no conversion (bug #47458)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21517
diff changeset
3535 done = true;
936ab0fca2f7 Textscan returns emptyVal for rows with literal but no conversion (bug #47458)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21517
diff changeset
3536 }
936ab0fca2f7 Textscan returns emptyVal for rows with literal but no conversion (bug #47458)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21517
diff changeset
3537
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3538 if (this_conversion_failed)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3539 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3540 if (is.tellg () == pos && ! conversion_failed)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3541 { // done_after = first failure
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3542 done_after = i; // note fail, but parse others to get empty_val
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3543 conversion_failed = true;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3544 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3545 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3546 this_conversion_failed = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3547 }
21532
936ab0fca2f7 Textscan returns emptyVal for rows with literal but no conversion (bug #47458)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21517
diff changeset
3548 else if (! done && !conversion_failed)
936ab0fca2f7 Textscan returns emptyVal for rows with literal but no conversion (bug #47458)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21517
diff changeset
3549 nothing_worked = false;
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3550 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3551
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3552 if (done)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3553 is.setstate (std::ios::eofbit);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3554
21532
936ab0fca2f7 Textscan returns emptyVal for rows with literal but no conversion (bug #47458)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21517
diff changeset
3555 return no_conversions + (is.eof () ? 2 : 0)
936ab0fca2f7 Textscan returns emptyVal for rows with literal but no conversion (bug #47458)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21517
diff changeset
3556 + (conversion_failed ? 4 : 0)
936ab0fca2f7 Textscan returns emptyVal for rows with literal but no conversion (bug #47458)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21517
diff changeset
3557 + (nothing_worked ? 8 : 0);
936ab0fca2f7 Textscan returns emptyVal for rows with literal but no conversion (bug #47458)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21517
diff changeset
3558
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3559 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3560
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3561 void
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3562 textscan::parse_options (const octave_value_list& args,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3563 textscan_format_list& fmt_list)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3564 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3565 int last = args.length ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3566 int n = last;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3567
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3568 if (n & 1)
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3569 error ("%s: %d parameters given, but only %d values",
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3570 who.c_str (), n-n/2, n/2);
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3571
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3572 delim_len = 1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3573 bool have_delims = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3574 for (int i = 0; i < last; i += 2)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3575 {
21538
eb15bc7c5c0b textscan: Use error-checking value extractors for simpler parameter parsing
Mike Miller <mtmiller@octave.org>
parents: 21532
diff changeset
3576 std::string param = args(i).xstring_value ("%s: Invalid paramter type <%s> for parameter %d",
eb15bc7c5c0b textscan: Use error-checking value extractors for simpler parameter parsing
Mike Miller <mtmiller@octave.org>
parents: 21532
diff changeset
3577 who.c_str (),
eb15bc7c5c0b textscan: Use error-checking value extractors for simpler parameter parsing
Mike Miller <mtmiller@octave.org>
parents: 21532
diff changeset
3578 args(i).type_name ().c_str (),
eb15bc7c5c0b textscan: Use error-checking value extractors for simpler parameter parsing
Mike Miller <mtmiller@octave.org>
parents: 21532
diff changeset
3579 i/2 + 1);
eb15bc7c5c0b textscan: Use error-checking value extractors for simpler parameter parsing
Mike Miller <mtmiller@octave.org>
parents: 21532
diff changeset
3580 std::transform (param.begin (), param.end (), param.begin (), ::tolower);
eb15bc7c5c0b textscan: Use error-checking value extractors for simpler parameter parsing
Mike Miller <mtmiller@octave.org>
parents: 21532
diff changeset
3581
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3582 if (param == "delimiter")
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3583 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3584 bool invalid = true;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3585 if (args(i+1).is_string ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3586 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3587 invalid = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3588 have_delims = true;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3589 delims = args(i+1).string_value ();
21551
990c6c31a684 Accept escape sequences in single quoted strings for "delimiter" (bug #47539)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21538
diff changeset
3590 if (args(i+1).is_sq_string ())
990c6c31a684 Accept escape sequences in single quoted strings for "delimiter" (bug #47539)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21538
diff changeset
3591 delims = do_string_escapes (delims);
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3592 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3593 else if (args(i+1).is_cell ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3594 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3595 invalid = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3596 delim_list = args(i+1).cell_value ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3597 delim_table = " "; // non-empty, to flag non-default delim
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3598
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3599 // Check that all elements are strings, and find max length
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3600 for (int j = 0; j < delim_list.numel (); j++)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3601 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3602 if (! delim_list(j).is_string ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3603 invalid = true;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3604 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3605 {
21551
990c6c31a684 Accept escape sequences in single quoted strings for "delimiter" (bug #47539)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21538
diff changeset
3606 if (delim_list(j).is_sq_string ())
990c6c31a684 Accept escape sequences in single quoted strings for "delimiter" (bug #47539)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21538
diff changeset
3607 delim_list(j) = do_string_escapes (delim_list(j)
990c6c31a684 Accept escape sequences in single quoted strings for "delimiter" (bug #47539)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21538
diff changeset
3608 .string_value ());
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3609 octave_idx_type len = delim_list(j).string_value ()
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3610 .length ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3611 delim_len = std::max (static_cast<int> (len), delim_len);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3612 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3613 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3614 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3615 if (invalid)
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3616 error ("%s: Delimiters must be either a string or cell array of strings",
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3617 who.c_str ());
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3618 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3619 else if (param == "commentstyle")
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3620 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3621 if (args(i+1).is_string ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3622 { // check here for names like "C++", "C", "shell", ...?
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3623 comment_style = Cell (args(i+1));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3624 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3625 else if (args(i+1).is_cell ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3626 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3627 comment_style = args(i+1).cell_value ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3628 int len = comment_style.numel ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3629 if ((len >= 1 && ! comment_style (0).is_string ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3630 || (len >= 2 && ! comment_style (1).is_string ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3631 || (len >= 3))
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3632 error ("%s: CommentStyle must be either a string or cell array of one or two strings",
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3633 who.c_str ());
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3634 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3635 else
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3636 error ("%s: CommentStyle must be either a string or cell array of one or two strings",
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3637 who.c_str ());
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3638
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3639 // How far ahead do we need to look to detect an open comment
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3640 // and which character do we look for?
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3641 if (comment_style.numel () >= 1)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3642 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3643 comment_len = comment_style (0).string_value ().size ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3644 comment_char = comment_style (0).string_value ()[0];
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3645 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3646 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3647 else if (param == "treatasempty")
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3648 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3649 bool invalid = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3650 if (args(i+1).is_string ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3651 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3652 treat_as_empty = Cell (args(i+1));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3653 treat_as_empty_len = args(i+1).string_value ().size ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3654 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3655 else if (args(i+1).is_cell ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3656 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3657 treat_as_empty = args(i+1).cell_value ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3658 for (int j = 0; j < treat_as_empty.numel (); j++)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3659 if (! treat_as_empty (j).is_string ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3660 invalid = true;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3661 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3662 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3663 int k = treat_as_empty (j).string_value ().size ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3664 if (k > treat_as_empty_len)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3665 treat_as_empty_len = k;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3666 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3667 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3668 if (invalid)
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3669 error ("%s: TreatAsEmpty must be either a string or cell array of one or two strings",
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3670 who.c_str ());
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3671
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3672 // FIXME Ensure none is a prefix of a later one. Sort by length?
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3673 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3674 else if (param == "collectoutput")
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3675 {
21538
eb15bc7c5c0b textscan: Use error-checking value extractors for simpler parameter parsing
Mike Miller <mtmiller@octave.org>
parents: 21532
diff changeset
3676 collect_output = args(i+1).xbool_value ("%s: CollectOutput must be logical or numeric", who.c_str ());
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3677 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3678 else if (param == "emptyvalue")
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3679 {
21538
eb15bc7c5c0b textscan: Use error-checking value extractors for simpler parameter parsing
Mike Miller <mtmiller@octave.org>
parents: 21532
diff changeset
3680 empty_value = args(i+1).xscalar_value ("%s: EmptyValue must be numeric", who.c_str ());
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3681 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3682 else if (param == "headerlines")
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3683 {
21538
eb15bc7c5c0b textscan: Use error-checking value extractors for simpler parameter parsing
Mike Miller <mtmiller@octave.org>
parents: 21532
diff changeset
3684 header_lines = args(i+1).xscalar_value ("%s: HeaderLines must be numeric", who.c_str ());
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3685 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3686 else if (param == "bufsize")
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3687 {
21538
eb15bc7c5c0b textscan: Use error-checking value extractors for simpler parameter parsing
Mike Miller <mtmiller@octave.org>
parents: 21532
diff changeset
3688 buffer_size = args(i+1).xscalar_value ("%s: BufSize must be numeric", who.c_str ());
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3689 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3690 else if (param == "multipledelimsasone")
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3691 {
21538
eb15bc7c5c0b textscan: Use error-checking value extractors for simpler parameter parsing
Mike Miller <mtmiller@octave.org>
parents: 21532
diff changeset
3692 multiple_delims_as_one = args(i+1).xbool_value ("%s: MultipleDelimsAsOne must be logical or numeric", who.c_str ());
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3693 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3694 else if (param == "returnonerror")
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3695 {
21538
eb15bc7c5c0b textscan: Use error-checking value extractors for simpler parameter parsing
Mike Miller <mtmiller@octave.org>
parents: 21532
diff changeset
3696 if (args(i+1).is_string () && args(i+1).string_value () == "continue")
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3697 return_on_error = 2;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3698 else
21538
eb15bc7c5c0b textscan: Use error-checking value extractors for simpler parameter parsing
Mike Miller <mtmiller@octave.org>
parents: 21532
diff changeset
3699 return_on_error = args(i+1).xbool_value ("%s: ReturnOnError must be logical, numeric, or the string \"continue\"", who.c_str ());
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3700 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3701 else if (param == "whitespace")
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3702 {
21538
eb15bc7c5c0b textscan: Use error-checking value extractors for simpler parameter parsing
Mike Miller <mtmiller@octave.org>
parents: 21532
diff changeset
3703 whitespace = args(i+1).xstring_value ("%s: Whitespace must be a character string", who.c_str ());
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3704 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3705 else if (param == "expchars")
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3706 {
21538
eb15bc7c5c0b textscan: Use error-checking value extractors for simpler parameter parsing
Mike Miller <mtmiller@octave.org>
parents: 21532
diff changeset
3707 exp_chars = args(i+1).xstring_value ("%s: ExpChars must be a character string", who.c_str ());
eb15bc7c5c0b textscan: Use error-checking value extractors for simpler parameter parsing
Mike Miller <mtmiller@octave.org>
parents: 21532
diff changeset
3708 default_exp = false;
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3709 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3710 else if (param == "endofline")
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3711 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3712 bool valid = true;
21538
eb15bc7c5c0b textscan: Use error-checking value extractors for simpler parameter parsing
Mike Miller <mtmiller@octave.org>
parents: 21532
diff changeset
3713 std::string s = args(i+1).xstring_value ("%s: EndOfLine must be at most one character or '\\r\\n'", who.c_str ());
eb15bc7c5c0b textscan: Use error-checking value extractors for simpler parameter parsing
Mike Miller <mtmiller@octave.org>
parents: 21532
diff changeset
3714 if (args(i+1).is_sq_string ())
eb15bc7c5c0b textscan: Use error-checking value extractors for simpler parameter parsing
Mike Miller <mtmiller@octave.org>
parents: 21532
diff changeset
3715 s = do_string_escapes (s);
eb15bc7c5c0b textscan: Use error-checking value extractors for simpler parameter parsing
Mike Miller <mtmiller@octave.org>
parents: 21532
diff changeset
3716 int l = s.length ();
eb15bc7c5c0b textscan: Use error-checking value extractors for simpler parameter parsing
Mike Miller <mtmiller@octave.org>
parents: 21532
diff changeset
3717 if (l == 0)
eb15bc7c5c0b textscan: Use error-checking value extractors for simpler parameter parsing
Mike Miller <mtmiller@octave.org>
parents: 21532
diff changeset
3718 eol1 = eol2 = -2;
eb15bc7c5c0b textscan: Use error-checking value extractors for simpler parameter parsing
Mike Miller <mtmiller@octave.org>
parents: 21532
diff changeset
3719 else if (l == 1)
eb15bc7c5c0b textscan: Use error-checking value extractors for simpler parameter parsing
Mike Miller <mtmiller@octave.org>
parents: 21532
diff changeset
3720 eol1 = eol2 = s.c_str ()[0];
eb15bc7c5c0b textscan: Use error-checking value extractors for simpler parameter parsing
Mike Miller <mtmiller@octave.org>
parents: 21532
diff changeset
3721 else if (l == 2)
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3722 {
21538
eb15bc7c5c0b textscan: Use error-checking value extractors for simpler parameter parsing
Mike Miller <mtmiller@octave.org>
parents: 21532
diff changeset
3723 eol1 = s.c_str ()[0];
eb15bc7c5c0b textscan: Use error-checking value extractors for simpler parameter parsing
Mike Miller <mtmiller@octave.org>
parents: 21532
diff changeset
3724 eol2 = s.c_str ()[1];
eb15bc7c5c0b textscan: Use error-checking value extractors for simpler parameter parsing
Mike Miller <mtmiller@octave.org>
parents: 21532
diff changeset
3725 if (eol1 != '\r' || eol2 != '\n') // Why limit it?
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3726 valid = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3727 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3728 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3729 valid = false;
21538
eb15bc7c5c0b textscan: Use error-checking value extractors for simpler parameter parsing
Mike Miller <mtmiller@octave.org>
parents: 21532
diff changeset
3730
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3731 if (! valid)
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3732 error ("%s: EndOfLine must be at most one character or '\\r\\n'",
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3733 who.c_str ());
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3734 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3735 else
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3736 error ("%s: unrecognised option '%s'", who.c_str (), param.c_str ());
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3737 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3738
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3739 whitespace_table = std::string (256, '\0');
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3740 for (unsigned int i = 0; i < whitespace.length (); i++)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3741 whitespace_table[whitespace[i]] = '1';
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3742
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3743 // For Matlab compatibility, add 0x20 to whitespace, unless
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3744 // whitespace is explicitly ignored.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3745 if (! (whitespace.empty () && fmt_list.has_string))
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3746 whitespace_table[' '] = '1';
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3747
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3748 // Create look-up table of delimiters, based on 'delimiter'
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3749 delim_table = std::string (256, '\0');
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3750 if (eol1 >= 0 && eol1 < 256)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3751 delim_table[eol1] = '1'; // EOL is always a delimiter
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3752 if (eol2 >= 0 && eol2 < 256)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3753 delim_table[eol2] = '1'; // EOL is always a delimiter
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3754 if (! have_delims)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3755 for (unsigned int i = 0; i < 256; i++)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3756 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3757 if (isspace (i))
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3758 delim_table[i] = '1';
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3759 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3760 else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3761 for (unsigned int i = 0; i < delims.length (); i++)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3762 delim_table[delims[i]] = '1';
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3763 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3764
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3765 // Skip comments, and characters specified by the "Whitespace" option.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3766 // If EOLstop == true, don't skip end of line.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3767
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3768 int
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3769 textscan::skip_whitespace (delimited_stream& is, bool EOLstop)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3770 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3771 int c1 = std::istream::traits_type::eof ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3772 bool found_comment = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3773
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3774 do
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3775 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3776 found_comment = false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3777 int prev = -1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3778 while (is && (c1 = is.get_undelim ()) != std::istream::traits_type::eof ()
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3779 && ( ( (c1 == eol1 || c1 == eol2) && ++lines && ! EOLstop)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3780 || isspace (c1)))
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3781 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3782 if (prev == eol1 && eol1 != eol2 && c1 == eol2)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3783 lines--;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3784 prev = c1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3785 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3786
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3787 if (c1 == comment_char) // see if we match an open comment
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3788 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3789 // save stream state in case we have to restore it
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3790 char *pos = is.tellg ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3791 std::ios::iostate state = is.rdstate ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3792
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3793 std::string tmp (comment_len, '\0');
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3794 char *look = is.read (&tmp[0], comment_len-1, pos); // already read first char
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3795 if (is && ! strncmp (comment_style(0).string_value ().substr (1)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3796 .c_str (), look, comment_len-1))
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3797 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3798 found_comment = true;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3799
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3800 std::string dummy;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3801 if (comment_style.numel () == 1) // skip to end of line
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3802 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3803 std::string eol (3, '\0');
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3804 eol[0] = eol1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3805 eol[1] = eol2;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3806
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3807 scan_caret (is, eol, dummy);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3808 c1 = is.get_undelim ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3809 if (c1 == eol1 && eol1 != eol2 && is.peek_undelim () == eol2)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3810 is.get_undelim ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3811 lines++;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3812 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3813 else // matching pair
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3814 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3815 std::string end_c = comment_style(1).string_value ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3816 // last char of end-comment sequence
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3817 std::string last = end_c.substr (end_c.size () - 1);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3818 std::string may_match ("");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3819 do
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3820 { // find sequence ending with last char
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3821 scan_caret (is, last, dummy);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3822 is.get_undelim (); // (read LAST itself)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3823
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3824 may_match = may_match + dummy + last;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3825 if (may_match.length () > end_c.length ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3826 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3827 size_t start = may_match.length () - end_c.length ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3828 may_match = may_match.substr (start);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3829 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3830 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3831 while (may_match != end_c && is && ! is.eof ());
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3832 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3833 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3834 else // wasn't really a comment; restore state
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3835 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3836 is.clear (state);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3837 is.seekg (pos);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3838 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3839 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3840 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3841 while (found_comment);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3842
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3843 if (c1 != std::istream::traits_type::eof ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3844 is.putback (c1);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3845 return c1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3846 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3847
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3848 // See if the next few characters match one of the strings in target.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3849 // For efficiency, MAX_LEN is the cached longest length of any target.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3850 // Return -1 if none is found, or the index of the match.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3851
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3852 int
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3853 textscan::lookahead (delimited_stream& is, const Cell& targets, int max_len,
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3854 bool case_sensitive) const
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3855 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3856 // target strings may be different sizes.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3857 // Read ahead longest, put it all back, then re-read the string
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3858 // that matches.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3859
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3860 char *pos = is.tellg ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3861
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3862 std::string tmp (max_len, '\0');
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3863 char *look = is.read (&tmp[0], tmp.size (), pos);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3864
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3865 is.clear ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3866 is.seekg (pos); // reset to position before look-ahead
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3867 // FIXME pos may be corrupted by is.read
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3868
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3869 int i;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3870 int (*compare)(const char *, const char *, size_t);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3871 compare = case_sensitive ? strncmp : strncasecmp;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3872
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3873 for (i = 0; i < targets.numel (); i++)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3874 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3875 std::string s = targets (i).string_value ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3876 if (! (*compare) (s.c_str (), look, s.size ()))
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3877 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3878 is.read (&tmp[0], s.size (), pos); // read just the right amount
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3879 break;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3880 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3881 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3882
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3883 if (i == targets.numel ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3884 i = -1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3885
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3886 return i;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3887 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3888
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3889 // Skip delimiters -- multiple if MultipleDelimsAsOne specified.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3890 int
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3891 textscan::skip_delim (delimited_stream& is)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3892 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3893 int c1 = skip_whitespace (is, true); // 'true': stop once EOL is read
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3894 if (delim_list.numel () == 0) // single character delimiter
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3895 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3896 if (is_delim (c1) || c1 == eol1 || c1 == eol2)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3897 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3898 is.get ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3899 if (c1 == eol1 && is.peek_undelim () == eol2)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3900 is.get_undelim (); // if \r\n, skip the \n too.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3901
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3902 if (multiple_delims_as_one)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3903 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3904 int prev = -1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3905 // skip multiple delims.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3906 // Increment lines for each end-of-line seen; for \r\n, decrement
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3907 while (is && ((c1 = is.get_undelim ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3908 != std::istream::traits_type::eof ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3909 && (((c1 == eol1 || c1 == eol2) && ++lines)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3910 || isspace (c1) || is_delim (c1)))
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3911 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3912 if (prev == eol1 && eol1 != eol2 && c1 == eol2)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3913 lines--;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3914 prev = c1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3915 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3916 if (c1 != std::istream::traits_type::eof ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3917 is.putback (c1);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3918 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3919 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3920 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3921 else // multi-character delimiter
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3922 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3923 int first_match;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3924
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3925 if (c1 == eol1 || c1 == eol2
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3926 || (-1 != (first_match = lookahead (is, delim_list, delim_len))))
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3927 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3928 if (c1 == eol1)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3929 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3930 is.get_undelim ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3931 if (is.peek_undelim () == eol2)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3932 is.get_undelim ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3933 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3934 else if (c1 == eol2)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3935 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3936 is.get_undelim ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3937 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3938
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3939 if (multiple_delims_as_one)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3940 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3941 int prev = -1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3942 // skip multiple delims.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3943 // Increment lines for each end-of-line seen; for \r\n, decrement
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3944 while (is && ((c1 = skip_whitespace (is, true))
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3945 != std::istream::traits_type::eof ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3946 && (((c1 == eol1 || c1 == eol2) && ++lines)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3947 || -1 != lookahead (is, delim_list, delim_len)))
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3948 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3949 if (prev == eol1 && eol1 != eol2 && c1 == eol2)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3950 lines--;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3951 prev = c1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3952 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3953 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3954 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3955 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3956
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3957 return c1;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3958 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3959
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3960 // Read in as much of the input as coincides with the literal in the
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3961 // format string. Return "true" if the entire literal is matched, else
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3962 // false (and set failbit).
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3963
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3964 bool
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3965 textscan::match_literal (delimited_stream& is, const textscan_format_elt& fmt)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3966 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3967 // "false" -> treat EOL as normal space
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3968 // since a delimiter at the start of a line is a mismatch, not empty field
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3969 skip_whitespace (is, false);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3970
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3971 for (unsigned int i = 0; i < fmt.width; i++)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3972 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3973 int ch = is.get_undelim ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3974 if (ch != fmt.text[i])
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3975 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3976 if (ch != std::istream::traits_type::eof ())
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3977 is.putback (ch);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3978 is.setstate (std::ios::failbit);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3979 return false;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3980 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3981 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3982 return true;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3983 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3984
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
3985 void
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
3986 octave_base_stream::error (const std::string& msg)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
3987 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
3988 fail = true;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
3989 errmsg = msg;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
3990 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
3991
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
3992 void
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
3993 octave_base_stream::error (const std::string& who, const std::string& msg)
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
3994 {
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
3995 fail = true;
6296
85eb75190e01 [project @ 2007-02-10 07:10:42 by jwe]
jwe
parents: 6109
diff changeset
3996 errmsg = who + ": " + msg;
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
3997 }
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
3998
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
3999 void
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4000 octave_base_stream::clear (void)
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4001 {
4889
658aad1c1b05 [project @ 2004-04-30 22:38:30 by jwe]
jwe
parents: 4888
diff changeset
4002 fail = false;
658aad1c1b05 [project @ 2004-04-30 22:38:30 by jwe]
jwe
parents: 4888
diff changeset
4003 errmsg = "";
658aad1c1b05 [project @ 2004-04-30 22:38:30 by jwe]
jwe
parents: 4888
diff changeset
4004 }
658aad1c1b05 [project @ 2004-04-30 22:38:30 by jwe]
jwe
parents: 4888
diff changeset
4005
658aad1c1b05 [project @ 2004-04-30 22:38:30 by jwe]
jwe
parents: 4888
diff changeset
4006 void
658aad1c1b05 [project @ 2004-04-30 22:38:30 by jwe]
jwe
parents: 4888
diff changeset
4007 octave_base_stream::clearerr (void)
658aad1c1b05 [project @ 2004-04-30 22:38:30 by jwe]
jwe
parents: 4888
diff changeset
4008 {
4888
4a796c5fc7a3 [project @ 2004-04-30 18:27:07 by jwe]
jwe
parents: 4874
diff changeset
4009 std::istream *is = input_stream ();
4a796c5fc7a3 [project @ 2004-04-30 18:27:07 by jwe]
jwe
parents: 4874
diff changeset
4010 std::ostream *os = output_stream ();
4a796c5fc7a3 [project @ 2004-04-30 18:27:07 by jwe]
jwe
parents: 4874
diff changeset
4011
4a796c5fc7a3 [project @ 2004-04-30 18:27:07 by jwe]
jwe
parents: 4874
diff changeset
4012 if (is)
4a796c5fc7a3 [project @ 2004-04-30 18:27:07 by jwe]
jwe
parents: 4874
diff changeset
4013 is->clear ();
4a796c5fc7a3 [project @ 2004-04-30 18:27:07 by jwe]
jwe
parents: 4874
diff changeset
4014
4a796c5fc7a3 [project @ 2004-04-30 18:27:07 by jwe]
jwe
parents: 4874
diff changeset
4015 if (os)
4a796c5fc7a3 [project @ 2004-04-30 18:27:07 by jwe]
jwe
parents: 4874
diff changeset
4016 os->clear ();
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4017 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4018
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4019 // Functions that are defined for all input streams (input streams
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4020 // are those that define is).
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4021
3536
e8fbc8c3d6d9 [project @ 2000-02-02 12:01:00 by jwe]
jwe
parents: 3535
diff changeset
4022 std::string
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5266
diff changeset
4023 octave_base_stream::do_gets (octave_idx_type max_len, bool& err,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4024 bool strip_newline, const std::string& who)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4025 {
19849
1c9ed5b4c73d input.h: change meaning of interactive and forced_interactive global variables.
Carnë Draug <carandraug@octave.org>
parents: 19739
diff changeset
4026 if (interactive && file_number () == 0)
20831
35241c4b696c eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents: 20768
diff changeset
4027 ::error ("%s: unable to read from stdin while running interactively",
35241c4b696c eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents: 20768
diff changeset
4028 who.c_str ());
8773
9e3111d203c0 disallow reading from stdin while running interactively
John W. Eaton <jwe@octave.org>
parents: 8739
diff changeset
4029
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
4030 std::string retval;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4031
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4032 err = false;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4033
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
4034 std::istream *isp = input_stream ();
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4035
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4036 if (! isp)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4037 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4038 err = true;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4039 invalid_operation (who, "reading");
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4040 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4041 else
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4042 {
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
4043 std::istream& is = *isp;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4044
5765
7ba9ad1fec11 [project @ 2006-04-17 05:05:15 by jwe]
jwe
parents: 5760
diff changeset
4045 std::ostringstream buf;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4046
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4047 int c = 0;
3553
c5600b44bef9 [project @ 2000-02-03 03:22:37 by jwe]
jwe
parents: 3552
diff changeset
4048 int char_count = 0;
6345
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
4049
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
4050 if (max_len != 0)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4051 {
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4052 while (is && (c = is.get ()) != std::istream::traits_type::eof ())
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4053 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4054 char_count++;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4055
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4056 // Handle CRLF, CR, or LF as line ending.
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4057 if (c == '\r')
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4058 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4059 if (! strip_newline)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4060 buf << static_cast<char> (c);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4061
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4062 c = is.get ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4063
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4064 if (c != std::istream::traits_type::eof ())
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4065 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4066 if (c == '\n')
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4067 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4068 char_count++;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4069
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4070 if (! strip_newline)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4071 buf << static_cast<char> (c);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4072 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4073 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4074 is.putback (c);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4075 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4076
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4077 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4078 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4079 else if (c == '\n')
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4080 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4081 if (! strip_newline)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4082 buf << static_cast<char> (c);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4083
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4084 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4085 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4086 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4087 buf << static_cast<char> (c);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4088
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4089 if (max_len > 0 && char_count == max_len)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4090 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4091 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4092 }
6345
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
4093
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
4094 if (! is.eof () && char_count > 0)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4095 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4096 // GAGME. Matlab seems to check for EOF even if the last character
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4097 // in a file is a newline character. This is NOT what the
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4098 // corresponding C-library functions do.
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4099 int disgusting_compatibility_hack = is.get ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4100 if (! is.eof ())
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4101 is.putback (disgusting_compatibility_hack);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4102 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4103
4224
0179e6309248 [project @ 2002-12-18 00:34:29 by jwe]
jwe
parents: 4223
diff changeset
4104 if (is.good () || (is.eof () && char_count > 0))
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4105 retval = buf.str ();
4224
0179e6309248 [project @ 2002-12-18 00:34:29 by jwe]
jwe
parents: 4223
diff changeset
4106 else
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4107 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4108 err = true;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4109
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4110 if (is.eof () && char_count == 0)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4111 error (who, "at end of file");
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4112 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4113 error (who, "read error");
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4114 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4115 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4116
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4117 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4118 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4119
3536
e8fbc8c3d6d9 [project @ 2000-02-02 12:01:00 by jwe]
jwe
parents: 3535
diff changeset
4120 std::string
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
4121 octave_base_stream::getl (octave_idx_type max_len, bool& err,
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
4122 const std::string& who)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4123 {
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
4124 return do_gets (max_len, err, true, who);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4125 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4126
3536
e8fbc8c3d6d9 [project @ 2000-02-02 12:01:00 by jwe]
jwe
parents: 3535
diff changeset
4127 std::string
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
4128 octave_base_stream::gets (octave_idx_type max_len, bool& err,
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
4129 const std::string& who)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4130 {
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
4131 return do_gets (max_len, err, false, who);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4132 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4133
16011
8122286c69a9 initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents: 15467
diff changeset
4134 off_t
8122286c69a9 initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents: 15467
diff changeset
4135 octave_base_stream::skipl (off_t num, bool& err, const std::string& who)
9701
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4136 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4137 if (interactive && file_number () == 0)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4138 ::error ("%s: unable to read from stdin while running interactively",
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4139 who.c_str ());
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4140
16011
8122286c69a9 initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents: 15467
diff changeset
4141 off_t cnt = -1;
9701
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4142
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4143 err = false;
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4144
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4145 std::istream *isp = input_stream ();
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4146
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4147 if (! isp)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4148 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4149 err = true;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4150 invalid_operation (who, "reading");
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4151 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4152 else
9701
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4153 {
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4154 std::istream& is = *isp;
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4155
18100
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
4156 int c = 0;
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
4157 int lastc = -1;
9701
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4158 cnt = 0;
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4159
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4160 while (is && (c = is.get ()) != std::istream::traits_type::eof ())
9701
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4161 {
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4162 // Handle CRLF, CR, or LF as line ending.
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4163 if (c == '\r' || (c == '\n' && lastc != '\r'))
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4164 {
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4165 if (++cnt == num)
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4166 break;
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4167 }
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4168
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4169 lastc = c;
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4170 }
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4171
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4172 // Maybe eat the following \n if \r was just met.
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4173 if (c == '\r' && is.peek () == '\n')
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
4174 is.get ();
9701
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4175
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4176 if (is.bad ())
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4177 {
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4178 err = true;
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4179 error (who, "read error");
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4180 }
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4181
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4182 if (err)
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4183 cnt = -1;
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4184 }
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4185
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4186 return cnt;
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4187 }
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4188
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21129
diff changeset
4189 template <typename T>
3636
0aeae5fe4ab7 [project @ 2000-03-24 04:27:23 by jwe]
jwe
parents: 3635
diff changeset
4190 std::istream&
6767
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4191 octave_scan_1 (std::istream& is, const scanf_format_elt& fmt, T* valptr)
3636
0aeae5fe4ab7 [project @ 2000-03-24 04:27:23 by jwe]
jwe
parents: 3635
diff changeset
4192 {
3779
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4193 T& ref = *valptr;
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4194
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4195 switch (fmt.type)
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4196 {
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4197 case 'o':
4926
e80587ad9503 [project @ 2004-08-02 23:48:40 by jwe]
jwe
parents: 4889
diff changeset
4198 is >> std::oct >> ref >> std::dec;
3779
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4199 break;
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4200
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4201 case 'x':
4926
e80587ad9503 [project @ 2004-08-02 23:48:40 by jwe]
jwe
parents: 4889
diff changeset
4202 is >> std::hex >> ref >> std::dec;
e80587ad9503 [project @ 2004-08-02 23:48:40 by jwe]
jwe
parents: 4889
diff changeset
4203 break;
e80587ad9503 [project @ 2004-08-02 23:48:40 by jwe]
jwe
parents: 4889
diff changeset
4204
e80587ad9503 [project @ 2004-08-02 23:48:40 by jwe]
jwe
parents: 4889
diff changeset
4205 case 'i':
e80587ad9503 [project @ 2004-08-02 23:48:40 by jwe]
jwe
parents: 4889
diff changeset
4206 {
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4207 int c1 = std::istream::traits_type::eof ();
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4208
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4209 while (is && (c1 = is.get ()) != std::istream::traits_type::eof ()
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4210 && isspace (c1))
20946
6eff66fb8a02 style fixes for comments
John W. Eaton <jwe@octave.org>
parents: 20940
diff changeset
4211 ; // skip whitespace
14675
757f729fd41d skip leading whitespace for scanf %i format
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
4212
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4213 if (c1 != std::istream::traits_type::eof ())
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4214 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4215 if (c1 == '0')
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4216 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4217 int c2 = is.peek ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4218
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4219 if (c2 == 'x' || c2 == 'X')
7709
fa41af732801 octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents: 7538
diff changeset
4220 {
fa41af732801 octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents: 7538
diff changeset
4221 is.ignore ();
fa41af732801 octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents: 7538
diff changeset
4222 if (std::isxdigit (is.peek ()))
fa41af732801 octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents: 7538
diff changeset
4223 is >> std::hex >> ref >> std::dec;
fa41af732801 octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents: 7538
diff changeset
4224 else
fa41af732801 octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents: 7538
diff changeset
4225 ref = 0;
fa41af732801 octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents: 7538
diff changeset
4226 }
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4227 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4228 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4229 if (c2 == '0' || c2 == '1' || c2 == '2'
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4230 || c2 == '3' || c2 == '4' || c2 == '5'
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4231 || c2 == '6' || c2 == '7')
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4232 is >> std::oct >> ref >> std::dec;
21114
47908b818865 oct-stream.cc: Add FIXME note about bug #46493.
Rik <rik@octave.org>
parents: 21102
diff changeset
4233 else if (c2 == '8' || c2 == '9')
47908b818865 oct-stream.cc: Add FIXME note about bug #46493.
Rik <rik@octave.org>
parents: 21102
diff changeset
4234 {
47908b818865 oct-stream.cc: Add FIXME note about bug #46493.
Rik <rik@octave.org>
parents: 21102
diff changeset
4235 // FIXME: Would like to set error state on octave stream.
47908b818865 oct-stream.cc: Add FIXME note about bug #46493.
Rik <rik@octave.org>
parents: 21102
diff changeset
4236 // See bug #46493. But only std::istream is input to fcn
47908b818865 oct-stream.cc: Add FIXME note about bug #46493.
Rik <rik@octave.org>
parents: 21102
diff changeset
4237 // error ("internal failure to match octal format");
47908b818865 oct-stream.cc: Add FIXME note about bug #46493.
Rik <rik@octave.org>
parents: 21102
diff changeset
4238 ref = 0;
47908b818865 oct-stream.cc: Add FIXME note about bug #46493.
Rik <rik@octave.org>
parents: 21102
diff changeset
4239 }
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4240 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4241 ref = 0;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4242 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4243 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4244 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4245 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4246 is.putback (c1);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4247
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4248 is >> ref;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4249 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4250 }
4926
e80587ad9503 [project @ 2004-08-02 23:48:40 by jwe]
jwe
parents: 4889
diff changeset
4251 }
3779
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4252 break;
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4253
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4254 default:
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4255 is >> ref;
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4256 break;
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4257 }
3639
fed1847dfd6c [project @ 2000-03-24 05:12:46 by jwe]
jwe
parents: 3638
diff changeset
4258
3638
a76fa215a476 [project @ 2000-03-24 04:53:45 by jwe]
jwe
parents: 3637
diff changeset
4259 return is;
3636
0aeae5fe4ab7 [project @ 2000-03-24 04:27:23 by jwe]
jwe
parents: 3635
diff changeset
4260 }
0aeae5fe4ab7 [project @ 2000-03-24 04:27:23 by jwe]
jwe
parents: 3635
diff changeset
4261
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21129
diff changeset
4262 template <typename T>
6767
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4263 std::istream&
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4264 octave_scan (std::istream& is, const scanf_format_elt& fmt, T* valptr)
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4265 {
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4266 if (fmt.width)
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4267 {
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4268 // Limit input to fmt.width characters by reading into a
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4269 // temporary stringstream buffer.
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4270 std::string tmp;
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4271
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4272 is.width (fmt.width);
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4273 is >> tmp;
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4274
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4275 std::istringstream ss (tmp);
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4276
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4277 octave_scan_1 (ss, fmt, valptr);
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4278 }
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4279 else
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4280 octave_scan_1 (is, fmt, valptr);
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4281
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4282 return is;
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4283 }
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4284
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
4285 // Note that this specialization is only used for reading characters, not
3779
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4286 // character strings. See BEGIN_S_CONVERSION for details.
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4287
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21129
diff changeset
4288 template <>
3779
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4289 std::istream&
4661
12b6fbd57436 [project @ 2003-11-25 05:41:35 by jwe]
jwe
parents: 4649
diff changeset
4290 octave_scan<> (std::istream& is, const scanf_format_elt& /* fmt */,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4291 char* valptr)
3779
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4292 {
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4293 return is >> valptr;
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4294 }
3636
0aeae5fe4ab7 [project @ 2000-03-24 04:27:23 by jwe]
jwe
parents: 3635
diff changeset
4295
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21129
diff changeset
4296 template <>
5176
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4297 std::istream&
5403
7bdddf96f028 [project @ 2005-07-07 16:46:23 by jwe]
jwe
parents: 5389
diff changeset
4298 octave_scan<> (std::istream& is, const scanf_format_elt& fmt, double* valptr)
5176
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4299 {
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4300 double& ref = *valptr;
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4301
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4302 switch (fmt.type)
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4303 {
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4304 case 'e':
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4305 case 'f':
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4306 case 'g':
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4307 {
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4308 int c1 = std::istream::traits_type::eof ();
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4309
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4310 while (is && (c1 = is.get ()) != std::istream::traits_type::eof ()
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4311 && isspace (c1))
20946
6eff66fb8a02 style fixes for comments
John W. Eaton <jwe@octave.org>
parents: 20940
diff changeset
4312 ; // skip whitespace
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4313
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4314 if (c1 != std::istream::traits_type::eof ())
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4315 {
12936
b74cb659e757 accept but discard sign when reading NA and NaN values
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
4316 is.putback (c1);
b74cb659e757 accept but discard sign when reading NA and NaN values
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
4317
b74cb659e757 accept but discard sign when reading NA and NaN values
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
4318 ref = octave_read_value<double> (is);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4319 }
5176
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4320 }
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4321 break;
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4322
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4323 default:
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4324 panic_impossible ();
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4325 break;
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4326 }
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4327
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4328 return is;
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4329 }
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4330
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21129
diff changeset
4331 template <typename T>
2572
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
4332 void
3636
0aeae5fe4ab7 [project @ 2000-03-24 04:27:23 by jwe]
jwe
parents: 3635
diff changeset
4333 do_scanf_conv (std::istream& is, const scanf_format_elt& fmt,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4334 T valptr, Matrix& mval, double *data, octave_idx_type& idx,
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
4335 octave_idx_type& conversion_count, octave_idx_type nr,
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
4336 octave_idx_type max_size, bool discard)
2572
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
4337 {
21147
95feb42d7a97 * oct-stream.cc (OCTAVE_SCAN): Eliminate trivial macro.
John W. Eaton <jwe@octave.org>
parents: 21139
diff changeset
4338 octave_scan (is, fmt, valptr);
2572
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
4339
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4340 if (! is)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4341 return;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4342
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4343 if (idx == max_size && ! discard)
2572
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
4344 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4345 max_size *= 2;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4346
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4347 if (nr > 0)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4348 mval.resize (nr, max_size / nr, 0.0);
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4349 else
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4350 mval.resize (max_size, 1, 0.0);
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4351
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4352 data = mval.fortran_vec ();
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4353 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4354
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4355 if (! discard)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4356 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4357 conversion_count++;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4358 data[idx++] = *(valptr);
2572
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
4359 }
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
4360 }
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
4361
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
4362 template void
3636
0aeae5fe4ab7 [project @ 2000-03-24 04:27:23 by jwe]
jwe
parents: 3635
diff changeset
4363 do_scanf_conv (std::istream&, const scanf_format_elt&, double*,
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
4364 Matrix&, double*, octave_idx_type&, octave_idx_type&,
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
4365 octave_idx_type, octave_idx_type, bool);
2572
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
4366
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4367 #define DO_WHITESPACE_CONVERSION() \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4368 do \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4369 { \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4370 int c = std::istream::traits_type::eof (); \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4371 \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4372 while (is && (c = is.get ()) != std::istream::traits_type::eof () \
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4373 && isspace (c)) \
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4374 { /* skip whitespace */ } \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4375 \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4376 if (c != std::istream::traits_type::eof ()) \
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4377 is.putback (c); \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4378 } \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4379 while (0)
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4380
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4381 #define DO_LITERAL_CONVERSION() \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4382 do \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4383 { \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4384 int c = std::istream::traits_type::eof (); \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4385 \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4386 int n = strlen (fmt); \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4387 int i = 0; \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4388 \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4389 while (i < n && is && (c = is.get ()) != std::istream::traits_type::eof ()) \
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4390 { \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4391 if (c == static_cast<unsigned char> (fmt[i])) \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4392 { \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4393 i++; \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4394 continue; \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4395 } \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4396 else \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4397 { \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4398 is.putback (c); \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4399 break; \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4400 } \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4401 } \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4402 \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4403 if (i != n) \
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4404 is.setstate (std::ios::failbit); \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4405 } \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4406 while (0)
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4407
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
4408 #define DO_PCT_CONVERSION() \
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
4409 do \
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
4410 { \
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
4411 int c = is.get (); \
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
4412 \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4413 if (c != std::istream::traits_type::eof ()) \
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4414 { \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4415 if (c != '%') \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4416 { \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4417 is.putback (c); \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4418 is.setstate (std::ios::failbit); \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4419 } \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4420 } \
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
4421 else \
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4422 is.setstate (std::ios::failbit); \
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
4423 } \
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
4424 while (0)
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
4425
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4426 #define BEGIN_C_CONVERSION() \
3538
0ff7323dab8b [project @ 2000-02-02 12:36:25 by jwe]
jwe
parents: 3536
diff changeset
4427 is.unsetf (std::ios::skipws); \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4428 \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4429 int width = elt->width ? elt->width : 1; \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4430 \
9952
7cd2e1b372e5 allow scanf to store ASCII NUL values
John W. Eaton <jwe@octave.org>
parents: 9941
diff changeset
4431 std::string tmp (width, '\0'); \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4432 \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4433 int c = std::istream::traits_type::eof (); \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4434 int n = 0; \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4435 \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4436 while (is && n < width && (c = is.get ()) != std::istream::traits_type::eof ()) \
9952
7cd2e1b372e5 allow scanf to store ASCII NUL values
John W. Eaton <jwe@octave.org>
parents: 9941
diff changeset
4437 tmp[n++] = static_cast<char> (c); \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4438 \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4439 if (n > 0 && c == std::istream::traits_type::eof ()) \
12984
7626f8934466 correctly resize scanf output for %c formats (bug #34037)
John W. Eaton <jwe@octave.org>
parents: 12966
diff changeset
4440 is.clear (); \
7626f8934466 correctly resize scanf output for %c formats (bug #34037)
John W. Eaton <jwe@octave.org>
parents: 12966
diff changeset
4441 \
7626f8934466 correctly resize scanf output for %c formats (bug #34037)
John W. Eaton <jwe@octave.org>
parents: 12966
diff changeset
4442 tmp.resize (n)
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4443
15466
d174210ce1ec use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
4444 // For a '%s' format, skip initial whitespace and then read until the
5338
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4445 // next whitespace character or until WIDTH characters have been read.
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4446 #define BEGIN_S_CONVERSION() \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4447 int width = elt->width; \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4448 \
4051
b79da8779a0e [project @ 2002-08-17 19:38:32 by jwe]
jwe
parents: 3969
diff changeset
4449 std::string tmp; \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4450 \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4451 do \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4452 { \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4453 if (width) \
5338
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4454 { \
10076
4b270d1540f7 avoid various GCC warnings
John W. Eaton <jwe@octave.org>
parents: 9952
diff changeset
4455 tmp = std::string (width, '\0'); \
5338
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4456 \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4457 int c = std::istream::traits_type::eof (); \
5338
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4458 \
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4459 int n = 0; \
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4460 \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4461 while (is && (c = is.get ()) != std::istream::traits_type::eof ()) \
5338
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4462 { \
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4463 if (! isspace (c)) \
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4464 { \
9952
7cd2e1b372e5 allow scanf to store ASCII NUL values
John W. Eaton <jwe@octave.org>
parents: 9941
diff changeset
4465 tmp[n++] = static_cast<char> (c); \
5338
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4466 break; \
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4467 } \
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4468 } \
4051
b79da8779a0e [project @ 2002-08-17 19:38:32 by jwe]
jwe
parents: 3969
diff changeset
4469 \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4470 while (is && n < width \
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4471 && (c = is.get ()) != std::istream::traits_type::eof ()) \
5338
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4472 { \
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4473 if (isspace (c)) \
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4474 { \
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4475 is.putback (c); \
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4476 break; \
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4477 } \
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4478 else \
9952
7cd2e1b372e5 allow scanf to store ASCII NUL values
John W. Eaton <jwe@octave.org>
parents: 9941
diff changeset
4479 tmp[n++] = static_cast<char> (c); \
5338
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4480 } \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4481 \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4482 if (n > 0 && c == std::istream::traits_type::eof ()) \
5338
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4483 is.clear (); \
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4484 \
9952
7cd2e1b372e5 allow scanf to store ASCII NUL values
John W. Eaton <jwe@octave.org>
parents: 9941
diff changeset
4485 tmp.resize (n); \
5338
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4486 } \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4487 else \
5338
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4488 { \
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4489 is >> std::ws >> tmp; \
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4490 } \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4491 } \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4492 while (0)
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4493
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4494 // This format must match a nonempty sequence of characters.
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4495 #define BEGIN_CHAR_CLASS_CONVERSION() \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4496 int width = elt->width; \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4497 \
4051
b79da8779a0e [project @ 2002-08-17 19:38:32 by jwe]
jwe
parents: 3969
diff changeset
4498 std::string tmp; \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4499 \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4500 do \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4501 { \
7426
b9df9abdffbb [project @ 2008-01-28 21:07:58 by jwe]
jwe
parents: 7352
diff changeset
4502 if (! width) \
15215
9020dddc925a use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents: 15195
diff changeset
4503 width = std::numeric_limits<int>::max (); \
7427
65f0a8ced9d2 [project @ 2008-01-28 22:42:18 by jwe]
jwe
parents: 7426
diff changeset
4504 \
7426
b9df9abdffbb [project @ 2008-01-28 21:07:58 by jwe]
jwe
parents: 7352
diff changeset
4505 std::ostringstream buf; \
b9df9abdffbb [project @ 2008-01-28 21:07:58 by jwe]
jwe
parents: 7352
diff changeset
4506 \
b9df9abdffbb [project @ 2008-01-28 21:07:58 by jwe]
jwe
parents: 7352
diff changeset
4507 std::string char_class = elt->char_class; \
4051
b79da8779a0e [project @ 2002-08-17 19:38:32 by jwe]
jwe
parents: 3969
diff changeset
4508 \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4509 int c = std::istream::traits_type::eof (); \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4510 \
7426
b9df9abdffbb [project @ 2008-01-28 21:07:58 by jwe]
jwe
parents: 7352
diff changeset
4511 if (elt->type == '[') \
b9df9abdffbb [project @ 2008-01-28 21:07:58 by jwe]
jwe
parents: 7352
diff changeset
4512 { \
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4513 int chars_read = 0; \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4514 while (is && chars_read++ < width \
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4515 && (c = is.get ()) != std::istream::traits_type::eof () \
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4516 && char_class.find (c) != std::string::npos) \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4517 buf << static_cast<char> (c); \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4518 } \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4519 else \
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4520 { \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4521 int chars_read = 0; \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4522 while (is && chars_read++ < width \
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4523 && (c = is.get ()) != std::istream::traits_type::eof () \
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4524 && char_class.find (c) == std::string::npos) \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4525 buf << static_cast<char> (c); \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4526 } \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4527 \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4528 if (width == std::numeric_limits<int>::max () \
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4529 && c != std::istream::traits_type::eof ()) \
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4530 is.putback (c); \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4531 \
7426
b9df9abdffbb [project @ 2008-01-28 21:07:58 by jwe]
jwe
parents: 7352
diff changeset
4532 tmp = buf.str (); \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4533 \
7426
b9df9abdffbb [project @ 2008-01-28 21:07:58 by jwe]
jwe
parents: 7352
diff changeset
4534 if (tmp.empty ()) \
b9df9abdffbb [project @ 2008-01-28 21:07:58 by jwe]
jwe
parents: 7352
diff changeset
4535 is.setstate (std::ios::failbit); \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4536 else if (c == std::istream::traits_type::eof ()) \
10969
da355a1a6d44 fix scanf char class conversion bug
John W. Eaton <jwe@octave.org>
parents: 10350
diff changeset
4537 is.clear (); \
da355a1a6d44 fix scanf char class conversion bug
John W. Eaton <jwe@octave.org>
parents: 10350
diff changeset
4538 \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4539 } \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4540 while (0)
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4541
3410
ee5f222025c3 [project @ 2000-01-07 04:19:05 by jwe]
jwe
parents: 3342
diff changeset
4542 #define FINISH_CHARACTER_CONVERSION() \
ee5f222025c3 [project @ 2000-01-07 04:19:05 by jwe]
jwe
parents: 3342
diff changeset
4543 do \
ee5f222025c3 [project @ 2000-01-07 04:19:05 by jwe]
jwe
parents: 3342
diff changeset
4544 { \
4051
b79da8779a0e [project @ 2002-08-17 19:38:32 by jwe]
jwe
parents: 3969
diff changeset
4545 width = tmp.length (); \
3410
ee5f222025c3 [project @ 2000-01-07 04:19:05 by jwe]
jwe
parents: 3342
diff changeset
4546 \
ee5f222025c3 [project @ 2000-01-07 04:19:05 by jwe]
jwe
parents: 3342
diff changeset
4547 if (is) \
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4548 { \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4549 int i = 0; \
3410
ee5f222025c3 [project @ 2000-01-07 04:19:05 by jwe]
jwe
parents: 3342
diff changeset
4550 \
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4551 if (! discard) \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4552 { \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4553 conversion_count++; \
3410
ee5f222025c3 [project @ 2000-01-07 04:19:05 by jwe]
jwe
parents: 3342
diff changeset
4554 \
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4555 while (i < width) \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4556 { \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4557 if (data_index == max_size) \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4558 { \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4559 max_size *= 2; \
3410
ee5f222025c3 [project @ 2000-01-07 04:19:05 by jwe]
jwe
parents: 3342
diff changeset
4560 \
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4561 if (all_char_conv) \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4562 { \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4563 if (one_elt_size_spec) \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4564 mval.resize (1, max_size, 0.0); \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4565 else if (nr > 0) \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4566 mval.resize (nr, max_size / nr, 0.0); \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4567 else \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4568 panic_impossible (); \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4569 } \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4570 else if (nr > 0) \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4571 mval.resize (nr, max_size / nr, 0.0); \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4572 else \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4573 mval.resize (max_size, 1, 0.0); \
3410
ee5f222025c3 [project @ 2000-01-07 04:19:05 by jwe]
jwe
parents: 3342
diff changeset
4574 \
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4575 data = mval.fortran_vec (); \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4576 } \
3410
ee5f222025c3 [project @ 2000-01-07 04:19:05 by jwe]
jwe
parents: 3342
diff changeset
4577 \
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4578 data[data_index++] = tmp[i++]; \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4579 } \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4580 } \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4581 } \
3410
ee5f222025c3 [project @ 2000-01-07 04:19:05 by jwe]
jwe
parents: 3342
diff changeset
4582 } \
ee5f222025c3 [project @ 2000-01-07 04:19:05 by jwe]
jwe
parents: 3342
diff changeset
4583 while (0)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4584
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4585 octave_value
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4586 octave_base_stream::do_scanf (scanf_format_list& fmt_list,
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
4587 octave_idx_type nr, octave_idx_type nc,
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
4588 bool one_elt_size_spec,
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
4589 octave_idx_type& conversion_count,
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
4590 const std::string& who)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4591 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4592 if (interactive && file_number () == 0)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4593 ::error ("%s: unable to read from stdin while running interactively",
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4594 who.c_str ());
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4595
8773
9e3111d203c0 disallow reading from stdin while running interactively
John W. Eaton <jwe@octave.org>
parents: 8739
diff changeset
4596 octave_value retval = Matrix ();
9e3111d203c0 disallow reading from stdin while running interactively
John W. Eaton <jwe@octave.org>
parents: 8739
diff changeset
4597
3268
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
4598 conversion_count = 0;
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
4599
10187
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4600 octave_idx_type nconv = fmt_list.num_conversions ();
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
4601
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5266
diff changeset
4602 octave_idx_type data_index = 0;
2121
bc6ecd8f1175 [project @ 1996-05-12 06:45:55 by jwe]
jwe
parents: 2117
diff changeset
4603
3268
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
4604 if (nr == 0 || nc == 0)
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
4605 {
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
4606 if (one_elt_size_spec)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4607 nc = 0;
3268
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
4608
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
4609 return Matrix (nr, nc, 0.0);
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
4610 }
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
4611
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
4612 std::istream *isp = input_stream ();
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4613
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4614 bool all_char_conv = fmt_list.all_character_conversions ();
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4615
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4616 Matrix mval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4617 double *data = 0;
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5266
diff changeset
4618 octave_idx_type max_size = 0;
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5266
diff changeset
4619 octave_idx_type max_conv = 0;
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5266
diff changeset
4620
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5266
diff changeset
4621 octave_idx_type final_nr = 0;
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5266
diff changeset
4622 octave_idx_type final_nc = 0;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4623
3268
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
4624 if (all_char_conv)
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
4625 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4626 // Any of these could be resized later (if we have %s conversions,
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4627 // we may read more than one element for each conversion).
3268
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
4628 if (one_elt_size_spec)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4629 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4630 max_size = 512;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4631 mval.resize (1, max_size, 0.0);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4632
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4633 if (nr > 0)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4634 max_conv = nr;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4635 }
4420
3dfdbcaf4582 [project @ 2003-05-28 18:24:42 by jwe]
jwe
parents: 4326
diff changeset
4636 else if (nr > 0)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4637 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4638 if (nc > 0)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4639 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4640 mval.resize (nr, nc, 0.0);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4641 max_size = max_conv = nr * nc;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4642 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4643 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4644 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4645 mval.resize (nr, 32, 0.0);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4646 max_size = nr * 32;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4647 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4648 }
4420
3dfdbcaf4582 [project @ 2003-05-28 18:24:42 by jwe]
jwe
parents: 4326
diff changeset
4649 else
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4650 panic_impossible ();
3268
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
4651 }
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
4652 else if (nr > 0)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4653 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4654 if (nc > 0)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4655 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4656 // Will not resize later.
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4657 mval.resize (nr, nc, 0.0);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4658 max_size = nr * nc;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4659 max_conv = max_size;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4660 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4661 else
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4662 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4663 // Maybe resize later.
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4664 mval.resize (nr, 32, 0.0);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4665 max_size = nr * 32;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4666 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4667 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4668 else
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4669 {
4420
3dfdbcaf4582 [project @ 2003-05-28 18:24:42 by jwe]
jwe
parents: 4326
diff changeset
4670 // Maybe resize later.
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4671 mval.resize (32, 1, 0.0);
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4672 max_size = 32;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4673 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4674
4420
3dfdbcaf4582 [project @ 2003-05-28 18:24:42 by jwe]
jwe
parents: 4326
diff changeset
4675 data = mval.fortran_vec ();
3dfdbcaf4582 [project @ 2003-05-28 18:24:42 by jwe]
jwe
parents: 4326
diff changeset
4676
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4677 if (isp)
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4678 {
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
4679 std::istream& is = *isp;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4680
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4681 const scanf_format_elt *elt = fmt_list.first ();
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4682
3538
0ff7323dab8b [project @ 2000-02-02 12:36:25 by jwe]
jwe
parents: 3536
diff changeset
4683 std::ios::fmtflags flags = is.flags ();
2213
c4a887694274 [project @ 1996-05-16 04:13:28 by jwe]
jwe
parents: 2127
diff changeset
4684
10187
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4685 octave_idx_type trips = 0;
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4686
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4687 octave_idx_type num_fmt_elts = fmt_list.length ();
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4688
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4689 for (;;)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4690 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4691 octave_quit ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4692
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4693 if (elt)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4694 {
20465
44eb1102f8a8 don't recycle scanf format string if all conversions are done (bug #45808)
John W. Eaton <jwe@octave.org>
parents: 20432
diff changeset
4695 if (elt->type == scanf_format_elt::null
44eb1102f8a8 don't recycle scanf format string if all conversions are done (bug #45808)
John W. Eaton <jwe@octave.org>
parents: 20432
diff changeset
4696 || (! (elt->type == scanf_format_elt::whitespace_conversion
44eb1102f8a8 don't recycle scanf format string if all conversions are done (bug #45808)
John W. Eaton <jwe@octave.org>
parents: 20432
diff changeset
4697 || elt->type == scanf_format_elt::literal_conversion
44eb1102f8a8 don't recycle scanf format string if all conversions are done (bug #45808)
John W. Eaton <jwe@octave.org>
parents: 20432
diff changeset
4698 || elt->type == '%')
44eb1102f8a8 don't recycle scanf format string if all conversions are done (bug #45808)
John W. Eaton <jwe@octave.org>
parents: 20432
diff changeset
4699 && max_conv > 0 && conversion_count == max_conv))
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4700 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4701 // We are done, either because we have reached the end of the
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4702 // format string and are not cycling through the format again
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4703 // or because we've converted all the values that have been
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4704 // requested and the next format element is a conversion.
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4705 // Determine final array size and exit.
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4706 if (all_char_conv && one_elt_size_spec)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4707 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4708 final_nr = 1;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4709 final_nc = data_index;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4710 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4711 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4712 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4713 final_nr = nr;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4714 final_nc = (data_index - 1) / nr + 1;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4715 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4716
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4717 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4718 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4719 else if (data_index == max_size)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4720 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4721 max_size *= 2;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4722
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4723 if (all_char_conv)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4724 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4725 if (one_elt_size_spec)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4726 mval.resize (1, max_size, 0.0);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4727 else if (nr > 0)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4728 mval.resize (nr, max_size / nr, 0.0);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4729 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4730 panic_impossible ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4731 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4732 else if (nr > 0)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4733 mval.resize (nr, max_size / nr, 0.0);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4734 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4735 mval.resize (max_size, 1, 0.0);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4736
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4737 data = mval.fortran_vec ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4738 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4739
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4740 const char *fmt = elt->text;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4741
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4742 bool discard = elt->discard;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4743
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4744 switch (elt->type)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4745 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4746 case scanf_format_elt::whitespace_conversion:
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4747 DO_WHITESPACE_CONVERSION ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4748 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4749
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4750 case scanf_format_elt::literal_conversion:
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4751 DO_LITERAL_CONVERSION ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4752 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4753
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4754 case '%':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4755 DO_PCT_CONVERSION ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4756 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4757
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4758 case 'd': case 'i':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4759 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4760 switch (elt->modifier)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4761 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4762 case 'h':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4763 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4764 short int tmp;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4765 do_scanf_conv (is, *elt, &tmp, mval, data,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4766 data_index, conversion_count,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4767 nr, max_size, discard);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4768 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4769 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4770
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4771 case 'l':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4772 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4773 long int tmp;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4774 do_scanf_conv (is, *elt, &tmp, mval, data,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4775 data_index, conversion_count,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4776 nr, max_size, discard);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4777 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4778 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4779
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4780 default:
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4781 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4782 int tmp;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4783 do_scanf_conv (is, *elt, &tmp, mval, data,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4784 data_index, conversion_count,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4785 nr, max_size, discard);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4786 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4787 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4788 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4789 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4790 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4791
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4792 case 'o': case 'u': case 'x':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4793 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4794 switch (elt->modifier)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4795 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4796 case 'h':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4797 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4798 unsigned short int tmp;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4799 do_scanf_conv (is, *elt, &tmp, mval, data,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4800 data_index, conversion_count,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4801 nr, max_size, discard);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4802 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4803 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4804
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4805 case 'l':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4806 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4807 unsigned long int tmp;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4808 do_scanf_conv (is, *elt, &tmp, mval, data,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4809 data_index, conversion_count,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4810 nr, max_size, discard);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4811 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4812 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4813
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4814 default:
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4815 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4816 unsigned int tmp;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4817 do_scanf_conv (is, *elt, &tmp, mval, data,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4818 data_index, conversion_count,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4819 nr, max_size, discard);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4820 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4821 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4822 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4823 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4824 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4825
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4826 case 'e': case 'f': case 'g':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4827 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4828 double tmp;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4829
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4830 do_scanf_conv (is, *elt, &tmp, mval, data,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4831 data_index, conversion_count,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4832 nr, max_size, discard);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4833 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4834 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4835
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4836 case 'c':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4837 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4838 BEGIN_C_CONVERSION ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4839
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4840 FINISH_CHARACTER_CONVERSION ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4841
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4842 is.setf (flags);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4843 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4844 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4845
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4846 case 's':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4847 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4848 BEGIN_S_CONVERSION ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4849
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4850 FINISH_CHARACTER_CONVERSION ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4851 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4852 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4853
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4854 case '[': case '^':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4855 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4856 BEGIN_CHAR_CLASS_CONVERSION ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4857
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4858 FINISH_CHARACTER_CONVERSION ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4859 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4860 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4861
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4862 case 'p':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4863 error ("%s: unsupported format specifier", who.c_str ());
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4864 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4865
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4866 default:
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4867 error ("%s: internal format error", who.c_str ());
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4868 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4869 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4870
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4871 if (! ok ())
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4872 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4873 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4874 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4875 else if (! is)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4876 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4877 if (all_char_conv)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4878 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4879 if (one_elt_size_spec)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4880 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4881 final_nr = 1;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4882 final_nc = data_index;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4883 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4884 else if (data_index > nr)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4885 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4886 final_nr = nr;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4887 final_nc = (data_index - 1) / nr + 1;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4888 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4889 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4890 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4891 final_nr = data_index;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4892 final_nc = 1;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4893 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4894 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4895 else if (nr > 0)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4896 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4897 if (data_index > nr)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4898 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4899 final_nr = nr;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4900 final_nc = (data_index - 1) / nr + 1;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4901 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4902 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4903 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4904 final_nr = data_index;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4905 final_nc = 1;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4906 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4907 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4908 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4909 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4910 final_nr = data_index;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4911 final_nc = 1;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4912 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4913
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4914 // If it looks like we have a matching failure, then
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4915 // reset the failbit in the stream state.
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4916 if (is.rdstate () & std::ios::failbit)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4917 is.clear (is.rdstate () & (~std::ios::failbit));
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4918
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
4919 // FIXME: is this the right thing to do?
19849
1c9ed5b4c73d input.h: change meaning of interactive and forced_interactive global variables.
Carnë Draug <carandraug@octave.org>
parents: 19739
diff changeset
4920 if (interactive && ! forced_interactive && name () == "stdin")
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4921 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4922 is.clear ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4923
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4924 // Skip to end of line.
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4925 bool err;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4926 do_gets (-1, err, false, who);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4927 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4928
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4929 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4930 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4931 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4932 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4933 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4934 error ("%s: internal format error", who.c_str ());
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4935 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4936 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4937
10187
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4938 if (nconv == 0 && ++trips == num_fmt_elts)
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4939 {
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4940 if (all_char_conv && one_elt_size_spec)
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4941 {
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4942 final_nr = 1;
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4943 final_nc = data_index;
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4944 }
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4945 else
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4946 {
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4947 final_nr = nr;
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4948 final_nc = (data_index - 1) / nr + 1;
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4949 }
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4950
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4951 break;
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4952 }
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4953 else
20465
44eb1102f8a8 don't recycle scanf format string if all conversions are done (bug #45808)
John W. Eaton <jwe@octave.org>
parents: 20432
diff changeset
4954 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4955 // Cycle through the format list more than once if we have some
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4956 // conversions to make and we haven't reached the limit on the
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4957 // number of values to convert (possibly because there is no
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4958 // specified limit).
20465
44eb1102f8a8 don't recycle scanf format string if all conversions are done (bug #45808)
John W. Eaton <jwe@octave.org>
parents: 20432
diff changeset
4959 elt = fmt_list.next (nconv > 0
44eb1102f8a8 don't recycle scanf format string if all conversions are done (bug #45808)
John W. Eaton <jwe@octave.org>
parents: 20432
diff changeset
4960 && (max_conv == 0
44eb1102f8a8 don't recycle scanf format string if all conversions are done (bug #45808)
John W. Eaton <jwe@octave.org>
parents: 20432
diff changeset
4961 || conversion_count < max_conv));
44eb1102f8a8 don't recycle scanf format string if all conversions are done (bug #45808)
John W. Eaton <jwe@octave.org>
parents: 20432
diff changeset
4962 }
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4963 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4964 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4965
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4966 if (ok ())
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4967 {
2121
bc6ecd8f1175 [project @ 1996-05-12 06:45:55 by jwe]
jwe
parents: 2117
diff changeset
4968 mval.resize (final_nr, final_nc, 0.0);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4969
3268
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
4970 retval = mval;
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
4971
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4972 if (all_char_conv)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4973 retval = retval.convert_to_str (false, true);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4974 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4975
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4976 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4977 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4978
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4979 octave_value
3810
f19f00339363 [project @ 2001-03-29 21:03:01 by jwe]
jwe
parents: 3779
diff changeset
4980 octave_base_stream::scanf (const std::string& fmt, const Array<double>& size,
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
4981 octave_idx_type& conversion_count,
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
4982 const std::string& who)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4983 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4984 octave_value retval = Matrix ();
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4985
3559
12d7ec415f35 [project @ 2000-02-03 05:17:36 by jwe]
jwe
parents: 3553
diff changeset
4986 conversion_count = 0;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4987
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
4988 std::istream *isp = input_stream ();
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4989
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4990 if (! isp)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4991 invalid_operation (who, "reading");
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4992 else
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4993 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4994 scanf_format_list fmt_list (fmt);
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4995
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
4996 if (fmt_list.num_conversions () == -1)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4997 ::error ("%s: invalid format specified", who.c_str ());
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4998
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4999 octave_idx_type nr = -1;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5000 octave_idx_type nc = -1;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5001
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5002 bool one_elt_size_spec;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5003
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5004 get_size (size, nr, nc, one_elt_size_spec, who);
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5005
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5006 retval = do_scanf (fmt_list, nr, nc, one_elt_size_spec,
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5007 conversion_count, who);
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
5008 }
2572
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5009
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5010 return retval;
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5011 }
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5012
2712
2ff2b536cc9d [project @ 1997-02-21 21:38:21 by jwe]
jwe
parents: 2687
diff changeset
5013 bool
2ff2b536cc9d [project @ 1997-02-21 21:38:21 by jwe]
jwe
parents: 2687
diff changeset
5014 octave_base_stream::do_oscanf (const scanf_format_elt *elt,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5015 octave_value& retval, const std::string& who)
2572
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5016 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5017 std::istream *isp = input_stream ();
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5018
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5019 if (! isp)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5020 return false;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5021
2712
2ff2b536cc9d [project @ 1997-02-21 21:38:21 by jwe]
jwe
parents: 2687
diff changeset
5022 bool quit = false;
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
5023
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5024 std::istream& is = *isp;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5025
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5026 std::ios::fmtflags flags = is.flags ();
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5027
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5028 if (elt)
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
5029 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5030 const char *fmt = elt->text;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5031
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5032 bool discard = elt->discard;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5033
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5034 switch (elt->type)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5035 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5036 case scanf_format_elt::whitespace_conversion:
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5037 DO_WHITESPACE_CONVERSION ();
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5038 break;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5039
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5040 case scanf_format_elt::literal_conversion:
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5041 DO_LITERAL_CONVERSION ();
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5042 break;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5043
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5044 case '%':
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5045 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5046 DO_PCT_CONVERSION ();
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5047
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5048 if (! is)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5049 quit = true;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5050 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5051 break;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5052
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5053 case 'd': case 'i':
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5054 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5055 int tmp;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5056
21147
95feb42d7a97 * oct-stream.cc (OCTAVE_SCAN): Eliminate trivial macro.
John W. Eaton <jwe@octave.org>
parents: 21139
diff changeset
5057 if (octave_scan (is, *elt, &tmp))
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5058 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5059 if (! discard)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5060 retval = tmp;
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5061 }
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5062 else
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5063 quit = true;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5064 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5065 break;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5066
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5067 case 'o': case 'u': case 'x':
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5068 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5069 long int tmp;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5070
21147
95feb42d7a97 * oct-stream.cc (OCTAVE_SCAN): Eliminate trivial macro.
John W. Eaton <jwe@octave.org>
parents: 21139
diff changeset
5071 if (octave_scan (is, *elt, &tmp))
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5072 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5073 if (! discard)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5074 retval = tmp;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5075 }
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5076 else
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5077 quit = true;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5078 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5079 break;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5080
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5081 case 'e': case 'f': case 'g':
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5082 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5083 double tmp;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5084
21147
95feb42d7a97 * oct-stream.cc (OCTAVE_SCAN): Eliminate trivial macro.
John W. Eaton <jwe@octave.org>
parents: 21139
diff changeset
5085 if (octave_scan (is, *elt, &tmp))
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5086 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5087 if (! discard)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5088 retval = tmp;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5089 }
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5090 else
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5091 quit = true;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5092 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5093 break;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5094
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5095 case 'c':
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5096 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5097 BEGIN_C_CONVERSION ();
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5098
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5099 if (! discard)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5100 retval = tmp;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5101
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5102 if (! is)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5103 quit = true;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5104
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5105 is.setf (flags);
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5106 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5107 break;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5108
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5109 case 's':
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5110 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5111 BEGIN_S_CONVERSION ();
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5112
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5113 if (! discard)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5114 retval = tmp;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5115
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5116 if (! is)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5117 quit = true;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5118 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5119 break;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5120
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5121 case '[':
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5122 case '^':
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5123 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5124 BEGIN_CHAR_CLASS_CONVERSION ();
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5125
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5126 if (! discard)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5127 retval = tmp;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5128
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5129 if (! is)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5130 quit = true;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5131 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5132 break;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5133
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5134 case 'p':
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5135 error ("%s: unsupported format specifier", who.c_str ());
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5136 break;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5137
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5138 default:
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5139 error ("%s: internal format error", who.c_str ());
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5140 break;
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5141 }
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5142 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5143
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5144 if (ok () && is.fail ())
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5145 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5146 error ("%s: read error", who.c_str ());
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5147
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5148 // FIXME: is this the right thing to do?
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5149
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5150 if (interactive && ! forced_interactive && name () == "stdin")
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5151 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5152 // Skip to end of line.
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5153 bool err;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5154 do_gets (-1, err, false, who);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5155 }
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
5156 }
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
5157
2712
2ff2b536cc9d [project @ 1997-02-21 21:38:21 by jwe]
jwe
parents: 2687
diff changeset
5158 return quit;
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
5159 }
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
5160
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
5161 octave_value_list
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
5162 octave_base_stream::oscanf (const std::string& fmt, const std::string& who)
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
5163 {
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
5164 octave_value_list retval;
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
5165
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
5166 std::istream *isp = input_stream ();
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
5167
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5168 if (! isp)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5169 invalid_operation (who, "reading");
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5170 else
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
5171 {
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
5172 std::istream& is = *isp;
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
5173
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
5174 scanf_format_list fmt_list (fmt);
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
5175
10187
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
5176 octave_idx_type nconv = fmt_list.num_conversions ();
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
5177
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
5178 if (nconv == -1)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5179 ::error ("%s: invalid format specified", who.c_str ());
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5180
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5181 is.clear ();
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5182
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5183 octave_idx_type len = fmt_list.length ();
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5184
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5185 retval.resize (nconv+2, Matrix ());
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5186
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5187 const scanf_format_elt *elt = fmt_list.first ();
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5188
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5189 int num_values = 0;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5190
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5191 bool quit = false;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5192
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5193 for (octave_idx_type i = 0; i < len; i++)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5194 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5195 octave_value tmp;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5196
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5197 quit = do_oscanf (elt, tmp, who);
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5198
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5199 if (quit)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5200 break;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5201 else
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5202 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5203 if (tmp.is_defined ())
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5204 retval(num_values++) = tmp;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5205
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5206 if (! ok ())
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5207 break;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5208
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5209 elt = fmt_list.next (nconv > 0);
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5210 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5211 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5212
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5213 retval(nconv) = num_values;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5214
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5215 int err_num;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5216 retval(nconv+1) = error (false, err_num);
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5217
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5218 if (! quit)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5219 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5220 // Pick up any trailing stuff.
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5221 if (ok () && len > nconv)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5222 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5223 octave_value tmp;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5224
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5225 elt = fmt_list.next ();
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5226
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5227 do_oscanf (elt, tmp, who);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5228 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5229 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5230 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5231
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5232 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5233 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5234
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5235 octave_value
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5236 octave_base_stream::do_textscan (const std::string& fmt,
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5237 octave_idx_type ntimes,
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5238 const octave_value_list& options,
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5239 const std::string& who,
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5240 octave_idx_type& read_count)
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5241 {
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5242 if (interactive && file_number () == 0)
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5243 ::error ("%s: unable to read from stdin while running interactively",
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5244 who.c_str ());
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5245
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5246 octave_value retval = Cell (dim_vector (1, 1), Matrix (0, 1));
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5247
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5248 std::istream *isp = input_stream ();
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5249
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5250 if (! isp)
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5251 invalid_operation (who, "reading");
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5252 else
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5253 {
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5254 textscan scanner (who);
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5255
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5256 retval = scanner.scan (*isp, fmt, ntimes, options, read_count);
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5257 }
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5258
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5259 return retval;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5260 }
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5261
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5262 // Functions that are defined for all output streams
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5263 // (output streams are those that define os).
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5264
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5265 int
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5266 octave_base_stream::flush (void)
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5267 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5268 int retval = -1;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5269
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
5270 std::ostream *os = output_stream ();
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5271
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5272 if (! os)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5273 invalid_operation ("fflush", "writing");
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5274 else
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5275 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5276 os->flush ();
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5277
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5278 if (os->good ())
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5279 retval = 0;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5280 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5281
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5282 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5283 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5284
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5285 class
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5286 printf_value_cache
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5287 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5288 public:
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5289
3653
84b2f30007d5 [project @ 2000-03-31 23:33:25 by jwe]
jwe
parents: 3645
diff changeset
5290 enum state { ok, conversion_error };
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5291
7352
fab9bc33b949 [project @ 2008-01-07 18:54:45 by jwe]
jwe
parents: 7227
diff changeset
5292 printf_value_cache (const octave_value_list& args, const std::string& who)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5293 : values (args), val_idx (0), elt_idx (0),
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5294 n_vals (values.length ()), n_elts (0), have_data (false),
7352
fab9bc33b949 [project @ 2008-01-07 18:54:45 by jwe]
jwe
parents: 7227
diff changeset
5295 curr_state (ok)
fab9bc33b949 [project @ 2008-01-07 18:54:45 by jwe]
jwe
parents: 7227
diff changeset
5296 {
fab9bc33b949 [project @ 2008-01-07 18:54:45 by jwe]
jwe
parents: 7227
diff changeset
5297 for (octave_idx_type i = 0; i < values.length (); i++)
fab9bc33b949 [project @ 2008-01-07 18:54:45 by jwe]
jwe
parents: 7227
diff changeset
5298 {
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5299 octave_value val = values(i);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5300
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5301 if (val.is_map () || val.is_cell () || val.is_object ())
21129
228b65504557 maint: Eliminate useless statements after err_XXX.
Rik <rik@octave.org>
parents: 21114
diff changeset
5302 err_wrong_type_arg (who, val);
7352
fab9bc33b949 [project @ 2008-01-07 18:54:45 by jwe]
jwe
parents: 7227
diff changeset
5303 }
fab9bc33b949 [project @ 2008-01-07 18:54:45 by jwe]
jwe
parents: 7227
diff changeset
5304 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5305
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5306 ~printf_value_cache (void) { }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5307
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5308 // Get the current value as a double and advance the internal pointer.
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5309 octave_value get_next_value (char type = 0);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5310
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5311 // Get the current value as an int and advance the internal pointer.
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5312 int int_value (void);
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5313
3145
0d640dc625c7 [project @ 1998-02-05 08:44:59 by jwe]
jwe
parents: 3013
diff changeset
5314 operator bool () const { return (curr_state == ok); }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5315
3653
84b2f30007d5 [project @ 2000-03-31 23:33:25 by jwe]
jwe
parents: 3645
diff changeset
5316 bool exhausted (void) { return (val_idx >= n_vals); }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5317
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5318 private:
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5319
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5320 const octave_value_list values;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5321 int val_idx;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5322 int elt_idx;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5323 int n_vals;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5324 int n_elts;
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5325 bool have_data;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5326 octave_value curr_val;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5327 state curr_state;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5328
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5329 // Must create value cache with values!
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5330
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5331 printf_value_cache (void);
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5332
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5333 // No copying!
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5334
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5335 printf_value_cache (const printf_value_cache&);
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5336
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5337 printf_value_cache& operator = (const printf_value_cache&);
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5338 };
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5339
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5340 octave_value
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5341 printf_value_cache::get_next_value (char type)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5342 {
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5343 octave_value retval;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5344
3711
60db0e500f10 [project @ 2000-08-03 05:09:39 by jwe]
jwe
parents: 3704
diff changeset
5345 if (exhausted ())
60db0e500f10 [project @ 2000-08-03 05:09:39 by jwe]
jwe
parents: 3704
diff changeset
5346 curr_state = conversion_error;
60db0e500f10 [project @ 2000-08-03 05:09:39 by jwe]
jwe
parents: 3704
diff changeset
5347
60db0e500f10 [project @ 2000-08-03 05:09:39 by jwe]
jwe
parents: 3704
diff changeset
5348 while (! exhausted ())
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5349 {
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5350 if (! have_data)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5351 {
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5352 curr_val = values (val_idx);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5353
20741
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
5354 elt_idx = 0;
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
5355 n_elts = curr_val.numel ();
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
5356 have_data = true;
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5357 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5358
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5359 if (elt_idx < n_elts)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5360 {
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5361 if (type == 's')
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5362 {
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5363 if (curr_val.is_string ())
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5364 {
21296
596e19e7e571 fix printf regression (bug #47192)
John W. Eaton <jwe@octave.org>
parents: 21235
diff changeset
5365 dim_vector dv (1, curr_val.numel ());
596e19e7e571 fix printf regression (bug #47192)
John W. Eaton <jwe@octave.org>
parents: 21235
diff changeset
5366 octave_value tmp = curr_val.reshape (dv);
596e19e7e571 fix printf regression (bug #47192)
John W. Eaton <jwe@octave.org>
parents: 21235
diff changeset
5367
596e19e7e571 fix printf regression (bug #47192)
John W. Eaton <jwe@octave.org>
parents: 21235
diff changeset
5368 std::string sval = tmp.string_value ();
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5369
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5370 retval = sval.substr (elt_idx);
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5371
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5372 // We've consumed the rest of the value.
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5373 elt_idx = n_elts;
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5374 }
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5375 else
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5376 {
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5377 // Convert to character string while values are
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5378 // integers in the range [0 : char max]
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5379 const NDArray val = curr_val.array_value ();
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5380
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5381 octave_idx_type idx = elt_idx;
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5382
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5383 for (; idx < n_elts; idx++)
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5384 {
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5385 double dval = val(idx);
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5386
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5387 if (D_NINT (dval) != dval || dval < 0 || dval > 255)
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5388 break;
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5389 }
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5390
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5391 octave_idx_type n = idx - elt_idx;
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5392
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5393 if (n > 0)
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5394 {
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5395 std::string sval (n, '\0');
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5396
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5397 for (octave_idx_type i = 0; i < n; i++)
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5398 sval[i] = val(elt_idx++);
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5399
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5400 retval = sval;
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5401 }
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5402 else
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5403 retval = curr_val.fast_elem_extract (elt_idx++);
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5404 }
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5405 }
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5406 else
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5407 {
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5408 retval = curr_val.fast_elem_extract (elt_idx++);
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5409
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5410 if (type == 'c' && ! retval.is_string ())
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5411 {
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5412 double dval = retval.double_value ();
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5413
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5414 if (D_NINT (dval) == dval && dval >= 0 && dval < 256)
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5415 retval = static_cast<char> (dval);
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5416 }
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5417 }
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5418
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5419 if (elt_idx >= n_elts)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5420 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5421 elt_idx = 0;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5422 val_idx++;
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5423 have_data = false;
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5424 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5425
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5426 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5427 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5428 else
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5429 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5430 val_idx++;
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5431 have_data = false;
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5432
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5433 if (n_elts == 0)
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5434 {
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5435 if (elt_idx == 0 && (type == 's' || type == 'c'))
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5436 {
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5437 retval = "";
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5438 break;
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5439 }
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5440
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5441 if (exhausted ())
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5442 curr_state = conversion_error;
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5443 }
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5444 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5445 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5446
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5447 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5448 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5449
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5450 int
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5451 printf_value_cache::int_value (void)
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5452 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5453 int retval = 0;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5454
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5455 octave_value val = get_next_value ();
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5456
20558
1a0a433c8263 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20465
diff changeset
5457 double dval = val.double_value (true);
1a0a433c8263 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20465
diff changeset
5458
20704
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
5459 if (D_NINT (dval) == dval)
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
5460 retval = NINT (dval);
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
5461 else
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
5462 curr_state = conversion_error;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5463
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5464 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5465 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5466
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5467 // Ugh again and again.
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5468
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21129
diff changeset
5469 template <typename T>
3620
0886bbb236cb [project @ 2000-03-23 05:17:23 by jwe]
jwe
parents: 3559
diff changeset
5470 int
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
5471 do_printf_conv (std::ostream& os, const char *fmt, int nsa, int sa_1,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5472 int sa_2, T arg, const std::string& who)
2572
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5473 {
3620
0886bbb236cb [project @ 2000-03-23 05:17:23 by jwe]
jwe
parents: 3559
diff changeset
5474 int retval = 0;
0886bbb236cb [project @ 2000-03-23 05:17:23 by jwe]
jwe
parents: 3559
diff changeset
5475
2572
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5476 switch (nsa)
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5477 {
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5478 case 2:
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
5479 retval = octave_format (os, fmt, sa_1, sa_2, arg);
2572
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5480 break;
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5481
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5482 case 1:
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
5483 retval = octave_format (os, fmt, sa_1, arg);
2572
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5484 break;
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5485
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5486 case 0:
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
5487 retval = octave_format (os, fmt, arg);
2572
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5488 break;
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5489
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5490 default:
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
5491 ::error ("%s: internal error handling format", who.c_str ());
2572
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5492 break;
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5493 }
3620
0886bbb236cb [project @ 2000-03-23 05:17:23 by jwe]
jwe
parents: 3559
diff changeset
5494
0886bbb236cb [project @ 2000-03-23 05:17:23 by jwe]
jwe
parents: 3559
diff changeset
5495 return retval;
2572
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5496 }
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5497
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5498 static size_t
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5499 do_printf_string (std::ostream& os, const printf_format_elt *elt,
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5500 int nsa, int sa_1, int sa_2, const std::string& arg,
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5501 const std::string& who)
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5502 {
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5503 if (nsa > 2)
20831
35241c4b696c eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents: 20768
diff changeset
5504 ::error ("%s: internal error handling format", who.c_str ());
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5505
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5506 std::string flags = elt->flags;
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5507
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5508 bool left = flags.find ('-') != std::string::npos;
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5509
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5510 size_t len = arg.length ();
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5511
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5512 size_t fw = nsa > 0 ? sa_1 : (elt->fw == -1 ? len : elt->fw);
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5513 size_t prec = nsa > 1 ? sa_2 : (elt->prec == -1 ? len : elt->prec);
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5514
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5515 os << std::setw (fw)
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5516 << (left ? std::left : std::right)
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5517 << (prec < len ? arg.substr (0, prec) : arg);
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5518
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5519 return len > fw ? len : fw;
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5520 }
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5521
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5522 static bool
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5523 is_nan_or_inf (const octave_value& val)
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5524 {
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5525 octave_value ov_isnan = val.isnan ();
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5526 octave_value ov_isinf = val.isinf ();
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5527
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5528 return (ov_isnan.is_true () || ov_isinf.is_true ());
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5529 }
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5530
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5531 static bool
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5532 ok_for_signed_int_conv (const octave_value& val)
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5533 {
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5534 uint64_t limit = std::numeric_limits<int64_t>::max ();
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5535
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5536 if (val.is_string ())
20252
7fa170cc14fe Return correct hex value for printf when used with string inputs (bug #45263).
John W. Eaton <jwe@octave.org>
parents: 19864
diff changeset
5537 return true;
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5538 else if (val.is_integer_type ())
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5539 {
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5540 if (val.is_uint64_type ())
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5541 {
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5542 octave_uint64 ival = val.uint64_scalar_value ();
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5543
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5544 if (ival.value () <= limit)
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5545 return true;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5546 }
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5547 else
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5548 return true;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5549 }
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5550 else
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5551 {
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5552 double dval = val.double_value (true);
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5553
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5554 if (dval == xround (dval) && dval <= limit)
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5555 return true;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5556 }
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5557
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5558 return false;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5559 }
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5560
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5561 static bool
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5562 ok_for_unsigned_int_conv (const octave_value& val)
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5563 {
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5564 if (val.is_string ())
20252
7fa170cc14fe Return correct hex value for printf when used with string inputs (bug #45263).
John W. Eaton <jwe@octave.org>
parents: 19864
diff changeset
5565 return true;
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5566 else if (val.is_integer_type ())
18653
7d0014bb9e4e also switch from unsigned integer to real format for negative values
John W. Eaton <jwe@octave.org>
parents: 18650
diff changeset
5567 {
7d0014bb9e4e also switch from unsigned integer to real format for negative values
John W. Eaton <jwe@octave.org>
parents: 18650
diff changeset
5568 // Easier than dispatching here...
7d0014bb9e4e also switch from unsigned integer to real format for negative values
John W. Eaton <jwe@octave.org>
parents: 18650
diff changeset
5569
7d0014bb9e4e also switch from unsigned integer to real format for negative values
John W. Eaton <jwe@octave.org>
parents: 18650
diff changeset
5570 octave_value ov_is_ge_zero
7d0014bb9e4e also switch from unsigned integer to real format for negative values
John W. Eaton <jwe@octave.org>
parents: 18650
diff changeset
5571 = do_binary_op (octave_value::op_ge, val, octave_value (0.0));
7d0014bb9e4e also switch from unsigned integer to real format for negative values
John W. Eaton <jwe@octave.org>
parents: 18650
diff changeset
5572
7d0014bb9e4e also switch from unsigned integer to real format for negative values
John W. Eaton <jwe@octave.org>
parents: 18650
diff changeset
5573 return ov_is_ge_zero.is_true ();
7d0014bb9e4e also switch from unsigned integer to real format for negative values
John W. Eaton <jwe@octave.org>
parents: 18650
diff changeset
5574 }
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5575 else
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5576 {
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5577 double dval = val.double_value (true);
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5578
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5579 uint64_t limit = std::numeric_limits<uint64_t>::max ();
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5580
18653
7d0014bb9e4e also switch from unsigned integer to real format for negative values
John W. Eaton <jwe@octave.org>
parents: 18650
diff changeset
5581 if (dval == xround (dval) && dval >= 0 && dval <= limit)
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5582 return true;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5583 }
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5584
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5585 return false;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5586 }
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5587
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5588 static std::string
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5589 switch_to_g_format (const printf_format_elt *elt)
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5590 {
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5591 std::string tfmt = elt->text;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5592
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5593 tfmt.replace (tfmt.rfind (elt->type), 1, "g");
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5594
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5595 return tfmt;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5596 }
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5597
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5598 int
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5599 octave_base_stream::do_numeric_printf_conv (std::ostream& os,
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5600 const printf_format_elt *elt,
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5601 int nsa, int sa_1, int sa_2,
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5602 const octave_value& val,
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5603 const std::string& who)
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5604 {
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5605 int retval = 0;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5606
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5607 const char *fmt = elt->text;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5608
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5609 if (is_nan_or_inf (val))
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5610 {
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5611 double dval = val.double_value ();
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5612
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5613 std::string tfmt = fmt;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5614 std::string::size_type i1, i2;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5615
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5616 tfmt.replace ((i1 = tfmt.rfind (elt->type)), 1, 1, 's');
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5617
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5618 if ((i2 = tfmt.rfind ('.')) != std::string::npos && i2 < i1)
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5619 {
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5620 tfmt.erase (i2, i1-i2);
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5621 if (elt->prec == -2)
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5622 nsa--;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5623 }
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5624
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5625 const char *tval;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5626 if (lo_ieee_isinf (dval))
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5627 {
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5628 if (elt->flags.find ('+') != std::string::npos)
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5629 tval = (dval < 0 ? "-Inf" : "+Inf");
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5630 else
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5631 tval = (dval < 0 ? "-Inf" : "Inf");
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5632 }
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5633 else
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5634 {
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5635 if (elt->flags.find ('+') != std::string::npos)
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5636 tval = (lo_ieee_is_NA (dval) ? "+NA" : "+NaN");
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5637 else
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5638 tval = (lo_ieee_is_NA (dval) ? "NA" : "NaN");
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5639 }
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5640
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5641 retval += do_printf_conv (os, tfmt.c_str (), nsa, sa_1, sa_2, tval, who);
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5642 }
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5643 else
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5644 {
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5645 static std::string llmod
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5646 = sizeof (long) == sizeof (int64_t) ? "l" : "ll";
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5647
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5648 char type = elt->type;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5649
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5650 switch (type)
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5651 {
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5652 case 'd': case 'i': case 'c':
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5653 if (ok_for_signed_int_conv (val))
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5654 {
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5655 octave_int64 tval = val.int64_scalar_value ();
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5656
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5657 // Insert "long" modifier.
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5658 std::string tfmt = fmt;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5659 tfmt.replace (tfmt.rfind (type), 1, llmod + type);
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5660
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5661 retval += do_printf_conv (os, tfmt.c_str (), nsa, sa_1, sa_2,
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5662 tval.value (), who);
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5663 }
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5664 else
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5665 {
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5666 std::string tfmt = switch_to_g_format (elt);
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5667
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5668 double dval = val.double_value (true);
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5669
20558
1a0a433c8263 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20465
diff changeset
5670 retval += do_printf_conv (os, tfmt.c_str (), nsa,
1a0a433c8263 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20465
diff changeset
5671 sa_1, sa_2, dval, who);
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5672 }
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5673 break;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5674
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5675 case 'o': case 'x': case 'X': case 'u':
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5676 if (ok_for_unsigned_int_conv (val))
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5677 {
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5678 octave_uint64 tval = val.uint64_scalar_value ();
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5679
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5680 // Insert "long" modifier.
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5681 std::string tfmt = fmt;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5682 tfmt.replace (tfmt.rfind (type), 1, llmod + type);
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5683
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5684 retval += do_printf_conv (os, tfmt.c_str (), nsa, sa_1, sa_2,
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5685 tval.value (), who);
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5686 }
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5687 else
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5688 {
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5689 std::string tfmt = switch_to_g_format (elt);
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5690
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5691 double dval = val.double_value (true);
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5692
20558
1a0a433c8263 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20465
diff changeset
5693 retval += do_printf_conv (os, tfmt.c_str (), nsa,
1a0a433c8263 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20465
diff changeset
5694 sa_1, sa_2, dval, who);
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5695 }
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5696 break;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5697
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5698 case 'f': case 'e': case 'E':
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5699 case 'g': case 'G':
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5700 {
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5701 double dval = val.double_value (true);
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5702
20558
1a0a433c8263 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20465
diff changeset
5703 retval += do_printf_conv (os, fmt, nsa, sa_1, sa_2, dval, who);
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5704 }
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5705 break;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5706
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5707 default:
20938
aac911d8847b choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents: 20931
diff changeset
5708 // Note: error is member fcn from octave_base_stream, not ::error.
aac911d8847b choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents: 20931
diff changeset
5709 // This error does not halt execution so "return ..." must exist.
20831
35241c4b696c eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents: 20768
diff changeset
5710 error ("%s: invalid format specifier", who.c_str ());
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5711 return -1;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5712 break;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5713 }
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5714 }
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5715
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5716 return retval;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5717 }
6492
bcfdc9f0d267 [project @ 2007-04-05 06:13:30 by jwe]
jwe
parents: 6483
diff changeset
5718
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5719 int
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5720 octave_base_stream::do_printf (printf_format_list& fmt_list,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5721 const octave_value_list& args,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5722 const std::string& who)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5723 {
3620
0886bbb236cb [project @ 2000-03-23 05:17:23 by jwe]
jwe
parents: 3559
diff changeset
5724 int retval = 0;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5725
10187
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
5726 octave_idx_type nconv = fmt_list.num_conversions ();
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
5727
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
5728 std::ostream *osp = output_stream ();
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5729
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5730 if (! osp)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5731 invalid_operation (who, "writing");
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5732 else
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5733 {
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
5734 std::ostream& os = *osp;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5735
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5736 const printf_format_elt *elt = fmt_list.first ();
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5737
7352
fab9bc33b949 [project @ 2008-01-07 18:54:45 by jwe]
jwe
parents: 7227
diff changeset
5738 printf_value_cache val_cache (args, who);
fab9bc33b949 [project @ 2008-01-07 18:54:45 by jwe]
jwe
parents: 7227
diff changeset
5739
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5740 for (;;)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5741 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5742 octave_quit ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5743
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5744 if (! elt)
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5745 ::error ("%s: internal error handling format", who.c_str ());
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5746
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5747 // NSA is the number of 'star' args to convert.
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5748 int nsa = (elt->fw == -2) + (elt->prec == -2);
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5749
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5750 int sa_1 = 0;
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5751 int sa_2 = 0;
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5752
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5753 if (nsa > 0)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5754 {
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5755 sa_1 = val_cache.int_value ();
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5756
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5757 if (! val_cache)
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5758 break;
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5759 else
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5760 {
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5761 if (nsa > 1)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5762 {
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5763 sa_2 = val_cache.int_value ();
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5764
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5765 if (! val_cache)
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5766 break;
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5767 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5768 }
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5769 }
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5770
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5771 if (elt->type == '%')
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5772 {
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5773 os << "%";
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5774 retval++;
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5775 }
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5776 else if (elt->args == 0 && elt->text)
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5777 {
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5778 os << elt->text;
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5779 retval += strlen (elt->text);
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5780 }
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5781 else if (elt->type == 's' || elt->type == 'c')
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5782 {
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5783 octave_value val = val_cache.get_next_value (elt->type);
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5784
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5785 if (val_cache)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5786 {
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5787 if (val.is_string ())
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5788 {
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5789 std::string sval = val.string_value ();
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5790
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5791 retval += do_printf_string (os, elt, nsa, sa_1,
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5792 sa_2, sval, who);
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5793 }
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5794 else
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5795 retval += do_numeric_printf_conv (os, elt, nsa, sa_1,
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5796 sa_2, val, who);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5797 }
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5798 else
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5799 break;
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5800 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5801 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5802 {
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5803 octave_value val = val_cache.get_next_value ();
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5804
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5805 if (val_cache)
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5806 retval += do_numeric_printf_conv (os, elt, nsa, sa_1,
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5807 sa_2, val, who);
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5808 else
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5809 break;
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5810 }
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5811
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5812 if (! os)
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5813 {
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5814 error ("%s: write error", who.c_str ());
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5815 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5816 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5817
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5818 elt = fmt_list.next (nconv > 0 && ! val_cache.exhausted ());
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5819
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5820 if (! elt || (val_cache.exhausted () && elt->args > 0))
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5821 break;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
5822 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5823 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5824
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5825 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5826 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5827
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5828 int
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
5829 octave_base_stream::printf (const std::string& fmt,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5830 const octave_value_list& args,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5831 const std::string& who)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5832 {
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
5833 printf_format_list fmt_list (fmt);
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
5834
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
5835 if (fmt_list.num_conversions () == -1)
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
5836 ::error ("%s: invalid format specified", who.c_str ());
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5837
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5838 return do_printf (fmt_list, args, who);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5839 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5840
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5841 int
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
5842 octave_base_stream::puts (const std::string& s, const std::string& who)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5843 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5844 int retval = -1;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5845
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
5846 std::ostream *osp = output_stream ();
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5847
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5848 if (! osp)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5849 invalid_operation (who, "writing");
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5850 else
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5851 {
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
5852 std::ostream& os = *osp;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5853
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5854 os << s;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5855
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5856 if (! os)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5857 error ("%s: write error", who.c_str ());
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5858 else
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5859 {
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
5860 // FIXME: why does this seem to be necessary?
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5861 // Without it, output from a loop like
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5862 //
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5863 // for i = 1:100, fputs (stdout, "foo\n"); endfor
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5864 //
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5865 // doesn't seem to go to the pager immediately.
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5866 os.flush ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5867
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5868 if (os)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5869 retval = 0;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5870 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5871 error ("%s: write error", who.c_str ());
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5872 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5873 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5874
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5875 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5876 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5877
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5878 // Return current error message for this stream.
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5879
3536
e8fbc8c3d6d9 [project @ 2000-02-02 12:01:00 by jwe]
jwe
parents: 3535
diff changeset
5880 std::string
2435
3be97fe02051 [project @ 1996-10-27 21:31:29 by jwe]
jwe
parents: 2341
diff changeset
5881 octave_base_stream::error (bool clear_err, int& err_num)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5882 {
2435
3be97fe02051 [project @ 1996-10-27 21:31:29 by jwe]
jwe
parents: 2341
diff changeset
5883 err_num = fail ? -1 : 0;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5884
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
5885 std::string tmp = errmsg;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5886
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5887 if (clear_err)
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5888 clear ();
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5889
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5890 return tmp;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5891 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5892
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5893 void
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
5894 octave_base_stream::invalid_operation (const std::string& who, const char *rw)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5895 {
20938
aac911d8847b choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents: 20931
diff changeset
5896 // Note: This calls the member fcn error, not ::error from error.h.
6297
a943cb9c8068 [project @ 2007-02-10 08:42:27 by jwe]
jwe
parents: 6296
diff changeset
5897 error (who, std::string ("stream not open for ") + rw);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5898 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5899
3552
41daa489833a [project @ 2000-02-03 03:05:28 by jwe]
jwe
parents: 3548
diff changeset
5900 octave_stream::octave_stream (octave_base_stream *bs)
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5901 : rep (bs)
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5902 {
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5903 if (rep)
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5904 rep->count = 1;
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5905 }
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5906
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5907 octave_stream::~octave_stream (void)
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5908 {
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5909 if (rep && --rep->count == 0)
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5910 delete rep;
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5911 }
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5912
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5913 octave_stream::octave_stream (const octave_stream& s)
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5914 : rep (s.rep)
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5915 {
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5916 if (rep)
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5917 rep->count++;
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5918 }
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5919
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5920 octave_stream&
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5921 octave_stream::operator = (const octave_stream& s)
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5922 {
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5923 if (rep != s.rep)
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5924 {
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5925 if (rep && --rep->count == 0)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5926 delete rep;
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5927
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5928 rep = s.rep;
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5929
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5930 if (rep)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5931 rep->count++;
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5932 }
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5933
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5934 return *this;
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5935 }
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5936
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5937 int
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5938 octave_stream::flush (void)
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5939 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5940 int retval = -1;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5941
5659
960f4b9a26af [project @ 2006-03-10 15:35:20 by jwe]
jwe
parents: 5649
diff changeset
5942 if (stream_ok ())
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5943 retval = rep->flush ();
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5944
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5945 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5946 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5947
3536
e8fbc8c3d6d9 [project @ 2000-02-02 12:01:00 by jwe]
jwe
parents: 3535
diff changeset
5948 std::string
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5266
diff changeset
5949 octave_stream::getl (octave_idx_type max_len, bool& err, const std::string& who)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5950 {
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
5951 std::string retval;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5952
5659
960f4b9a26af [project @ 2006-03-10 15:35:20 by jwe]
jwe
parents: 5649
diff changeset
5953 if (stream_ok ())
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
5954 retval = rep->getl (max_len, err, who);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5955
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5956 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5957 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5958
3536
e8fbc8c3d6d9 [project @ 2000-02-02 12:01:00 by jwe]
jwe
parents: 3535
diff changeset
5959 std::string
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
5960 octave_stream::getl (const octave_value& tc_max_len, bool& err,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5961 const std::string& who)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5962 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5963 err = false;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5964
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5965 int conv_err = 0;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5966
6345
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
5967 int max_len = -1;
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
5968
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
5969 if (tc_max_len.is_defined ())
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5970 {
6345
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
5971 max_len = convert_to_valid_int (tc_max_len, conv_err);
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
5972
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
5973 if (conv_err || max_len < 0)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5974 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5975 err = true;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5976 ::error ("%s: invalid maximum length specified", who.c_str ());
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5977 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5978 }
6345
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
5979
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5980 return getl (max_len, err, who);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5981 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5982
3536
e8fbc8c3d6d9 [project @ 2000-02-02 12:01:00 by jwe]
jwe
parents: 3535
diff changeset
5983 std::string
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5266
diff changeset
5984 octave_stream::gets (octave_idx_type max_len, bool& err, const std::string& who)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5985 {
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
5986 std::string retval;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5987
5659
960f4b9a26af [project @ 2006-03-10 15:35:20 by jwe]
jwe
parents: 5649
diff changeset
5988 if (stream_ok ())
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
5989 retval = rep->gets (max_len, err, who);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5990
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5991 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5992 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5993
3536
e8fbc8c3d6d9 [project @ 2000-02-02 12:01:00 by jwe]
jwe
parents: 3535
diff changeset
5994 std::string
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
5995 octave_stream::gets (const octave_value& tc_max_len, bool& err,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5996 const std::string& who)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5997 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5998 err = false;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5999
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6000 int conv_err = 0;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6001
6345
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
6002 int max_len = -1;
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
6003
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
6004 if (tc_max_len.is_defined ())
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6005 {
6345
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
6006 max_len = convert_to_valid_int (tc_max_len, conv_err);
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
6007
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
6008 if (conv_err || max_len < 0)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6009 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6010 err = true;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6011 ::error ("%s: invalid maximum length specified", who.c_str ());
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6012 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6013 }
6345
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
6014
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6015 return gets (max_len, err, who);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6016 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6017
16011
8122286c69a9 initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents: 15467
diff changeset
6018 off_t
8122286c69a9 initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents: 15467
diff changeset
6019 octave_stream::skipl (off_t count, bool& err, const std::string& who)
9701
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6020 {
16011
8122286c69a9 initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents: 15467
diff changeset
6021 off_t retval = -1;
9701
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6022
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6023 if (stream_ok ())
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6024 retval = rep->skipl (count, err, who);
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6025
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6026 return retval;
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6027 }
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6028
16011
8122286c69a9 initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents: 15467
diff changeset
6029 off_t
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
6030 octave_stream::skipl (const octave_value& tc_count, bool& err,
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
6031 const std::string& who)
9701
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6032 {
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6033 err = false;
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6034
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6035 int conv_err = 0;
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6036
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6037 int count = 1;
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6038
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6039 if (tc_count.is_defined ())
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6040 {
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6041 if (tc_count.is_scalar_type () && xisinf (tc_count.scalar_value ()))
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6042 count = -1;
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6043 else
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6044 {
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6045 count = convert_to_valid_int (tc_count, conv_err);
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6046
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6047 if (conv_err || count < 0)
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6048 {
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6049 err = true;
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6050 ::error ("%s: invalid number of lines specified", who.c_str ());
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6051 }
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6052 }
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6053 }
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6054
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6055 return skipl (count, err, who);
9701
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6056 }
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6057
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6058 int
16011
8122286c69a9 initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents: 15467
diff changeset
6059 octave_stream::seek (off_t offset, int origin)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6060 {
5065
1312d3af9eb0 [project @ 2004-11-04 20:12:00 by jwe]
jwe
parents: 5030
diff changeset
6061 int status = -1;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6062
5659
960f4b9a26af [project @ 2006-03-10 15:35:20 by jwe]
jwe
parents: 5649
diff changeset
6063 if (stream_ok ())
4889
658aad1c1b05 [project @ 2004-04-30 22:38:30 by jwe]
jwe
parents: 4888
diff changeset
6064 {
658aad1c1b05 [project @ 2004-04-30 22:38:30 by jwe]
jwe
parents: 4888
diff changeset
6065 clearerr ();
658aad1c1b05 [project @ 2004-04-30 22:38:30 by jwe]
jwe
parents: 4888
diff changeset
6066
12957
fb69561e5901 maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
6067 // Find current position so we can return to it if needed.
16011
8122286c69a9 initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents: 15467
diff changeset
6068 off_t orig_pos = rep->tell ();
5065
1312d3af9eb0 [project @ 2004-11-04 20:12:00 by jwe]
jwe
parents: 5030
diff changeset
6069
12957
fb69561e5901 maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
6070 // Move to end of file. If successful, find the offset of the end.
fb69561e5901 maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
6071 status = rep->seek (0, SEEK_END);
5065
1312d3af9eb0 [project @ 2004-11-04 20:12:00 by jwe]
jwe
parents: 5030
diff changeset
6072
1312d3af9eb0 [project @ 2004-11-04 20:12:00 by jwe]
jwe
parents: 5030
diff changeset
6073 if (status == 0)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6074 {
16011
8122286c69a9 initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents: 15467
diff changeset
6075 off_t eof_pos = rep->tell ();
12957
fb69561e5901 maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
6076
12943
8372d50de75a improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents: 12936
diff changeset
6077 if (origin == SEEK_CUR)
8372d50de75a improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents: 12936
diff changeset
6078 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6079 // Move back to original position, otherwise we will be seeking
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6080 // from the end of file which is probably not the original
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6081 // location.
12943
8372d50de75a improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents: 12936
diff changeset
6082 rep->seek (orig_pos, SEEK_SET);
8372d50de75a improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents: 12936
diff changeset
6083 }
8372d50de75a improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents: 12936
diff changeset
6084
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6085 // Attempt to move to desired position; may be outside bounds of
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6086 // existing file.
12957
fb69561e5901 maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
6087 status = rep->seek (offset, origin);
fb69561e5901 maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
6088
fb69561e5901 maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
6089 if (status == 0)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6090 {
12957
fb69561e5901 maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
6091 // Where are we after moving to desired position?
16011
8122286c69a9 initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents: 15467
diff changeset
6092 off_t desired_pos = rep->tell ();
12957
fb69561e5901 maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
6093
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6094 // I don't think save_pos can be less than zero,
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6095 // but we'll check anyway...
12957
fb69561e5901 maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
6096 if (desired_pos > eof_pos || desired_pos < 0)
fb69561e5901 maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
6097 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6098 // Seek outside bounds of file.
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6099 // Failure should leave position unchanged.
12957
fb69561e5901 maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
6100 rep->seek (orig_pos, SEEK_SET);
fb69561e5901 maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
6101
fb69561e5901 maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
6102 status = -1;
fb69561e5901 maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
6103 }
fb69561e5901 maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
6104 }
fb69561e5901 maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
6105 else
fb69561e5901 maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
6106 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6107 // Seeking to the desired position failed.
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6108 // Move back to original position and return failure status.
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6109 rep->seek (orig_pos, SEEK_SET);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6110
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6111 status = -1;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6112 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6113 }
4889
658aad1c1b05 [project @ 2004-04-30 22:38:30 by jwe]
jwe
parents: 4888
diff changeset
6114 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6115
5065
1312d3af9eb0 [project @ 2004-11-04 20:12:00 by jwe]
jwe
parents: 5030
diff changeset
6116 return status;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6117 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6118
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6119 int
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6120 octave_stream::seek (const octave_value& tc_offset,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6121 const octave_value& tc_origin)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6122 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6123 int retval = -1;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6124
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
6125 // FIXME: should we have octave_value methods that handle off_t explicitly?
20704
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6126 octave_int64 val = tc_offset.xint64_scalar_value ("fseek: invalid value for offset");
16011
8122286c69a9 initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents: 15467
diff changeset
6127 off_t xoffset = val.value ();
4645
bd2067547b40 [project @ 2003-11-23 08:07:52 by jwe]
jwe
parents: 4643
diff changeset
6128
20704
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6129 int conv_err = 0;
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6130
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6131 int origin = SEEK_SET;
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6132
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6133 if (tc_origin.is_string ())
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6134 {
20704
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6135 std::string xorigin = tc_origin.string_value ("fseek: invalid value for origin");
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6136
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6137 if (xorigin == "bof")
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6138 origin = SEEK_SET;
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6139 else if (xorigin == "cof")
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6140 origin = SEEK_CUR;
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6141 else if (xorigin == "eof")
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6142 origin = SEEK_END;
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6143 else
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6144 conv_err = -1;
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6145 }
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6146 else
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6147 {
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6148 int xorigin = convert_to_valid_int (tc_origin, conv_err);
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6149
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6150 if (! conv_err)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6151 {
20704
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6152 if (xorigin == -1)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6153 origin = SEEK_SET;
20704
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6154 else if (xorigin == 0)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6155 origin = SEEK_CUR;
20704
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6156 else if (xorigin == 1)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6157 origin = SEEK_END;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6158 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6159 conv_err = -1;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6160 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6161 }
20704
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6162
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
6163 if (conv_err)
20938
aac911d8847b choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents: 20931
diff changeset
6164 ::error ("fseek: invalid value for origin");
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6165
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
6166 retval = seek (xoffset, origin);
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
6167
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
6168 if (retval != 0)
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
6169 // Note: error is member fcn from octave_stream, not ::error.
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
6170 error ("fseek: failed to seek to requested position");
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6171
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6172 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6173 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6174
16011
8122286c69a9 initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents: 15467
diff changeset
6175 off_t
4797
19b8225bdaa2 [project @ 2004-02-23 22:10:31 by jwe]
jwe
parents: 4693
diff changeset
6176 octave_stream::tell (void)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6177 {
16011
8122286c69a9 initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents: 15467
diff changeset
6178 off_t retval = -1;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6179
5659
960f4b9a26af [project @ 2006-03-10 15:35:20 by jwe]
jwe
parents: 5649
diff changeset
6180 if (stream_ok ())
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6181 retval = rep->tell ();
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6182
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6183 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6184 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6185
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6186 int
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6187 octave_stream::rewind (void)
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6188 {
6296
85eb75190e01 [project @ 2007-02-10 07:10:42 by jwe]
jwe
parents: 6109
diff changeset
6189 return seek (0, SEEK_SET);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6190 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6191
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
6192 bool
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
6193 octave_stream::is_open (void) const
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
6194 {
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
6195 bool retval = false;
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
6196
5659
960f4b9a26af [project @ 2006-03-10 15:35:20 by jwe]
jwe
parents: 5649
diff changeset
6197 if (stream_ok ())
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
6198 retval = rep->is_open ();
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
6199
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
6200 return retval;
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
6201 }
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
6202
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
6203 void
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
6204 octave_stream::close (void)
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
6205 {
5659
960f4b9a26af [project @ 2006-03-10 15:35:20 by jwe]
jwe
parents: 5649
diff changeset
6206 if (stream_ok ())
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
6207 rep->close ();
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
6208 }
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
6209
21335
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6210 // FIXME: maybe these should be defined in lo-ieee.h?
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6211
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6212 template <typename T>
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6213 static inline bool
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6214 is_old_NA (T)
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6215 {
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6216 return false;
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6217 }
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6218
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6219 template <>
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6220 inline bool
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6221 is_old_NA<double> (double val)
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6222 {
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6223 return __lo_ieee_is_old_NA (val);
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6224 }
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6225
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6226 template <typename T>
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6227 static inline T
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6228 replace_old_NA (T val)
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6229 {
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6230 return val;
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6231 }
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6232
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6233 template <>
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6234 inline double
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6235 replace_old_NA<double> (double val)
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6236 {
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6237 return __lo_ieee_replace_old_NA (val);
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6238 }
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6239
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21129
diff changeset
6240 template <typename SRC_T, typename DST_T>
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6241 static octave_value
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6242 convert_and_copy (std::list<void *>& input_buf_list,
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6243 octave_idx_type input_buf_elts,
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6244 octave_idx_type elts_read,
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6245 octave_idx_type nr, octave_idx_type nc, bool swap,
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6246 bool do_float_fmt_conv, bool do_NA_conv,
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6247 oct_mach_info::float_format from_flt_fmt)
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6248 {
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6249 typedef typename DST_T::element_type dst_elt_type;
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6250
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6251 DST_T conv (dim_vector (nr, nc));
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6252
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6253 dst_elt_type *conv_data = conv.fortran_vec ();
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6254
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6255 octave_idx_type j = 0;
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6256
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6257 for (std::list<void *>::const_iterator it = input_buf_list.begin ();
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6258 it != input_buf_list.end (); it++)
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6259 {
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6260 SRC_T *data = static_cast<SRC_T *> (*it);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6261
17463
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6262 if (swap || do_float_fmt_conv)
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6263 {
21335
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6264 if (do_NA_conv)
17463
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6265 {
21335
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6266 for (octave_idx_type i = 0; i < input_buf_elts && j < elts_read;
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6267 i++, j++)
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6268 {
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6269 if (swap)
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6270 swap_bytes<sizeof (SRC_T)> (&data[i]);
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6271 else if (do_float_fmt_conv)
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6272 do_float_format_conversion (&data[i], sizeof (SRC_T),
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6273 1, from_flt_fmt,
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6274 oct_mach_info::native_float_format ());
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6275
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6276 dst_elt_type tmp (data[i]);
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6277
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6278 if (is_old_NA (tmp))
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6279 tmp = replace_old_NA (tmp);
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6280
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6281 conv_data[j] = tmp;
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6282 }
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6283 }
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6284 else
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6285 {
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6286 for (octave_idx_type i = 0; i < input_buf_elts && j < elts_read;
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6287 i++, j++)
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6288 {
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6289 if (swap)
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6290 swap_bytes<sizeof (SRC_T)> (&data[i]);
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6291 else if (do_float_fmt_conv)
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6292 do_float_format_conversion (&data[i], sizeof (SRC_T),
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6293 1, from_flt_fmt,
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6294 oct_mach_info::native_float_format ());
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6295
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6296 conv_data[j] = data[i];
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6297 }
17463
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6298 }
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6299 }
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6300 else
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6301 {
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6302 if (do_NA_conv)
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6303 {
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6304 for (octave_idx_type i = 0; i < input_buf_elts && j < elts_read;
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6305 i++, j++)
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6306 {
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6307 dst_elt_type tmp (data[i]);
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6308
21335
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6309 if (is_old_NA (tmp))
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6310 tmp = replace_old_NA (tmp);
17463
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6311
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6312 conv_data[j] = tmp;
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6313 }
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6314 }
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6315 else
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6316 {
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6317 for (octave_idx_type i = 0; i < input_buf_elts && j < elts_read;
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6318 i++, j++)
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6319 conv_data[j] = data[i];
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6320 }
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6321 }
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6322
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6323 delete [] data;
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6324 }
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6325
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6326 input_buf_list.clear ();
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6327
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6328 for (octave_idx_type i = elts_read; i < nr * nc; i++)
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6329 conv_data[i] = dst_elt_type (0);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6330
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6331 return conv;
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6332 }
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6333
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6334 typedef octave_value (*conv_fptr)
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6335 (std::list<void *>& input_buf_list, octave_idx_type input_buf_elts,
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6336 octave_idx_type elts_read, octave_idx_type nr, octave_idx_type nc,
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6337 bool swap, bool do_float_fmt_conv, bool do_NA_conv,
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6338 oct_mach_info::float_format from_flt_fmt);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6339
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6340 #define TABLE_ELT(T, U, V, W) \
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6341 conv_fptr_table[oct_data_conv::T][oct_data_conv::U] = convert_and_copy<V, W>
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6342
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6343 #define FILL_TABLE_ROW(T, V) \
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6344 TABLE_ELT (T, dt_int8, V, int8NDArray); \
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6345 TABLE_ELT (T, dt_uint8, V, uint8NDArray); \
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6346 TABLE_ELT (T, dt_int16, V, int16NDArray); \
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6347 TABLE_ELT (T, dt_uint16, V, uint16NDArray); \
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6348 TABLE_ELT (T, dt_int32, V, int32NDArray); \
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6349 TABLE_ELT (T, dt_uint32, V, uint32NDArray); \
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6350 TABLE_ELT (T, dt_int64, V, int64NDArray); \
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6351 TABLE_ELT (T, dt_uint64, V, uint64NDArray); \
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6352 TABLE_ELT (T, dt_single, V, FloatNDArray); \
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6353 TABLE_ELT (T, dt_double, V, NDArray); \
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6354 TABLE_ELT (T, dt_char, V, charNDArray); \
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6355 TABLE_ELT (T, dt_schar, V, charNDArray); \
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6356 TABLE_ELT (T, dt_uchar, V, charNDArray); \
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6357 TABLE_ELT (T, dt_logical, V, boolNDArray);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6358
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6359 octave_value
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6360 octave_stream::finalize_read (std::list<void *>& input_buf_list,
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6361 octave_idx_type input_buf_elts,
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6362 octave_idx_type elts_read,
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6363 octave_idx_type nr, octave_idx_type nc,
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6364 oct_data_conv::data_type input_type,
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6365 oct_data_conv::data_type output_type,
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6366 oct_mach_info::float_format ffmt)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6367 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6368 octave_value retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6369
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6370 static bool initialized = false;
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6371
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6372 // Table function pointers for return types x read types.
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6373
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6374 static conv_fptr conv_fptr_table[oct_data_conv::dt_unknown][14];
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6375
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6376 if (! initialized)
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6377 {
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6378 for (int i = 0; i < oct_data_conv::dt_unknown; i++)
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6379 for (int j = 0; j < 14; j++)
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6380 conv_fptr_table[i][j] = 0;
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6381
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6382 FILL_TABLE_ROW (dt_int8, int8_t);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6383 FILL_TABLE_ROW (dt_uint8, uint8_t);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6384 FILL_TABLE_ROW (dt_int16, int16_t);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6385 FILL_TABLE_ROW (dt_uint16, uint16_t);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6386 FILL_TABLE_ROW (dt_int32, int32_t);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6387 FILL_TABLE_ROW (dt_uint32, uint32_t);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6388 FILL_TABLE_ROW (dt_int64, int64_t);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6389 FILL_TABLE_ROW (dt_uint64, uint64_t);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6390 FILL_TABLE_ROW (dt_single, float);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6391 FILL_TABLE_ROW (dt_double, double);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6392 FILL_TABLE_ROW (dt_char, char);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6393 FILL_TABLE_ROW (dt_schar, signed char);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6394 FILL_TABLE_ROW (dt_uchar, unsigned char);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6395 FILL_TABLE_ROW (dt_logical, bool);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6396
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6397 initialized = true;
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6398 }
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6399
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6400 bool swap = false;
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6401
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6402 if (ffmt == oct_mach_info::flt_fmt_unknown)
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6403 ffmt = float_format ();
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6404
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6405 if (oct_mach_info::words_big_endian ())
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6406 swap = (ffmt == oct_mach_info::flt_fmt_ieee_little_endian);
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6407 else
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6408 swap = (ffmt == oct_mach_info::flt_fmt_ieee_big_endian);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6409
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6410 bool do_float_fmt_conv = ((input_type == oct_data_conv::dt_double
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6411 || input_type == oct_data_conv::dt_single)
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6412 && ffmt != float_format ());
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6413
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6414 bool do_NA_conv = (output_type == oct_data_conv::dt_double);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6415
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6416 switch (output_type)
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6417 {
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6418 case oct_data_conv::dt_int8:
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6419 case oct_data_conv::dt_uint8:
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6420 case oct_data_conv::dt_int16:
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6421 case oct_data_conv::dt_uint16:
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6422 case oct_data_conv::dt_int32:
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6423 case oct_data_conv::dt_uint32:
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6424 case oct_data_conv::dt_int64:
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6425 case oct_data_conv::dt_uint64:
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6426 case oct_data_conv::dt_single:
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6427 case oct_data_conv::dt_double:
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6428 case oct_data_conv::dt_char:
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6429 case oct_data_conv::dt_schar:
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6430 case oct_data_conv::dt_uchar:
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6431 case oct_data_conv::dt_logical:
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6432 {
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6433 conv_fptr fptr = conv_fptr_table[input_type][output_type];
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6434
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6435 retval = fptr (input_buf_list, input_buf_elts, elts_read,
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6436 nr, nc, swap, do_float_fmt_conv, do_NA_conv, ffmt);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6437 }
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6438 break;
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6439
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6440 default:
20938
aac911d8847b choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents: 20931
diff changeset
6441 ::error ("read: invalid type specification");
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6442 }
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
6443
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6444 return retval;
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6445 }
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6446
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6447 octave_value
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5266
diff changeset
6448 octave_stream::read (const Array<double>& size, octave_idx_type block_size,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6449 oct_data_conv::data_type input_type,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6450 oct_data_conv::data_type output_type,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6451 octave_idx_type skip, oct_mach_info::float_format ffmt,
18210
a0abcf377ec5 return elements read, not bytes in fread second output (bug #41091)
John W. Eaton <jwe@octave.org>
parents: 17787
diff changeset
6452 octave_idx_type& count)
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6453 {
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6454 octave_value retval;
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6455
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6456 octave_idx_type nr = -1;
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6457 octave_idx_type nc = -1;
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6458
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6459 bool one_elt_size_spec = false;
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6460
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6461 if (! stream_ok ())
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6462 return retval;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6463
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6464 // FIXME: we may eventually want to make this extensible.
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6465
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6466 // FIXME: we need a better way to ensure that this
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6467 // numbering stays consistent with the order of the elements in the
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6468 // data_type enum in the oct_data_conv class.
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6469
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6470 // Expose this in a future version?
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6471 octave_idx_type char_count = 0;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6472
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6473 count = 0;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6474
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6475 try
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6476 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6477 get_size (size, nr, nc, one_elt_size_spec, "fread");
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6478 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6479 catch (const octave_execution_exception&)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6480 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6481 invalid_operation ("fread", "reading");
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6482 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6483
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6484 octave_idx_type elts_to_read;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6485
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6486 if (one_elt_size_spec)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6487 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6488 // If NR == 0, Matlab returns [](0x0).
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6489
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6490 // If NR > 0, the result will be a column vector with the given
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6491 // number of rows.
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6492
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6493 // If NR < 0, then we have Inf and the result will be a column
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6494 // vector but we have to wait to see how big NR will be.
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6495
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6496 if (nr == 0)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6497 nr = nc = 0;
20741
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
6498 else
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6499 nc = 1;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6500 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6501 else
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6502 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6503 // Matlab returns [] even if there are two elements in the size
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6504 // specification and one is nonzero.
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6505
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6506 // If NC < 0 we have [NR, Inf] and we'll wait to decide how big NC
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6507 // should be.
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6508
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6509 if (nr == 0 || nc == 0)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6510 nr = nc = 0;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6511 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6512
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6513 // FIXME: Ensure that this does not overflow.
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6514 // Maybe try comparing nr * nc computed in double with
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6515 // std::numeric_limits<octave_idx_type>::max ();
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6516 elts_to_read = nr * nc;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6517
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6518 bool read_to_eof = elts_to_read < 0;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6519
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6520 octave_idx_type input_buf_elts = -1;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6521
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6522 if (skip == 0)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6523 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6524 if (read_to_eof)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6525 input_buf_elts = 1024 * 1024;
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6526 else
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6527 input_buf_elts = elts_to_read;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6528 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6529 else
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6530 input_buf_elts = block_size;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6531
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6532 octave_idx_type input_elt_size
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6533 = oct_data_conv::data_type_size (input_type);
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6534
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6535 octave_idx_type input_buf_size = input_buf_elts * input_elt_size;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6536
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6537 assert (input_buf_size >= 0);
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6538
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6539 // Must also work and return correct type object
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6540 // for 0 elements to read.
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6541 std::istream *isp = input_stream ();
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6542
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6543 if (! isp)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6544 error ("fread: invalid input stream");
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6545 else
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6546 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6547 std::istream& is = *isp;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6548
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6549 std::list <void *> input_buf_list;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6550
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6551 while (is && ! is.eof ()
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6552 && (read_to_eof || count < elts_to_read))
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6553 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6554 if (! read_to_eof)
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6555 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6556 octave_idx_type remaining_elts = elts_to_read - count;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6557
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6558 if (remaining_elts < input_buf_elts)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6559 input_buf_size = remaining_elts * input_elt_size;
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6560 }
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6561
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6562 char *input_buf = new char [input_buf_size];
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6563
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6564 is.read (input_buf, input_buf_size);
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6565
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6566 size_t gcount = is.gcount ();
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6567
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6568 char_count += gcount;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6569
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6570 octave_idx_type nel = gcount / input_elt_size;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6571
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6572 count += nel;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6573
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6574 input_buf_list.push_back (input_buf);
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6575
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6576 if (is && skip != 0 && nel == block_size)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6577 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6578 // Seek to skip.
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6579 // If skip would move past EOF, position at EOF.
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6580
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6581 off_t orig_pos = tell ();
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6582
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6583 seek (0, SEEK_END);
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6584
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6585 off_t eof_pos = tell ();
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6586
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6587 // Is it possible for this to fail to return us to
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6588 // the original position?
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6589 seek (orig_pos, SEEK_SET);
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6590
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6591 off_t remaining = eof_pos - orig_pos;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6592
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6593 if (remaining < skip)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6594 seek (0, SEEK_END);
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6595 else
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6596 seek (skip, SEEK_CUR);
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6597
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6598 if (! is)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6599 break;
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6600 }
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6601 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6602
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6603 if (read_to_eof)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6604 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6605 if (nc < 0)
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6606 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6607 nc = count / nr;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6608
20741
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
6609 if (count % nr != 0)
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6610 nc++;
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6611 }
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6612 else
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6613 nr = count;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6614 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6615 else if (count == 0)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6616 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6617 nr = 0;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6618 nc = 0;
20741
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
6619 }
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6620 else if (count != nr * nc)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6621 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6622 if (count % nr != 0)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6623 nc = count / nr + 1;
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6624 else
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6625 nc = count / nr;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6626
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6627 if (count < nr)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6628 nr = count;
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6629 }
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6630
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6631 retval = finalize_read (input_buf_list, input_buf_elts, count,
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6632 nr, nc, input_type, output_type, ffmt);
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6633 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6634
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6635 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6636 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6637
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5266
diff changeset
6638 octave_idx_type
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5266
diff changeset
6639 octave_stream::write (const octave_value& data, octave_idx_type block_size,
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
6640 oct_data_conv::data_type output_type,
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
6641 octave_idx_type skip, oct_mach_info::float_format flt_fmt)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6642 {
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5266
diff changeset
6643 octave_idx_type retval = -1;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6644
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6645 if (! stream_ok ())
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6646 invalid_operation ("fwrite", "writing");
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6647 else
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6648 {
20741
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
6649 if (flt_fmt == oct_mach_info::flt_fmt_unknown)
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
6650 flt_fmt = float_format ();
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
6651
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
6652 octave_idx_type status = data.write (*this, block_size, output_type,
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
6653 skip, flt_fmt);
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
6654
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
6655 if (status < 0)
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
6656 error ("fwrite: write error");
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6657 else
20741
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
6658 retval = status;
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6659 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6660
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6661 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6662 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6663
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21129
diff changeset
6664 template <typename T, typename V>
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6665 static void
18616
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6666 convert_chars (const void *data, void *conv_data, octave_idx_type n_elts)
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6667 {
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6668 const T *tt_data = static_cast<const T *> (data);
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6669
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6670 V *vt_data = static_cast<V *> (conv_data);
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6671
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6672 for (octave_idx_type i = 0; i < n_elts; i++)
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6673 vt_data[i] = tt_data[i];
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6674 }
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6675
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21129
diff changeset
6676 template <typename T, typename V>
18616
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6677 static void
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6678 convert_ints (const T *data, void *conv_data, octave_idx_type n_elts,
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6679 bool swap)
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6680 {
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6681 typedef typename V::val_type val_type;
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6682
19739
3fa35defe495 Adjust spacing of static_cast<> calls to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19729
diff changeset
6683 val_type *vt_data = static_cast<val_type *> (conv_data);
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6684
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6685 for (octave_idx_type i = 0; i < n_elts; i++)
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6686 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6687 // Yes, we want saturation semantics when converting to an integer type.
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6688 V val (data[i]);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6689
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6690 vt_data[i] = val.value ();
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6691
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6692 if (swap)
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6693 swap_bytes<sizeof (val_type)> (&vt_data[i]);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6694 }
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6695 }
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6696
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21129
diff changeset
6697 template <typename T>
18616
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6698 class ultimate_element_type
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6699 {
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6700 public:
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6701 typedef T type;
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6702 };
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6703
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21129
diff changeset
6704 template <typename T>
18616
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6705 class ultimate_element_type<octave_int<T> >
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6706 {
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6707 public:
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6708 typedef T type;
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6709 };
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6710
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21129
diff changeset
6711 template <typename T>
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6712 static bool
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6713 convert_data (const T *data, void *conv_data, octave_idx_type n_elts,
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6714 oct_data_conv::data_type output_type,
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6715 oct_mach_info::float_format flt_fmt)
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6716 {
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6717 bool retval = true;
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6718
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6719 bool swap
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6720 = ((oct_mach_info::words_big_endian ()
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6721 && flt_fmt == oct_mach_info::flt_fmt_ieee_little_endian)
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6722 || flt_fmt == oct_mach_info::flt_fmt_ieee_big_endian);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6723
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6724 bool do_float_conversion = flt_fmt != oct_mach_info::float_format ();
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6725
18616
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6726 typedef typename ultimate_element_type<T>::type ult_elt_type;
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6727
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6728 switch (output_type)
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6729 {
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6730 case oct_data_conv::dt_char:
18616
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6731 convert_chars<ult_elt_type, char> (data, conv_data, n_elts);
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6732 break;
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6733
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6734 case oct_data_conv::dt_schar:
18616
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6735 convert_chars<ult_elt_type, signed char> (data, conv_data, n_elts);
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6736 break;
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6737
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6738 case oct_data_conv::dt_uchar:
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6739 convert_chars<ult_elt_type, unsigned char> (data, conv_data, n_elts);
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6740 break;
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6741
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6742 case oct_data_conv::dt_int8:
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6743 convert_ints<T, octave_int8> (data, conv_data, n_elts, swap);
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6744 break;
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6745
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6746 case oct_data_conv::dt_uint8:
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6747 convert_ints<T, octave_uint8> (data, conv_data, n_elts, swap);
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6748 break;
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6749
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6750 case oct_data_conv::dt_int16:
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6751 convert_ints<T, octave_int16> (data, conv_data, n_elts, swap);
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6752 break;
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6753
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6754 case oct_data_conv::dt_uint16:
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6755 convert_ints<T, octave_uint16> (data, conv_data, n_elts, swap);
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6756 break;
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6757
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6758 case oct_data_conv::dt_int32:
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6759 convert_ints<T, octave_int32> (data, conv_data, n_elts, swap);
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6760 break;
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6761
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6762 case oct_data_conv::dt_uint32:
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6763 convert_ints<T, octave_uint32> (data, conv_data, n_elts, swap);
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6764 break;
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6765
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6766 case oct_data_conv::dt_int64:
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6767 convert_ints<T, octave_int64> (data, conv_data, n_elts, swap);
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6768 break;
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6769
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6770 case oct_data_conv::dt_uint64:
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6771 convert_ints<T, octave_uint64> (data, conv_data, n_elts, swap);
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6772 break;
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6773
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6774 case oct_data_conv::dt_single:
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6775 {
19739
3fa35defe495 Adjust spacing of static_cast<> calls to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19729
diff changeset
6776 float *vt_data = static_cast<float *> (conv_data);
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6777
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6778 for (octave_idx_type i = 0; i < n_elts; i++)
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6779 {
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6780 vt_data[i] = data[i];
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6781
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6782 if (do_float_conversion)
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6783 do_float_format_conversion (&vt_data[i], 1, flt_fmt);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6784 }
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6785 }
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6786 break;
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6787
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6788 case oct_data_conv::dt_double:
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6789 {
19739
3fa35defe495 Adjust spacing of static_cast<> calls to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19729
diff changeset
6790 double *vt_data = static_cast<double *> (conv_data);
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6791
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6792 for (octave_idx_type i = 0; i < n_elts; i++)
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6793 {
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6794 vt_data[i] = data[i];
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6795
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6796 if (do_float_conversion)
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6797 do_double_format_conversion (&vt_data[i], 1, flt_fmt);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6798 }
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6799 }
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6800 break;
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6801
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6802 default:
20938
aac911d8847b choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents: 20931
diff changeset
6803 ::error ("write: invalid type specification");
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6804 }
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6805
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6806 return retval;
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6807 }
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6808
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6809 bool
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6810 octave_stream::write_bytes (const void *data, size_t nbytes)
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6811 {
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6812 bool status = false;
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6813
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6814 std::ostream *osp = output_stream ();
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6815
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6816 if (osp)
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6817 {
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6818 std::ostream& os = *osp;
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6819
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6820 if (os)
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6821 {
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6822 os.write (static_cast<const char *> (data), nbytes);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6823
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6824 if (os)
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6825 status = true;
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6826 }
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6827 }
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6828
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6829 return status;
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6830 }
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6831
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6832 bool
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6833 octave_stream::skip_bytes (size_t skip)
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6834 {
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6835 bool status = false;
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6836
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6837 std::ostream *osp = output_stream ();
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6838
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6839 if (! osp)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6840 return false;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6841
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6842 std::ostream& os = *osp;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6843
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6844 // Seek to skip when inside bounds of existing file.
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6845 // Otherwise, write NUL to skip.
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6846 off_t orig_pos = tell ();
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6847
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6848 seek (0, SEEK_END);
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6849
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6850 off_t eof_pos = tell ();
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6851
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6852 // Is it possible for this to fail to return us to the original position?
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6853 seek (orig_pos, SEEK_SET);
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6854
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6855 size_t remaining = eof_pos - orig_pos;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6856
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6857 if (remaining < skip)
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6858 {
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6859 seek (0, SEEK_END);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6860
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6861 // FIXME: probably should try to write larger blocks...
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6862 unsigned char zero = 0;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6863 for (size_t j = 0; j < skip - remaining; j++)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6864 os.write (reinterpret_cast<const char *> (&zero), 1);
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6865 }
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6866 else
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6867 seek (skip, SEEK_CUR);
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6868
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6869 if (os)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6870 status = true;
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6871
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6872 return status;
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6873 }
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6874
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21129
diff changeset
6875 template <typename T>
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5266
diff changeset
6876 octave_idx_type
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5266
diff changeset
6877 octave_stream::write (const Array<T>& data, octave_idx_type block_size,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6878 oct_data_conv::data_type output_type,
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6879 octave_idx_type skip,
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6880 oct_mach_info::float_format flt_fmt)
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6881 {
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
6882 bool swap = ((oct_mach_info::words_big_endian ()
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
6883 && flt_fmt == oct_mach_info::flt_fmt_ieee_little_endian)
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
6884 || flt_fmt == oct_mach_info::flt_fmt_ieee_big_endian);
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
6885
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
6886 bool do_data_conversion = (swap || ! is_equivalent_type<T> (output_type)
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
6887 || flt_fmt != oct_mach_info::float_format ());
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6888
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6889 octave_idx_type nel = data.numel ();
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6890
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6891 octave_idx_type chunk_size;
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6892
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6893 if (skip != 0)
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6894 chunk_size = block_size;
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6895 else if (do_data_conversion)
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6896 chunk_size = 1024 * 1024;
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6897 else
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6898 chunk_size = nel;
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6899
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6900 octave_idx_type i = 0;
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6901
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6902 const T *pdata = data.data ();
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6903
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6904 while (i < nel)
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6905 {
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6906 if (skip != 0)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6907 {
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6908 if (! skip_bytes (skip))
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6909 return -1;
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6910 }
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6911
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6912 octave_idx_type remaining_nel = nel - i;
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6913
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6914 if (chunk_size > remaining_nel)
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6915 chunk_size = remaining_nel;
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6916
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6917 bool status = false;
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6918
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6919 if (do_data_conversion)
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6920 {
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6921 size_t output_size
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6922 = chunk_size * oct_data_conv::data_type_size (output_type);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6923
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6924 OCTAVE_LOCAL_BUFFER (unsigned char, conv_data, output_size);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6925
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6926 status = convert_data (&pdata[i], conv_data, chunk_size,
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6927 output_type, flt_fmt);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6928
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6929 if (status)
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6930 status = write_bytes (conv_data, output_size);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6931 }
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6932 else
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6933 status = write_bytes (pdata, sizeof (T) * chunk_size);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6934
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6935 if (! status)
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6936 return -1;
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6937
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6938 i += chunk_size;
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6939 }
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6940
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6941 return nel;
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6942 }
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6943
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6944 #define INSTANTIATE_WRITE(T) \
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6945 template \
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6946 octave_idx_type \
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6947 octave_stream::write (const Array<T>& data, octave_idx_type block_size, \
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6948 oct_data_conv::data_type output_type, \
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6949 octave_idx_type skip, \
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6950 oct_mach_info::float_format flt_fmt)
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6951
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6952 INSTANTIATE_WRITE (octave_int8);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6953 INSTANTIATE_WRITE (octave_uint8);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6954 INSTANTIATE_WRITE (octave_int16);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6955 INSTANTIATE_WRITE (octave_uint16);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6956 INSTANTIATE_WRITE (octave_int32);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6957 INSTANTIATE_WRITE (octave_uint32);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6958 INSTANTIATE_WRITE (octave_int64);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6959 INSTANTIATE_WRITE (octave_uint64);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6960 INSTANTIATE_WRITE (int8_t);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6961 INSTANTIATE_WRITE (uint8_t);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6962 INSTANTIATE_WRITE (int16_t);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6963 INSTANTIATE_WRITE (uint16_t);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6964 INSTANTIATE_WRITE (int32_t);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6965 INSTANTIATE_WRITE (uint32_t);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6966 INSTANTIATE_WRITE (int64_t);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6967 INSTANTIATE_WRITE (uint64_t);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6968 INSTANTIATE_WRITE (bool);
21354
06d15e4e611a move public data type macros to octave-config.h
John W. Eaton <jwe@octave.org>
parents: 21335
diff changeset
6969 #if defined (OCTAVE_HAVE_OVERLOAD_CHAR_INT8_TYPES)
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6970 INSTANTIATE_WRITE (char);
21235
b48d65c5df5b Disable char/int8_t function overloads where not permitted (bug #45411)
Mike Miller <mtmiller@octave.org>
parents: 20252
diff changeset
6971 #endif
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6972 INSTANTIATE_WRITE (float);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6973 INSTANTIATE_WRITE (double);
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6974
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6975 octave_value
3810
f19f00339363 [project @ 2001-03-29 21:03:01 by jwe]
jwe
parents: 3779
diff changeset
6976 octave_stream::scanf (const std::string& fmt, const Array<double>& size,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6977 octave_idx_type& count, const std::string& who)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6978 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6979 octave_value retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6980
5659
960f4b9a26af [project @ 2006-03-10 15:35:20 by jwe]
jwe
parents: 5649
diff changeset
6981 if (stream_ok ())
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
6982 retval = rep->scanf (fmt, size, count, who);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6983
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6984 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6985 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6986
5279
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
6987 octave_value
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
6988 octave_stream::scanf (const octave_value& fmt, const Array<double>& size,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6989 octave_idx_type& count, const std::string& who)
5279
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
6990 {
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
6991 octave_value retval = Matrix ();
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
6992
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
6993 if (fmt.is_string ())
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
6994 {
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
6995 std::string sfmt = fmt.string_value ();
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
6996
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
6997 if (fmt.is_sq_string ())
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6998 sfmt = do_string_escapes (sfmt);
5279
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
6999
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7000 retval = scanf (sfmt, size, count, who);
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7001 }
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7002 else
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7003 {
20938
aac911d8847b choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents: 20931
diff changeset
7004 // Note: error is member fcn from octave_stream, not ::error.
5279
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7005 error (who + ": format must be a string");
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7006 }
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7007
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7008 return retval;
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7009 }
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7010
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
7011 octave_value_list
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
7012 octave_stream::oscanf (const std::string& fmt, const std::string& who)
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
7013 {
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
7014 octave_value_list retval;
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
7015
5659
960f4b9a26af [project @ 2006-03-10 15:35:20 by jwe]
jwe
parents: 5649
diff changeset
7016 if (stream_ok ())
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
7017 retval = rep->oscanf (fmt, who);
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
7018
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
7019 return retval;
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
7020 }
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
7021
5279
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7022 octave_value_list
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7023 octave_stream::oscanf (const octave_value& fmt, const std::string& who)
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7024 {
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7025 octave_value_list retval;
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7026
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7027 if (fmt.is_string ())
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7028 {
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7029 std::string sfmt = fmt.string_value ();
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7030
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7031 if (fmt.is_sq_string ())
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7032 sfmt = do_string_escapes (sfmt);
5279
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7033
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7034 retval = oscanf (sfmt, who);
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7035 }
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7036 else
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7037 {
20938
aac911d8847b choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents: 20931
diff changeset
7038 // Note: error is member fcn from octave_stream, not ::error.
5279
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7039 error (who + ": format must be a string");
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7040 }
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7041
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7042 return retval;
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7043 }
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7044
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
7045 octave_value
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
7046 octave_stream::textscan (const std::string& fmt, octave_idx_type ntimes,
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
7047 const octave_value_list& options,
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
7048 const std::string& who, octave_idx_type& count)
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
7049 {
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
7050 return (stream_ok ()
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
7051 ? rep->do_textscan (fmt, ntimes, options, who, count)
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
7052 : octave_value ());
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
7053 }
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
7054
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7055 int
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
7056 octave_stream::printf (const std::string& fmt, const octave_value_list& args,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7057 const std::string& who)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7058 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7059 int retval = -1;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7060
5659
960f4b9a26af [project @ 2006-03-10 15:35:20 by jwe]
jwe
parents: 5649
diff changeset
7061 if (stream_ok ())
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
7062 retval = rep->printf (fmt, args, who);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7063
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7064 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7065 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7066
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7067 int
5279
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7068 octave_stream::printf (const octave_value& fmt, const octave_value_list& args,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7069 const std::string& who)
5279
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7070 {
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7071 int retval = 0;
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7072
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7073 if (fmt.is_string ())
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7074 {
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7075 std::string sfmt = fmt.string_value ();
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7076
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7077 if (fmt.is_sq_string ())
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7078 sfmt = do_string_escapes (sfmt);
5279
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7079
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7080 retval = printf (sfmt, args, who);
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7081 }
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7082 else
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7083 {
20938
aac911d8847b choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents: 20931
diff changeset
7084 // Note: error is member fcn from octave_stream, not ::error.
5279
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7085 error (who + ": format must be a string");
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7086 }
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7087
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7088 return retval;
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7089 }
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7090
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7091 int
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
7092 octave_stream::puts (const std::string& s, const std::string& who)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7093 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7094 int retval = -1;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7095
5659
960f4b9a26af [project @ 2006-03-10 15:35:20 by jwe]
jwe
parents: 5649
diff changeset
7096 if (stream_ok ())
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
7097 retval = rep->puts (s, who);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7098
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7099 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7100 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7101
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
7102 // FIXME: maybe this should work for string arrays too.
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7103
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7104 int
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
7105 octave_stream::puts (const octave_value& tc_s, const std::string& who)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7106 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7107 int retval = -1;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7108
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7109 if (tc_s.is_string ())
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7110 {
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
7111 std::string s = tc_s.string_value ();
5279
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7112 retval = puts (s, who);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7113 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7114 else
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
7115 {
20938
aac911d8847b choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents: 20931
diff changeset
7116 // Note: error is member fcn from octave_stream, not ::error.
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
7117 error (who + ": argument must be a string");
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
7118 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7119
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7120 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7121 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7122
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7123 bool
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7124 octave_stream::eof (void) const
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7125 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7126 int retval = -1;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7127
5659
960f4b9a26af [project @ 2006-03-10 15:35:20 by jwe]
jwe
parents: 5649
diff changeset
7128 if (stream_ok ())
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7129 retval = rep->eof ();
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7130
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7131 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7132 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7133
3536
e8fbc8c3d6d9 [project @ 2000-02-02 12:01:00 by jwe]
jwe
parents: 3535
diff changeset
7134 std::string
2435
3be97fe02051 [project @ 1996-10-27 21:31:29 by jwe]
jwe
parents: 2341
diff changeset
7135 octave_stream::error (bool clear, int& err_num)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7136 {
5649
d24b97246b9b [project @ 2006-03-08 20:31:25 by jwe]
jwe
parents: 5581
diff changeset
7137 std::string retval = "invalid stream object";
d24b97246b9b [project @ 2006-03-08 20:31:25 by jwe]
jwe
parents: 5581
diff changeset
7138
5659
960f4b9a26af [project @ 2006-03-10 15:35:20 by jwe]
jwe
parents: 5649
diff changeset
7139 if (stream_ok (false))
2435
3be97fe02051 [project @ 1996-10-27 21:31:29 by jwe]
jwe
parents: 2341
diff changeset
7140 retval = rep->error (clear, err_num);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7141
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7142 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7143 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7144
3536
e8fbc8c3d6d9 [project @ 2000-02-02 12:01:00 by jwe]
jwe
parents: 3535
diff changeset
7145 std::string
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
7146 octave_stream::name (void) const
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7147 {
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
7148 std::string retval;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7149
5659
960f4b9a26af [project @ 2006-03-10 15:35:20 by jwe]
jwe
parents: 5649
diff changeset
7150 if (stream_ok ())
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7151 retval = rep->name ();
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7152
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7153 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7154 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7155
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7156 int
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
7157 octave_stream::mode (void) const
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7158 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7159 int retval = 0;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7160
5659
960f4b9a26af [project @ 2006-03-10 15:35:20 by jwe]
jwe
parents: 5649
diff changeset
7161 if (stream_ok ())
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7162 retval = rep->mode ();
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7163
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7164 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7165 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7166
2317
8c09c04f7747 [project @ 1996-07-14 22:30:15 by jwe]
jwe
parents: 2305
diff changeset
7167 oct_mach_info::float_format
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
7168 octave_stream::float_format (void) const
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7169 {
4574
6cb22b9e3942 [project @ 2003-10-31 05:57:43 by jwe]
jwe
parents: 4468
diff changeset
7170 oct_mach_info::float_format retval = oct_mach_info::flt_fmt_unknown;
2317
8c09c04f7747 [project @ 1996-07-14 22:30:15 by jwe]
jwe
parents: 2305
diff changeset
7171
5659
960f4b9a26af [project @ 2006-03-10 15:35:20 by jwe]
jwe
parents: 5649
diff changeset
7172 if (stream_ok ())
2317
8c09c04f7747 [project @ 1996-07-14 22:30:15 by jwe]
jwe
parents: 2305
diff changeset
7173 retval = rep->float_format ();
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7174
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7175 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7176 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7177
3536
e8fbc8c3d6d9 [project @ 2000-02-02 12:01:00 by jwe]
jwe
parents: 3535
diff changeset
7178 std::string
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7179 octave_stream::mode_as_string (int mode)
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7180 {
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
7181 std::string retval = "???";
3775
13905c3a24af [project @ 2001-02-06 01:56:59 by jwe]
jwe
parents: 3716
diff changeset
7182 std::ios::openmode in_mode = static_cast<std::ios::openmode> (mode);
13905c3a24af [project @ 2001-02-06 01:56:59 by jwe]
jwe
parents: 3716
diff changeset
7183
13905c3a24af [project @ 2001-02-06 01:56:59 by jwe]
jwe
parents: 3716
diff changeset
7184 if (in_mode == std::ios::in)
13905c3a24af [project @ 2001-02-06 01:56:59 by jwe]
jwe
parents: 3716
diff changeset
7185 retval = "r";
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
7186 else if (in_mode == std::ios::out
4078
bd51df637cb3 [project @ 2002-09-27 23:23:20 by jwe]
jwe
parents: 4070
diff changeset
7187 || in_mode == (std::ios::out | std::ios::trunc))
3775
13905c3a24af [project @ 2001-02-06 01:56:59 by jwe]
jwe
parents: 3716
diff changeset
7188 retval = "w";
4078
bd51df637cb3 [project @ 2002-09-27 23:23:20 by jwe]
jwe
parents: 4070
diff changeset
7189 else if (in_mode == (std::ios::out | std::ios::app))
3775
13905c3a24af [project @ 2001-02-06 01:56:59 by jwe]
jwe
parents: 3716
diff changeset
7190 retval = "a";
4078
bd51df637cb3 [project @ 2002-09-27 23:23:20 by jwe]
jwe
parents: 4070
diff changeset
7191 else if (in_mode == (std::ios::in | std::ios::out))
3775
13905c3a24af [project @ 2001-02-06 01:56:59 by jwe]
jwe
parents: 3716
diff changeset
7192 retval = "r+";
4078
bd51df637cb3 [project @ 2002-09-27 23:23:20 by jwe]
jwe
parents: 4070
diff changeset
7193 else if (in_mode == (std::ios::in | std::ios::out | std::ios::trunc))
3775
13905c3a24af [project @ 2001-02-06 01:56:59 by jwe]
jwe
parents: 3716
diff changeset
7194 retval = "w+";
4078
bd51df637cb3 [project @ 2002-09-27 23:23:20 by jwe]
jwe
parents: 4070
diff changeset
7195 else if (in_mode == (std::ios::in | std::ios::out | std::ios::ate))
3775
13905c3a24af [project @ 2001-02-06 01:56:59 by jwe]
jwe
parents: 3716
diff changeset
7196 retval = "a+";
4078
bd51df637cb3 [project @ 2002-09-27 23:23:20 by jwe]
jwe
parents: 4070
diff changeset
7197 else if (in_mode == (std::ios::in | std::ios::binary))
3775
13905c3a24af [project @ 2001-02-06 01:56:59 by jwe]
jwe
parents: 3716
diff changeset
7198 retval = "rb";
4078
bd51df637cb3 [project @ 2002-09-27 23:23:20 by jwe]
jwe
parents: 4070
diff changeset
7199 else if (in_mode == (std::ios::out | std::ios::binary)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7200 || in_mode == (std::ios::out | std::ios::trunc | std::ios::binary))
3775
13905c3a24af [project @ 2001-02-06 01:56:59 by jwe]
jwe
parents: 3716
diff changeset
7201 retval = "wb";
4078
bd51df637cb3 [project @ 2002-09-27 23:23:20 by jwe]
jwe
parents: 4070
diff changeset
7202 else if (in_mode == (std::ios::out | std::ios::app | std::ios::binary))
3775
13905c3a24af [project @ 2001-02-06 01:56:59 by jwe]
jwe
parents: 3716
diff changeset
7203 retval = "ab";
4078
bd51df637cb3 [project @ 2002-09-27 23:23:20 by jwe]
jwe
parents: 4070
diff changeset
7204 else if (in_mode == (std::ios::in | std::ios::out | std::ios::binary))
3775
13905c3a24af [project @ 2001-02-06 01:56:59 by jwe]
jwe
parents: 3716
diff changeset
7205 retval = "r+b";
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
7206 else if (in_mode == (std::ios::in | std::ios::out | std::ios::trunc
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7207 | std::ios::binary))
3775
13905c3a24af [project @ 2001-02-06 01:56:59 by jwe]
jwe
parents: 3716
diff changeset
7208 retval = "w+b";
4078
bd51df637cb3 [project @ 2002-09-27 23:23:20 by jwe]
jwe
parents: 4070
diff changeset
7209 else if (in_mode == (std::ios::in | std::ios::out | std::ios::ate
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7210 | std::ios::binary))
3775
13905c3a24af [project @ 2001-02-06 01:56:59 by jwe]
jwe
parents: 3716
diff changeset
7211 retval = "a+b";
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7212
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7213 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7214 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7215
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7216 octave_stream_list *octave_stream_list::instance = 0;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7217
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7218 bool
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7219 octave_stream_list::instance_ok (void)
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7220 {
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7221 bool retval = true;
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7222
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7223 if (! instance)
13983
7dd7cccf0757 clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents: 13271
diff changeset
7224 {
7dd7cccf0757 clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents: 13271
diff changeset
7225 instance = new octave_stream_list ();
7dd7cccf0757 clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents: 13271
diff changeset
7226
7dd7cccf0757 clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents: 13271
diff changeset
7227 if (instance)
7dd7cccf0757 clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents: 13271
diff changeset
7228 singleton_cleanup_list::add (cleanup_instance);
7dd7cccf0757 clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents: 13271
diff changeset
7229 }
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7230
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7231 if (! instance)
20831
35241c4b696c eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents: 20768
diff changeset
7232 ::error ("unable to create stream list object!");
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7233
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7234 return retval;
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7235 }
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7236
5353
df230b7df93c [project @ 2005-05-18 02:18:24 by jwe]
jwe
parents: 5338
diff changeset
7237 int
6757
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7238 octave_stream_list::insert (octave_stream& os)
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7239 {
5353
df230b7df93c [project @ 2005-05-18 02:18:24 by jwe]
jwe
parents: 5338
diff changeset
7240 return (instance_ok ()) ? instance->do_insert (os) : -1;
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7241 }
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7242
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
7243 octave_stream
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
7244 octave_stream_list::lookup (int fid, const std::string& who)
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7245 {
3341
14cfc9475fe4 [project @ 1999-11-05 08:11:58 by jwe]
jwe
parents: 3340
diff changeset
7246 return (instance_ok ()) ? instance->do_lookup (fid, who) : octave_stream ();
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7247 }
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7248
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
7249 octave_stream
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
7250 octave_stream_list::lookup (const octave_value& fid, const std::string& who)
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7251 {
3341
14cfc9475fe4 [project @ 1999-11-05 08:11:58 by jwe]
jwe
parents: 3340
diff changeset
7252 return (instance_ok ()) ? instance->do_lookup (fid, who) : octave_stream ();
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7253 }
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7254
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7255 int
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
7256 octave_stream_list::remove (int fid, const std::string& who)
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7257 {
3341
14cfc9475fe4 [project @ 1999-11-05 08:11:58 by jwe]
jwe
parents: 3340
diff changeset
7258 return (instance_ok ()) ? instance->do_remove (fid, who) : -1;
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7259 }
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7260
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7261 int
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
7262 octave_stream_list::remove (const octave_value& fid, const std::string& who)
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7263 {
3341
14cfc9475fe4 [project @ 1999-11-05 08:11:58 by jwe]
jwe
parents: 3340
diff changeset
7264 return (instance_ok ()) ? instance->do_remove (fid, who) : -1;
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7265 }
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7266
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7267 void
8902
5d5db7a347c6 erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8773
diff changeset
7268 octave_stream_list::clear (bool flush)
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7269 {
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7270 if (instance)
8902
5d5db7a347c6 erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8773
diff changeset
7271 instance->do_clear (flush);
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7272 }
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7273
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7274 string_vector
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7275 octave_stream_list::get_info (int fid)
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7276 {
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7277 return (instance_ok ()) ? instance->do_get_info (fid) : string_vector ();
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7278 }
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7279
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7280 string_vector
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7281 octave_stream_list::get_info (const octave_value& fid)
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7282 {
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7283 return (instance_ok ()) ? instance->do_get_info (fid) : string_vector ();
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7284 }
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7285
3536
e8fbc8c3d6d9 [project @ 2000-02-02 12:01:00 by jwe]
jwe
parents: 3535
diff changeset
7286 std::string
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7287 octave_stream_list::list_open_files (void)
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7288 {
21017
93748bcaec17 maint: Replace emtpy 'std::string ()' calls with "".
Rik <rik@octave.org>
parents: 20982
diff changeset
7289 return (instance_ok ()) ? instance->do_list_open_files () : "";
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7290 }
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7291
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7292 octave_value
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7293 octave_stream_list::open_file_numbers (void)
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7294 {
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7295 return (instance_ok ())
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
7296 ? instance->do_open_file_numbers () : octave_value ();
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7297 }
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7298
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7299 int
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7300 octave_stream_list::get_file_number (const octave_value& fid)
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7301 {
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7302 return (instance_ok ()) ? instance->do_get_file_number (fid) : -1;
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7303 }
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7304
5353
df230b7df93c [project @ 2005-05-18 02:18:24 by jwe]
jwe
parents: 5338
diff changeset
7305 int
6757
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7306 octave_stream_list::do_insert (octave_stream& os)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7307 {
6757
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7308 // Insert item with key corresponding to file-descriptor.
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7309
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
7310 int stream_number = os.file_number ();
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
7311
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
7312 if (stream_number == -1)
6757
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7313 return stream_number;
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7314
19864
17d647821d61 maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents: 19861
diff changeset
7315 // Should we test for
17d647821d61 maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents: 19861
diff changeset
7316 //
17d647821d61 maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents: 19861
diff changeset
7317 // (list.find (stream_number) != list.end ()
17d647821d61 maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents: 19861
diff changeset
7318 // && list[stream_number].is_open ())
17d647821d61 maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents: 19861
diff changeset
7319 //
17d647821d61 maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents: 19861
diff changeset
7320 // and respond with "error ("internal error: ...")"? It should not
17d647821d61 maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents: 19861
diff changeset
7321 // happen except for some bug or if the user has opened a stream with
17d647821d61 maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents: 19861
diff changeset
7322 // an interpreted command, but closed it directly with a system call
17d647821d61 maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents: 19861
diff changeset
7323 // in an oct-file; then the kernel knows the fd is free, but Octave
17d647821d61 maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents: 19861
diff changeset
7324 // does not know. If it happens, it should not do harm here to simply
17d647821d61 maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents: 19861
diff changeset
7325 // overwrite this entry, although the wrong entry might have done harm
17d647821d61 maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents: 19861
diff changeset
7326 // before.
6757
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7327
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7328 if (list.size () >= list.max_size ())
20938
aac911d8847b choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents: 20931
diff changeset
7329 ::error ("could not create file id");
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7330
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7331 list[stream_number] = os;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7332
5353
df230b7df93c [project @ 2005-05-18 02:18:24 by jwe]
jwe
parents: 5338
diff changeset
7333 return stream_number;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7334 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7335
21040
3e7cfee5f786 maint: Rename attributes GCC_ATTR_XXX to OCTAVE_XXX.
Rik <rik@octave.org>
parents: 21029
diff changeset
7336 OCTAVE_NORETURN static
21029
e3b3bb522d62 maint: Move GCC_ attributes to start of declaration for future compatibility.
Rik <rik@octave.org>
parents: 21017
diff changeset
7337 void
21102
dfcb9d74b253 Rename local gripe_XXX functions to err_XXX or warn_XXX.
Rik <rik@octave.org>
parents: 21100
diff changeset
7338 err_invalid_file_id (int fid, const std::string& who)
3341
14cfc9475fe4 [project @ 1999-11-05 08:11:58 by jwe]
jwe
parents: 3340
diff changeset
7339 {
14cfc9475fe4 [project @ 1999-11-05 08:11:58 by jwe]
jwe
parents: 3340
diff changeset
7340 if (who.empty ())
14cfc9475fe4 [project @ 1999-11-05 08:11:58 by jwe]
jwe
parents: 3340
diff changeset
7341 ::error ("invalid stream number = %d", fid);
14cfc9475fe4 [project @ 1999-11-05 08:11:58 by jwe]
jwe
parents: 3340
diff changeset
7342 else
14cfc9475fe4 [project @ 1999-11-05 08:11:58 by jwe]
jwe
parents: 3340
diff changeset
7343 ::error ("%s: invalid stream number = %d", who.c_str (), fid);
14cfc9475fe4 [project @ 1999-11-05 08:11:58 by jwe]
jwe
parents: 3340
diff changeset
7344 }
14cfc9475fe4 [project @ 1999-11-05 08:11:58 by jwe]
jwe
parents: 3340
diff changeset
7345
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
7346 octave_stream
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
7347 octave_stream_list::do_lookup (int fid, const std::string& who) const
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7348 {
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
7349 octave_stream retval;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7350
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7351 if (fid < 0)
21102
dfcb9d74b253 Rename local gripe_XXX functions to err_XXX or warn_XXX.
Rik <rik@octave.org>
parents: 21100
diff changeset
7352 err_invalid_file_id (fid, who);
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7353
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7354 if (lookup_cache != list.end () && lookup_cache->first == fid)
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7355 retval = lookup_cache->second;
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7356 else
6757
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7357 {
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7358 ostrl_map::const_iterator iter = list.find (fid);
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7359
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7360 if (iter == list.end ())
21102
dfcb9d74b253 Rename local gripe_XXX functions to err_XXX or warn_XXX.
Rik <rik@octave.org>
parents: 21100
diff changeset
7361 err_invalid_file_id (fid, who);
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7362
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7363 retval = iter->second;
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7364 lookup_cache = iter;
6757
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7365 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7366
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7367 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7368 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7369
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
7370 octave_stream
3341
14cfc9475fe4 [project @ 1999-11-05 08:11:58 by jwe]
jwe
parents: 3340
diff changeset
7371 octave_stream_list::do_lookup (const octave_value& fid,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7372 const std::string& who) const
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7373 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7374 int i = get_file_number (fid);
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7375
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
7376 return do_lookup (i, who);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7377 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7378
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7379 int
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
7380 octave_stream_list::do_remove (int fid, const std::string& who)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7381 {
20764
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7382 // Can't remove stdin (std::cin), stdout (std::cout), or stderr (std::cerr).
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
7383 if (fid < 3)
21102
dfcb9d74b253 Rename local gripe_XXX functions to err_XXX or warn_XXX.
Rik <rik@octave.org>
parents: 21100
diff changeset
7384 err_invalid_file_id (fid, who);
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7385
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7386 ostrl_map::iterator iter = list.find (fid);
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7387
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7388 if (iter == list.end ())
21102
dfcb9d74b253 Rename local gripe_XXX functions to err_XXX or warn_XXX.
Rik <rik@octave.org>
parents: 21100
diff changeset
7389 err_invalid_file_id (fid, who);
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7390
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7391 octave_stream os = iter->second;
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7392 list.erase (iter);
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7393 lookup_cache = list.end ();
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7394
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7395 // FIXME: is this check redundant?
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7396 if (! os.is_valid ())
21102
dfcb9d74b253 Rename local gripe_XXX functions to err_XXX or warn_XXX.
Rik <rik@octave.org>
parents: 21100
diff changeset
7397 err_invalid_file_id (fid, who);
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7398
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7399 os.close ();
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7400
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7401 return 0;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7402 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7403
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7404 int
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
7405 octave_stream_list::do_remove (const octave_value& fid, const std::string& who)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7406 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7407 int retval = -1;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7408
6054
769b203ede37 [project @ 2006-10-16 17:53:53 by jwe]
jwe
parents: 5887
diff changeset
7409 if (fid.is_string () && fid.string_value () == "all")
769b203ede37 [project @ 2006-10-16 17:53:53 by jwe]
jwe
parents: 5887
diff changeset
7410 {
8902
5d5db7a347c6 erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8773
diff changeset
7411 do_clear (false);
6054
769b203ede37 [project @ 2006-10-16 17:53:53 by jwe]
jwe
parents: 5887
diff changeset
7412
769b203ede37 [project @ 2006-10-16 17:53:53 by jwe]
jwe
parents: 5887
diff changeset
7413 retval = 0;
769b203ede37 [project @ 2006-10-16 17:53:53 by jwe]
jwe
parents: 5887
diff changeset
7414 }
769b203ede37 [project @ 2006-10-16 17:53:53 by jwe]
jwe
parents: 5887
diff changeset
7415 else
769b203ede37 [project @ 2006-10-16 17:53:53 by jwe]
jwe
parents: 5887
diff changeset
7416 {
769b203ede37 [project @ 2006-10-16 17:53:53 by jwe]
jwe
parents: 5887
diff changeset
7417 int i = get_file_number (fid);
769b203ede37 [project @ 2006-10-16 17:53:53 by jwe]
jwe
parents: 5887
diff changeset
7418
20558
1a0a433c8263 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20465
diff changeset
7419 retval = do_remove (i, who);
6054
769b203ede37 [project @ 2006-10-16 17:53:53 by jwe]
jwe
parents: 5887
diff changeset
7420 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7421
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7422 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7423 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7424
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7425 void
8902
5d5db7a347c6 erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8773
diff changeset
7426 octave_stream_list::do_clear (bool flush)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7427 {
8902
5d5db7a347c6 erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8773
diff changeset
7428 if (flush)
5d5db7a347c6 erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8773
diff changeset
7429 {
20764
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7430 // Flush stdout and stderr.
8902
5d5db7a347c6 erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8773
diff changeset
7431 list[1].flush ();
20764
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7432 list[2].flush ();
8902
5d5db7a347c6 erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8773
diff changeset
7433 }
5d5db7a347c6 erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8773
diff changeset
7434
20764
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7435 for (ostrl_map::iterator iter = list.begin (); iter != list.end (); )
6757
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7436 {
8902
5d5db7a347c6 erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8773
diff changeset
7437 int fid = iter->first;
20764
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7438 if (fid < 3) // Don't delete stdin, stdout, stderr
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7439 {
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7440 iter++;
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7441 continue;
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7442 }
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7443
8902
5d5db7a347c6 erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8773
diff changeset
7444 octave_stream os = iter->second;
20768
7655b399abff eliminate trailing whitespace in source files we maintain
John W. Eaton <jwe@octave.org>
parents: 20764
diff changeset
7445
7655b399abff eliminate trailing whitespace in source files we maintain
John W. Eaton <jwe@octave.org>
parents: 20764
diff changeset
7446 std::string name = os.name ();
20764
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7447 std::transform (name.begin (), name.end (), name.begin (), tolower);
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7448
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7449 // FIXME: This test for gnuplot is hardly foolproof.
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7450 if (name.find ("gnuplot") != std::string::npos)
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7451 {
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7452 // Don't close down pipes to gnuplot
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7453 iter++;
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7454 continue;
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7455 }
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7456
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7457 // Normal file handle. Close and delete from list.
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7458 if (os.is_valid ())
8902
5d5db7a347c6 erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8773
diff changeset
7459 os.close ();
20764
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7460
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7461 list.erase (iter++);
6757
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7462 }
20764
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7463
8902
5d5db7a347c6 erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8773
diff changeset
7464 lookup_cache = list.end ();
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7465 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7466
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7467 string_vector
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7468 octave_stream_list::do_get_info (int fid) const
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7469 {
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
7470 octave_stream os = do_lookup (fid);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7471
20892
c07bee629973 2015 Code Sprint: use ovl ().
Rik <rik@octave.org>
parents: 20831
diff changeset
7472 if (! os.is_valid ())
3341
14cfc9475fe4 [project @ 1999-11-05 08:11:58 by jwe]
jwe
parents: 3340
diff changeset
7473 ::error ("invalid file id = %d", fid);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7474
20892
c07bee629973 2015 Code Sprint: use ovl ().
Rik <rik@octave.org>
parents: 20831
diff changeset
7475 string_vector retval (3);
c07bee629973 2015 Code Sprint: use ovl ().
Rik <rik@octave.org>
parents: 20831
diff changeset
7476
c07bee629973 2015 Code Sprint: use ovl ().
Rik <rik@octave.org>
parents: 20831
diff changeset
7477 retval(0) = os.name ();
c07bee629973 2015 Code Sprint: use ovl ().
Rik <rik@octave.org>
parents: 20831
diff changeset
7478 retval(1) = octave_stream::mode_as_string (os.mode ());
c07bee629973 2015 Code Sprint: use ovl ().
Rik <rik@octave.org>
parents: 20831
diff changeset
7479 retval(2) = oct_mach_info::float_format_as_string (os.float_format ());
c07bee629973 2015 Code Sprint: use ovl ().
Rik <rik@octave.org>
parents: 20831
diff changeset
7480
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7481 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7482 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7483
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7484 string_vector
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7485 octave_stream_list::do_get_info (const octave_value& fid) const
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7486 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7487 int conv_err = 0;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7488
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7489 int int_fid = convert_to_valid_int (fid, conv_err);
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7490
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
7491 if (conv_err)
2915
649549662cf6 [project @ 1997-04-30 20:51:40 by jwe]
jwe
parents: 2902
diff changeset
7492 ::error ("file id must be a file object or integer value");
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7493
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
7494 return do_get_info (int_fid);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7495 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7496
3536
e8fbc8c3d6d9 [project @ 2000-02-02 12:01:00 by jwe]
jwe
parents: 3535
diff changeset
7497 std::string
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7498 octave_stream_list::do_list_open_files (void) const
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7499 {
5765
7ba9ad1fec11 [project @ 2006-04-17 05:05:15 by jwe]
jwe
parents: 5760
diff changeset
7500 std::ostringstream buf;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7501
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7502 buf << "\n"
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7503 << " number mode arch name\n"
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7504 << " ------ ---- ---- ----\n";
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7505
6757
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7506 for (ostrl_map::const_iterator p = list.begin (); p != list.end (); p++)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7507 {
6757
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7508 octave_stream os = p->second;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7509
4326
1cae4472c624 [project @ 2003-02-15 23:14:47 by jwe]
jwe
parents: 4312
diff changeset
7510 buf << " "
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7511 << std::setiosflags (std::ios::right)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7512 << std::setw (4) << p->first << " "
19123
c8f03ccdc9ee Align columns for freport() output.
Rik <rik@octave.org>
parents: 18676
diff changeset
7513 // reset necessary in addition to setiosflags since this is one stmt.
c8f03ccdc9ee Align columns for freport() output.
Rik <rik@octave.org>
parents: 18676
diff changeset
7514 << std::resetiosflags (std::ios::adjustfield)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7515 << std::setiosflags (std::ios::left)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7516 << std::setw (3)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7517 << octave_stream::mode_as_string (os.mode ())
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7518 << " "
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7519 << std::setw (9)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7520 << oct_mach_info::float_format_as_string (os.float_format ())
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7521 << " "
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7522 << os.name () << "\n";
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7523 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7524
5765
7ba9ad1fec11 [project @ 2006-04-17 05:05:15 by jwe]
jwe
parents: 5760
diff changeset
7525 buf << "\n";
7ba9ad1fec11 [project @ 2006-04-17 05:05:15 by jwe]
jwe
parents: 5760
diff changeset
7526
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
7527 return buf.str ();
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7528 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7529
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7530 octave_value
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7531 octave_stream_list::do_open_file_numbers (void) const
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7532 {
6757
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7533 Matrix retval (1, list.size (), 0.0);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7534
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7535 int num_open = 0;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7536
6757
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7537 for (ostrl_map::const_iterator p = list.begin (); p != list.end (); p++)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7538 {
6757
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7539 // Skip stdin, stdout, and stderr.
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7540 if (p->first > 2 && p->second)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7541 retval(0,num_open++) = p->first;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7542 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7543
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7544 retval.resize ((num_open > 0), num_open);
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7545
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7546 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7547 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7548
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7549 int
2609
12bc9d0a50b5 [project @ 1997-01-18 00:17:36 by jwe]
jwe
parents: 2601
diff changeset
7550 octave_stream_list::do_get_file_number (const octave_value& fid) const
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7551 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7552 int retval = -1;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7553
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7554 if (fid.is_string ())
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7555 {
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
7556 std::string nm = fid.string_value ();
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7557
6757
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7558 for (ostrl_map::const_iterator p = list.begin (); p != list.end (); p++)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7559 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
7560 // stdin, stdout, and stderr are unnamed.
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7561 if (p->first > 2)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7562 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7563 octave_stream os = p->second;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7564
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7565 if (os && os.name () == nm)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7566 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7567 retval = p->first;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7568 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7569 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7570 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7571 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7572 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7573 else
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7574 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7575 int conv_err = 0;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7576
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7577 int int_fid = convert_to_valid_int (fid, conv_err);
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7578
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7579 if (conv_err)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7580 ::error ("file id must be a file object, std::string, or integer value");
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
7581
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
7582 retval = int_fid;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7583 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7584
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7585 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7586 }