annotate libinterp/corefcn/oct-stream.cc @ 21517:e626d8a3427e

avoid possible memory leak related to ostringstream objects * oct-stream.cc (scanf_format_list::buf, printf_format_list::buf, textscan_format_list:buf): Use std::ostringstream object instead of a pointer to one that must be allocated. Instead of deleting, clear flags and initialize internal string buffer. Update all uses.
author John W. Eaton <jwe@octave.org>
date Mon, 21 Mar 2016 22:37:34 -0400
parents 20bf0ec536e2
children 936ab0fca2f7
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.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2596 delimited_stream is (isp, whitespace + delims, max_lookahead, buf_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
2597
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2598 // 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
2599 // (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
2600 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
2601 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
2602 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
2603
7a19c5678f91 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 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
2605 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
2606
7a19c5678f91 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 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
2608 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
2609
7a19c5678f91 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 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
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 // 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
2613 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
2614 {
7a19c5678f91 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 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
2616 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
2617
7a19c5678f91 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 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
2619 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
2620 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
2621 }
7a19c5678f91 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 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
2623 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
2624
7a19c5678f91 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 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
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 // 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
2628 // 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
2629 // 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
2630 // 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
2631 // 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
2632 // 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
2633 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
2634 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
2635 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
2636 {
7a19c5678f91 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 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
2638 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
2639 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
2640 {
7a19c5678f91 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 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
2642 || (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
2643 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
2644 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
2645 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
2646
7a19c5678f91 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 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
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 }
7a19c5678f91 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 // 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
2652 // 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
2653 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
2654 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
2655
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2656 // 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
2657 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
2658 {
7a19c5678f91 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 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
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 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
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 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
2664 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
2665 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
2666 *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
2667 }
7a19c5678f91 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
7a19c5678f91 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 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
2670 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
2671
7a19c5678f91 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 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
2673 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
2674 }
7a19c5678f91 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 }
7a19c5678f91 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 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
2678 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
2679 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
2680
7a19c5678f91 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 // 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
2682 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
2683 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
2684 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
2685 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
2686 {
7a19c5678f91 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 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
2688 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
2689 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
2690 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
2691 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
2692 }
7a19c5678f91 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
7a19c5678f91 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 // 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
2695 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
2696
7a19c5678f91 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 // (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
2698 // 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
2699 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
2700 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
2701
7a19c5678f91 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 int valid_rows = (row == ntimes) ? ntimes : ((err & 1) ? row : 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
2703 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
2704
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
2705 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
2706 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
2707 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
2708 {
7a19c5678f91 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 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
2710 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
2711 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
2712 {
7a19c5678f91 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 // 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
2714 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
2715 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
2716
7a19c5678f91 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 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
2718 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
2719 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
2720 }
7a19c5678f91 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 }
7a19c5678f91 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 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
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 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
2725 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
2726 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
2727
7a19c5678f91 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 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
2729 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
2730 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
2731 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
2732 {
7a19c5678f91 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 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
2734 {
7a19c5678f91 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 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
2736 {
7a19c5678f91 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 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
2738 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
2739 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
2740 }
7a19c5678f91 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 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
2742 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
2743 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
2744 }
7a19c5678f91 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 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
2746 {
7a19c5678f91 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 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
2748 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
2749 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
2750 }
7a19c5678f91 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 }
7a19c5678f91 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(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
2753 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
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
7a19c5678f91 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 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
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 // 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
2760 // 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
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 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
2763 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
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 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
2766
7a19c5678f91 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 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
2768 {
7a19c5678f91 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 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
2770 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
2771 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
2772 }
7a19c5678f91 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
7a19c5678f91 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 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
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 // 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
2778 // 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
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 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
2781 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
2782 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
2783 {
7a19c5678f91 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 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
2785 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
2786 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
2787 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
2788
7a19c5678f91 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 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
2790
7a19c5678f91 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 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
2792 {
7a19c5678f91 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 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
2794 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
2795 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
2796 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
2797 }
7a19c5678f91 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 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
2799 {
7a19c5678f91 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 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
2801 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
2802 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
2803 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
2804 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
2805 }
7a19c5678f91 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
7a19c5678f91 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 // 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
2808 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
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 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
2811 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
2812 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
2813 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
2814 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
2815 }
7a19c5678f91 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
7a19c5678f91 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 // 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
2818 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
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 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
2821 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
2822 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
2823
7a19c5678f91 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 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
2825 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
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 (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
2828 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
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 (! 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
2831 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
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 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
2834 {
7a19c5678f91 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 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
2836 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
2837 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
2838 {
7a19c5678f91 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 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
2840 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
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 }
7a19c5678f91 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
7a19c5678f91 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 // 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
2845 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
2846 && 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
2847 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
2848
7a19c5678f91 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 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
2850 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
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 // 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
2853 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
2854 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
2855 ; // 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
2856
7a19c5678f91 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 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
2858 }
7a19c5678f91 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 // 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
2861 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
2862 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
2863 {
7a19c5678f91 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 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
2865 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
2866 { // 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
2867 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
2868 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
2869 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
2870 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
2871 {
7a19c5678f91 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 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
2873 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
2874 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
2875 }
7a19c5678f91 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 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
2877 {
7a19c5678f91 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 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
2879 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
2880 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
2881 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
2882 }
7a19c5678f91 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 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
2884 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
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 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
2887 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
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 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
2890 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
2891 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
2892
7a19c5678f91 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 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
2894 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
2895 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
2896 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
2897 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
2898
7a19c5678f91 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 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
2900 }
7a19c5678f91 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 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
2903 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
2904 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
2905
7a19c5678f91 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 // 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
2907 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
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 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
2910 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
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 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
2913 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
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 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
2916 {
7a19c5678f91 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 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
2918 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
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 }
7a19c5678f91 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
7a19c5678f91 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 // 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
2923 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
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 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
2926 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
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 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
2929 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
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 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
2932 {
7a19c5678f91 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 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
2934 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
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 }
7a19c5678f91 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
7a19c5678f91 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 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
2939 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
2940 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
2941 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
2942
7a19c5678f91 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 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
2944 }
7a19c5678f91 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 // 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
2947 // 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
2948 // 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
2949
7a19c5678f91 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 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
2951 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
2952 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
2953 {
7a19c5678f91 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 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
2955 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
2956 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
2957 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
2958
7a19c5678f91 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 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
2960 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
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 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
2963 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
2964 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
2965 {
7a19c5678f91 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 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
2967 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
2968 // 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
2969 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
2970 {
7a19c5678f91 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 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
2972 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
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 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
2975 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
2976 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
2977 {
7a19c5678f91 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 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
2979 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
2980 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
2981 }
7a19c5678f91 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 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
2983 {
7a19c5678f91 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 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
2985 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
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 }
7a19c5678f91 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
7a19c5678f91 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 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
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 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
2992 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
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
7a19c5678f91 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 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
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 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
2998 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
2999 //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
3000 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
3001
7a19c5678f91 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 // 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
3003 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
3004 && (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
3005 || 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
3006 {
7a19c5678f91 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
7a19c5678f91 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 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
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 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
3011 {
7a19c5678f91 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 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
3013 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
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 }
7a19c5678f91 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 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
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 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
3019
7a19c5678f91 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 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
3021 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
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 // 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
3024 // 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
3025 // 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
3026 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
3027 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
3028
7a19c5678f91 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 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
3030 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
3031 // 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
3032
7a19c5678f91 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 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
3034 {
7a19c5678f91 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 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
3036 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
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 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
3039 // 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
3040 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
3041 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
3042 }
7a19c5678f91 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 }
7a19c5678f91 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 }
7a19c5678f91 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 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
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 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
3050 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
3051 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
3052 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
3053 {
7a19c5678f91 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 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
3055 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
3056 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
3057 }
7a19c5678f91 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 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
3059 {
7a19c5678f91 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 // 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
3061 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
3062 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
3063
7a19c5678f91 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 //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
3065 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
3066 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
3067 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
3068
7a19c5678f91 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.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
3070 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
3071 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
3072 {
7a19c5678f91 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 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
3074 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
3075 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
3076 }
7a19c5678f91 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 }
7a19c5678f91 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 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
3079 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
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 }
7a19c5678f91 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 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
3083 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
3084 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
3085 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
3086 }
7a19c5678f91 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
7a19c5678f91 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 // 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
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 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
3091 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
3092 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
3093 {
7a19c5678f91 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 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
3095 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
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 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
3098 ? 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
3099 : 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
3100 != 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
3101 && 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
3102 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
3103
7a19c5678f91 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 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
3105
7a19c5678f91 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 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
3107 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
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 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
3110 }
7a19c5678f91 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 // 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
3113 // 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
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 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
3116 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
3117 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
3118 {
7a19c5678f91 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 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
3120 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
3121 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
3122 { // 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
3123 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
3124 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
3125 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
3126
7a19c5678f91 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 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
3128 ? 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
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 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
3131 {
7a19c5678f91 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 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
3133 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
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 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
3136 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
3137 ? 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
3138 : 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
3139 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
3140 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
3141 {
7a19c5678f91 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 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
3143 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
3144 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
3145 }
7a19c5678f91 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 }
7a19c5678f91 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 }
7a19c5678f91 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 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
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 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
3152 }
7a19c5678f91 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
7a19c5678f91 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 // 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
3156 // 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
3157 // 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
3158
7a19c5678f91 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 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
3160 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
3161 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
3162 {
7a19c5678f91 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 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
3164 {
7a19c5678f91 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 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
3166 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
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 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
3169 {
7a19c5678f91 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 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
3171 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
3172 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
3173 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
3174 {
7a19c5678f91 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 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
3176 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
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 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
3179 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
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 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
3182 }
7a19c5678f91 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 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
3184 {
7a19c5678f91 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 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
3186 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
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 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
3189 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
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 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
3192 }
7a19c5678f91 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
7a19c5678f91 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 // 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
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 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
3198 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
3199 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
3200 {
7a19c5678f91 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 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
3202 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
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 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
3205 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
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 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
3208 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
3209 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
3210 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
3211 }
7a19c5678f91 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
7a19c5678f91 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 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
3214 // 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
3215 // 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
3216
7a19c5678f91 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 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
3218 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
3219 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
3220 {
7a19c5678f91 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 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
3222
7a19c5678f91 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 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
3224 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
3225 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
3226 {
7a19c5678f91 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 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
3228 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
3229 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
3230
7a19c5678f91 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 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
3232 { // 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
3233 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
3234 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
3235 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
3236 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
3237 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
3238 }
7a19c5678f91 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 }
7a19c5678f91 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 }
7a19c5678f91 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 // 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
3243 // 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
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 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
3246 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
3247 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
3248 {
7a19c5678f91 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 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
3250
7a19c5678f91 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 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
3252 {
7a19c5678f91 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 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
3254 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
3255 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
3256 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
3257 {
7a19c5678f91 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.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
3259 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
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 }
7a19c5678f91 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 }
7a19c5678f91 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 // 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
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 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
3268 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
3269 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
3270 {
7a19c5678f91 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 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
3272
7a19c5678f91 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 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
3274
7a19c5678f91 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 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
3276 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
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 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
3279 {
7a19c5678f91 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 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
3281 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
3282 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
3283
7a19c5678f91 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 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
3285 {
7a19c5678f91 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 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
3287 {
7a19c5678f91 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 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
3289 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
3290 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
3291 {
7a19c5678f91 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 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
3293 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
3294 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
3295 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
3296 }
7a19c5678f91 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 }
7a19c5678f91 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 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
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 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
3301 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
3302 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
3303 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
3304 {
7a19c5678f91 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 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
3306 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
3307 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
3308 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
3309 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
3310 }
7a19c5678f91 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 }
7a19c5678f91 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 }
7a19c5678f91 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 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
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 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
3317 // 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
3318 // 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
3319 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
3320 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
3321 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
3322 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
3323 {
7a19c5678f91 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 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
3325 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
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 '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
3328 {
7a19c5678f91 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 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
3330 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
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 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
3333
7a19c5678f91 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 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
3335 {
7a19c5678f91 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 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
3337 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
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 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
3340 }
7a19c5678f91 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 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
3342
7a19c5678f91 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 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
3344 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
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 '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
3347 {
7a19c5678f91 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 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
3349 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
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 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
3352
7a19c5678f91 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 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
3354 {
7a19c5678f91 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 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
3356 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
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 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
3359 }
7a19c5678f91 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 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
3361
7a19c5678f91 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 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
3363 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
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 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
3366 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
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 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
3369 {
7a19c5678f91 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 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
3371 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
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 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
3374
7a19c5678f91 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 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
3376 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
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 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
3379 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
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 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
3382 {
7a19c5678f91 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 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
3384 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
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 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
3387 }
7a19c5678f91 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
7a19c5678f91 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 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
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 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
3393 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
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 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
3396 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
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 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
3399 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
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::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
3402 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
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 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
3405 }
7a19c5678f91 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
7a19c5678f91 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 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
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 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
3412 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
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 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
3415 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
3416 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
3417
7a19c5678f91 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 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
3419 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
3420 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
3421
7a19c5678f91 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 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
3423 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
3424 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
3425
7a19c5678f91 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 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
3427 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
3428 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
3429
7a19c5678f91 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 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
3431 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
3432 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
3433 }
7a19c5678f91 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
7a19c5678f91 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 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
3436 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
3437 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
3438
7a19c5678f91 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 // 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
3440 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
3441 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
3442 }
7a19c5678f91 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
7a19c5678f91 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.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
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 // 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
3448 // 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
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 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
3451 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
3452 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
3453 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
3454 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
3455 {
7a19c5678f91 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 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
3457 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
3458 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
3459 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
3460 bool conversion_failed = false; // Record for 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
3461
7a19c5678f91 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 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
3463
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3464 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
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 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
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 // 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
3469 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
3470
7a19c5678f91 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 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
3472 {
7a19c5678f91 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 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
3474 case 'D':
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3475 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
3476 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
3477 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
3478
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3479 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
3480 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
3481 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
3482 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
3483 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
3484 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
3485 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
3486 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
3487 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
3488 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
3489 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
3490
7a19c5678f91 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 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
3492 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
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 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
3496 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
3497 }
7a19c5678f91 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 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
3500 {
7a19c5678f91 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 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
3502 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
3503 }
7a19c5678f91 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 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
3505 {
7a19c5678f91 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 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
3507 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
3508
7a19c5678f91 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 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
3510 }
7a19c5678f91 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
7a19c5678f91 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 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
3513 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
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 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
3516 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
3517
7a19c5678f91 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 // 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
3519 // 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
3520 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
3521 // && 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
3522 )
7a19c5678f91 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 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
3524
7a19c5678f91 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 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
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 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
3528 { // 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
3529 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
3530 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
3531 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3532 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
3533 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
3534 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3535
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3536 if (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
3537 {
7a19c5678f91 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 (! 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
3539 done_after = 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
3540
7a19c5678f91 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 // note EOF, but process 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
3542 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
3543 }
7a19c5678f91 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
7a19c5678f91 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 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
3547 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
3548
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3549 // Returning -3 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
3550 if (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
3551 return (2 + no_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
3552
7a19c5678f91 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 if (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
3554 return (4 + no_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
3555
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3556 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
3557 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3558
7a19c5678f91 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 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
3560 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
3561 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
3562 {
7a19c5678f91 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 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
3564 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
3565
7a19c5678f91 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 if (n & 1)
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3567 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
3568 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
3569
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3570 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
3571 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
3572 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
3573 {
7a19c5678f91 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 if (! args(i).is_string ())
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3575 error ("%s: Invalid paramter type <%s> for parameter %d",
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3576 who.c_str (), args(i).type_name ().c_str (), i/2 + 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
3577
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3578 std::string param = args(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
3579 std::transform (param.begin (), param.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
3580 param.begin (), ::tolower);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3581 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
3582 {
7a19c5678f91 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 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
3584 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
3585 {
7a19c5678f91 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 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
3587 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
3588 delims = args(i+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
3589 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3590 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
3591 {
7a19c5678f91 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 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
3593 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
3594 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
3595
7a19c5678f91 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 // 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
3597 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
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 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
3600 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
3601 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
3602 {
7a19c5678f91 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 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
3604 .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
3605 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
3606 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3607 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3608 }
7a19c5678f91 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 if (invalid)
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3610 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
3611 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
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 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
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 (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
3616 { // 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
3617 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
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 (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
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 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
3622 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
3623 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
3624 || (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
3625 || (len >= 3))
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3626 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
3627 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
3628 }
7a19c5678f91 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 else
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3630 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
3631 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
3632
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3633 // 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
3634 // 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
3635 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
3636 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3637 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
3638 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
3639 }
7a19c5678f91 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 }
7a19c5678f91 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 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
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 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
3644 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
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 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
3647 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
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 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
3650 {
7a19c5678f91 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 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
3652 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
3653 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
3654 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
3655 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
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 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
3658 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
3659 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
3660 }
7a19c5678f91 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 }
7a19c5678f91 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 if (invalid)
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3663 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
3664 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
3665
7a19c5678f91 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 // 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
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 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
3669 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3670 if (args(i+1).is_numeric_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
3671 collect_output = args(i+1).bool_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
3672 else
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3673 error ("%s: CollectOutput must be logical or numeric",
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3674 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
3675 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3676 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
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 if (args(i+1).is_numeric_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
3679 empty_value = args(i+1).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
3680 else
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3681 error ("%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
3682 }
7a19c5678f91 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 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
3684 {
7a19c5678f91 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 if (args(i+1).is_numeric_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
3686 header_lines = args(i+1).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
3687 else
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3688 error ("%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
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 == "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
3691 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3692 if (args(i+1).is_numeric_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
3693 buffer_size = args(i+1).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
3694 else
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3695 error ("%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
3696 }
7a19c5678f91 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 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
3698 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3699 if (args(i+1).is_numeric_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
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 if (args(i+1).bool_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
3702 multiple_delims_as_one = 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
3703 }
7a19c5678f91 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 else
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3705 error ("%s: MultipleDimsAsOne must be logical or numeric",
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3706 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
3707 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3708 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
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 if (args(i+1).is_numeric_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
3711 return_on_error = args(i+1).bool_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
3712 else 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
3713 && args(i+1).string_value () == "continue")
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3714 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
3715 else
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3716 error ("%s: ReturnOnError must be logical or numeric, or 'continue'",
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3717 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
3718 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3719 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
3720 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3721 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
3722 whitespace = args(i+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
3723 else
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3724 error ("%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
3725 }
7a19c5678f91 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 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
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 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
3729 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3730 exp_chars = args(i+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
3731 default_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
3732 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3733 else
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3734 error ("%s: ExpChars 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
3735 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3736 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
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 bool 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
3739 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
3740 {
7a19c5678f91 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 std::string s = args(i+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
3742 if (args(i+1).is_sq_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
3743 s = do_string_escapes (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
3744 int l = 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
3745 if (l == 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
3746 eol1 = eol2 = -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
3747 else if (l == 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
3748 eol1 = eol2 = s.c_str ()[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
3749 else if (l == 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
3750 {
7a19c5678f91 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 eol1 = s.c_str ()[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
3752 eol2 = s.c_str ()[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
3753 if (eol1 != '\r' || eol2 != '\n') // Why limit 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
3754 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
3755 }
7a19c5678f91 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 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
3757 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
3758 }
7a19c5678f91 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 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
3760 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
3761 if (! valid)
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3762 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
3763 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
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 else
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
3766 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
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
7a19c5678f91 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 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
3770 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
3771 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
3772
7a19c5678f91 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 // 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
3774 // 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
3775 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
3776 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
3777
7a19c5678f91 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 // 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
3779 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
3780 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
3781 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
3782 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
3783 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
3784 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
3785 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
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 (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
3788 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
3789 }
7a19c5678f91 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 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
3791 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
3792 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
3793 }
7a19c5678f91 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
7a19c5678f91 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 // 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
3796 // 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
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 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
3799 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
3800 {
7a19c5678f91 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 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
3802 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
3803
7a19c5678f91 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 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
3805 {
7a19c5678f91 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 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
3807 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
3808 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
3809 && ( ( (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
3810 || 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
3811 {
7a19c5678f91 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 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
3813 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
3814 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
3815 }
7a19c5678f91 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
7a19c5678f91 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 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
3818 {
7a19c5678f91 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 // 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
3820 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
3821 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
3822
7a19c5678f91 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 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
3824 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
3825 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
3826 .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
3827 {
7a19c5678f91 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 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
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 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
3831 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
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 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
3834 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
3835 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
3836
7a19c5678f91 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 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
3838 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
3839 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
3840 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
3841 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
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 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
3844 {
7a19c5678f91 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 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
3846 // 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
3847 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
3848 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
3849 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
3850 { // 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
3851 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
3852 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
3853
7a19c5678f91 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 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
3855 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
3856 {
7a19c5678f91 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 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
3858 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
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 }
7a19c5678f91 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 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
3862 }
7a19c5678f91 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 }
7a19c5678f91 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 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
3865 {
7a19c5678f91 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.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
3867 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
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 }
7a19c5678f91 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 }
7a19c5678f91 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 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
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 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
3874 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
3875 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
3876 }
7a19c5678f91 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 // 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
3879 // 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
3880 // 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
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 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
3883 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
3884 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
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 // 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
3887 // 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
3888 // 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
3889
7a19c5678f91 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 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
3891
7a19c5678f91 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 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
3893 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
3894
7a19c5678f91 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 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
3896 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
3897 // 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
3898
7a19c5678f91 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 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
3900 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
3901 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
3902
7a19c5678f91 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 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
3904 {
7a19c5678f91 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 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
3906 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
3907 {
7a19c5678f91 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 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
3909 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
3910 }
7a19c5678f91 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
7a19c5678f91 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 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
3914 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
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 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
3917 }
7a19c5678f91 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 // 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
3920 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
3921 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
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 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
3924 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
3925 {
7a19c5678f91 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 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
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 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
3929 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
3930 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
3931
7a19c5678f91 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 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
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 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
3935 // 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
3936 // 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
3937 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
3938 != 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
3939 && (((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
3940 || 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
3941 {
7a19c5678f91 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 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
3943 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
3944 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
3945 }
7a19c5678f91 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 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
3947 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
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 }
7a19c5678f91 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 }
7a19c5678f91 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 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
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 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
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 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
3956 || (-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
3957 {
7a19c5678f91 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 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
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 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
3961 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
3962 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
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 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
3965 {
7a19c5678f91 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 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
3967 }
7a19c5678f91 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
7a19c5678f91 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 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
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 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
3972 // 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
3973 // 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
3974 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
3975 != 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
3976 && (((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
3977 || -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
3978 {
7a19c5678f91 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 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
3980 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
3981 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
3982 }
7a19c5678f91 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 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3985 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3986
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3987 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
3988 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3989
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3990 // 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
3991 // 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
3992 // 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
3993
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3994 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
3995 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
3996 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
3997 // "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
3998 // 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
3999 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
4000
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
4001 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
4002 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
4003 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
4004 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
4005 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
4006 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
4007 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
4008 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
4009 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
4010 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
4011 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
4012 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
4013 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21477
diff changeset
4014
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4015 void
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
4016 octave_base_stream::error (const std::string& msg)
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 fail = true;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4019 errmsg = msg;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4020 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4021
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4022 void
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
4023 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
4024 {
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
4025 fail = true;
6296
85eb75190e01 [project @ 2007-02-10 07:10:42 by jwe]
jwe
parents: 6109
diff changeset
4026 errmsg = who + ": " + msg;
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
4027 }
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
4028
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
4029 void
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4030 octave_base_stream::clear (void)
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4031 {
4889
658aad1c1b05 [project @ 2004-04-30 22:38:30 by jwe]
jwe
parents: 4888
diff changeset
4032 fail = false;
658aad1c1b05 [project @ 2004-04-30 22:38:30 by jwe]
jwe
parents: 4888
diff changeset
4033 errmsg = "";
658aad1c1b05 [project @ 2004-04-30 22:38:30 by jwe]
jwe
parents: 4888
diff changeset
4034 }
658aad1c1b05 [project @ 2004-04-30 22:38:30 by jwe]
jwe
parents: 4888
diff changeset
4035
658aad1c1b05 [project @ 2004-04-30 22:38:30 by jwe]
jwe
parents: 4888
diff changeset
4036 void
658aad1c1b05 [project @ 2004-04-30 22:38:30 by jwe]
jwe
parents: 4888
diff changeset
4037 octave_base_stream::clearerr (void)
658aad1c1b05 [project @ 2004-04-30 22:38:30 by jwe]
jwe
parents: 4888
diff changeset
4038 {
4888
4a796c5fc7a3 [project @ 2004-04-30 18:27:07 by jwe]
jwe
parents: 4874
diff changeset
4039 std::istream *is = input_stream ();
4a796c5fc7a3 [project @ 2004-04-30 18:27:07 by jwe]
jwe
parents: 4874
diff changeset
4040 std::ostream *os = output_stream ();
4a796c5fc7a3 [project @ 2004-04-30 18:27:07 by jwe]
jwe
parents: 4874
diff changeset
4041
4a796c5fc7a3 [project @ 2004-04-30 18:27:07 by jwe]
jwe
parents: 4874
diff changeset
4042 if (is)
4a796c5fc7a3 [project @ 2004-04-30 18:27:07 by jwe]
jwe
parents: 4874
diff changeset
4043 is->clear ();
4a796c5fc7a3 [project @ 2004-04-30 18:27:07 by jwe]
jwe
parents: 4874
diff changeset
4044
4a796c5fc7a3 [project @ 2004-04-30 18:27:07 by jwe]
jwe
parents: 4874
diff changeset
4045 if (os)
4a796c5fc7a3 [project @ 2004-04-30 18:27:07 by jwe]
jwe
parents: 4874
diff changeset
4046 os->clear ();
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4047 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4048
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4049 // Functions that are defined for all input streams (input streams
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4050 // are those that define is).
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4051
3536
e8fbc8c3d6d9 [project @ 2000-02-02 12:01:00 by jwe]
jwe
parents: 3535
diff changeset
4052 std::string
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5266
diff changeset
4053 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
4054 bool strip_newline, const std::string& who)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4055 {
19849
1c9ed5b4c73d input.h: change meaning of interactive and forced_interactive global variables.
Carnë Draug <carandraug@octave.org>
parents: 19739
diff changeset
4056 if (interactive && file_number () == 0)
20831
35241c4b696c eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents: 20768
diff changeset
4057 ::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
4058 who.c_str ());
8773
9e3111d203c0 disallow reading from stdin while running interactively
John W. Eaton <jwe@octave.org>
parents: 8739
diff changeset
4059
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
4060 std::string retval;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4061
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4062 err = false;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4063
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
4064 std::istream *isp = input_stream ();
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4065
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4066 if (! isp)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4067 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4068 err = true;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4069 invalid_operation (who, "reading");
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4070 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4071 else
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4072 {
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
4073 std::istream& is = *isp;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4074
5765
7ba9ad1fec11 [project @ 2006-04-17 05:05:15 by jwe]
jwe
parents: 5760
diff changeset
4075 std::ostringstream buf;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4076
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4077 int c = 0;
3553
c5600b44bef9 [project @ 2000-02-03 03:22:37 by jwe]
jwe
parents: 3552
diff changeset
4078 int char_count = 0;
6345
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
4079
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
4080 if (max_len != 0)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4081 {
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4082 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
4083 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4084 char_count++;
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 // Handle CRLF, CR, or LF as line ending.
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4087 if (c == '\r')
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 (! strip_newline)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4090 buf << static_cast<char> (c);
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 c = is.get ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4093
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4094 if (c != std::istream::traits_type::eof ())
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4095 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4096 if (c == '\n')
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4097 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4098 char_count++;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4099
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4100 if (! strip_newline)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4101 buf << static_cast<char> (c);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4102 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4103 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4104 is.putback (c);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4105 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4106
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4107 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4108 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4109 else if (c == '\n')
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4110 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4111 if (! strip_newline)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4112 buf << static_cast<char> (c);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4113
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4114 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4115 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4116 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4117 buf << static_cast<char> (c);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4118
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4119 if (max_len > 0 && char_count == max_len)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4120 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4121 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4122 }
6345
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
4123
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
4124 if (! is.eof () && char_count > 0)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4125 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4126 // 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
4127 // 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
4128 // corresponding C-library functions do.
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4129 int disgusting_compatibility_hack = is.get ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4130 if (! is.eof ())
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4131 is.putback (disgusting_compatibility_hack);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4132 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4133
4224
0179e6309248 [project @ 2002-12-18 00:34:29 by jwe]
jwe
parents: 4223
diff changeset
4134 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
4135 retval = buf.str ();
4224
0179e6309248 [project @ 2002-12-18 00:34:29 by jwe]
jwe
parents: 4223
diff changeset
4136 else
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4137 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4138 err = true;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4139
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4140 if (is.eof () && char_count == 0)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4141 error (who, "at end of file");
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4142 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4143 error (who, "read error");
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4144 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4145 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4146
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4147 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4148 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4149
3536
e8fbc8c3d6d9 [project @ 2000-02-02 12:01:00 by jwe]
jwe
parents: 3535
diff changeset
4150 std::string
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
4151 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
4152 const std::string& who)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4153 {
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
4154 return do_gets (max_len, err, true, who);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4155 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4156
3536
e8fbc8c3d6d9 [project @ 2000-02-02 12:01:00 by jwe]
jwe
parents: 3535
diff changeset
4157 std::string
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
4158 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
4159 const std::string& who)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4160 {
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
4161 return do_gets (max_len, err, false, who);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4162 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4163
16011
8122286c69a9 initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents: 15467
diff changeset
4164 off_t
8122286c69a9 initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents: 15467
diff changeset
4165 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
4166 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4167 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
4168 ::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
4169 who.c_str ());
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4170
16011
8122286c69a9 initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents: 15467
diff changeset
4171 off_t cnt = -1;
9701
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4172
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4173 err = false;
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4174
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4175 std::istream *isp = input_stream ();
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4176
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4177 if (! isp)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4178 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4179 err = true;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4180 invalid_operation (who, "reading");
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4181 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4182 else
9701
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4183 {
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4184 std::istream& is = *isp;
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4185
18100
6a71e5030df5 Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents: 17787
diff changeset
4186 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
4187 int lastc = -1;
9701
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4188 cnt = 0;
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4189
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4190 while (is && (c = is.get ()) != std::istream::traits_type::eof ())
9701
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4191 {
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4192 // Handle CRLF, CR, or LF as line ending.
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4193 if (c == '\r' || (c == '\n' && lastc != '\r'))
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4194 {
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4195 if (++cnt == num)
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4196 break;
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4197 }
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4198
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4199 lastc = c;
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4200 }
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4201
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4202 // Maybe eat the following \n if \r was just met.
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4203 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
4204 is.get ();
9701
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4205
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4206 if (is.bad ())
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4207 {
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4208 err = true;
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4209 error (who, "read error");
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4210 }
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4211
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4212 if (err)
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4213 cnt = -1;
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4214 }
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4215
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4216 return cnt;
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4217 }
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
4218
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21129
diff changeset
4219 template <typename T>
3636
0aeae5fe4ab7 [project @ 2000-03-24 04:27:23 by jwe]
jwe
parents: 3635
diff changeset
4220 std::istream&
6767
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4221 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
4222 {
3779
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4223 T& ref = *valptr;
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4224
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4225 switch (fmt.type)
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4226 {
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4227 case 'o':
4926
e80587ad9503 [project @ 2004-08-02 23:48:40 by jwe]
jwe
parents: 4889
diff changeset
4228 is >> std::oct >> ref >> std::dec;
3779
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4229 break;
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4230
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4231 case 'x':
4926
e80587ad9503 [project @ 2004-08-02 23:48:40 by jwe]
jwe
parents: 4889
diff changeset
4232 is >> std::hex >> ref >> std::dec;
e80587ad9503 [project @ 2004-08-02 23:48:40 by jwe]
jwe
parents: 4889
diff changeset
4233 break;
e80587ad9503 [project @ 2004-08-02 23:48:40 by jwe]
jwe
parents: 4889
diff changeset
4234
e80587ad9503 [project @ 2004-08-02 23:48:40 by jwe]
jwe
parents: 4889
diff changeset
4235 case 'i':
e80587ad9503 [project @ 2004-08-02 23:48:40 by jwe]
jwe
parents: 4889
diff changeset
4236 {
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4237 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
4238
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4239 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
4240 && isspace (c1))
20946
6eff66fb8a02 style fixes for comments
John W. Eaton <jwe@octave.org>
parents: 20940
diff changeset
4241 ; // skip whitespace
14675
757f729fd41d skip leading whitespace for scanf %i format
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
4242
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4243 if (c1 != std::istream::traits_type::eof ())
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4244 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4245 if (c1 == '0')
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4246 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4247 int c2 = is.peek ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4248
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4249 if (c2 == 'x' || c2 == 'X')
7709
fa41af732801 octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents: 7538
diff changeset
4250 {
fa41af732801 octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents: 7538
diff changeset
4251 is.ignore ();
fa41af732801 octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents: 7538
diff changeset
4252 if (std::isxdigit (is.peek ()))
fa41af732801 octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents: 7538
diff changeset
4253 is >> std::hex >> ref >> std::dec;
fa41af732801 octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents: 7538
diff changeset
4254 else
fa41af732801 octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents: 7538
diff changeset
4255 ref = 0;
fa41af732801 octave_scan_1: fix reading of hex numbers
Jaroslav Hajek <highegg@gmail.com>
parents: 7538
diff changeset
4256 }
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4257 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4258 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4259 if (c2 == '0' || c2 == '1' || c2 == '2'
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4260 || c2 == '3' || c2 == '4' || c2 == '5'
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4261 || c2 == '6' || c2 == '7')
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4262 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
4263 else if (c2 == '8' || c2 == '9')
47908b818865 oct-stream.cc: Add FIXME note about bug #46493.
Rik <rik@octave.org>
parents: 21102
diff changeset
4264 {
47908b818865 oct-stream.cc: Add FIXME note about bug #46493.
Rik <rik@octave.org>
parents: 21102
diff changeset
4265 // 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
4266 // 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
4267 // 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
4268 ref = 0;
47908b818865 oct-stream.cc: Add FIXME note about bug #46493.
Rik <rik@octave.org>
parents: 21102
diff changeset
4269 }
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4270 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4271 ref = 0;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4272 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4273 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4274 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4275 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4276 is.putback (c1);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4277
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4278 is >> ref;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4279 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4280 }
4926
e80587ad9503 [project @ 2004-08-02 23:48:40 by jwe]
jwe
parents: 4889
diff changeset
4281 }
3779
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4282 break;
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4283
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4284 default:
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4285 is >> ref;
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4286 break;
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4287 }
3639
fed1847dfd6c [project @ 2000-03-24 05:12:46 by jwe]
jwe
parents: 3638
diff changeset
4288
3638
a76fa215a476 [project @ 2000-03-24 04:53:45 by jwe]
jwe
parents: 3637
diff changeset
4289 return is;
3636
0aeae5fe4ab7 [project @ 2000-03-24 04:27:23 by jwe]
jwe
parents: 3635
diff changeset
4290 }
0aeae5fe4ab7 [project @ 2000-03-24 04:27:23 by jwe]
jwe
parents: 3635
diff changeset
4291
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21129
diff changeset
4292 template <typename T>
6767
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4293 std::istream&
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4294 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
4295 {
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4296 if (fmt.width)
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4297 {
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4298 // 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
4299 // temporary stringstream buffer.
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4300 std::string tmp;
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4301
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4302 is.width (fmt.width);
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4303 is >> tmp;
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4304
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4305 std::istringstream ss (tmp);
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4306
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4307 octave_scan_1 (ss, fmt, valptr);
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4308 }
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4309 else
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4310 octave_scan_1 (is, fmt, valptr);
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4311
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4312 return is;
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4313 }
a6c8000f113e [project @ 2007-06-28 19:42:42 by jwe]
jwe
parents: 6757
diff changeset
4314
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
4315 // 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
4316 // character strings. See BEGIN_S_CONVERSION for details.
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4317
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21129
diff changeset
4318 template <>
3779
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4319 std::istream&
4661
12b6fbd57436 [project @ 2003-11-25 05:41:35 by jwe]
jwe
parents: 4649
diff changeset
4320 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
4321 char* valptr)
3779
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4322 {
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4323 return is >> valptr;
3001e15555e9 [project @ 2001-02-07 04:47:51 by jwe]
jwe
parents: 3775
diff changeset
4324 }
3636
0aeae5fe4ab7 [project @ 2000-03-24 04:27:23 by jwe]
jwe
parents: 3635
diff changeset
4325
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21129
diff changeset
4326 template <>
5176
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4327 std::istream&
5403
7bdddf96f028 [project @ 2005-07-07 16:46:23 by jwe]
jwe
parents: 5389
diff changeset
4328 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
4329 {
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4330 double& ref = *valptr;
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4331
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4332 switch (fmt.type)
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4333 {
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4334 case 'e':
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4335 case 'f':
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4336 case 'g':
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4337 {
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4338 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
4339
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4340 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
4341 && isspace (c1))
20946
6eff66fb8a02 style fixes for comments
John W. Eaton <jwe@octave.org>
parents: 20940
diff changeset
4342 ; // skip whitespace
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4343
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4344 if (c1 != std::istream::traits_type::eof ())
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4345 {
12936
b74cb659e757 accept but discard sign when reading NA and NaN values
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
4346 is.putback (c1);
b74cb659e757 accept but discard sign when reading NA and NaN values
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
4347
b74cb659e757 accept but discard sign when reading NA and NaN values
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
4348 ref = octave_read_value<double> (is);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4349 }
5176
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4350 }
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4351 break;
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4352
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4353 default:
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4354 panic_impossible ();
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4355 break;
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4356 }
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4357
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4358 return is;
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4359 }
10172b519695 [project @ 2005-03-02 19:14:13 by jwe]
jwe
parents: 5065
diff changeset
4360
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21129
diff changeset
4361 template <typename T>
2572
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
4362 void
3636
0aeae5fe4ab7 [project @ 2000-03-24 04:27:23 by jwe]
jwe
parents: 3635
diff changeset
4363 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
4364 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
4365 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
4366 octave_idx_type max_size, bool discard)
2572
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
4367 {
21147
95feb42d7a97 * oct-stream.cc (OCTAVE_SCAN): Eliminate trivial macro.
John W. Eaton <jwe@octave.org>
parents: 21139
diff changeset
4368 octave_scan (is, fmt, valptr);
2572
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
4369
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4370 if (! is)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4371 return;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4372
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4373 if (idx == max_size && ! discard)
2572
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
4374 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4375 max_size *= 2;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4376
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4377 if (nr > 0)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4378 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
4379 else
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4380 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
4381
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4382 data = mval.fortran_vec ();
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4383 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4384
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4385 if (! discard)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4386 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4387 conversion_count++;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4388 data[idx++] = *(valptr);
2572
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
4389 }
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
4390 }
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
4391
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
4392 template void
3636
0aeae5fe4ab7 [project @ 2000-03-24 04:27:23 by jwe]
jwe
parents: 3635
diff changeset
4393 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
4394 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
4395 octave_idx_type, octave_idx_type, bool);
2572
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
4396
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4397 #define DO_WHITESPACE_CONVERSION() \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4398 do \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4399 { \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4400 int c = std::istream::traits_type::eof (); \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4401 \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4402 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
4403 && isspace (c)) \
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4404 { /* skip whitespace */ } \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4405 \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4406 if (c != std::istream::traits_type::eof ()) \
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4407 is.putback (c); \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4408 } \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4409 while (0)
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4410
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4411 #define DO_LITERAL_CONVERSION() \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4412 do \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4413 { \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4414 int c = std::istream::traits_type::eof (); \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4415 \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4416 int n = strlen (fmt); \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4417 int i = 0; \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4418 \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4419 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
4420 { \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4421 if (c == static_cast<unsigned char> (fmt[i])) \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4422 { \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4423 i++; \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4424 continue; \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4425 } \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4426 else \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4427 { \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4428 is.putback (c); \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4429 break; \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4430 } \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4431 } \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4432 \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4433 if (i != n) \
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4434 is.setstate (std::ios::failbit); \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4435 } \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4436 while (0)
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4437
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
4438 #define DO_PCT_CONVERSION() \
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
4439 do \
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
4440 { \
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
4441 int c = is.get (); \
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
4442 \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4443 if (c != std::istream::traits_type::eof ()) \
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4444 { \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4445 if (c != '%') \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4446 { \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4447 is.putback (c); \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4448 is.setstate (std::ios::failbit); \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4449 } \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4450 } \
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
4451 else \
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4452 is.setstate (std::ios::failbit); \
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
4453 } \
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
4454 while (0)
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
4455
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4456 #define BEGIN_C_CONVERSION() \
3538
0ff7323dab8b [project @ 2000-02-02 12:36:25 by jwe]
jwe
parents: 3536
diff changeset
4457 is.unsetf (std::ios::skipws); \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4458 \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4459 int width = elt->width ? elt->width : 1; \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4460 \
9952
7cd2e1b372e5 allow scanf to store ASCII NUL values
John W. Eaton <jwe@octave.org>
parents: 9941
diff changeset
4461 std::string tmp (width, '\0'); \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4462 \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4463 int c = std::istream::traits_type::eof (); \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4464 int n = 0; \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4465 \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4466 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
4467 tmp[n++] = static_cast<char> (c); \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4468 \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4469 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
4470 is.clear (); \
7626f8934466 correctly resize scanf output for %c formats (bug #34037)
John W. Eaton <jwe@octave.org>
parents: 12966
diff changeset
4471 \
7626f8934466 correctly resize scanf output for %c formats (bug #34037)
John W. Eaton <jwe@octave.org>
parents: 12966
diff changeset
4472 tmp.resize (n)
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4473
15466
d174210ce1ec use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents: 14138
diff changeset
4474 // 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
4475 // 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
4476 #define BEGIN_S_CONVERSION() \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4477 int width = elt->width; \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4478 \
4051
b79da8779a0e [project @ 2002-08-17 19:38:32 by jwe]
jwe
parents: 3969
diff changeset
4479 std::string tmp; \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4480 \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4481 do \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4482 { \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4483 if (width) \
5338
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4484 { \
10076
4b270d1540f7 avoid various GCC warnings
John W. Eaton <jwe@octave.org>
parents: 9952
diff changeset
4485 tmp = std::string (width, '\0'); \
5338
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4486 \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4487 int c = std::istream::traits_type::eof (); \
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 int n = 0; \
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4490 \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4491 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
4492 { \
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4493 if (! isspace (c)) \
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4494 { \
9952
7cd2e1b372e5 allow scanf to store ASCII NUL values
John W. Eaton <jwe@octave.org>
parents: 9941
diff changeset
4495 tmp[n++] = static_cast<char> (c); \
5338
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4496 break; \
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4497 } \
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4498 } \
4051
b79da8779a0e [project @ 2002-08-17 19:38:32 by jwe]
jwe
parents: 3969
diff changeset
4499 \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4500 while (is && n < width \
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4501 && (c = is.get ()) != std::istream::traits_type::eof ()) \
5338
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4502 { \
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4503 if (isspace (c)) \
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4504 { \
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4505 is.putback (c); \
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4506 break; \
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4507 } \
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4508 else \
9952
7cd2e1b372e5 allow scanf to store ASCII NUL values
John W. Eaton <jwe@octave.org>
parents: 9941
diff changeset
4509 tmp[n++] = static_cast<char> (c); \
5338
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4510 } \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4511 \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4512 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
4513 is.clear (); \
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4514 \
9952
7cd2e1b372e5 allow scanf to store ASCII NUL values
John W. Eaton <jwe@octave.org>
parents: 9941
diff changeset
4515 tmp.resize (n); \
5338
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4516 } \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4517 else \
5338
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4518 { \
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4519 is >> std::ws >> tmp; \
c4b55d47122e [project @ 2005-05-05 17:28:51 by jwe]
jwe
parents: 5326
diff changeset
4520 } \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4521 } \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4522 while (0)
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4523
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4524 // This format must match a nonempty sequence of characters.
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4525 #define BEGIN_CHAR_CLASS_CONVERSION() \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4526 int width = elt->width; \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4527 \
4051
b79da8779a0e [project @ 2002-08-17 19:38:32 by jwe]
jwe
parents: 3969
diff changeset
4528 std::string tmp; \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4529 \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4530 do \
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 if (! width) \
15215
9020dddc925a use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents: 15195
diff changeset
4533 width = std::numeric_limits<int>::max (); \
7427
65f0a8ced9d2 [project @ 2008-01-28 22:42:18 by jwe]
jwe
parents: 7426
diff changeset
4534 \
7426
b9df9abdffbb [project @ 2008-01-28 21:07:58 by jwe]
jwe
parents: 7352
diff changeset
4535 std::ostringstream buf; \
b9df9abdffbb [project @ 2008-01-28 21:07:58 by jwe]
jwe
parents: 7352
diff changeset
4536 \
b9df9abdffbb [project @ 2008-01-28 21:07:58 by jwe]
jwe
parents: 7352
diff changeset
4537 std::string char_class = elt->char_class; \
4051
b79da8779a0e [project @ 2002-08-17 19:38:32 by jwe]
jwe
parents: 3969
diff changeset
4538 \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4539 int c = std::istream::traits_type::eof (); \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4540 \
7426
b9df9abdffbb [project @ 2008-01-28 21:07:58 by jwe]
jwe
parents: 7352
diff changeset
4541 if (elt->type == '[') \
b9df9abdffbb [project @ 2008-01-28 21:07:58 by jwe]
jwe
parents: 7352
diff changeset
4542 { \
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4543 int chars_read = 0; \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4544 while (is && chars_read++ < width \
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4545 && (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
4546 && char_class.find (c) != std::string::npos) \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4547 buf << static_cast<char> (c); \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4548 } \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4549 else \
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4550 { \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4551 int chars_read = 0; \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4552 while (is && chars_read++ < width \
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4553 && (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
4554 && char_class.find (c) == std::string::npos) \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4555 buf << static_cast<char> (c); \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4556 } \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4557 \
21184
3db899f1d54c use istream::traits_type::eof () instead of EOF
John W. Eaton <jwe@octave.org>
parents: 21147
diff changeset
4558 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
4559 && c != std::istream::traits_type::eof ()) \
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4560 is.putback (c); \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4561 \
7426
b9df9abdffbb [project @ 2008-01-28 21:07:58 by jwe]
jwe
parents: 7352
diff changeset
4562 tmp = buf.str (); \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4563 \
7426
b9df9abdffbb [project @ 2008-01-28 21:07:58 by jwe]
jwe
parents: 7352
diff changeset
4564 if (tmp.empty ()) \
b9df9abdffbb [project @ 2008-01-28 21:07:58 by jwe]
jwe
parents: 7352
diff changeset
4565 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
4566 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
4567 is.clear (); \
da355a1a6d44 fix scanf char class conversion bug
John W. Eaton <jwe@octave.org>
parents: 10350
diff changeset
4568 \
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4569 } \
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4570 while (0)
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3410
diff changeset
4571
3410
ee5f222025c3 [project @ 2000-01-07 04:19:05 by jwe]
jwe
parents: 3342
diff changeset
4572 #define FINISH_CHARACTER_CONVERSION() \
ee5f222025c3 [project @ 2000-01-07 04:19:05 by jwe]
jwe
parents: 3342
diff changeset
4573 do \
ee5f222025c3 [project @ 2000-01-07 04:19:05 by jwe]
jwe
parents: 3342
diff changeset
4574 { \
4051
b79da8779a0e [project @ 2002-08-17 19:38:32 by jwe]
jwe
parents: 3969
diff changeset
4575 width = tmp.length (); \
3410
ee5f222025c3 [project @ 2000-01-07 04:19:05 by jwe]
jwe
parents: 3342
diff changeset
4576 \
ee5f222025c3 [project @ 2000-01-07 04:19:05 by jwe]
jwe
parents: 3342
diff changeset
4577 if (is) \
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4578 { \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4579 int i = 0; \
3410
ee5f222025c3 [project @ 2000-01-07 04:19:05 by jwe]
jwe
parents: 3342
diff changeset
4580 \
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4581 if (! discard) \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4582 { \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4583 conversion_count++; \
3410
ee5f222025c3 [project @ 2000-01-07 04:19:05 by jwe]
jwe
parents: 3342
diff changeset
4584 \
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4585 while (i < width) \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4586 { \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4587 if (data_index == max_size) \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4588 { \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4589 max_size *= 2; \
3410
ee5f222025c3 [project @ 2000-01-07 04:19:05 by jwe]
jwe
parents: 3342
diff changeset
4590 \
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4591 if (all_char_conv) \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4592 { \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4593 if (one_elt_size_spec) \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4594 mval.resize (1, max_size, 0.0); \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4595 else if (nr > 0) \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4596 mval.resize (nr, max_size / nr, 0.0); \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4597 else \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4598 panic_impossible (); \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4599 } \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4600 else if (nr > 0) \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4601 mval.resize (nr, max_size / nr, 0.0); \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4602 else \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4603 mval.resize (max_size, 1, 0.0); \
3410
ee5f222025c3 [project @ 2000-01-07 04:19:05 by jwe]
jwe
parents: 3342
diff changeset
4604 \
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4605 data = mval.fortran_vec (); \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4606 } \
3410
ee5f222025c3 [project @ 2000-01-07 04:19:05 by jwe]
jwe
parents: 3342
diff changeset
4607 \
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4608 data[data_index++] = tmp[i++]; \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4609 } \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4610 } \
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4611 } \
3410
ee5f222025c3 [project @ 2000-01-07 04:19:05 by jwe]
jwe
parents: 3342
diff changeset
4612 } \
ee5f222025c3 [project @ 2000-01-07 04:19:05 by jwe]
jwe
parents: 3342
diff changeset
4613 while (0)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4614
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4615 octave_value
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4616 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
4617 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
4618 bool one_elt_size_spec,
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
4619 octave_idx_type& conversion_count,
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
4620 const std::string& who)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4621 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4622 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
4623 ::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
4624 who.c_str ());
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4625
8773
9e3111d203c0 disallow reading from stdin while running interactively
John W. Eaton <jwe@octave.org>
parents: 8739
diff changeset
4626 octave_value retval = Matrix ();
9e3111d203c0 disallow reading from stdin while running interactively
John W. Eaton <jwe@octave.org>
parents: 8739
diff changeset
4627
3268
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
4628 conversion_count = 0;
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
4629
10187
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4630 octave_idx_type nconv = fmt_list.num_conversions ();
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
4631
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5266
diff changeset
4632 octave_idx_type data_index = 0;
2121
bc6ecd8f1175 [project @ 1996-05-12 06:45:55 by jwe]
jwe
parents: 2117
diff changeset
4633
3268
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
4634 if (nr == 0 || nc == 0)
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
4635 {
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
4636 if (one_elt_size_spec)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4637 nc = 0;
3268
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
4638
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
4639 return Matrix (nr, nc, 0.0);
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
4640 }
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
4641
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
4642 std::istream *isp = input_stream ();
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4643
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4644 bool all_char_conv = fmt_list.all_character_conversions ();
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4645
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4646 Matrix mval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4647 double *data = 0;
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5266
diff changeset
4648 octave_idx_type max_size = 0;
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5266
diff changeset
4649 octave_idx_type max_conv = 0;
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5266
diff changeset
4650
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5266
diff changeset
4651 octave_idx_type final_nr = 0;
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5266
diff changeset
4652 octave_idx_type final_nc = 0;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4653
3268
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
4654 if (all_char_conv)
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
4655 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4656 // 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
4657 // 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
4658 if (one_elt_size_spec)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4659 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4660 max_size = 512;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4661 mval.resize (1, max_size, 0.0);
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 if (nr > 0)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4664 max_conv = nr;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4665 }
4420
3dfdbcaf4582 [project @ 2003-05-28 18:24:42 by jwe]
jwe
parents: 4326
diff changeset
4666 else if (nr > 0)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4667 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4668 if (nc > 0)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4669 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4670 mval.resize (nr, nc, 0.0);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4671 max_size = max_conv = nr * nc;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4672 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4673 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4674 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4675 mval.resize (nr, 32, 0.0);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4676 max_size = nr * 32;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4677 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4678 }
4420
3dfdbcaf4582 [project @ 2003-05-28 18:24:42 by jwe]
jwe
parents: 4326
diff changeset
4679 else
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4680 panic_impossible ();
3268
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
4681 }
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
4682 else if (nr > 0)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4683 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4684 if (nc > 0)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4685 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4686 // Will not resize later.
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4687 mval.resize (nr, nc, 0.0);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4688 max_size = nr * nc;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4689 max_conv = max_size;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4690 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4691 else
10315
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 // Maybe resize later.
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4694 mval.resize (nr, 32, 0.0);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4695 max_size = nr * 32;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4696 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4697 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4698 else
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4699 {
4420
3dfdbcaf4582 [project @ 2003-05-28 18:24:42 by jwe]
jwe
parents: 4326
diff changeset
4700 // Maybe resize later.
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4701 mval.resize (32, 1, 0.0);
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4702 max_size = 32;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4703 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4704
4420
3dfdbcaf4582 [project @ 2003-05-28 18:24:42 by jwe]
jwe
parents: 4326
diff changeset
4705 data = mval.fortran_vec ();
3dfdbcaf4582 [project @ 2003-05-28 18:24:42 by jwe]
jwe
parents: 4326
diff changeset
4706
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4707 if (isp)
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4708 {
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
4709 std::istream& is = *isp;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4710
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4711 const scanf_format_elt *elt = fmt_list.first ();
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4712
3538
0ff7323dab8b [project @ 2000-02-02 12:36:25 by jwe]
jwe
parents: 3536
diff changeset
4713 std::ios::fmtflags flags = is.flags ();
2213
c4a887694274 [project @ 1996-05-16 04:13:28 by jwe]
jwe
parents: 2127
diff changeset
4714
10187
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4715 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
4716
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4717 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
4718
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4719 for (;;)
10315
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 octave_quit ();
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 (elt)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4724 {
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
4725 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
4726 || (! (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
4727 || 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
4728 || 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
4729 && max_conv > 0 && conversion_count == max_conv))
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4730 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4731 // 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
4732 // 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
4733 // 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
4734 // 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
4735 // Determine final array size and exit.
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4736 if (all_char_conv && one_elt_size_spec)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4737 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4738 final_nr = 1;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4739 final_nc = data_index;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4740 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4741 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4742 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4743 final_nr = nr;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4744 final_nc = (data_index - 1) / nr + 1;
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
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4747 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4748 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4749 else if (data_index == max_size)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4750 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4751 max_size *= 2;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4752
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4753 if (all_char_conv)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4754 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4755 if (one_elt_size_spec)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4756 mval.resize (1, max_size, 0.0);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4757 else if (nr > 0)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4758 mval.resize (nr, max_size / nr, 0.0);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4759 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4760 panic_impossible ();
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 else if (nr > 0)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4763 mval.resize (nr, max_size / nr, 0.0);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4764 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4765 mval.resize (max_size, 1, 0.0);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4766
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4767 data = mval.fortran_vec ();
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
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4770 const char *fmt = elt->text;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4771
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4772 bool discard = elt->discard;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4773
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4774 switch (elt->type)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4775 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4776 case scanf_format_elt::whitespace_conversion:
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4777 DO_WHITESPACE_CONVERSION ();
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 case scanf_format_elt::literal_conversion:
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4781 DO_LITERAL_CONVERSION ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4782 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4783
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4784 case '%':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4785 DO_PCT_CONVERSION ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4786 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4787
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4788 case 'd': case 'i':
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 switch (elt->modifier)
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 'h':
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 short int tmp;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4795 do_scanf_conv (is, *elt, &tmp, mval, data,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4796 data_index, conversion_count,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4797 nr, max_size, discard);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4798 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4799 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4800
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4801 case 'l':
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 long int tmp;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4804 do_scanf_conv (is, *elt, &tmp, mval, data,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4805 data_index, conversion_count,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4806 nr, max_size, discard);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4807 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4808 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4809
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4810 default:
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 int tmp;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4813 do_scanf_conv (is, *elt, &tmp, mval, data,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4814 data_index, conversion_count,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4815 nr, max_size, discard);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4816 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4817 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4818 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4819 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4820 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4821
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4822 case 'o': case 'u': case 'x':
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 switch (elt->modifier)
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 'h':
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 unsigned short int tmp;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4829 do_scanf_conv (is, *elt, &tmp, mval, data,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4830 data_index, conversion_count,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4831 nr, max_size, discard);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4832 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4833 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4834
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4835 case 'l':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4836 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4837 unsigned long int tmp;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4838 do_scanf_conv (is, *elt, &tmp, mval, data,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4839 data_index, conversion_count,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4840 nr, max_size, discard);
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 break;
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 default:
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 unsigned int tmp;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4847 do_scanf_conv (is, *elt, &tmp, mval, data,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4848 data_index, conversion_count,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4849 nr, max_size, discard);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4850 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4851 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4852 }
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 break;
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 case 'e': case 'f': case 'g':
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 double tmp;
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 do_scanf_conv (is, *elt, &tmp, mval, data,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4861 data_index, conversion_count,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4862 nr, max_size, discard);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4863 }
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 case 'c':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4867 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4868 BEGIN_C_CONVERSION ();
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 FINISH_CHARACTER_CONVERSION ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4871
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4872 is.setf (flags);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4873 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4874 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4875
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4876 case 's':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4877 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4878 BEGIN_S_CONVERSION ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4879
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4880 FINISH_CHARACTER_CONVERSION ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4881 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4882 break;
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 case '[': case '^':
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 BEGIN_CHAR_CLASS_CONVERSION ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4887
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4888 FINISH_CHARACTER_CONVERSION ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4889 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4890 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4891
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4892 case 'p':
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4893 error ("%s: unsupported format specifier", who.c_str ());
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4894 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4895
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4896 default:
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4897 error ("%s: internal format error", who.c_str ());
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4898 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4899 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4900
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4901 if (! ok ())
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4902 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4903 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4904 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4905 else if (! is)
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 if (all_char_conv)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4908 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4909 if (one_elt_size_spec)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4910 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4911 final_nr = 1;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4912 final_nc = data_index;
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 else if (data_index > nr)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4915 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4916 final_nr = nr;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4917 final_nc = (data_index - 1) / nr + 1;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4918 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4919 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4920 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4921 final_nr = data_index;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4922 final_nc = 1;
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 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4925 else if (nr > 0)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4926 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4927 if (data_index > nr)
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 final_nr = nr;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4930 final_nc = (data_index - 1) / nr + 1;
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 final_nr = data_index;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4935 final_nc = 1;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4936 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4937 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4938 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4939 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4940 final_nr = data_index;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4941 final_nc = 1;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4942 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4943
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4944 // 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
4945 // reset the failbit in the stream state.
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4946 if (is.rdstate () & std::ios::failbit)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4947 is.clear (is.rdstate () & (~std::ios::failbit));
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4948
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
4949 // 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
4950 if (interactive && ! forced_interactive && name () == "stdin")
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4951 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4952 is.clear ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4953
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4954 // Skip to end of line.
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4955 bool err;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4956 do_gets (-1, err, false, who);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4957 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4958
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4959 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4960 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4961 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4962 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4963 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4964 error ("%s: internal format error", who.c_str ());
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4965 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4966 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4967
10187
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4968 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
4969 {
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4970 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
4971 {
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4972 final_nr = 1;
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4973 final_nc = data_index;
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4974 }
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4975 else
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4976 {
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4977 final_nr = nr;
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4978 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
4979 }
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4980
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4981 break;
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4982 }
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
4983 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
4984 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
4985 // 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
4986 // 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
4987 // 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
4988 // 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
4989 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
4990 && (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
4991 || 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
4992 }
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
4993 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4994 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4995
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4996 if (ok ())
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4997 {
2121
bc6ecd8f1175 [project @ 1996-05-12 06:45:55 by jwe]
jwe
parents: 2117
diff changeset
4998 mval.resize (final_nr, final_nc, 0.0);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
4999
3268
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
5000 retval = mval;
fdc7dd08cd85 [project @ 1999-09-10 05:16:49 by jwe]
jwe
parents: 3267
diff changeset
5001
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5002 if (all_char_conv)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5003 retval = retval.convert_to_str (false, true);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5004 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5005
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5006 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5007 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5008
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5009 octave_value
3810
f19f00339363 [project @ 2001-03-29 21:03:01 by jwe]
jwe
parents: 3779
diff changeset
5010 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
5011 octave_idx_type& conversion_count,
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
5012 const std::string& who)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5013 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5014 octave_value retval = Matrix ();
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5015
3559
12d7ec415f35 [project @ 2000-02-03 05:17:36 by jwe]
jwe
parents: 3553
diff changeset
5016 conversion_count = 0;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5017
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
5018 std::istream *isp = input_stream ();
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5019
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5020 if (! isp)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5021 invalid_operation (who, "reading");
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5022 else
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5023 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5024 scanf_format_list fmt_list (fmt);
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5025
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
5026 if (fmt_list.num_conversions () == -1)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5027 ::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
5028
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5029 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
5030 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
5031
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5032 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
5033
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5034 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
5035
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5036 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
5037 conversion_count, who);
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
5038 }
2572
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5039
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5040 return retval;
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5041 }
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5042
2712
2ff2b536cc9d [project @ 1997-02-21 21:38:21 by jwe]
jwe
parents: 2687
diff changeset
5043 bool
2ff2b536cc9d [project @ 1997-02-21 21:38:21 by jwe]
jwe
parents: 2687
diff changeset
5044 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
5045 octave_value& retval, const std::string& who)
2572
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5046 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5047 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
5048
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5049 if (! isp)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5050 return false;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5051
2712
2ff2b536cc9d [project @ 1997-02-21 21:38:21 by jwe]
jwe
parents: 2687
diff changeset
5052 bool quit = false;
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
5053
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5054 std::istream& is = *isp;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5055
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5056 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
5057
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5058 if (elt)
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
5059 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5060 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
5061
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5062 bool discard = elt->discard;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5063
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5064 switch (elt->type)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5065 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5066 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
5067 DO_WHITESPACE_CONVERSION ();
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5068 break;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5069
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5070 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
5071 DO_LITERAL_CONVERSION ();
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5072 break;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5073
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5074 case '%':
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5075 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5076 DO_PCT_CONVERSION ();
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5077
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5078 if (! is)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5079 quit = true;
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 break;
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 case 'd': case 'i':
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5084 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5085 int tmp;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5086
21147
95feb42d7a97 * oct-stream.cc (OCTAVE_SCAN): Eliminate trivial macro.
John W. Eaton <jwe@octave.org>
parents: 21139
diff changeset
5087 if (octave_scan (is, *elt, &tmp))
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5088 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5089 if (! discard)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5090 retval = tmp;
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5091 }
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5092 else
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5093 quit = true;
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 break;
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 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
5098 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5099 long int tmp;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5100
21147
95feb42d7a97 * oct-stream.cc (OCTAVE_SCAN): Eliminate trivial macro.
John W. Eaton <jwe@octave.org>
parents: 21139
diff changeset
5101 if (octave_scan (is, *elt, &tmp))
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5102 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5103 if (! discard)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5104 retval = tmp;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5105 }
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5106 else
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5107 quit = true;
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 break;
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 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
5112 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5113 double tmp;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5114
21147
95feb42d7a97 * oct-stream.cc (OCTAVE_SCAN): Eliminate trivial macro.
John W. Eaton <jwe@octave.org>
parents: 21139
diff changeset
5115 if (octave_scan (is, *elt, &tmp))
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5116 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5117 if (! discard)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5118 retval = tmp;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5119 }
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5120 else
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5121 quit = true;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5122 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5123 break;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5124
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5125 case 'c':
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5126 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5127 BEGIN_C_CONVERSION ();
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 (! discard)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5130 retval = tmp;
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 if (! is)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5133 quit = true;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5134
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5135 is.setf (flags);
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5136 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5137 break;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5138
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5139 case 's':
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5140 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5141 BEGIN_S_CONVERSION ();
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 if (! discard)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5144 retval = tmp;
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 if (! is)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5147 quit = true;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5148 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5149 break;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5150
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5151 case '[':
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5152 case '^':
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5153 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5154 BEGIN_CHAR_CLASS_CONVERSION ();
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5155
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5156 if (! discard)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5157 retval = tmp;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5158
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5159 if (! is)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5160 quit = true;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5161 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5162 break;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5163
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5164 case 'p':
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5165 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
5166 break;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5167
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5168 default:
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5169 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
5170 break;
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5171 }
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5172 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5173
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5174 if (ok () && is.fail ())
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5175 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5176 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
5177
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5178 // 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
5179
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5180 if (interactive && ! forced_interactive && name () == "stdin")
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5181 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5182 // 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
5183 bool err;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5184 do_gets (-1, err, false, who);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5185 }
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
5186 }
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
5187
2712
2ff2b536cc9d [project @ 1997-02-21 21:38:21 by jwe]
jwe
parents: 2687
diff changeset
5188 return quit;
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
5189 }
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
5190
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
5191 octave_value_list
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
5192 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
5193 {
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
5194 octave_value_list retval;
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
5195
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
5196 std::istream *isp = input_stream ();
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
5197
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5198 if (! isp)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5199 invalid_operation (who, "reading");
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5200 else
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
5201 {
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
5202 std::istream& is = *isp;
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
5203
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
5204 scanf_format_list fmt_list (fmt);
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
5205
10187
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
5206 octave_idx_type nconv = fmt_list.num_conversions ();
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
5207
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
5208 if (nconv == -1)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5209 ::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
5210
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5211 is.clear ();
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 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
5214
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5215 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
5216
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5217 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
5218
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5219 int num_values = 0;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5220
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5221 bool quit = false;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5222
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5223 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
5224 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5225 octave_value tmp;
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 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
5228
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5229 if (quit)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5230 break;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5231 else
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5232 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5233 if (tmp.is_defined ())
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5234 retval(num_values++) = tmp;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5235
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5236 if (! ok ())
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5237 break;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5238
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5239 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
5240 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5241 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5242
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5243 retval(nconv) = num_values;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5244
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5245 int err_num;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5246 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
5247
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5248 if (! quit)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5249 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5250 // 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
5251 if (ok () && len > nconv)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5252 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5253 octave_value tmp;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5254
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5255 elt = fmt_list.next ();
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5256
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5257 do_oscanf (elt, tmp, who);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5258 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5259 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5260 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5261
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5262 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5263 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5264
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5265 octave_value
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5266 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
5267 octave_idx_type ntimes,
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5268 const octave_value_list& options,
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5269 const std::string& who,
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5270 octave_idx_type& read_count)
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5271 {
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5272 if (interactive && file_number () == 0)
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5273 ::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
5274 who.c_str ());
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5275
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5276 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
5277
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5278 std::istream *isp = input_stream ();
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5279
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5280 if (! isp)
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5281 invalid_operation (who, "reading");
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5282 else
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5283 {
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5284 textscan scanner (who);
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5285
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5286 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
5287 }
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5288
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5289 return retval;
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5290 }
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
5291
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5292 // 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
5293 // (output streams are those that define os).
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5294
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5295 int
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5296 octave_base_stream::flush (void)
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5297 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5298 int retval = -1;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5299
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
5300 std::ostream *os = output_stream ();
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5301
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5302 if (! os)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5303 invalid_operation ("fflush", "writing");
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5304 else
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 os->flush ();
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 if (os->good ())
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5309 retval = 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
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5312 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5313 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5314
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5315 class
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5316 printf_value_cache
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 public:
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5319
3653
84b2f30007d5 [project @ 2000-03-31 23:33:25 by jwe]
jwe
parents: 3645
diff changeset
5320 enum state { ok, conversion_error };
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5321
7352
fab9bc33b949 [project @ 2008-01-07 18:54:45 by jwe]
jwe
parents: 7227
diff changeset
5322 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
5323 : 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
5324 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
5325 curr_state (ok)
fab9bc33b949 [project @ 2008-01-07 18:54:45 by jwe]
jwe
parents: 7227
diff changeset
5326 {
fab9bc33b949 [project @ 2008-01-07 18:54:45 by jwe]
jwe
parents: 7227
diff changeset
5327 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
5328 {
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5329 octave_value val = values(i);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5330
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5331 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
5332 err_wrong_type_arg (who, val);
7352
fab9bc33b949 [project @ 2008-01-07 18:54:45 by jwe]
jwe
parents: 7227
diff changeset
5333 }
fab9bc33b949 [project @ 2008-01-07 18:54:45 by jwe]
jwe
parents: 7227
diff changeset
5334 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5335
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5336 ~printf_value_cache (void) { }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5337
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5338 // 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
5339 octave_value get_next_value (char type = 0);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5340
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5341 // 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
5342 int int_value (void);
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5343
3145
0d640dc625c7 [project @ 1998-02-05 08:44:59 by jwe]
jwe
parents: 3013
diff changeset
5344 operator bool () const { return (curr_state == ok); }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5345
3653
84b2f30007d5 [project @ 2000-03-31 23:33:25 by jwe]
jwe
parents: 3645
diff changeset
5346 bool exhausted (void) { return (val_idx >= n_vals); }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5347
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5348 private:
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5349
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5350 const octave_value_list values;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5351 int val_idx;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5352 int elt_idx;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5353 int n_vals;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5354 int n_elts;
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5355 bool have_data;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5356 octave_value curr_val;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5357 state curr_state;
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 // Must create value cache with values!
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5360
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5361 printf_value_cache (void);
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5362
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5363 // No copying!
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5364
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5365 printf_value_cache (const printf_value_cache&);
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5366
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5367 printf_value_cache& operator = (const printf_value_cache&);
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5368 };
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5369
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5370 octave_value
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5371 printf_value_cache::get_next_value (char type)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5372 {
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5373 octave_value retval;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5374
3711
60db0e500f10 [project @ 2000-08-03 05:09:39 by jwe]
jwe
parents: 3704
diff changeset
5375 if (exhausted ())
60db0e500f10 [project @ 2000-08-03 05:09:39 by jwe]
jwe
parents: 3704
diff changeset
5376 curr_state = conversion_error;
60db0e500f10 [project @ 2000-08-03 05:09:39 by jwe]
jwe
parents: 3704
diff changeset
5377
60db0e500f10 [project @ 2000-08-03 05:09:39 by jwe]
jwe
parents: 3704
diff changeset
5378 while (! exhausted ())
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5379 {
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5380 if (! have_data)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5381 {
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5382 curr_val = values (val_idx);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5383
20741
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
5384 elt_idx = 0;
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
5385 n_elts = curr_val.numel ();
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
5386 have_data = true;
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5387 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5388
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5389 if (elt_idx < n_elts)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5390 {
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5391 if (type == 's')
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 (curr_val.is_string ())
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5394 {
21296
596e19e7e571 fix printf regression (bug #47192)
John W. Eaton <jwe@octave.org>
parents: 21235
diff changeset
5395 dim_vector dv (1, curr_val.numel ());
596e19e7e571 fix printf regression (bug #47192)
John W. Eaton <jwe@octave.org>
parents: 21235
diff changeset
5396 octave_value tmp = curr_val.reshape (dv);
596e19e7e571 fix printf regression (bug #47192)
John W. Eaton <jwe@octave.org>
parents: 21235
diff changeset
5397
596e19e7e571 fix printf regression (bug #47192)
John W. Eaton <jwe@octave.org>
parents: 21235
diff changeset
5398 std::string sval = tmp.string_value ();
19729
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.substr (elt_idx);
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 // We've consumed the rest of the value.
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5403 elt_idx = n_elts;
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 else
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5406 {
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5407 // Convert to character string while values are
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5408 // integers in the range [0 : char max]
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5409 const NDArray val = curr_val.array_value ();
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5410
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5411 octave_idx_type idx = elt_idx;
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5412
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5413 for (; idx < n_elts; idx++)
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5414 {
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5415 double dval = val(idx);
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 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
5418 break;
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5419 }
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5420
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5421 octave_idx_type n = idx - elt_idx;
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5422
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5423 if (n > 0)
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5424 {
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5425 std::string sval (n, '\0');
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5426
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5427 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
5428 sval[i] = val(elt_idx++);
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5429
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5430 retval = sval;
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5431 }
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5432 else
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5433 retval = curr_val.fast_elem_extract (elt_idx++);
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 }
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5436 else
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5437 {
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5438 retval = curr_val.fast_elem_extract (elt_idx++);
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 if (type == 'c' && ! retval.is_string ())
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5441 {
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5442 double dval = retval.double_value ();
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5443
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5444 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
5445 retval = static_cast<char> (dval);
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5446 }
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5447 }
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5448
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5449 if (elt_idx >= n_elts)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5450 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5451 elt_idx = 0;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5452 val_idx++;
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5453 have_data = false;
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5454 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5455
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5456 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5457 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5458 else
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5459 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5460 val_idx++;
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5461 have_data = false;
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5462
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5463 if (n_elts == 0)
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5464 {
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5465 if (elt_idx == 0 && (type == 's' || type == 'c'))
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5466 {
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5467 retval = "";
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5468 break;
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5469 }
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5470
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5471 if (exhausted ())
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5472 curr_state = conversion_error;
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5473 }
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5474 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5475 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5476
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5477 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5478 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5479
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5480 int
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5481 printf_value_cache::int_value (void)
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5482 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5483 int retval = 0;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5484
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5485 octave_value val = get_next_value ();
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5486
20558
1a0a433c8263 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20465
diff changeset
5487 double dval = val.double_value (true);
1a0a433c8263 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20465
diff changeset
5488
20704
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
5489 if (D_NINT (dval) == dval)
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
5490 retval = NINT (dval);
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
5491 else
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
5492 curr_state = conversion_error;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5493
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5494 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5495 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5496
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5497 // Ugh again and again.
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5498
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21129
diff changeset
5499 template <typename T>
3620
0886bbb236cb [project @ 2000-03-23 05:17:23 by jwe]
jwe
parents: 3559
diff changeset
5500 int
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
5501 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
5502 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
5503 {
3620
0886bbb236cb [project @ 2000-03-23 05:17:23 by jwe]
jwe
parents: 3559
diff changeset
5504 int retval = 0;
0886bbb236cb [project @ 2000-03-23 05:17:23 by jwe]
jwe
parents: 3559
diff changeset
5505
2572
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5506 switch (nsa)
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5507 {
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5508 case 2:
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
5509 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
5510 break;
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5511
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5512 case 1:
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
5513 retval = octave_format (os, fmt, sa_1, arg);
2572
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5514 break;
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5515
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5516 case 0:
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
5517 retval = octave_format (os, fmt, arg);
2572
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5518 break;
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5519
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5520 default:
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
5521 ::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
5522 break;
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5523 }
3620
0886bbb236cb [project @ 2000-03-23 05:17:23 by jwe]
jwe
parents: 3559
diff changeset
5524
0886bbb236cb [project @ 2000-03-23 05:17:23 by jwe]
jwe
parents: 3559
diff changeset
5525 return retval;
2572
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5526 }
5c2be7c820ed [project @ 1996-12-08 04:02:46 by jwe]
jwe
parents: 2435
diff changeset
5527
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5528 static size_t
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5529 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
5530 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
5531 const std::string& who)
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5532 {
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5533 if (nsa > 2)
20831
35241c4b696c eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents: 20768
diff changeset
5534 ::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
5535
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5536 std::string flags = elt->flags;
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5537
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5538 bool left = flags.find ('-') != std::string::npos;
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5539
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5540 size_t len = arg.length ();
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5541
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5542 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
5543 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
5544
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5545 os << std::setw (fw)
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5546 << (left ? std::left : std::right)
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5547 << (prec < len ? arg.substr (0, prec) : arg);
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5548
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5549 return len > fw ? len : fw;
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5550 }
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5551
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5552 static bool
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5553 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
5554 {
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5555 octave_value ov_isnan = val.isnan ();
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5556 octave_value ov_isinf = val.isinf ();
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 (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
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_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
5563 {
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5564 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
5565
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5566 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
5567 return true;
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5568 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
5569 {
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5570 if (val.is_uint64_type ())
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5571 {
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5572 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
5573
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5574 if (ival.value () <= limit)
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5575 return true;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5576 }
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5577 else
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5578 return true;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5579 }
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5580 else
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5581 {
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5582 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
5583
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5584 if (dval == xround (dval) && dval <= limit)
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5585 return true;
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 return false;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5589 }
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 static bool
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5592 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
5593 {
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5594 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
5595 return true;
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5596 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
5597 {
7d0014bb9e4e also switch from unsigned integer to real format for negative values
John W. Eaton <jwe@octave.org>
parents: 18650
diff changeset
5598 // 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
5599
7d0014bb9e4e also switch from unsigned integer to real format for negative values
John W. Eaton <jwe@octave.org>
parents: 18650
diff changeset
5600 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
5601 = 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
5602
7d0014bb9e4e also switch from unsigned integer to real format for negative values
John W. Eaton <jwe@octave.org>
parents: 18650
diff changeset
5603 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
5604 }
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5605 else
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5606 {
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5607 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
5608
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5609 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
5610
18653
7d0014bb9e4e also switch from unsigned integer to real format for negative values
John W. Eaton <jwe@octave.org>
parents: 18650
diff changeset
5611 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
5612 return true;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5613 }
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5614
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5615 return false;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5616 }
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5617
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5618 static std::string
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5619 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
5620 {
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5621 std::string tfmt = elt->text;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5622
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5623 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
5624
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5625 return tfmt;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5626 }
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 int
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5629 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
5630 const printf_format_elt *elt,
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5631 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
5632 const octave_value& val,
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5633 const std::string& who)
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 int retval = 0;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5636
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5637 const char *fmt = elt->text;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5638
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5639 if (is_nan_or_inf (val))
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 double dval = val.double_value ();
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 std::string tfmt = fmt;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5644 std::string::size_type i1, i2;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5645
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5646 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
5647
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5648 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
5649 {
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5650 tfmt.erase (i2, i1-i2);
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5651 if (elt->prec == -2)
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5652 nsa--;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5653 }
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 const char *tval;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5656 if (lo_ieee_isinf (dval))
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5657 {
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5658 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
5659 tval = (dval < 0 ? "-Inf" : "+Inf");
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5660 else
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5661 tval = (dval < 0 ? "-Inf" : "Inf");
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5662 }
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5663 else
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5664 {
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5665 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
5666 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
5667 else
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5668 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
5669 }
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5670
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5671 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
5672 }
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5673 else
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 static std::string llmod
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5676 = 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
5677
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5678 char type = elt->type;
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 switch (type)
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5681 {
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5682 case 'd': case 'i': case 'c':
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5683 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
5684 {
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5685 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
5686
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5687 // Insert "long" modifier.
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5688 std::string tfmt = fmt;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5689 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
5690
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5691 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
5692 tval.value (), who);
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5693 }
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5694 else
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 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
5697
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5698 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
5699
20558
1a0a433c8263 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20465
diff changeset
5700 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
5701 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
5702 }
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5703 break;
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 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
5706 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
5707 {
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5708 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
5709
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5710 // Insert "long" modifier.
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5711 std::string tfmt = fmt;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5712 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
5713
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5714 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
5715 tval.value (), who);
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5716 }
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5717 else
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5718 {
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5719 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
5720
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5721 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
5722
20558
1a0a433c8263 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20465
diff changeset
5723 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
5724 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
5725 }
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5726 break;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5727
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5728 case 'f': case 'e': case 'E':
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5729 case 'g': case 'G':
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5730 {
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5731 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
5732
20558
1a0a433c8263 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20465
diff changeset
5733 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
5734 }
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5735 break;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5736
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5737 default:
20938
aac911d8847b choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents: 20931
diff changeset
5738 // 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
5739 // 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
5740 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
5741 return -1;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5742 break;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5743 }
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5744 }
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5745
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5746 return retval;
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5747 }
6492
bcfdc9f0d267 [project @ 2007-04-05 06:13:30 by jwe]
jwe
parents: 6483
diff changeset
5748
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5749 int
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5750 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
5751 const octave_value_list& args,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5752 const std::string& who)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5753 {
3620
0886bbb236cb [project @ 2000-03-23 05:17:23 by jwe]
jwe
parents: 3559
diff changeset
5754 int retval = 0;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5755
10187
a44d15813a39 don't skip literal text elements in scanf formats
John W. Eaton <jwe@octave.org>
parents: 10160
diff changeset
5756 octave_idx_type nconv = fmt_list.num_conversions ();
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
5757
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
5758 std::ostream *osp = output_stream ();
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5759
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5760 if (! osp)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5761 invalid_operation (who, "writing");
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5762 else
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5763 {
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
5764 std::ostream& os = *osp;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5765
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5766 const printf_format_elt *elt = fmt_list.first ();
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5767
7352
fab9bc33b949 [project @ 2008-01-07 18:54:45 by jwe]
jwe
parents: 7227
diff changeset
5768 printf_value_cache val_cache (args, who);
fab9bc33b949 [project @ 2008-01-07 18:54:45 by jwe]
jwe
parents: 7227
diff changeset
5769
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5770 for (;;)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5771 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5772 octave_quit ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5773
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5774 if (! elt)
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5775 ::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
5776
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5777 // 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
5778 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
5779
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5780 int sa_1 = 0;
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5781 int sa_2 = 0;
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 if (nsa > 0)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5784 {
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5785 sa_1 = val_cache.int_value ();
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5786
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5787 if (! val_cache)
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5788 break;
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5789 else
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5790 {
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5791 if (nsa > 1)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5792 {
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5793 sa_2 = val_cache.int_value ();
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5794
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5795 if (! val_cache)
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5796 break;
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5797 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5798 }
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5799 }
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5800
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5801 if (elt->type == '%')
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5802 {
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5803 os << "%";
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5804 retval++;
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5805 }
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5806 else if (elt->args == 0 && elt->text)
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5807 {
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5808 os << elt->text;
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5809 retval += strlen (elt->text);
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 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
5812 {
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5813 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
5814
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5815 if (val_cache)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5816 {
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5817 if (val.is_string ())
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5818 {
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5819 std::string sval = val.string_value ();
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5820
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5821 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
5822 sa_2, sval, who);
19729
17a7e9f26e50 improve compatibility of printf functions
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
5823 }
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5824 else
18650
491b0adfec95 compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents: 18620
diff changeset
5825 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
5826 sa_2, val, who);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5827 }
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5828 else
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5829 break;
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5830 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5831 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5832 {
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5833 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
5834
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5835 if (val_cache)
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5836 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
5837 sa_2, val, who);
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5838 else
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5839 break;
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5840 }
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5841
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5842 if (! os)
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5843 {
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
5844 error ("%s: write error", who.c_str ());
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5845 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5846 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5847
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5848 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
5849
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5850 if (! elt || (val_cache.exhausted () && elt->args > 0))
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5851 break;
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
5852 }
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
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5855 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5856 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5857
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5858 int
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
5859 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
5860 const octave_value_list& args,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5861 const std::string& who)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5862 {
3640
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
5863 printf_format_list fmt_list (fmt);
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
5864
d3b0ff09dda7 [project @ 2000-03-24 10:28:19 by jwe]
jwe
parents: 3639
diff changeset
5865 if (fmt_list.num_conversions () == -1)
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
5866 ::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
5867
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5868 return do_printf (fmt_list, args, who);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5869 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5870
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5871 int
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
5872 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
5873 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5874 int retval = -1;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5875
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
5876 std::ostream *osp = output_stream ();
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5877
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5878 if (! osp)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5879 invalid_operation (who, "writing");
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5880 else
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5881 {
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
5882 std::ostream& os = *osp;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5883
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5884 os << s;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5885
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5886 if (! os)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
5887 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
5888 else
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5889 {
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
5890 // 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
5891 // Without it, output from a loop like
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5892 //
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5893 // 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
5894 //
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5895 // 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
5896 os.flush ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5897
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5898 if (os)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5899 retval = 0;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5900 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5901 error ("%s: write error", who.c_str ());
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5902 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5903 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5904
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5905 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5906 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5907
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5908 // Return current error message for this stream.
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5909
3536
e8fbc8c3d6d9 [project @ 2000-02-02 12:01:00 by jwe]
jwe
parents: 3535
diff changeset
5910 std::string
2435
3be97fe02051 [project @ 1996-10-27 21:31:29 by jwe]
jwe
parents: 2341
diff changeset
5911 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
5912 {
2435
3be97fe02051 [project @ 1996-10-27 21:31:29 by jwe]
jwe
parents: 2341
diff changeset
5913 err_num = fail ? -1 : 0;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5914
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
5915 std::string tmp = errmsg;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5916
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5917 if (clear_err)
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5918 clear ();
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5919
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5920 return tmp;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5921 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5922
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5923 void
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
5924 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
5925 {
20938
aac911d8847b choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents: 20931
diff changeset
5926 // 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
5927 error (who, std::string ("stream not open for ") + rw);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5928 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5929
3552
41daa489833a [project @ 2000-02-03 03:05:28 by jwe]
jwe
parents: 3548
diff changeset
5930 octave_stream::octave_stream (octave_base_stream *bs)
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5931 : rep (bs)
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 if (rep)
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5934 rep->count = 1;
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
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5937 octave_stream::~octave_stream (void)
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5938 {
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5939 if (rep && --rep->count == 0)
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5940 delete rep;
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5941 }
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5942
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5943 octave_stream::octave_stream (const octave_stream& s)
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5944 : rep (s.rep)
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5945 {
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5946 if (rep)
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5947 rep->count++;
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5948 }
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5949
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5950 octave_stream&
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5951 octave_stream::operator = (const octave_stream& s)
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5952 {
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5953 if (rep != s.rep)
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5954 {
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5955 if (rep && --rep->count == 0)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5956 delete rep;
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5957
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5958 rep = s.rep;
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5959
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5960 if (rep)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
5961 rep->count++;
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5962 }
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5963
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5964 return *this;
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5965 }
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
5966
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5967 int
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5968 octave_stream::flush (void)
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5969 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5970 int retval = -1;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5971
5659
960f4b9a26af [project @ 2006-03-10 15:35:20 by jwe]
jwe
parents: 5649
diff changeset
5972 if (stream_ok ())
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5973 retval = rep->flush ();
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5974
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5975 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5976 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5977
3536
e8fbc8c3d6d9 [project @ 2000-02-02 12:01:00 by jwe]
jwe
parents: 3535
diff changeset
5978 std::string
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5266
diff changeset
5979 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
5980 {
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
5981 std::string retval;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5982
5659
960f4b9a26af [project @ 2006-03-10 15:35:20 by jwe]
jwe
parents: 5649
diff changeset
5983 if (stream_ok ())
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
5984 retval = rep->getl (max_len, err, who);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5985
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5986 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5987 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5988
3536
e8fbc8c3d6d9 [project @ 2000-02-02 12:01:00 by jwe]
jwe
parents: 3535
diff changeset
5989 std::string
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
5990 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
5991 const std::string& who)
2117
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 err = false;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5994
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5995 int conv_err = 0;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
5996
6345
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
5997 int max_len = -1;
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
5998
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
5999 if (tc_max_len.is_defined ())
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6000 {
6345
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
6001 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
6002
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
6003 if (conv_err || max_len < 0)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6004 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6005 err = true;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6006 ::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
6007 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6008 }
6345
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
6009
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6010 return getl (max_len, err, who);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6011 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6012
3536
e8fbc8c3d6d9 [project @ 2000-02-02 12:01:00 by jwe]
jwe
parents: 3535
diff changeset
6013 std::string
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5266
diff changeset
6014 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
6015 {
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
6016 std::string retval;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6017
5659
960f4b9a26af [project @ 2006-03-10 15:35:20 by jwe]
jwe
parents: 5649
diff changeset
6018 if (stream_ok ())
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
6019 retval = rep->gets (max_len, err, who);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6020
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6021 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6022 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6023
3536
e8fbc8c3d6d9 [project @ 2000-02-02 12:01:00 by jwe]
jwe
parents: 3535
diff changeset
6024 std::string
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
6025 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
6026 const std::string& who)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6027 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6028 err = false;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6029
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6030 int conv_err = 0;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6031
6345
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
6032 int max_len = -1;
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
6033
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
6034 if (tc_max_len.is_defined ())
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6035 {
6345
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
6036 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
6037
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
6038 if (conv_err || max_len < 0)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6039 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6040 err = true;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6041 ::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
6042 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6043 }
6345
9e058e5fa8a7 [project @ 2007-02-22 22:22:53 by jwe]
jwe
parents: 6297
diff changeset
6044
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6045 return gets (max_len, err, who);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6046 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6047
16011
8122286c69a9 initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents: 15467
diff changeset
6048 off_t
8122286c69a9 initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents: 15467
diff changeset
6049 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
6050 {
16011
8122286c69a9 initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents: 15467
diff changeset
6051 off_t retval = -1;
9701
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 if (stream_ok ())
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6054 retval = rep->skipl (count, err, who);
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6055
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6056 return retval;
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6057 }
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6058
16011
8122286c69a9 initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents: 15467
diff changeset
6059 off_t
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
6060 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
6061 const std::string& who)
9701
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6062 {
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6063 err = false;
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6064
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6065 int conv_err = 0;
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6066
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6067 int count = 1;
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6068
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6069 if (tc_count.is_defined ())
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6070 {
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6071 if (tc_count.is_scalar_type () && xisinf (tc_count.scalar_value ()))
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6072 count = -1;
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6073 else
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6074 {
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6075 count = convert_to_valid_int (tc_count, conv_err);
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6076
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6077 if (conv_err || count < 0)
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6078 {
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6079 err = true;
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6080 ::error ("%s: invalid number of lines specified", who.c_str ());
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6081 }
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6082 }
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6083 }
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
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 return skipl (count, err, who);
9701
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6086 }
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9689
diff changeset
6087
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6088 int
16011
8122286c69a9 initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents: 15467
diff changeset
6089 octave_stream::seek (off_t offset, int origin)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6090 {
5065
1312d3af9eb0 [project @ 2004-11-04 20:12:00 by jwe]
jwe
parents: 5030
diff changeset
6091 int status = -1;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6092
5659
960f4b9a26af [project @ 2006-03-10 15:35:20 by jwe]
jwe
parents: 5649
diff changeset
6093 if (stream_ok ())
4889
658aad1c1b05 [project @ 2004-04-30 22:38:30 by jwe]
jwe
parents: 4888
diff changeset
6094 {
658aad1c1b05 [project @ 2004-04-30 22:38:30 by jwe]
jwe
parents: 4888
diff changeset
6095 clearerr ();
658aad1c1b05 [project @ 2004-04-30 22:38:30 by jwe]
jwe
parents: 4888
diff changeset
6096
12957
fb69561e5901 maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
6097 // 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
6098 off_t orig_pos = rep->tell ();
5065
1312d3af9eb0 [project @ 2004-11-04 20:12:00 by jwe]
jwe
parents: 5030
diff changeset
6099
12957
fb69561e5901 maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
6100 // 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
6101 status = rep->seek (0, SEEK_END);
5065
1312d3af9eb0 [project @ 2004-11-04 20:12:00 by jwe]
jwe
parents: 5030
diff changeset
6102
1312d3af9eb0 [project @ 2004-11-04 20:12:00 by jwe]
jwe
parents: 5030
diff changeset
6103 if (status == 0)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6104 {
16011
8122286c69a9 initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents: 15467
diff changeset
6105 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
6106
12943
8372d50de75a improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents: 12936
diff changeset
6107 if (origin == SEEK_CUR)
8372d50de75a improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents: 12936
diff changeset
6108 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6109 // 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
6110 // 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
6111 // location.
12943
8372d50de75a improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents: 12936
diff changeset
6112 rep->seek (orig_pos, SEEK_SET);
8372d50de75a improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents: 12936
diff changeset
6113 }
8372d50de75a improve logic of octave_stream::seek funtion
John W. Eaton <jwe@octave.org>
parents: 12936
diff changeset
6114
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6115 // 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
6116 // existing file.
12957
fb69561e5901 maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
6117 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
6118
fb69561e5901 maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
6119 if (status == 0)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6120 {
12957
fb69561e5901 maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
6121 // 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
6122 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
6123
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6124 // 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
6125 // 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
6126 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
6127 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6128 // 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
6129 // 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
6130 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
6131
fb69561e5901 maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
6132 status = -1;
fb69561e5901 maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
6133 }
fb69561e5901 maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
6134 }
fb69561e5901 maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
6135 else
fb69561e5901 maint: fix missing line continuation in src/Makefile.am
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
6136 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6137 // 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
6138 // 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
6139 rep->seek (orig_pos, SEEK_SET);
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6140
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6141 status = -1;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6142 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6143 }
4889
658aad1c1b05 [project @ 2004-04-30 22:38:30 by jwe]
jwe
parents: 4888
diff changeset
6144 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6145
5065
1312d3af9eb0 [project @ 2004-11-04 20:12:00 by jwe]
jwe
parents: 5030
diff changeset
6146 return status;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6147 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6148
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6149 int
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6150 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
6151 const octave_value& tc_origin)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6152 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6153 int retval = -1;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6154
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
6155 // 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
6156 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
6157 off_t xoffset = val.value ();
4645
bd2067547b40 [project @ 2003-11-23 08:07:52 by jwe]
jwe
parents: 4643
diff changeset
6158
20704
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6159 int conv_err = 0;
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6160
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6161 int origin = SEEK_SET;
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6162
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6163 if (tc_origin.is_string ())
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6164 {
20704
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6165 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
6166
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6167 if (xorigin == "bof")
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6168 origin = SEEK_SET;
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6169 else if (xorigin == "cof")
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6170 origin = SEEK_CUR;
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6171 else if (xorigin == "eof")
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6172 origin = SEEK_END;
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6173 else
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6174 conv_err = -1;
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6175 }
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6176 else
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6177 {
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6178 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
6179
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6180 if (! conv_err)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6181 {
20704
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6182 if (xorigin == -1)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6183 origin = SEEK_SET;
20704
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6184 else if (xorigin == 0)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6185 origin = SEEK_CUR;
20704
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6186 else if (xorigin == 1)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6187 origin = SEEK_END;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6188 else
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6189 conv_err = -1;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6190 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6191 }
20704
571508c1ed06 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20650
diff changeset
6192
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
6193 if (conv_err)
20938
aac911d8847b choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents: 20931
diff changeset
6194 ::error ("fseek: invalid value for origin");
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6195
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
6196 retval = seek (xoffset, origin);
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
6197
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
6198 if (retval != 0)
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
6199 // 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
6200 error ("fseek: failed to seek to requested position");
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6201
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6202 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6203 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6204
16011
8122286c69a9 initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents: 15467
diff changeset
6205 off_t
4797
19b8225bdaa2 [project @ 2004-02-23 22:10:31 by jwe]
jwe
parents: 4693
diff changeset
6206 octave_stream::tell (void)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6207 {
16011
8122286c69a9 initial large file support for 32-bit systems
John W. Eaton <jwe@octave.org>
parents: 15467
diff changeset
6208 off_t retval = -1;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6209
5659
960f4b9a26af [project @ 2006-03-10 15:35:20 by jwe]
jwe
parents: 5649
diff changeset
6210 if (stream_ok ())
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6211 retval = rep->tell ();
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6212
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6213 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6214 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6215
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6216 int
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6217 octave_stream::rewind (void)
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6218 {
6296
85eb75190e01 [project @ 2007-02-10 07:10:42 by jwe]
jwe
parents: 6109
diff changeset
6219 return seek (0, SEEK_SET);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6220 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6221
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
6222 bool
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
6223 octave_stream::is_open (void) const
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
6224 {
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
6225 bool retval = false;
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
6226
5659
960f4b9a26af [project @ 2006-03-10 15:35:20 by jwe]
jwe
parents: 5649
diff changeset
6227 if (stream_ok ())
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
6228 retval = rep->is_open ();
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
6229
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
6230 return retval;
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
6231 }
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
6232
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
6233 void
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
6234 octave_stream::close (void)
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
6235 {
5659
960f4b9a26af [project @ 2006-03-10 15:35:20 by jwe]
jwe
parents: 5649
diff changeset
6236 if (stream_ok ())
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
6237 rep->close ();
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
6238 }
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
6239
21335
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6240 // 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
6241
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6242 template <typename T>
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6243 static inline bool
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6244 is_old_NA (T)
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6245 {
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6246 return false;
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6247 }
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6248
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6249 template <>
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6250 inline bool
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6251 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
6252 {
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6253 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
6254 }
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6255
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6256 template <typename T>
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6257 static inline T
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6258 replace_old_NA (T val)
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6259 {
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6260 return val;
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6261 }
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6262
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6263 template <>
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6264 inline double
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6265 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
6266 {
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6267 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
6268 }
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6269
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21129
diff changeset
6270 template <typename SRC_T, typename DST_T>
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6271 static octave_value
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6272 convert_and_copy (std::list<void *>& input_buf_list,
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6273 octave_idx_type input_buf_elts,
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6274 octave_idx_type elts_read,
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6275 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
6276 bool do_float_fmt_conv, bool do_NA_conv,
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6277 oct_mach_info::float_format from_flt_fmt)
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6278 {
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6279 typedef typename DST_T::element_type dst_elt_type;
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6280
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6281 DST_T conv (dim_vector (nr, nc));
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6282
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6283 dst_elt_type *conv_data = conv.fortran_vec ();
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6284
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6285 octave_idx_type j = 0;
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6286
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6287 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
6288 it != input_buf_list.end (); it++)
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6289 {
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6290 SRC_T *data = static_cast<SRC_T *> (*it);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6291
17463
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6292 if (swap || do_float_fmt_conv)
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6293 {
21335
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6294 if (do_NA_conv)
17463
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6295 {
21335
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6296 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
6297 i++, j++)
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6298 {
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6299 if (swap)
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6300 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
6301 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
6302 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
6303 1, from_flt_fmt,
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6304 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
6305
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6306 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
6307
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6308 if (is_old_NA (tmp))
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6309 tmp = replace_old_NA (tmp);
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6310
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6311 conv_data[j] = tmp;
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6312 }
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6313 }
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6314 else
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6315 {
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6316 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
6317 i++, j++)
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6318 {
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6319 if (swap)
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6320 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
6321 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
6322 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
6323 1, from_flt_fmt,
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6324 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
6325
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6326 conv_data[j] = data[i];
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6327 }
17463
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6328 }
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6329 }
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6330 else
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6331 {
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6332 if (do_NA_conv)
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6333 {
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6334 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
6335 i++, j++)
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6336 {
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6337 dst_elt_type tmp (data[i]);
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6338
21335
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6339 if (is_old_NA (tmp))
e078d2208d86 avoid warnings about implicit float to booll conversions
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
6340 tmp = replace_old_NA (tmp);
17463
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6341
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6342 conv_data[j] = tmp;
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6343 }
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6344 }
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6345 else
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6346 {
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6347 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
6348 i++, j++)
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6349 conv_data[j] = data[i];
cc13924a4266 snapshot 3.7.7
John W. Eaton <jwe@octave.org>
parents: 17453
diff changeset
6350 }
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6351 }
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6352
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6353 delete [] data;
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6354 }
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6355
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6356 input_buf_list.clear ();
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6357
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6358 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
6359 conv_data[i] = dst_elt_type (0);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6360
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6361 return conv;
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6362 }
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6363
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6364 typedef octave_value (*conv_fptr)
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6365 (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
6366 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
6367 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
6368 oct_mach_info::float_format from_flt_fmt);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6369
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6370 #define TABLE_ELT(T, U, V, W) \
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6371 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
6372
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6373 #define FILL_TABLE_ROW(T, V) \
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6374 TABLE_ELT (T, dt_int8, V, int8NDArray); \
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6375 TABLE_ELT (T, dt_uint8, V, uint8NDArray); \
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6376 TABLE_ELT (T, dt_int16, V, int16NDArray); \
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6377 TABLE_ELT (T, dt_uint16, V, uint16NDArray); \
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6378 TABLE_ELT (T, dt_int32, V, int32NDArray); \
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6379 TABLE_ELT (T, dt_uint32, V, uint32NDArray); \
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6380 TABLE_ELT (T, dt_int64, V, int64NDArray); \
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6381 TABLE_ELT (T, dt_uint64, V, uint64NDArray); \
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6382 TABLE_ELT (T, dt_single, V, FloatNDArray); \
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6383 TABLE_ELT (T, dt_double, V, NDArray); \
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6384 TABLE_ELT (T, dt_char, V, charNDArray); \
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6385 TABLE_ELT (T, dt_schar, V, charNDArray); \
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6386 TABLE_ELT (T, dt_uchar, V, charNDArray); \
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6387 TABLE_ELT (T, dt_logical, V, boolNDArray);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6388
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6389 octave_value
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6390 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
6391 octave_idx_type input_buf_elts,
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6392 octave_idx_type elts_read,
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6393 octave_idx_type nr, octave_idx_type nc,
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6394 oct_data_conv::data_type input_type,
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6395 oct_data_conv::data_type output_type,
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6396 oct_mach_info::float_format ffmt)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6397 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6398 octave_value retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6399
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6400 static bool initialized = false;
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6401
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6402 // Table function pointers for return types x read types.
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6403
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6404 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
6405
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6406 if (! initialized)
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6407 {
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6408 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
6409 for (int j = 0; j < 14; j++)
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6410 conv_fptr_table[i][j] = 0;
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6411
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6412 FILL_TABLE_ROW (dt_int8, int8_t);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6413 FILL_TABLE_ROW (dt_uint8, uint8_t);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6414 FILL_TABLE_ROW (dt_int16, int16_t);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6415 FILL_TABLE_ROW (dt_uint16, uint16_t);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6416 FILL_TABLE_ROW (dt_int32, int32_t);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6417 FILL_TABLE_ROW (dt_uint32, uint32_t);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6418 FILL_TABLE_ROW (dt_int64, int64_t);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6419 FILL_TABLE_ROW (dt_uint64, uint64_t);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6420 FILL_TABLE_ROW (dt_single, float);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6421 FILL_TABLE_ROW (dt_double, double);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6422 FILL_TABLE_ROW (dt_char, char);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6423 FILL_TABLE_ROW (dt_schar, signed char);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6424 FILL_TABLE_ROW (dt_uchar, unsigned char);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6425 FILL_TABLE_ROW (dt_logical, bool);
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6426
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6427 initialized = true;
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6428 }
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6429
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6430 bool swap = false;
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6431
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6432 if (ffmt == oct_mach_info::flt_fmt_unknown)
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6433 ffmt = float_format ();
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6434
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6435 if (oct_mach_info::words_big_endian ())
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6436 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
6437 else
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6438 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
6439
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6440 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
6441 || input_type == oct_data_conv::dt_single)
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6442 && ffmt != float_format ());
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6443
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6444 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
6445
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6446 switch (output_type)
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6447 {
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6448 case oct_data_conv::dt_int8:
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6449 case oct_data_conv::dt_uint8:
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6450 case oct_data_conv::dt_int16:
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6451 case oct_data_conv::dt_uint16:
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6452 case oct_data_conv::dt_int32:
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6453 case oct_data_conv::dt_uint32:
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6454 case oct_data_conv::dt_int64:
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6455 case oct_data_conv::dt_uint64:
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6456 case oct_data_conv::dt_single:
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6457 case oct_data_conv::dt_double:
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6458 case oct_data_conv::dt_char:
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6459 case oct_data_conv::dt_schar:
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6460 case oct_data_conv::dt_uchar:
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6461 case oct_data_conv::dt_logical:
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6462 {
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6463 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
6464
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6465 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
6466 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
6467 }
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6468 break;
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6469
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6470 default:
20938
aac911d8847b choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents: 20931
diff changeset
6471 ::error ("read: invalid type specification");
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6472 }
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
6473
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6474 return retval;
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6475 }
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6476
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6477 octave_value
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5266
diff changeset
6478 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
6479 oct_data_conv::data_type input_type,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6480 oct_data_conv::data_type output_type,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6481 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
6482 octave_idx_type& count)
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6483 {
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6484 octave_value retval;
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6485
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6486 octave_idx_type nr = -1;
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6487 octave_idx_type nc = -1;
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6488
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6489 bool one_elt_size_spec = false;
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6490
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6491 if (! stream_ok ())
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6492 return retval;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6493
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6494 // 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
6495
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6496 // 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
6497 // 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
6498 // 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
6499
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6500 // 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
6501 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
6502
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6503 count = 0;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6504
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6505 try
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6506 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6507 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
6508 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6509 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
6510 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6511 invalid_operation ("fread", "reading");
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
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6514 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
6515
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6516 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
6517 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6518 // 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
6519
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6520 // 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
6521 // number of rows.
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6522
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6523 // 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
6524 // 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
6525
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6526 if (nr == 0)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6527 nr = nc = 0;
20741
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
6528 else
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6529 nc = 1;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6530 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6531 else
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6532 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6533 // 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
6534 // 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
6535
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6536 // 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
6537 // should be.
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 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
6540 nr = nc = 0;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6541 }
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 // 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
6544 // 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
6545 // 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
6546 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
6547
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6548 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
6549
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6550 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
6551
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6552 if (skip == 0)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6553 {
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)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6555 input_buf_elts = 1024 * 1024;
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6556 else
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6557 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
6558 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6559 else
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6560 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
6561
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6562 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
6563 = 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
6564
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6565 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
6566
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6567 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
6568
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6569 // 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
6570 // 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
6571 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
6572
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6573 if (! isp)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6574 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
6575 else
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6576 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6577 std::istream& is = *isp;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6578
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6579 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
6580
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6581 while (is && ! is.eof ()
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6582 && (read_to_eof || count < elts_to_read))
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6583 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6584 if (! read_to_eof)
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6585 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6586 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
6587
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6588 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
6589 input_buf_size = remaining_elts * input_elt_size;
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6590 }
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6591
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6592 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
6593
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6594 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
6595
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6596 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
6597
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6598 char_count += gcount;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6599
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6600 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
6601
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6602 count += nel;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6603
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6604 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
6605
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6606 if (is && skip != 0 && nel == block_size)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6607 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6608 // Seek to skip.
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6609 // 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
6610
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6611 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
6612
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6613 seek (0, SEEK_END);
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 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
6616
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6617 // 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
6618 // the original position?
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6619 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
6620
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6621 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
6622
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6623 if (remaining < skip)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6624 seek (0, SEEK_END);
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6625 else
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6626 seek (skip, SEEK_CUR);
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6627
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6628 if (! is)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6629 break;
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6630 }
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6631 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6632
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6633 if (read_to_eof)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6634 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6635 if (nc < 0)
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6636 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6637 nc = count / nr;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6638
20741
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
6639 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
6640 nc++;
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6641 }
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6642 else
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6643 nr = count;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6644 }
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6645 else if (count == 0)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6646 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6647 nr = 0;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6648 nc = 0;
20741
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
6649 }
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6650 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
6651 {
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6652 if (count % nr != 0)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6653 nc = count / nr + 1;
17453
669ad11f282d improve efficiency of fread
John W. Eaton <jwe@octave.org>
parents: 17416
diff changeset
6654 else
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6655 nc = count / nr;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6656
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6657 if (count < nr)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6658 nr = count;
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6659 }
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6660
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6661 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
6662 nr, nc, input_type, output_type, ffmt);
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6663 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6664
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6665 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6666 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6667
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5266
diff changeset
6668 octave_idx_type
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5266
diff changeset
6669 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
6670 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
6671 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
6672 {
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5266
diff changeset
6673 octave_idx_type retval = -1;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6674
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6675 if (! stream_ok ())
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6676 invalid_operation ("fwrite", "writing");
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6677 else
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6678 {
20741
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
6679 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
6680 flt_fmt = float_format ();
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
6681
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
6682 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
6683 skip, flt_fmt);
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
6684
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
6685 if (status < 0)
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
6686 error ("fwrite: write error");
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6687 else
20741
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20714
diff changeset
6688 retval = status;
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6689 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6690
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6691 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6692 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
6693
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21129
diff changeset
6694 template <typename T, typename V>
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6695 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
6696 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
6697 {
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6698 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
6699
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6700 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
6701
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6702 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
6703 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
6704 }
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6705
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21129
diff changeset
6706 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
6707 static void
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6708 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
6709 bool swap)
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6710 {
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6711 typedef typename V::val_type val_type;
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6712
19739
3fa35defe495 Adjust spacing of static_cast<> calls to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19729
diff changeset
6713 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
6714
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6715 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
6716 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6717 // 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
6718 V val (data[i]);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6719
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6720 vt_data[i] = val.value ();
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6721
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6722 if (swap)
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6723 swap_bytes<sizeof (val_type)> (&vt_data[i]);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6724 }
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6725 }
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6726
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21129
diff changeset
6727 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
6728 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
6729 {
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6730 public:
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6731 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
6732 };
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6733
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21129
diff changeset
6734 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
6735 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
6736 {
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6737 public:
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6738 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
6739 };
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6740
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21129
diff changeset
6741 template <typename T>
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6742 static bool
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6743 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
6744 oct_data_conv::data_type output_type,
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6745 oct_mach_info::float_format flt_fmt)
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6746 {
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6747 bool retval = true;
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6748
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6749 bool swap
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6750 = ((oct_mach_info::words_big_endian ()
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6751 && 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
6752 || 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
6753
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6754 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
6755
18616
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6756 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
6757
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6758 switch (output_type)
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6759 {
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6760 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
6761 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
6762 break;
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6763
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6764 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
6765 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
6766 break;
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6767
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6768 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
6769 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
6770 break;
aa861a98d84d fwrite: don't convert to octave_int for char output types
John W. Eaton <jwe@octave.org>
parents: 18518
diff changeset
6771
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6772 case oct_data_conv::dt_int8:
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6773 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
6774 break;
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6775
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6776 case oct_data_conv::dt_uint8:
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6777 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
6778 break;
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6779
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6780 case oct_data_conv::dt_int16:
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6781 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
6782 break;
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6783
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6784 case oct_data_conv::dt_uint16:
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6785 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
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_int32:
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6789 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
6790 break;
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6791
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6792 case oct_data_conv::dt_uint32:
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6793 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
6794 break;
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6795
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6796 case oct_data_conv::dt_int64:
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6797 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
6798 break;
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 case oct_data_conv::dt_uint64:
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6801 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
6802 break;
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6803
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6804 case oct_data_conv::dt_single:
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6805 {
19739
3fa35defe495 Adjust spacing of static_cast<> calls to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19729
diff changeset
6806 float *vt_data = static_cast<float *> (conv_data);
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6807
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6808 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
6809 {
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6810 vt_data[i] = data[i];
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 if (do_float_conversion)
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6813 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
6814 }
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6815 }
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6816 break;
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6817
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6818 case oct_data_conv::dt_double:
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6819 {
19739
3fa35defe495 Adjust spacing of static_cast<> calls to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19729
diff changeset
6820 double *vt_data = static_cast<double *> (conv_data);
17416
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 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
6823 {
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6824 vt_data[i] = data[i];
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6825
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6826 if (do_float_conversion)
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6827 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
6828 }
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6829 }
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6830 break;
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6831
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6832 default:
20938
aac911d8847b choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents: 20931
diff changeset
6833 ::error ("write: invalid type specification");
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6834 }
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6835
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6836 return retval;
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6837 }
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6838
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6839 bool
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6840 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
6841 {
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6842 bool status = false;
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6843
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6844 std::ostream *osp = output_stream ();
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6845
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6846 if (osp)
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6847 {
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6848 std::ostream& os = *osp;
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6849
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6850 if (os)
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6851 {
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6852 os.write (static_cast<const char *> (data), nbytes);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6853
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6854 if (os)
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6855 status = true;
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6856 }
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6857 }
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 return status;
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6860 }
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6861
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6862 bool
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6863 octave_stream::skip_bytes (size_t skip)
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6864 {
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6865 bool status = false;
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6866
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6867 std::ostream *osp = output_stream ();
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6868
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6869 if (! osp)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6870 return false;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6871
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6872 std::ostream& os = *osp;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6873
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6874 // 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
6875 // 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
6876 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
6877
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6878 seek (0, SEEK_END);
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6879
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6880 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
6881
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6882 // 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
6883 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
6884
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6885 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
6886
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6887 if (remaining < skip)
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 seek (0, SEEK_END);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6890
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6891 // 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
6892 unsigned char zero = 0;
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6893 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
6894 os.write (reinterpret_cast<const char *> (&zero), 1);
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6895 }
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6896 else
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6897 seek (skip, SEEK_CUR);
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6898
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6899 if (os)
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
6900 status = true;
17416
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 return status;
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
21139
538b57866b90 consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents: 21129
diff changeset
6905 template <typename T>
5275
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5266
diff changeset
6906 octave_idx_type
23b37da9fd5b [project @ 2005-04-08 16:07:35 by jwe]
jwe
parents: 5266
diff changeset
6907 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
6908 oct_data_conv::data_type output_type,
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6909 octave_idx_type skip,
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6910 oct_mach_info::float_format flt_fmt)
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6911 {
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
6912 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
6913 && 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
6914 || 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
6915
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
6916 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
6917 || flt_fmt != oct_mach_info::float_format ());
17416
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 octave_idx_type nel = data.numel ();
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 octave_idx_type chunk_size;
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6922
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6923 if (skip != 0)
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6924 chunk_size = block_size;
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6925 else if (do_data_conversion)
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6926 chunk_size = 1024 * 1024;
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6927 else
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6928 chunk_size = nel;
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6929
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6930 octave_idx_type i = 0;
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6931
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6932 const T *pdata = data.data ();
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6933
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6934 while (i < nel)
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6935 {
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6936 if (skip != 0)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6937 {
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6938 if (! skip_bytes (skip))
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6939 return -1;
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6940 }
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6941
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6942 octave_idx_type remaining_nel = nel - i;
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6943
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6944 if (chunk_size > remaining_nel)
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6945 chunk_size = remaining_nel;
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6946
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6947 bool status = false;
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6948
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6949 if (do_data_conversion)
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6950 {
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6951 size_t output_size
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6952 = 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
6953
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6954 OCTAVE_LOCAL_BUFFER (unsigned char, conv_data, output_size);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6955
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6956 status = convert_data (&pdata[i], conv_data, chunk_size,
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6957 output_type, flt_fmt);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6958
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6959 if (status)
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6960 status = write_bytes (conv_data, output_size);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
6961 }
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6962 else
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6963 status = write_bytes (pdata, sizeof (T) * chunk_size);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6964
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6965 if (! status)
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6966 return -1;
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6967
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6968 i += chunk_size;
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6969 }
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6970
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6971 return nel;
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6972 }
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
6973
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6974 #define INSTANTIATE_WRITE(T) \
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6975 template \
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6976 octave_idx_type \
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6977 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
6978 oct_data_conv::data_type output_type, \
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6979 octave_idx_type skip, \
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6980 oct_mach_info::float_format flt_fmt)
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6981
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6982 INSTANTIATE_WRITE (octave_int8);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6983 INSTANTIATE_WRITE (octave_uint8);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6984 INSTANTIATE_WRITE (octave_int16);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6985 INSTANTIATE_WRITE (octave_uint16);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6986 INSTANTIATE_WRITE (octave_int32);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6987 INSTANTIATE_WRITE (octave_uint32);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6988 INSTANTIATE_WRITE (octave_int64);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6989 INSTANTIATE_WRITE (octave_uint64);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6990 INSTANTIATE_WRITE (int8_t);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6991 INSTANTIATE_WRITE (uint8_t);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6992 INSTANTIATE_WRITE (int16_t);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6993 INSTANTIATE_WRITE (uint16_t);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6994 INSTANTIATE_WRITE (int32_t);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6995 INSTANTIATE_WRITE (uint32_t);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6996 INSTANTIATE_WRITE (int64_t);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6997 INSTANTIATE_WRITE (uint64_t);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
6998 INSTANTIATE_WRITE (bool);
21354
06d15e4e611a move public data type macros to octave-config.h
John W. Eaton <jwe@octave.org>
parents: 21335
diff changeset
6999 #if defined (OCTAVE_HAVE_OVERLOAD_CHAR_INT8_TYPES)
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
7000 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
7001 #endif
17416
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
7002 INSTANTIATE_WRITE (float);
6690dba6078a improve efficiency of fwrite
John W. Eaton <jwe@octave.org>
parents: 17415
diff changeset
7003 INSTANTIATE_WRITE (double);
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4927
diff changeset
7004
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7005 octave_value
3810
f19f00339363 [project @ 2001-03-29 21:03:01 by jwe]
jwe
parents: 3779
diff changeset
7006 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
7007 octave_idx_type& count, const std::string& who)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7008 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7009 octave_value retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7010
5659
960f4b9a26af [project @ 2006-03-10 15:35:20 by jwe]
jwe
parents: 5649
diff changeset
7011 if (stream_ok ())
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
7012 retval = rep->scanf (fmt, size, count, who);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7013
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7014 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7015 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7016
5279
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7017 octave_value
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7018 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
7019 octave_idx_type& count, const std::string& who)
5279
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7020 {
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7021 octave_value retval = Matrix ();
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7022
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7023 if (fmt.is_string ())
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 std::string sfmt = fmt.string_value ();
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_sq_string ())
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7028 sfmt = do_string_escapes (sfmt);
5279
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7029
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7030 retval = scanf (sfmt, size, count, who);
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7031 }
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7032 else
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7033 {
20938
aac911d8847b choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents: 20931
diff changeset
7034 // 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
7035 error (who + ": format must be a string");
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7036 }
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7037
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7038 return retval;
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7039 }
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7040
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
7041 octave_value_list
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
7042 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
7043 {
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
7044 octave_value_list retval;
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
7045
5659
960f4b9a26af [project @ 2006-03-10 15:35:20 by jwe]
jwe
parents: 5649
diff changeset
7046 if (stream_ok ())
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
7047 retval = rep->oscanf (fmt, who);
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
7048
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
7049 return retval;
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
7050 }
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2213
diff changeset
7051
5279
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7052 octave_value_list
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7053 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
7054 {
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7055 octave_value_list retval;
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7056
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7057 if (fmt.is_string ())
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7058 {
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7059 std::string sfmt = fmt.string_value ();
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7060
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7061 if (fmt.is_sq_string ())
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7062 sfmt = do_string_escapes (sfmt);
5279
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7063
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7064 retval = oscanf (sfmt, who);
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7065 }
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7066 else
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7067 {
20938
aac911d8847b choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents: 20931
diff changeset
7068 // 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
7069 error (who + ": format must be a string");
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
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7072 return retval;
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7073 }
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7074
21503
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
7075 octave_value
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
7076 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
7077 const octave_value_list& options,
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
7078 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
7079 {
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
7080 return (stream_ok ()
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
7081 ? 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
7082 : octave_value ());
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
7083 }
20bf0ec536e2 integrate textscan more completely with octave_stream class
John W. Eaton <jwe@octave.org>
parents: 21502
diff changeset
7084
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7085 int
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
7086 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
7087 const std::string& who)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7088 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7089 int retval = -1;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7090
5659
960f4b9a26af [project @ 2006-03-10 15:35:20 by jwe]
jwe
parents: 5649
diff changeset
7091 if (stream_ok ())
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
7092 retval = rep->printf (fmt, args, 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 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7095 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7096
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7097 int
5279
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7098 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
7099 const std::string& who)
5279
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7100 {
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7101 int retval = 0;
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7102
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7103 if (fmt.is_string ())
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7104 {
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7105 std::string sfmt = fmt.string_value ();
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7106
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7107 if (fmt.is_sq_string ())
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7108 sfmt = do_string_escapes (sfmt);
5279
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7109
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7110 retval = printf (sfmt, args, who);
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7111 }
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7112 else
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7113 {
20938
aac911d8847b choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents: 20931
diff changeset
7114 // 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
7115 error (who + ": format must be a string");
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7116 }
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7117
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7118 return retval;
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7119 }
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7120
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7121 int
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
7122 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
7123 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7124 int retval = -1;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7125
5659
960f4b9a26af [project @ 2006-03-10 15:35:20 by jwe]
jwe
parents: 5649
diff changeset
7126 if (stream_ok ())
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
7127 retval = rep->puts (s, who);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7128
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7129 return retval;
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
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
7132 // FIXME: maybe this should work for string arrays too.
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7133
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7134 int
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
7135 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
7136 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7137 int retval = -1;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7138
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7139 if (tc_s.is_string ())
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7140 {
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
7141 std::string s = tc_s.string_value ();
5279
bd32f770c09a [project @ 2005-04-12 21:55:31 by jwe]
jwe
parents: 5275
diff changeset
7142 retval = puts (s, who);
2117
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 else
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
7145 {
20938
aac911d8847b choose correct error function in stream classes
John W. Eaton <jwe@octave.org>
parents: 20931
diff changeset
7146 // 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
7147 error (who + ": argument must be a string");
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4425
diff changeset
7148 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7149
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7150 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7151 }
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 bool
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7154 octave_stream::eof (void) const
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 retval = -1;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7157
5659
960f4b9a26af [project @ 2006-03-10 15:35:20 by jwe]
jwe
parents: 5649
diff changeset
7158 if (stream_ok ())
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7159 retval = rep->eof ();
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7160
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7161 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7162 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7163
3536
e8fbc8c3d6d9 [project @ 2000-02-02 12:01:00 by jwe]
jwe
parents: 3535
diff changeset
7164 std::string
2435
3be97fe02051 [project @ 1996-10-27 21:31:29 by jwe]
jwe
parents: 2341
diff changeset
7165 octave_stream::error (bool clear, int& err_num)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7166 {
5649
d24b97246b9b [project @ 2006-03-08 20:31:25 by jwe]
jwe
parents: 5581
diff changeset
7167 std::string retval = "invalid stream object";
d24b97246b9b [project @ 2006-03-08 20:31:25 by jwe]
jwe
parents: 5581
diff changeset
7168
5659
960f4b9a26af [project @ 2006-03-10 15:35:20 by jwe]
jwe
parents: 5649
diff changeset
7169 if (stream_ok (false))
2435
3be97fe02051 [project @ 1996-10-27 21:31:29 by jwe]
jwe
parents: 2341
diff changeset
7170 retval = rep->error (clear, err_num);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7171
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7172 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7173 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7174
3536
e8fbc8c3d6d9 [project @ 2000-02-02 12:01:00 by jwe]
jwe
parents: 3535
diff changeset
7175 std::string
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
7176 octave_stream::name (void) const
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7177 {
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
7178 std::string retval;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7179
5659
960f4b9a26af [project @ 2006-03-10 15:35:20 by jwe]
jwe
parents: 5649
diff changeset
7180 if (stream_ok ())
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7181 retval = rep->name ();
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7182
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7183 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7184 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7185
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7186 int
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
7187 octave_stream::mode (void) const
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7188 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7189 int retval = 0;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7190
5659
960f4b9a26af [project @ 2006-03-10 15:35:20 by jwe]
jwe
parents: 5649
diff changeset
7191 if (stream_ok ())
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7192 retval = rep->mode ();
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7193
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7194 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7195 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7196
2317
8c09c04f7747 [project @ 1996-07-14 22:30:15 by jwe]
jwe
parents: 2305
diff changeset
7197 oct_mach_info::float_format
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
7198 octave_stream::float_format (void) const
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7199 {
4574
6cb22b9e3942 [project @ 2003-10-31 05:57:43 by jwe]
jwe
parents: 4468
diff changeset
7200 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
7201
5659
960f4b9a26af [project @ 2006-03-10 15:35:20 by jwe]
jwe
parents: 5649
diff changeset
7202 if (stream_ok ())
2317
8c09c04f7747 [project @ 1996-07-14 22:30:15 by jwe]
jwe
parents: 2305
diff changeset
7203 retval = rep->float_format ();
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7204
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7205 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7206 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7207
3536
e8fbc8c3d6d9 [project @ 2000-02-02 12:01:00 by jwe]
jwe
parents: 3535
diff changeset
7208 std::string
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7209 octave_stream::mode_as_string (int mode)
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7210 {
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
7211 std::string retval = "???";
3775
13905c3a24af [project @ 2001-02-06 01:56:59 by jwe]
jwe
parents: 3716
diff changeset
7212 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
7213
13905c3a24af [project @ 2001-02-06 01:56:59 by jwe]
jwe
parents: 3716
diff changeset
7214 if (in_mode == std::ios::in)
13905c3a24af [project @ 2001-02-06 01:56:59 by jwe]
jwe
parents: 3716
diff changeset
7215 retval = "r";
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
7216 else if (in_mode == std::ios::out
4078
bd51df637cb3 [project @ 2002-09-27 23:23:20 by jwe]
jwe
parents: 4070
diff changeset
7217 || in_mode == (std::ios::out | std::ios::trunc))
3775
13905c3a24af [project @ 2001-02-06 01:56:59 by jwe]
jwe
parents: 3716
diff changeset
7218 retval = "w";
4078
bd51df637cb3 [project @ 2002-09-27 23:23:20 by jwe]
jwe
parents: 4070
diff changeset
7219 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
7220 retval = "a";
4078
bd51df637cb3 [project @ 2002-09-27 23:23:20 by jwe]
jwe
parents: 4070
diff changeset
7221 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
7222 retval = "r+";
4078
bd51df637cb3 [project @ 2002-09-27 23:23:20 by jwe]
jwe
parents: 4070
diff changeset
7223 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
7224 retval = "w+";
4078
bd51df637cb3 [project @ 2002-09-27 23:23:20 by jwe]
jwe
parents: 4070
diff changeset
7225 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
7226 retval = "a+";
4078
bd51df637cb3 [project @ 2002-09-27 23:23:20 by jwe]
jwe
parents: 4070
diff changeset
7227 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
7228 retval = "rb";
4078
bd51df637cb3 [project @ 2002-09-27 23:23:20 by jwe]
jwe
parents: 4070
diff changeset
7229 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
7230 || 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
7231 retval = "wb";
4078
bd51df637cb3 [project @ 2002-09-27 23:23:20 by jwe]
jwe
parents: 4070
diff changeset
7232 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
7233 retval = "ab";
4078
bd51df637cb3 [project @ 2002-09-27 23:23:20 by jwe]
jwe
parents: 4070
diff changeset
7234 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
7235 retval = "r+b";
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11574
diff changeset
7236 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
7237 | std::ios::binary))
3775
13905c3a24af [project @ 2001-02-06 01:56:59 by jwe]
jwe
parents: 3716
diff changeset
7238 retval = "w+b";
4078
bd51df637cb3 [project @ 2002-09-27 23:23:20 by jwe]
jwe
parents: 4070
diff changeset
7239 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
7240 | std::ios::binary))
3775
13905c3a24af [project @ 2001-02-06 01:56:59 by jwe]
jwe
parents: 3716
diff changeset
7241 retval = "a+b";
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7242
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7243 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7244 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7245
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7246 octave_stream_list *octave_stream_list::instance = 0;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7247
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7248 bool
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7249 octave_stream_list::instance_ok (void)
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7250 {
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7251 bool retval = true;
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7252
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7253 if (! instance)
13983
7dd7cccf0757 clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents: 13271
diff changeset
7254 {
7dd7cccf0757 clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents: 13271
diff changeset
7255 instance = new octave_stream_list ();
7dd7cccf0757 clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents: 13271
diff changeset
7256
7dd7cccf0757 clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents: 13271
diff changeset
7257 if (instance)
7dd7cccf0757 clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents: 13271
diff changeset
7258 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
7259 }
2926
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 if (! instance)
20831
35241c4b696c eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents: 20768
diff changeset
7262 ::error ("unable to create stream list object!");
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7263
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7264 return retval;
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
5353
df230b7df93c [project @ 2005-05-18 02:18:24 by jwe]
jwe
parents: 5338
diff changeset
7267 int
6757
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7268 octave_stream_list::insert (octave_stream& os)
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7269 {
5353
df230b7df93c [project @ 2005-05-18 02:18:24 by jwe]
jwe
parents: 5338
diff changeset
7270 return (instance_ok ()) ? instance->do_insert (os) : -1;
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7271 }
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7272
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
7273 octave_stream
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
7274 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
7275 {
3341
14cfc9475fe4 [project @ 1999-11-05 08:11:58 by jwe]
jwe
parents: 3340
diff changeset
7276 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
7277 }
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7278
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
7279 octave_stream
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
7280 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
7281 {
3341
14cfc9475fe4 [project @ 1999-11-05 08:11:58 by jwe]
jwe
parents: 3340
diff changeset
7282 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
7283 }
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 int
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
7286 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
7287 {
3341
14cfc9475fe4 [project @ 1999-11-05 08:11:58 by jwe]
jwe
parents: 3340
diff changeset
7288 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
7289 }
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 int
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
7292 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
7293 {
3341
14cfc9475fe4 [project @ 1999-11-05 08:11:58 by jwe]
jwe
parents: 3340
diff changeset
7294 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
7295 }
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7296
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7297 void
8902
5d5db7a347c6 erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8773
diff changeset
7298 octave_stream_list::clear (bool flush)
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7299 {
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7300 if (instance)
8902
5d5db7a347c6 erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8773
diff changeset
7301 instance->do_clear (flush);
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7302 }
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 string_vector
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7305 octave_stream_list::get_info (int fid)
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7306 {
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7307 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
7308 }
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7309
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7310 string_vector
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7311 octave_stream_list::get_info (const octave_value& fid)
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7312 {
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7313 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
7314 }
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7315
3536
e8fbc8c3d6d9 [project @ 2000-02-02 12:01:00 by jwe]
jwe
parents: 3535
diff changeset
7316 std::string
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7317 octave_stream_list::list_open_files (void)
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7318 {
21017
93748bcaec17 maint: Replace emtpy 'std::string ()' calls with "".
Rik <rik@octave.org>
parents: 20982
diff changeset
7319 return (instance_ok ()) ? instance->do_list_open_files () : "";
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7320 }
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7321
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7322 octave_value
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7323 octave_stream_list::open_file_numbers (void)
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7324 {
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7325 return (instance_ok ())
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
7326 ? instance->do_open_file_numbers () : octave_value ();
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7327 }
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7328
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7329 int
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7330 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
7331 {
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7332 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
7333 }
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2915
diff changeset
7334
5353
df230b7df93c [project @ 2005-05-18 02:18:24 by jwe]
jwe
parents: 5338
diff changeset
7335 int
6757
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7336 octave_stream_list::do_insert (octave_stream& os)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7337 {
6757
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7338 // Insert item with key corresponding to file-descriptor.
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7339
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
7340 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
7341
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
7342 if (stream_number == -1)
6757
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7343 return stream_number;
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7344
19864
17d647821d61 maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents: 19861
diff changeset
7345 // 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
7346 //
17d647821d61 maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents: 19861
diff changeset
7347 // (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
7348 // && 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
7349 //
17d647821d61 maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents: 19861
diff changeset
7350 // 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
7351 // 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
7352 // 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
7353 // 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
7354 // 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
7355 // 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
7356 // before.
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 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
7359 ::error ("could not create file id");
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7360
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7361 list[stream_number] = os;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7362
5353
df230b7df93c [project @ 2005-05-18 02:18:24 by jwe]
jwe
parents: 5338
diff changeset
7363 return stream_number;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7364 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7365
21040
3e7cfee5f786 maint: Rename attributes GCC_ATTR_XXX to OCTAVE_XXX.
Rik <rik@octave.org>
parents: 21029
diff changeset
7366 OCTAVE_NORETURN static
21029
e3b3bb522d62 maint: Move GCC_ attributes to start of declaration for future compatibility.
Rik <rik@octave.org>
parents: 21017
diff changeset
7367 void
21102
dfcb9d74b253 Rename local gripe_XXX functions to err_XXX or warn_XXX.
Rik <rik@octave.org>
parents: 21100
diff changeset
7368 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
7369 {
14cfc9475fe4 [project @ 1999-11-05 08:11:58 by jwe]
jwe
parents: 3340
diff changeset
7370 if (who.empty ())
14cfc9475fe4 [project @ 1999-11-05 08:11:58 by jwe]
jwe
parents: 3340
diff changeset
7371 ::error ("invalid stream number = %d", fid);
14cfc9475fe4 [project @ 1999-11-05 08:11:58 by jwe]
jwe
parents: 3340
diff changeset
7372 else
14cfc9475fe4 [project @ 1999-11-05 08:11:58 by jwe]
jwe
parents: 3340
diff changeset
7373 ::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
7374 }
14cfc9475fe4 [project @ 1999-11-05 08:11:58 by jwe]
jwe
parents: 3340
diff changeset
7375
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
7376 octave_stream
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
7377 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
7378 {
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
7379 octave_stream retval;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7380
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7381 if (fid < 0)
21102
dfcb9d74b253 Rename local gripe_XXX functions to err_XXX or warn_XXX.
Rik <rik@octave.org>
parents: 21100
diff changeset
7382 err_invalid_file_id (fid, who);
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7383
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7384 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
7385 retval = lookup_cache->second;
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7386 else
6757
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7387 {
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7388 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
7389
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7390 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
7391 err_invalid_file_id (fid, who);
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7392
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7393 retval = iter->second;
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7394 lookup_cache = iter;
6757
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7395 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7396
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7397 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7398 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7399
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
7400 octave_stream
3341
14cfc9475fe4 [project @ 1999-11-05 08:11:58 by jwe]
jwe
parents: 3340
diff changeset
7401 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
7402 const std::string& who) const
2117
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 i = get_file_number (fid);
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7405
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
7406 return do_lookup (i, who);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7407 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7408
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7409 int
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
7410 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
7411 {
20764
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7412 // 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
7413 if (fid < 3)
21102
dfcb9d74b253 Rename local gripe_XXX functions to err_XXX or warn_XXX.
Rik <rik@octave.org>
parents: 21100
diff changeset
7414 err_invalid_file_id (fid, who);
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7415
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7416 ostrl_map::iterator iter = list.find (fid);
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7417
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7418 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
7419 err_invalid_file_id (fid, who);
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7420
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7421 octave_stream os = iter->second;
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7422 list.erase (iter);
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7423 lookup_cache = list.end ();
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7424
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7425 // FIXME: is this check redundant?
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7426 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
7427 err_invalid_file_id (fid, who);
20982
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7428
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7429 os.close ();
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7430
d27f66b4b8e6 maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents: 20946
diff changeset
7431 return 0;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7432 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7433
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7434 int
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
7435 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
7436 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7437 int retval = -1;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7438
6054
769b203ede37 [project @ 2006-10-16 17:53:53 by jwe]
jwe
parents: 5887
diff changeset
7439 if (fid.is_string () && fid.string_value () == "all")
769b203ede37 [project @ 2006-10-16 17:53:53 by jwe]
jwe
parents: 5887
diff changeset
7440 {
8902
5d5db7a347c6 erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8773
diff changeset
7441 do_clear (false);
6054
769b203ede37 [project @ 2006-10-16 17:53:53 by jwe]
jwe
parents: 5887
diff changeset
7442
769b203ede37 [project @ 2006-10-16 17:53:53 by jwe]
jwe
parents: 5887
diff changeset
7443 retval = 0;
769b203ede37 [project @ 2006-10-16 17:53:53 by jwe]
jwe
parents: 5887
diff changeset
7444 }
769b203ede37 [project @ 2006-10-16 17:53:53 by jwe]
jwe
parents: 5887
diff changeset
7445 else
769b203ede37 [project @ 2006-10-16 17:53:53 by jwe]
jwe
parents: 5887
diff changeset
7446 {
769b203ede37 [project @ 2006-10-16 17:53:53 by jwe]
jwe
parents: 5887
diff changeset
7447 int i = get_file_number (fid);
769b203ede37 [project @ 2006-10-16 17:53:53 by jwe]
jwe
parents: 5887
diff changeset
7448
20558
1a0a433c8263 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20465
diff changeset
7449 retval = do_remove (i, who);
6054
769b203ede37 [project @ 2006-10-16 17:53:53 by jwe]
jwe
parents: 5887
diff changeset
7450 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7451
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7452 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7453 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7454
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7455 void
8902
5d5db7a347c6 erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8773
diff changeset
7456 octave_stream_list::do_clear (bool flush)
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7457 {
8902
5d5db7a347c6 erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8773
diff changeset
7458 if (flush)
5d5db7a347c6 erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8773
diff changeset
7459 {
20764
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7460 // Flush stdout and stderr.
8902
5d5db7a347c6 erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8773
diff changeset
7461 list[1].flush ();
20764
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7462 list[2].flush ();
8902
5d5db7a347c6 erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8773
diff changeset
7463 }
5d5db7a347c6 erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8773
diff changeset
7464
20764
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7465 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
7466 {
8902
5d5db7a347c6 erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8773
diff changeset
7467 int fid = iter->first;
20764
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7468 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
7469 {
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7470 iter++;
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7471 continue;
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7472 }
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7473
8902
5d5db7a347c6 erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8773
diff changeset
7474 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
7475
7655b399abff eliminate trailing whitespace in source files we maintain
John W. Eaton <jwe@octave.org>
parents: 20764
diff changeset
7476 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
7477 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
7478
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7479 // 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
7480 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
7481 {
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7482 // 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
7483 iter++;
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7484 continue;
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7485 }
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7486
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7487 // 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
7488 if (os.is_valid ())
8902
5d5db7a347c6 erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8773
diff changeset
7489 os.close ();
20764
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7490
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7491 list.erase (iter++);
6757
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7492 }
20764
0ad449bc8848 Stop 'fclose ("all")' from closing down gnuplot pipes (bug #39504).
Rik <rik@octave.org>
parents: 20756
diff changeset
7493
8902
5d5db7a347c6 erase closed files from file list & cache lookup
Jaroslav Hajek <highegg@gmail.com>
parents: 8773
diff changeset
7494 lookup_cache = list.end ();
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
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7497 string_vector
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7498 octave_stream_list::do_get_info (int fid) const
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7499 {
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3337
diff changeset
7500 octave_stream os = do_lookup (fid);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7501
20892
c07bee629973 2015 Code Sprint: use ovl ().
Rik <rik@octave.org>
parents: 20831
diff changeset
7502 if (! os.is_valid ())
3341
14cfc9475fe4 [project @ 1999-11-05 08:11:58 by jwe]
jwe
parents: 3340
diff changeset
7503 ::error ("invalid file id = %d", fid);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7504
20892
c07bee629973 2015 Code Sprint: use ovl ().
Rik <rik@octave.org>
parents: 20831
diff changeset
7505 string_vector retval (3);
c07bee629973 2015 Code Sprint: use ovl ().
Rik <rik@octave.org>
parents: 20831
diff changeset
7506
c07bee629973 2015 Code Sprint: use ovl ().
Rik <rik@octave.org>
parents: 20831
diff changeset
7507 retval(0) = os.name ();
c07bee629973 2015 Code Sprint: use ovl ().
Rik <rik@octave.org>
parents: 20831
diff changeset
7508 retval(1) = octave_stream::mode_as_string (os.mode ());
c07bee629973 2015 Code Sprint: use ovl ().
Rik <rik@octave.org>
parents: 20831
diff changeset
7509 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
7510
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7511 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7512 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7513
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7514 string_vector
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7515 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
7516 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7517 int conv_err = 0;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7518
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7519 int int_fid = convert_to_valid_int (fid, conv_err);
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7520
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
7521 if (conv_err)
2915
649549662cf6 [project @ 1997-04-30 20:51:40 by jwe]
jwe
parents: 2902
diff changeset
7522 ::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
7523
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
7524 return do_get_info (int_fid);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7525 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7526
3536
e8fbc8c3d6d9 [project @ 2000-02-02 12:01:00 by jwe]
jwe
parents: 3535
diff changeset
7527 std::string
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7528 octave_stream_list::do_list_open_files (void) const
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7529 {
5765
7ba9ad1fec11 [project @ 2006-04-17 05:05:15 by jwe]
jwe
parents: 5760
diff changeset
7530 std::ostringstream buf;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7531
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7532 buf << "\n"
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7533 << " number mode arch name\n"
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7534 << " ------ ---- ---- ----\n";
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7535
6757
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7536 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
7537 {
6757
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7538 octave_stream os = p->second;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7539
4326
1cae4472c624 [project @ 2003-02-15 23:14:47 by jwe]
jwe
parents: 4312
diff changeset
7540 buf << " "
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7541 << std::setiosflags (std::ios::right)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7542 << std::setw (4) << p->first << " "
19123
c8f03ccdc9ee Align columns for freport() output.
Rik <rik@octave.org>
parents: 18676
diff changeset
7543 // 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
7544 << std::resetiosflags (std::ios::adjustfield)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7545 << std::setiosflags (std::ios::left)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7546 << std::setw (3)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7547 << octave_stream::mode_as_string (os.mode ())
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7548 << " "
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7549 << std::setw (9)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7550 << 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
7551 << " "
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7552 << os.name () << "\n";
2117
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
5765
7ba9ad1fec11 [project @ 2006-04-17 05:05:15 by jwe]
jwe
parents: 5760
diff changeset
7555 buf << "\n";
7ba9ad1fec11 [project @ 2006-04-17 05:05:15 by jwe]
jwe
parents: 5760
diff changeset
7556
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
7557 return buf.str ();
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7558 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7559
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7560 octave_value
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7561 octave_stream_list::do_open_file_numbers (void) const
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7562 {
6757
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7563 Matrix retval (1, list.size (), 0.0);
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7564
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7565 int num_open = 0;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7566
6757
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7567 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
7568 {
6757
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7569 // Skip stdin, stdout, and stderr.
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7570 if (p->first > 2 && p->second)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7571 retval(0,num_open++) = p->first;
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
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7574 retval.resize ((num_open > 0), num_open);
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7575
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7576 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7577 }
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 int
2609
12bc9d0a50b5 [project @ 1997-01-18 00:17:36 by jwe]
jwe
parents: 2601
diff changeset
7580 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
7581 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7582 int retval = -1;
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 if (fid.is_string ())
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7585 {
3523
b80bbb43a1a9 [project @ 2000-02-02 10:25:52 by jwe]
jwe
parents: 3503
diff changeset
7586 std::string nm = fid.string_value ();
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7587
6757
c3cd7d14fc5e [project @ 2007-06-25 19:06:42 by jwe]
jwe
parents: 6708
diff changeset
7588 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
7589 {
20931
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
7590 // stdin, stdout, and stderr are unnamed.
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7591 if (p->first > 2)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7592 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7593 octave_stream os = p->second;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7594
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7595 if (os && os.name () == nm)
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7596 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7597 retval = p->first;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7598 break;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7599 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7600 }
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7601 }
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7602 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7603 else
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7604 {
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7605 int conv_err = 0;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7606
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7607 int int_fid = convert_to_valid_int (fid, conv_err);
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7608
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7609 if (conv_err)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10293
diff changeset
7610 ::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
7611
69dcb58b9ada Clean up use of error() versus ::error() in stream-based code.
Rik <rik@octave.org>
parents: 20917
diff changeset
7612 retval = int_fid;
2117
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7613 }
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7614
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7615 return retval;
b240b2fce8ed [project @ 1996-05-10 07:20:36 by jwe]
jwe
parents:
diff changeset
7616 }