annotate libinterp/corefcn/file-io.cc @ 21992:03c692adf563

maint: strip trailing whitespace from files.
author John W. Eaton <jwe@octave.org>
date Wed, 29 Jun 2016 09:21:50 -0400
parents 80659e58609f
children 278fc29b69ca
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
1 /*
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
2
19697
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 19597
diff changeset
3 Copyright (C) 1993-2015 John W. Eaton
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
4
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
5 This file is part of Octave.
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
6
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
7 Octave is free software; you can redistribute it and/or modify it
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
8 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: 7001
diff changeset
9 Free Software Foundation; either version 3 of the License, or (at your
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
10 option) any later version.
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
11
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
12 Octave is distributed in the hope that it will be useful, but WITHOUT
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
15 for more details.
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
16
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
17 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: 7001
diff changeset
18 along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
19 <http://www.gnu.org/licenses/>.
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
20
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
21 */
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
22
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
23 // Originally written by John C. Campbell <jcc@bevo.che.wisc.edu>
1230
92609e161b29 [project @ 1995-04-10 01:08:57 by jwe]
jwe
parents: 1181
diff changeset
24 //
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
25 // Thomas Baier <baier@ci.tuwien.ac.at> added the original versions of
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
26 // the following functions:
1230
92609e161b29 [project @ 1995-04-10 01:08:57 by jwe]
jwe
parents: 1181
diff changeset
27 //
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
28 // popen
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
29 // pclose
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
30 // execute (now popen2.m)
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
31 // sync_system (now merged with system)
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
32 // async_system (now merged with system)
1230
92609e161b29 [project @ 1995-04-10 01:08:57 by jwe]
jwe
parents: 1181
diff changeset
33
9322
66fdc831c580 fix jwe email address
John W. Eaton <jwe@octave.org>
parents: 9236
diff changeset
34 // Extensively revised by John W. Eaton <jwe@octave.org>,
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
35 // April 1996.
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
36
21724
aba2e6293dd8 use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents: 21634
diff changeset
37 #if defined (HAVE_CONFIG_H)
21301
40de9f8f23a6 Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents: 21200
diff changeset
38 # include "config.h"
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
39 #endif
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
40
4797
19b8225bdaa2 [project @ 2004-02-23 22:10:31 by jwe]
jwe
parents: 4726
diff changeset
41 #include <cerrno>
19b8225bdaa2 [project @ 2004-02-23 22:10:31 by jwe]
jwe
parents: 4726
diff changeset
42 #include <cstdio>
1343
94bedeb289e5 [project @ 1995-09-04 00:29:21 by jwe]
jwe
parents: 1339
diff changeset
43
3503
d14c483b3c12 [project @ 2000-02-01 04:06:07 by jwe]
jwe
parents: 3491
diff changeset
44 #include <iostream>
15215
9020dddc925a use std::numeric_limits for integer max and min values
John W. Eaton <jwe@octave.org>
parents: 15195
diff changeset
45 #include <limits>
7336
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7097
diff changeset
46 #include <stack>
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
47 #include <string>
4726
14dc2267c343 [project @ 2004-01-23 20:04:35 by jwe]
jwe
parents: 4715
diff changeset
48 #include <vector>
1350
5f63d83dd694 [project @ 1995-09-05 07:37:59 by jwe]
jwe
parents: 1343
diff changeset
49
21724
aba2e6293dd8 use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents: 21634
diff changeset
50 #if defined (HAVE_ZLIB_H)
21200
fcac5dbbf9ed maint: Indent #ifdef blocks in libinterp.
Rik <rik@octave.org>
parents: 21127
diff changeset
51 # include <zlib.h>
5325
ecbe4aa87e51 [project @ 2005-04-29 20:48:35 by jwe]
jwe
parents: 5320
diff changeset
52 #endif
ecbe4aa87e51 [project @ 2005-04-29 20:48:35 by jwe]
jwe
parents: 5320
diff changeset
53
5102
b04b30d30c66 [project @ 2004-12-28 01:59:05 by jwe]
jwe
parents: 5095
diff changeset
54 #include "error.h"
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2902
diff changeset
55 #include "file-ops.h"
6159
645605133a6f [project @ 2006-11-14 02:55:05 by jwe]
jwe
parents: 6108
diff changeset
56 #include "file-stat.h"
5102
b04b30d30c66 [project @ 2004-12-28 01:59:05 by jwe]
jwe
parents: 5095
diff changeset
57 #include "lo-ieee.h"
21888
00f1249f2483 create wrappers for some gnulib functions
John W. Eaton <jwe@octave.org>
parents: 21852
diff changeset
58 #include "mkostemp-wrapper.h"
6159
645605133a6f [project @ 2006-11-14 02:55:05 by jwe]
jwe
parents: 6108
diff changeset
59 #include "oct-env.h"
8377
25bc2d31e1bf improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents: 8021
diff changeset
60 #include "oct-locbuf.h"
21991
80659e58609f provide wrapper for tmpfile (bug #48337)
John W. Eaton <jwe@octave.org>
parents: 21966
diff changeset
61 #include "tmpfile-wrapper.h"
21919
473414a91539 * file-io.cc (cleanup_tmp_files): Use octave_unlink_wrapper.
John W. Eaton <jwe@octave.org>
parents: 21911
diff changeset
62 #include "unistd-wrappers.h"
2926
66ef74ee5d9f [project @ 1997-05-05 03:20:52 by jwe]
jwe
parents: 2902
diff changeset
63
1352
19c10b8657d5 [project @ 1995-09-05 08:11:57 by jwe]
jwe
parents: 1350
diff changeset
64 #include "defun.h"
21126
ba0a4b4f021d include errwarn.h in files that use err_disabled_feature conditionally
John W. Eaton <jwe@octave.org>
parents: 21109
diff changeset
65 #include "errwarn.h"
6108
143b556ce725 [project @ 2006-10-27 01:45:54 by jwe]
jwe
parents: 6053
diff changeset
66 #include "file-io.h"
6159
645605133a6f [project @ 2006-11-14 02:55:05 by jwe]
jwe
parents: 6108
diff changeset
67 #include "load-path.h"
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
68 #include "oct-fstrm.h"
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
69 #include "oct-iostrm.h"
1377
44f5d41ff757 [project @ 1995-09-12 00:07:58 by jwe]
jwe
parents: 1363
diff changeset
70 #include "oct-map.h"
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
71 #include "oct-prcstrm.h"
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
72 #include "oct-stream.h"
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
73 #include "oct-strstrm.h"
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
74 #include "ov.h"
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
75 #include "ovl.h"
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
76 #include "pager.h"
444
ba637cc5c5f3 [project @ 1994-06-02 17:15:07 by jwe]
jwe
parents: 372
diff changeset
77 #include "sysdep.h"
1352
19c10b8657d5 [project @ 1995-09-05 08:11:57 by jwe]
jwe
parents: 1350
diff changeset
78 #include "utils.h"
2370
a45c7d64b5e3 [project @ 1996-10-11 23:51:15 by jwe]
jwe
parents: 2341
diff changeset
79 #include "variables.h"
1583
5383d2d4cf9d [project @ 1995-10-27 08:51:20 by jwe]
jwe
parents: 1488
diff changeset
80
2902
c5b7a019b9ed [project @ 1997-04-30 03:46:53 by jwe]
jwe
parents: 2875
diff changeset
81 static octave_value stdin_file;
c5b7a019b9ed [project @ 1997-04-30 03:46:53 by jwe]
jwe
parents: 2875
diff changeset
82 static octave_value stdout_file;
c5b7a019b9ed [project @ 1997-04-30 03:46:53 by jwe]
jwe
parents: 2875
diff changeset
83 static octave_value stderr_file;
c5b7a019b9ed [project @ 1997-04-30 03:46:53 by jwe]
jwe
parents: 2875
diff changeset
84
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4388
diff changeset
85 static octave_stream stdin_stream;
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4388
diff changeset
86 static octave_stream stdout_stream;
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4388
diff changeset
87 static octave_stream stderr_stream;
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4388
diff changeset
88
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
89 void
164
e2c950dd96d2 [project @ 1993-10-18 19:32:00 by jwe]
jwe
parents: 66
diff changeset
90 initialize_file_io (void)
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
91 {
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4388
diff changeset
92 stdin_stream = octave_istream::create (&std::cin, "stdin");
2116
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
93
3531
97cf542676e1 [project @ 2000-02-02 11:30:40 by jwe]
jwe
parents: 3523
diff changeset
94 // This uses octave_stdout (see pager.h), not std::cout so that Octave's
2116
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
95 // standard output stream will pass through the pager.
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
96
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4388
diff changeset
97 stdout_stream = octave_ostream::create (&octave_stdout, "stdout");
2116
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
98
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4388
diff changeset
99 stderr_stream = octave_ostream::create (&std::cerr, "stderr");
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
100
2902
c5b7a019b9ed [project @ 1997-04-30 03:46:53 by jwe]
jwe
parents: 2875
diff changeset
101 stdin_file = octave_stream_list::insert (stdin_stream);
c5b7a019b9ed [project @ 1997-04-30 03:46:53 by jwe]
jwe
parents: 2875
diff changeset
102 stdout_file = octave_stream_list::insert (stdout_stream);
c5b7a019b9ed [project @ 1997-04-30 03:46:53 by jwe]
jwe
parents: 2875
diff changeset
103 stderr_file = octave_stream_list::insert (stderr_stream);
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
104 }
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
105
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
106 void
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
107 close_files (void)
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
108 {
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
109 octave_stream_list::clear ();
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
110 }
636
fae2bd91c027 [project @ 1994-08-23 18:39:50 by jwe]
jwe
parents: 628
diff changeset
111
5102
b04b30d30c66 [project @ 2004-12-28 01:59:05 by jwe]
jwe
parents: 5095
diff changeset
112 // List of files to delete when we exit or crash.
b04b30d30c66 [project @ 2004-12-28 01:59:05 by jwe]
jwe
parents: 5095
diff changeset
113 //
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
114 // FIXME: this should really be static,
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
115 // but that causes problems on some systems.
5102
b04b30d30c66 [project @ 2004-12-28 01:59:05 by jwe]
jwe
parents: 5095
diff changeset
116 std::stack <std::string> tmp_files;
b04b30d30c66 [project @ 2004-12-28 01:59:05 by jwe]
jwe
parents: 5095
diff changeset
117
b04b30d30c66 [project @ 2004-12-28 01:59:05 by jwe]
jwe
parents: 5095
diff changeset
118 void
b04b30d30c66 [project @ 2004-12-28 01:59:05 by jwe]
jwe
parents: 5095
diff changeset
119 mark_for_deletion (const std::string& file)
b04b30d30c66 [project @ 2004-12-28 01:59:05 by jwe]
jwe
parents: 5095
diff changeset
120 {
b04b30d30c66 [project @ 2004-12-28 01:59:05 by jwe]
jwe
parents: 5095
diff changeset
121 tmp_files.push (file);
b04b30d30c66 [project @ 2004-12-28 01:59:05 by jwe]
jwe
parents: 5095
diff changeset
122 }
b04b30d30c66 [project @ 2004-12-28 01:59:05 by jwe]
jwe
parents: 5095
diff changeset
123
b04b30d30c66 [project @ 2004-12-28 01:59:05 by jwe]
jwe
parents: 5095
diff changeset
124 void
b04b30d30c66 [project @ 2004-12-28 01:59:05 by jwe]
jwe
parents: 5095
diff changeset
125 cleanup_tmp_files (void)
b04b30d30c66 [project @ 2004-12-28 01:59:05 by jwe]
jwe
parents: 5095
diff changeset
126 {
b04b30d30c66 [project @ 2004-12-28 01:59:05 by jwe]
jwe
parents: 5095
diff changeset
127 while (! tmp_files.empty ())
b04b30d30c66 [project @ 2004-12-28 01:59:05 by jwe]
jwe
parents: 5095
diff changeset
128 {
b04b30d30c66 [project @ 2004-12-28 01:59:05 by jwe]
jwe
parents: 5095
diff changeset
129 std::string filename = tmp_files.top ();
b04b30d30c66 [project @ 2004-12-28 01:59:05 by jwe]
jwe
parents: 5095
diff changeset
130 tmp_files.pop ();
21919
473414a91539 * file-io.cc (cleanup_tmp_files): Use octave_unlink_wrapper.
John W. Eaton <jwe@octave.org>
parents: 21911
diff changeset
131 octave_unlink_wrapper (filename.c_str ());
5102
b04b30d30c66 [project @ 2004-12-28 01:59:05 by jwe]
jwe
parents: 5095
diff changeset
132 }
b04b30d30c66 [project @ 2004-12-28 01:59:05 by jwe]
jwe
parents: 5095
diff changeset
133 }
b04b30d30c66 [project @ 2004-12-28 01:59:05 by jwe]
jwe
parents: 5095
diff changeset
134
16590
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
135 static void
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
136 normalize_fopen_mode (std::string& mode, bool& use_zlib)
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
137 {
16590
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
138 use_zlib = false;
896
4a2133b26fa7 [project @ 1994-11-09 16:04:06 by jwe]
jwe
parents: 870
diff changeset
139
16590
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
140 if (! mode.empty ())
368
5c987c27f3d7 [project @ 1994-02-16 08:33:09 by jwe]
jwe
parents: 367
diff changeset
141 {
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
142 // Could probably be faster, but does it really matter?
1766
e8e76be43e79 [project @ 1996-01-23 03:33:34 by jwe]
jwe
parents: 1755
diff changeset
143
16590
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
144 // Accept 'W', 'R', and 'A' as 'w', 'r', and 'a' but we warn about
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
145 // them because Matlab says they don't perform "automatic
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
146 // flushing" but we don't know precisely what action that implies.
7078
405cf85b435c [project @ 2007-10-30 14:04:59 by jwe]
jwe
parents: 7017
diff changeset
147
405cf85b435c [project @ 2007-10-30 14:04:59 by jwe]
jwe
parents: 7017
diff changeset
148 size_t pos = mode.find ('W');
405cf85b435c [project @ 2007-10-30 14:04:59 by jwe]
jwe
parents: 7017
diff changeset
149
8021
85184151822e fix typo in NPOS change
John W. Eaton <jwe@octave.org>
parents: 8019
diff changeset
150 if (pos != std::string::npos)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10259
diff changeset
151 {
16590
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
152 warning_with_id ("Octave:fopen-mode",
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
153 "fopen: treating mode \"W\" as equivalent to \"w\"");
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10259
diff changeset
154 mode[pos] = 'w';
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10259
diff changeset
155 }
7078
405cf85b435c [project @ 2007-10-30 14:04:59 by jwe]
jwe
parents: 7017
diff changeset
156
405cf85b435c [project @ 2007-10-30 14:04:59 by jwe]
jwe
parents: 7017
diff changeset
157 pos = mode.find ('R');
405cf85b435c [project @ 2007-10-30 14:04:59 by jwe]
jwe
parents: 7017
diff changeset
158
8021
85184151822e fix typo in NPOS change
John W. Eaton <jwe@octave.org>
parents: 8019
diff changeset
159 if (pos != std::string::npos)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10259
diff changeset
160 {
16590
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
161 warning_with_id ("Octave:fopen-mode",
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
162 "fopen: treating mode \"R\" as equivalent to \"r\"");
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10259
diff changeset
163 mode[pos] = 'r';
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10259
diff changeset
164 }
7078
405cf85b435c [project @ 2007-10-30 14:04:59 by jwe]
jwe
parents: 7017
diff changeset
165
16590
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
166 pos = mode.find ('A');
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
167
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
168 if (pos != std::string::npos)
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
169 {
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
170 warning_with_id ("Octave:fopen-mode",
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
171 "fopen: treating mode \"A\" as equivalent to \"a\"");
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
172 mode[pos] = 'a';
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
173 }
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
174
7078
405cf85b435c [project @ 2007-10-30 14:04:59 by jwe]
jwe
parents: 7017
diff changeset
175 pos = mode.find ('z');
5325
ecbe4aa87e51 [project @ 2005-04-29 20:48:35 by jwe]
jwe
parents: 5320
diff changeset
176
8021
85184151822e fix typo in NPOS change
John W. Eaton <jwe@octave.org>
parents: 8019
diff changeset
177 if (pos != std::string::npos)
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10259
diff changeset
178 {
5325
ecbe4aa87e51 [project @ 2005-04-29 20:48:35 by jwe]
jwe
parents: 5320
diff changeset
179 #if defined (HAVE_ZLIB)
16590
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
180 use_zlib = true;
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10259
diff changeset
181 mode.erase (pos, 1);
5325
ecbe4aa87e51 [project @ 2005-04-29 20:48:35 by jwe]
jwe
parents: 5320
diff changeset
182 #else
21109
bd1752782e56 Use err_disabled_feature, warn_disabled_feature throughout code base.
Rik <rik@octave.org>
parents: 21078
diff changeset
183 err_disabled_feature ("", "gzipped files (zlib)");
5325
ecbe4aa87e51 [project @ 2005-04-29 20:48:35 by jwe]
jwe
parents: 5320
diff changeset
184 #endif
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10259
diff changeset
185 }
5325
ecbe4aa87e51 [project @ 2005-04-29 20:48:35 by jwe]
jwe
parents: 5320
diff changeset
186
20561
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
187 // Use binary mode if 't' is not specified, but don't add
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
188 // 'b' if it is already present.
16590
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
189
20561
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
190 size_t bpos = mode.find ('b');
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
191 size_t tpos = mode.find ('t');
16590
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
192
20561
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
193 if (bpos == std::string::npos && tpos == std::string::npos)
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
194 mode += 'b';
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
195 }
16590
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
196 }
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
197
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
198 static std::ios::openmode
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
199 fopen_mode_to_ios_mode (const std::string& mode)
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
200 {
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
201 std::ios::openmode retval = std::ios::in;
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
202
20561
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
203 if (mode == "rt")
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
204 retval = std::ios::in;
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
205 else if (mode == "wt")
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
206 retval = std::ios::out | std::ios::trunc;
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
207 else if (mode == "at")
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
208 retval = std::ios::out | std::ios::app;
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
209 else if (mode == "r+t" || mode == "rt+")
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
210 retval = std::ios::in | std::ios::out;
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
211 else if (mode == "w+t" || mode == "wt+")
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
212 retval = std::ios::in | std::ios::out | std::ios::trunc;
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
213 else if (mode == "a+t" || mode == "at+")
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
214 retval = std::ios::in | std::ios::out | std::ios::app;
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
215 else if (mode == "rb" || mode == "r")
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
216 retval = std::ios::in | std::ios::binary;
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
217 else if (mode == "wb" || mode == "w")
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
218 retval = std::ios::out | std::ios::trunc | std::ios::binary;
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
219 else if (mode == "ab" || mode == "a")
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
220 retval = std::ios::out | std::ios::app | std::ios::binary;
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
221 else if (mode == "r+b" || mode == "rb+" || mode == "r+")
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
222 retval = std::ios::in | std::ios::out | std::ios::binary;
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
223 else if (mode == "w+b" || mode == "wb+" || mode == "w+")
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
224 retval = (std::ios::in | std::ios::out | std::ios::trunc
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
225 | std::ios::binary);
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
226 else if (mode == "a+b" || mode == "ab+" || mode == "a+")
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
227 retval = (std::ios::in | std::ios::out | std::ios::app
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
228 | std::ios::binary);
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
229 else
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
230 error ("invalid mode specified");
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
231
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
232 return retval;
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
233 }
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
234
1957
0e6a0a271998 [project @ 1996-02-15 01:08:15 by jwe]
jwe
parents: 1884
diff changeset
235 DEFUN (fclose, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
236 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
237 @deftypefn {} {} fclose (@var{fid})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
238 @deftypefnx {} {} fclose ("all")
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
239 @deftypefnx {} {@var{status} =} fclose ("all")
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
240 Close the file specified by the file descriptor @var{fid}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
241
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
242 If successful, @code{fclose} returns 0, otherwise, it returns -1. The
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
243 second form of the @code{fclose} call closes all open files except
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
244 @code{stdin}, @code{stdout}, @code{stderr}, and any FIDs associated
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
245 with gnuplot.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
246 @seealso{fopen, fflush, freport}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
247 @end deftypefn */)
529
7ea224e713cd [project @ 1994-07-20 18:54:27 by jwe]
jwe
parents: 506
diff changeset
248 {
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
249 if (args.length () != 1)
5823
080c08b192d8 [project @ 2006-05-19 05:32:17 by jwe]
jwe
parents: 5775
diff changeset
250 print_usage ();
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
251
21127
df7891224709 maint: Use "return ovl (...)" in DEFUN macros.
Rik <rik@octave.org>
parents: 21126
diff changeset
252 return ovl (octave_stream_list::remove (args(0), "fclose"));
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
253 }
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
254
5144
25b090e1be9f [project @ 2005-02-15 12:06:05 by jwe]
jwe
parents: 5109
diff changeset
255 DEFUN (fclear, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
256 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
257 @deftypefn {} {} fclear (@var{fid})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
258 Clear the stream state for the file specified by the file descriptor
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
259 @var{fid}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
260 @seealso{ferror, fopen}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
261 @end deftypefn */)
5144
25b090e1be9f [project @ 2005-02-15 12:06:05 by jwe]
jwe
parents: 5109
diff changeset
262 {
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
263 if (args.length () != 1)
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
264 print_usage ();
5144
25b090e1be9f [project @ 2005-02-15 12:06:05 by jwe]
jwe
parents: 5109
diff changeset
265
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
266 int fid = octave_stream_list::get_file_number (args(0));
5144
25b090e1be9f [project @ 2005-02-15 12:06:05 by jwe]
jwe
parents: 5109
diff changeset
267
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
268 octave_stream os = octave_stream_list::lookup (fid, "fclear");
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
269
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
270 os.clearerr ();
5144
25b090e1be9f [project @ 2005-02-15 12:06:05 by jwe]
jwe
parents: 5109
diff changeset
271
20941
a4f5da7c5463 maint: Replace "octave_value_list ()" with "ovl ()".
Rik <rik@octave.org>
parents: 20940
diff changeset
272 return ovl ();
5144
25b090e1be9f [project @ 2005-02-15 12:06:05 by jwe]
jwe
parents: 5109
diff changeset
273 }
25b090e1be9f [project @ 2005-02-15 12:06:05 by jwe]
jwe
parents: 5109
diff changeset
274
1957
0e6a0a271998 [project @ 1996-02-15 01:08:15 by jwe]
jwe
parents: 1884
diff changeset
275 DEFUN (fflush, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
276 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
277 @deftypefn {} {} fflush (@var{fid})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
278 Flush output to file descriptor @var{fid}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
279
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
280 @code{fflush} returns 0 on success and an OS dependent error value
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
281 (@minus{}1 on Unix) on error.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
282
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
283 Programming Note: Flushing is useful for ensuring that all pending output
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
284 makes it to the screen before some other event occurs. For example, it is
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
285 always a good idea to flush the standard output stream before calling
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
286 @code{input}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
287 @seealso{fopen, fclose}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
288 @end deftypefn */)
1181
b2b7c5264ac2 [project @ 1995-03-28 23:51:50 by jwe]
jwe
parents: 1086
diff changeset
289 {
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
290 if (args.length () != 1)
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
291 print_usage ();
1181
b2b7c5264ac2 [project @ 1995-03-28 23:51:50 by jwe]
jwe
parents: 1086
diff changeset
292
20918
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
293 octave_value retval = -1;
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
294
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
295 // FIXME: any way to avoid special case for stdout?
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
296 int fid = octave_stream_list::get_file_number (args(0));
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
297
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
298 if (fid == 1)
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
299 {
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
300 flush_octave_stdout ();
2609
12bc9d0a50b5 [project @ 1997-01-18 00:17:36 by jwe]
jwe
parents: 2599
diff changeset
301
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
302 retval = 0;
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
303 }
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
304 else
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
305 {
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
306 octave_stream os = octave_stream_list::lookup (fid, "fflush");
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
307
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
308 retval = os.flush ();
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
309 }
1181
b2b7c5264ac2 [project @ 1995-03-28 23:51:50 by jwe]
jwe
parents: 1086
diff changeset
310
b2b7c5264ac2 [project @ 1995-03-28 23:51:50 by jwe]
jwe
parents: 1086
diff changeset
311 return retval;
b2b7c5264ac2 [project @ 1995-03-28 23:51:50 by jwe]
jwe
parents: 1086
diff changeset
312 }
b2b7c5264ac2 [project @ 1995-03-28 23:51:50 by jwe]
jwe
parents: 1086
diff changeset
313
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
314 DEFUN (fgetl, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
315 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
316 @deftypefn {} {@var{str} =} fgetl (@var{fid})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
317 @deftypefnx {} {@var{str} =} fgetl (@var{fid}, @var{len})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
318 Read characters from a file, stopping after a newline, or EOF,
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
319 or @var{len} characters have been read.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
320
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
321 The characters read, excluding the possible trailing newline, are returned
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
322 as a string.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
323
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
324 If @var{len} is omitted, @code{fgetl} reads until the next newline
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
325 character.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
326
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
327 If there are no more characters to read, @code{fgetl} returns @minus{}1.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
328
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
329 To read a line and return the terminating newline see @code{fgets}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
330 @seealso{fgets, fscanf, fread, fopen}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
331 @end deftypefn */)
1339
e1dbc5585afd [project @ 1995-08-25 03:58:02 by jwe]
jwe
parents: 1338
diff changeset
332 {
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4388
diff changeset
333 static std::string who = "fgetl";
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4388
diff changeset
334
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
335 int nargin = args.length ();
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
336
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
337 if (nargin < 1 || nargin > 2)
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
338 print_usage ();
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
339
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
340 octave_stream os = octave_stream_list::lookup (args(0), who);
1339
e1dbc5585afd [project @ 1995-08-25 03:58:02 by jwe]
jwe
parents: 1338
diff changeset
341
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
342 octave_value len_arg = (nargin == 2) ? args(1) : octave_value ();
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
343
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
344 bool err = false;
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
345
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
346 std::string tmp = os.getl (len_arg, err, who);
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
347
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
348 if (! err)
20918
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
349 return ovl (tmp, tmp.length ());
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
350 else
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
351 return ovl (-1, 0);
1339
e1dbc5585afd [project @ 1995-08-25 03:58:02 by jwe]
jwe
parents: 1338
diff changeset
352 }
e1dbc5585afd [project @ 1995-08-25 03:58:02 by jwe]
jwe
parents: 1338
diff changeset
353
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
354 DEFUN (fgets, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
355 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
356 @deftypefn {} {@var{str} =} fgets (@var{fid})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
357 @deftypefnx {} {@var{str} =} fgets (@var{fid}, @var{len})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
358 Read characters from a file, stopping after a newline, or EOF,
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
359 or @var{len} characters have been read.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
360
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
361 The characters read, including the possible trailing newline, are returned
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
362 as a string.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
363
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
364 If @var{len} is omitted, @code{fgets} reads until the next newline
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
365 character.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
366
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
367 If there are no more characters to read, @code{fgets} returns @minus{}1.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
368
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
369 To read a line and discard the terminating newline see @code{fgetl}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
370 @seealso{fputs, fgetl, fscanf, fread, fopen}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
371 @end deftypefn */)
529
7ea224e713cd [project @ 1994-07-20 18:54:27 by jwe]
jwe
parents: 506
diff changeset
372 {
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4388
diff changeset
373 static std::string who = "fgets";
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4388
diff changeset
374
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
375 int nargin = args.length ();
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
376
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
377 if (nargin < 1 || nargin > 2)
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
378 print_usage ();
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
379
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
380 octave_stream os = octave_stream_list::lookup (args(0), who);
529
7ea224e713cd [project @ 1994-07-20 18:54:27 by jwe]
jwe
parents: 506
diff changeset
381
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
382 octave_value len_arg = (nargin == 2) ? args(1) : octave_value ();
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
383
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
384 bool err = false;
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
385
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
386 std::string tmp = os.gets (len_arg, err, who);
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
387
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
388 if (! err)
20918
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
389 return ovl (tmp, tmp.length ());
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
390 else
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
391 return ovl (-1.0, 0.0);
529
7ea224e713cd [project @ 1994-07-20 18:54:27 by jwe]
jwe
parents: 506
diff changeset
392 }
7ea224e713cd [project @ 1994-07-20 18:54:27 by jwe]
jwe
parents: 506
diff changeset
393
9701
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9322
diff changeset
394 DEFUN (fskipl, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
395 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
396 @deftypefn {} {@var{nlines} =} fskipl (@var{fid})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
397 @deftypefnx {} {@var{nlines} =} fskipl (@var{fid}, @var{count})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
398 @deftypefnx {} {@var{nlines} =} fskipl (@var{fid}, Inf)
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
399 Read and skip @var{count} lines from the file specified by the file
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
400 descriptor @var{fid}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
401
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
402 @code{fskipl} discards characters until an end-of-line is encountered
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
403 exactly @var{count}-times, or until the end-of-file marker is found.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
404
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
405 If @var{count} is omitted, it defaults to 1. @var{count} may also be
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
406 @code{Inf}, in which case lines are skipped until the end of the file.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
407 This form is suitable for counting the number of lines in a file.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
408
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
409 Returns the number of lines skipped (end-of-line sequences encountered).
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
410 @seealso{fgetl, fgets, fscanf, fopen}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
411 @end deftypefn */)
9701
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9322
diff changeset
412 {
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9322
diff changeset
413 static std::string who = "fskipl";
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9322
diff changeset
414
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9322
diff changeset
415 int nargin = args.length ();
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9322
diff changeset
416
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
417 if (nargin < 1 || nargin > 2)
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
418 print_usage ();
9701
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9322
diff changeset
419
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
420 octave_stream os = octave_stream_list::lookup (args(0), who);
9701
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9322
diff changeset
421
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
422 octave_value count_arg = (nargin == 2) ? args(1) : octave_value ();
9701
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9322
diff changeset
423
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
424 bool err = false;
9701
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9322
diff changeset
425
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
426 off_t tmp = os.skipl (count_arg, err, who);
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
427
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
428 if (! err)
20918
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
429 return ovl (tmp);
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
430 else
20941
a4f5da7c5463 maint: Replace "octave_value_list ()" with "ovl ()".
Rik <rik@octave.org>
parents: 20940
diff changeset
431 return ovl ();
9701
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9322
diff changeset
432 }
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9322
diff changeset
433
531280b07625 implement fskipl
Jaroslav Hajek <highegg@gmail.com>
parents: 9322
diff changeset
434
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3321
diff changeset
435 static octave_stream
16590
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
436 do_stream_open (const std::string& name, const std::string& mode_arg,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10259
diff changeset
437 const std::string& arch, int& fid)
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
438 {
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3321
diff changeset
439 octave_stream retval;
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
440
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
441 fid = -1;
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
442
16590
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
443 std::string mode = mode_arg;
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
444 bool use_zlib = false;
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
445 normalize_fopen_mode (mode, use_zlib);
2d968b7830d6 handle A, R, and W fopen modes correctly (bug #38851)
John W. Eaton <jwe@octave.org>
parents: 16308
diff changeset
446
4036
1432e11733d1 [project @ 2002-08-12 18:38:40 by jwe]
jwe
parents: 4028
diff changeset
447 std::ios::openmode md = fopen_mode_to_ios_mode (mode);
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
448
21739
c4ab2e54f100 use namespace for oct_mach_info class
John W. Eaton <jwe@octave.org>
parents: 21736
diff changeset
449 octave::mach_info::float_format flt_fmt =
c4ab2e54f100 use namespace for oct_mach_info class
John W. Eaton <jwe@octave.org>
parents: 21736
diff changeset
450 octave::mach_info::string_to_float_format (arch);
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
451
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
452 std::string fname = octave::sys::file_ops::tilde_expand (name);
6159
645605133a6f [project @ 2006-11-14 02:55:05 by jwe]
jwe
parents: 6108
diff changeset
453
21736
0504351a45e6 use namespace for file_stat classes
John W. Eaton <jwe@octave.org>
parents: 21733
diff changeset
454 octave::sys::file_stat fs (fname);
7730
b68e44c90afe file-io.cc (do_stream_open): return -1 for directories
John W. Eaton <jwe@octave.org>
parents: 7708
diff changeset
455
20561
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
456 if (! (md & std::ios::out))
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
457 fname = find_data_file_in_load_path ("fopen", fname);
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11572
diff changeset
458
20561
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
459 if (! fs.is_dir ())
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
460 {
6905
aee959a21c4b [project @ 2007-09-17 16:00:30 by jwe]
jwe
parents: 6838
diff changeset
461 #if defined (HAVE_ZLIB)
20561
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
462 if (use_zlib)
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
463 {
21942
aab79a1885cc limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents: 21919
diff changeset
464 FILE *fptr = std::fopen (fname.c_str (), mode.c_str ());
11004
594adb99a25e cache file id in octave_tstdiostream class
John W. Eaton <jwe@octave.org>
parents: 10840
diff changeset
465
20561
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
466 int fd = fileno (fptr);
11004
594adb99a25e cache file id in octave_tstdiostream class
John W. Eaton <jwe@octave.org>
parents: 10840
diff changeset
467
20561
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
468 gzFile gzf = ::gzdopen (fd, mode.c_str ());
5325
ecbe4aa87e51 [project @ 2005-04-29 20:48:35 by jwe]
jwe
parents: 5320
diff changeset
469
20561
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
470 if (fptr)
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
471 retval = octave_zstdiostream::create (fname, gzf, fd,
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
472 md, flt_fmt);
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
473 else
21942
aab79a1885cc limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents: 21919
diff changeset
474 retval.error (std::strerror (errno));
20561
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
475 }
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
476 else
5325
ecbe4aa87e51 [project @ 2005-04-29 20:48:35 by jwe]
jwe
parents: 5320
diff changeset
477 #endif
20561
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
478 {
21942
aab79a1885cc limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents: 21919
diff changeset
479 FILE *fptr = std::fopen (fname.c_str (), mode.c_str ());
5370
f07a9653b844 [project @ 2005-05-24 02:25:57 by jwe]
jwe
parents: 5353
diff changeset
480
20561
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
481 retval = octave_stdiostream::create (fname, fptr, md,
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
482 flt_fmt);
7730
b68e44c90afe file-io.cc (do_stream_open): return -1 for directories
John W. Eaton <jwe@octave.org>
parents: 7708
diff changeset
483
20561
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
484 if (! fptr)
21942
aab79a1885cc limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents: 21919
diff changeset
485 retval.error (std::strerror (errno));
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10259
diff changeset
486 }
20561
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
487
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
488 }
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
489
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
490 return retval;
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
491 }
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
492
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3321
diff changeset
493 static octave_stream
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
494 do_stream_open (const octave_value& tc_name, const octave_value& tc_mode,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10259
diff changeset
495 const octave_value& tc_arch, const char *fcn, int& fid)
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
496 {
3340
585a8809fd9b [project @ 1999-11-05 07:02:30 by jwe]
jwe
parents: 3321
diff changeset
497 octave_stream retval;
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
498
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
499 fid = -1;
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
500
20713
2469d78a1d8b Consistently use 'filename' rather than 'file name' throughout code base.
Rik <rik@octave.org>
parents: 20704
diff changeset
501 std::string name = tc_name.xstring_value ("%s: filename must be a string", fcn);
20700
68e3a747ca02 rename octave_value value extractors that accept error message args
John W. Eaton <jwe@octave.org>
parents: 20687
diff changeset
502 std::string mode = tc_mode.xstring_value ("%s: file mode must be a string", fcn);
68e3a747ca02 rename octave_value value extractors that accept error message args
John W. Eaton <jwe@octave.org>
parents: 20687
diff changeset
503 std::string arch = tc_arch.xstring_value ("%s: architecture type must be a string", fcn);
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
504
20581
fd0efcdb3718 use new string_value method to handle value extraction errors
John W. Eaton <jwe@octave.org>
parents: 20561
diff changeset
505 retval = do_stream_open (name, mode, arch, fid);
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
506
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
507 return retval;
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
508 }
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
509
11146
69b2f237060e file-io.cc (Ffopen): argument parsing tweak
John W. Eaton <jwe@octave.org>
parents: 11081
diff changeset
510 DEFUN (fopen, args, nargout,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
511 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
512 @deftypefn {} {@var{fid} =} fopen (@var{name})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
513 @deftypefnx {} {@var{fid} =} fopen (@var{name}, @var{mode})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
514 @deftypefnx {} {@var{fid} =} fopen (@var{name}, @var{mode}, @var{arch})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
515 @deftypefnx {} {[@var{fid}, @var{msg}] =} fopen (@dots{})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
516 @deftypefnx {} {@var{fid_list} =} fopen ("all")
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
517 @deftypefnx {} {[@var{file}, @var{mode}, @var{arch}] =} fopen (@var{fid})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
518 Open a file for low-level I/O or query open files and file descriptors.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
519
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
520 The first form of the @code{fopen} function opens the named file with
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
521 the specified mode (read-write, read-only, etc.) and architecture
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
522 interpretation (IEEE big endian, IEEE little endian, etc.), and returns
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
523 an integer value that may be used to refer to the file later. If an
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
524 error occurs, @var{fid} is set to @minus{}1 and @var{msg} contains the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
525 corresponding system error message. The @var{mode} is a one or two
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
526 character string that specifies whether the file is to be opened for
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
527 reading, writing, or both.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
528
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
529 The second form of the @code{fopen} function returns a vector of file ids
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
530 corresponding to all the currently open files, excluding the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
531 @code{stdin}, @code{stdout}, and @code{stderr} streams.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
532
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
533 The third form of the @code{fopen} function returns information about the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
534 open file given its file id.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
535
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
536 For example,
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
537
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
538 @example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
539 myfile = fopen ("splat.dat", "r", "ieee-le");
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
540 @end example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
541
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
542 @noindent
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
543 opens the file @file{splat.dat} for reading. If necessary, binary
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
544 numeric values will be read assuming they are stored in IEEE format with
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
545 the least significant bit first, and then converted to the native
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
546 representation.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
547
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
548 Opening a file that is already open simply opens it again and returns a
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
549 separate file id. It is not an error to open a file several times,
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
550 though writing to the same file through several different file ids may
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
551 produce unexpected results.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
552
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
553 The possible values @samp{mode} may have are
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
554
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
555 @table @asis
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
556 @item @samp{r} (default)
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
557 Open a file for reading.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
558
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
559 @item @samp{w}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
560 Open a file for writing. The previous contents are discarded.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
561
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
562 @item @samp{a}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
563 Open or create a file for writing at the end of the file.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
564
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
565 @item @samp{r+}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
566 Open an existing file for reading and writing.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
567
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
568 @item @samp{w+}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
569 Open a file for reading or writing. The previous contents are
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
570 discarded.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
571
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
572 @item @samp{a+}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
573 Open or create a file for reading or writing at the end of the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
574 file.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
575 @end table
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
576
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
577 Append a @qcode{"t"} to the mode string to open the file in text mode or a
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
578 @qcode{"b"} to open in binary mode. On Windows and Macintosh systems,
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
579 text mode reading and writing automatically converts linefeeds to the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
580 appropriate line end character for the system (carriage-return linefeed on
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
581 Windows, carriage-return on Macintosh). The default when no mode is
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
582 specified is binary mode.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
583
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
584 Additionally, you may append a @qcode{"z"} to the mode string to open a
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
585 gzipped file for reading or writing. For this to be successful, you
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
586 must also open the file in binary mode.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
587
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
588 The parameter @var{arch} is a string specifying the default data format
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
589 for the file. Valid values for @var{arch} are:
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
590
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
591 @table @asis
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
592 @item @qcode{"native"} or @qcode{"n"} (default)
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
593 The format of the current machine.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
594
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
595 @item @qcode{"ieee-be"} or @qcode{"b"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
596 IEEE big endian format.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
597
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
598 @item @qcode{"ieee-le"} or @qcode{"l"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
599 IEEE little endian format.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
600 @end table
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
601
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
602 @noindent
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
603 However, conversions are currently only supported for @samp{native},
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
604 @samp{ieee-be}, and @samp{ieee-le} formats.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
605
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
606 When opening a new file that does not yet exist, permissions will be set to
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
607 @code{0666 - @var{umask}}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
608 @seealso{fclose, fgets, fgetl, fscanf, fread, fputs, fdisp, fprintf, fwrite, fskipl, fseek, frewind, ftell, feof, ferror, fclear, fflush, freport, umask}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
609 @end deftypefn */)
529
7ea224e713cd [project @ 1994-07-20 18:54:27 by jwe]
jwe
parents: 506
diff changeset
610 {
7ea224e713cd [project @ 1994-07-20 18:54:27 by jwe]
jwe
parents: 506
diff changeset
611 int nargin = args.length ();
7ea224e713cd [project @ 1994-07-20 18:54:27 by jwe]
jwe
parents: 506
diff changeset
612
20861
6774cc464ea0 * file-io.cc (Ffopen): Fix nargin range check, fixes cset f2cd811f0f9e
Mike Miller <mtmiller@octave.org>
parents: 20853
diff changeset
613 if (nargin < 1 || nargin > 3)
20846
f2cd811f0f9e maint: Refactor C++ calls to print_usage to resemble m-files (2015 code sprint)
Mike Miller <mtmiller@octave.org>
parents: 20819
diff changeset
614 print_usage ();
f2cd811f0f9e maint: Refactor C++ calls to print_usage to resemble m-files (2015 code sprint)
Mike Miller <mtmiller@octave.org>
parents: 20819
diff changeset
615
20918
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
616 octave_value_list retval = ovl (-1.0);
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
617
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
618 if (nargin == 1)
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
619 {
12896
a19b50f6697f Correctly allow single string input form of fopen() (Bug #33535).
Rik <octave@nomad.inbox5.com>
parents: 12775
diff changeset
620 if (args(0).is_string ())
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10259
diff changeset
621 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10259
diff changeset
622 // If there is only one argument and it is a string but it
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10259
diff changeset
623 // is not the string "all", we assume it is a file to open
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10259
diff changeset
624 // with MODE = "r". To open a file called "all", you have
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10259
diff changeset
625 // to supply more than one argument.
12896
a19b50f6697f Correctly allow single string input form of fopen() (Bug #33535).
Rik <octave@nomad.inbox5.com>
parents: 12775
diff changeset
626 if (nargout < 2 && args(0).string_value () == "all")
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10259
diff changeset
627 return octave_stream_list::open_file_numbers ();
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10259
diff changeset
628 }
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
629 else
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10259
diff changeset
630 {
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10259
diff changeset
631 string_vector tmp = octave_stream_list::get_info (args(0));
529
7ea224e713cd [project @ 1994-07-20 18:54:27 by jwe]
jwe
parents: 506
diff changeset
632
20884
f1b2a2dbc0e1 2015 Code Sprint: use ovl () in C++ files.
José Luis García Pallero <jgpallero@gmail.com>
parents: 20861
diff changeset
633 retval = ovl (tmp(0), tmp(1), tmp(2));
3263
7d80b56e0dc8 [project @ 1999-08-20 19:10:09 by jwe]
jwe
parents: 3246
diff changeset
634
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10259
diff changeset
635 return retval;
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10259
diff changeset
636 }
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
637 }
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
638
20846
f2cd811f0f9e maint: Refactor C++ calls to print_usage to resemble m-files (2015 code sprint)
Mike Miller <mtmiller@octave.org>
parents: 20819
diff changeset
639 octave_value mode = (nargin == 2 || nargin == 3)
f2cd811f0f9e maint: Refactor C++ calls to print_usage to resemble m-files (2015 code sprint)
Mike Miller <mtmiller@octave.org>
parents: 20819
diff changeset
640 ? args(1) : octave_value ("r");
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
641
20846
f2cd811f0f9e maint: Refactor C++ calls to print_usage to resemble m-files (2015 code sprint)
Mike Miller <mtmiller@octave.org>
parents: 20819
diff changeset
642 octave_value arch = (nargin == 3)
f2cd811f0f9e maint: Refactor C++ calls to print_usage to resemble m-files (2015 code sprint)
Mike Miller <mtmiller@octave.org>
parents: 20819
diff changeset
643 ? args(2) : octave_value ("native");
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
644
20846
f2cd811f0f9e maint: Refactor C++ calls to print_usage to resemble m-files (2015 code sprint)
Mike Miller <mtmiller@octave.org>
parents: 20819
diff changeset
645 int fid = -1;
f2cd811f0f9e maint: Refactor C++ calls to print_usage to resemble m-files (2015 code sprint)
Mike Miller <mtmiller@octave.org>
parents: 20819
diff changeset
646
f2cd811f0f9e maint: Refactor C++ calls to print_usage to resemble m-files (2015 code sprint)
Mike Miller <mtmiller@octave.org>
parents: 20819
diff changeset
647 octave_stream os = do_stream_open (args(0), mode, arch, "fopen", fid);
5370
f07a9653b844 [project @ 2005-05-24 02:25:57 by jwe]
jwe
parents: 5353
diff changeset
648
20846
f2cd811f0f9e maint: Refactor C++ calls to print_usage to resemble m-files (2015 code sprint)
Mike Miller <mtmiller@octave.org>
parents: 20819
diff changeset
649 if (os)
20918
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
650 retval = ovl (octave_stream_list::insert (os), "");
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
651 else
20846
f2cd811f0f9e maint: Refactor C++ calls to print_usage to resemble m-files (2015 code sprint)
Mike Miller <mtmiller@octave.org>
parents: 20819
diff changeset
652 {
f2cd811f0f9e maint: Refactor C++ calls to print_usage to resemble m-files (2015 code sprint)
Mike Miller <mtmiller@octave.org>
parents: 20819
diff changeset
653 int error_number = 0;
f2cd811f0f9e maint: Refactor C++ calls to print_usage to resemble m-files (2015 code sprint)
Mike Miller <mtmiller@octave.org>
parents: 20819
diff changeset
654
20884
f1b2a2dbc0e1 2015 Code Sprint: use ovl () in C++ files.
José Luis García Pallero <jgpallero@gmail.com>
parents: 20861
diff changeset
655 retval = ovl (-1.0, os.error (false, error_number));
20846
f2cd811f0f9e maint: Refactor C++ calls to print_usage to resemble m-files (2015 code sprint)
Mike Miller <mtmiller@octave.org>
parents: 20819
diff changeset
656 }
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
657
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
658 return retval;
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
659 }
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
660
1957
0e6a0a271998 [project @ 1996-02-15 01:08:15 by jwe]
jwe
parents: 1884
diff changeset
661 DEFUN (freport, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
662 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
663 @deftypefn {} {} freport ()
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
664 Print a list of which files have been opened, and whether they are open
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
665 for reading, writing, or both.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
666
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
667 For example:
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
668
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
669 @example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
670 @group
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
671 freport ()
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
672
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
673 @print{} number mode arch name
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
674 @print{} ------ ---- ---- ----
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
675 @print{} 0 r ieee-le stdin
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
676 @print{} 1 w ieee-le stdout
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
677 @print{} 2 w ieee-le stderr
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
678 @print{} 3 r ieee-le myfile
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
679 @end group
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
680 @end example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
681 @seealso{fopen, fclose, is_valid_file_id}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
682 @end deftypefn */)
1181
b2b7c5264ac2 [project @ 1995-03-28 23:51:50 by jwe]
jwe
parents: 1086
diff changeset
683 {
20819
f428cbe7576f eliminate unnecessary uses of nargin
John W. Eaton <jwe@octave.org>
parents: 20812
diff changeset
684 if (args.length () > 0)
1181
b2b7c5264ac2 [project @ 1995-03-28 23:51:50 by jwe]
jwe
parents: 1086
diff changeset
685 warning ("freport: ignoring extra arguments");
b2b7c5264ac2 [project @ 1995-03-28 23:51:50 by jwe]
jwe
parents: 1086
diff changeset
686
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
687 octave_stdout << octave_stream_list::list_open_files ();
1181
b2b7c5264ac2 [project @ 1995-03-28 23:51:50 by jwe]
jwe
parents: 1086
diff changeset
688
21078
49852ff04747 maint: Remove unnecessary declarations of retval.
Rik <rik@octave.org>
parents: 21055
diff changeset
689 return ovl ();
1181
b2b7c5264ac2 [project @ 1995-03-28 23:51:50 by jwe]
jwe
parents: 1086
diff changeset
690 }
b2b7c5264ac2 [project @ 1995-03-28 23:51:50 by jwe]
jwe
parents: 1086
diff changeset
691
4715
7bed0ef1a6ad [project @ 2004-01-22 22:10:28 by jwe]
jwe
parents: 4649
diff changeset
692 DEFUN (frewind, args, nargout,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
693 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
694 @deftypefn {} {} frewind (@var{fid})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
695 @deftypefnx {} {@var{status} =} frewind (@var{fid})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
696 Move the file pointer to the beginning of the file specified by file
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
697 descriptor @var{fid}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
698
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
699 @code{frewind} returns 0 for success, and -1 if an error is encountered. It
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
700 is equivalent to @code{fseek (@var{fid}, 0, SEEK_SET)}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
701 @seealso{fseek, ftell, fopen}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
702 @end deftypefn */)
529
7ea224e713cd [project @ 1994-07-20 18:54:27 by jwe]
jwe
parents: 506
diff changeset
703 {
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
704 if (args.length () != 1)
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
705 print_usage ();
506
0f388340e607 [project @ 1994-07-09 06:10:34 by jwe]
jwe
parents: 497
diff changeset
706
20812
d9ca869ca124 maint: Clean-up more instances of print_usage().
Rik <rik@octave.org>
parents: 20805
diff changeset
707 int result = -1;
d9ca869ca124 maint: Clean-up more instances of print_usage().
Rik <rik@octave.org>
parents: 20805
diff changeset
708
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
709 octave_stream os = octave_stream_list::lookup (args(0), "frewind");
636
fae2bd91c027 [project @ 1994-08-23 18:39:50 by jwe]
jwe
parents: 628
diff changeset
710
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
711 result = os.rewind ();
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
712
4715
7bed0ef1a6ad [project @ 2004-01-22 22:10:28 by jwe]
jwe
parents: 4649
diff changeset
713 if (nargout > 0)
20918
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
714 return ovl (result);
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
715 else
20941
a4f5da7c5463 maint: Replace "octave_value_list ()" with "ovl ()".
Rik <rik@octave.org>
parents: 20940
diff changeset
716 return ovl ();
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
717 }
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
718
1957
0e6a0a271998 [project @ 1996-02-15 01:08:15 by jwe]
jwe
parents: 1884
diff changeset
719 DEFUN (fseek, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
720 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
721 @deftypefn {} {} fseek (@var{fid}, @var{offset})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
722 @deftypefnx {} {} fseek (@var{fid}, @var{offset}, @var{origin})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
723 @deftypefnx {} {@var{status} =} fseek (@dots{})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
724 Set the file pointer to the location @var{offset} within the file @var{fid}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
725
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
726 The pointer is positioned @var{offset} characters from the @var{origin},
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
727 which may be one of the predefined variables @w{@code{SEEK_CUR}} (current
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
728 position), @w{@code{SEEK_SET}} (beginning), or @w{@code{SEEK_END}} (end of
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
729 file) or strings @qcode{"cof"}, @qcode{"bof"} or @qcode{"eof"}. If
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
730 @var{origin} is omitted, @w{@code{SEEK_SET}} is assumed. @var{offset} may
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
731 be positive, negative, or zero but not all combinations of @var{origin} and
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
732 @var{offset} can be realized.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
733
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
734 @code{fseek} returns 0 on success and -1 on error.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
735 @seealso{fskipl, frewind, ftell, fopen}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
736 @end deftypefn */)
529
7ea224e713cd [project @ 1994-07-20 18:54:27 by jwe]
jwe
parents: 506
diff changeset
737 {
7ea224e713cd [project @ 1994-07-20 18:54:27 by jwe]
jwe
parents: 506
diff changeset
738 int nargin = args.length ();
7ea224e713cd [project @ 1994-07-20 18:54:27 by jwe]
jwe
parents: 506
diff changeset
739
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
740 if (nargin < 2 || nargin > 3)
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
741 print_usage ();
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
742
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
743 octave_stream os = octave_stream_list::lookup (args(0), "fseek");
1181
b2b7c5264ac2 [project @ 1995-03-28 23:51:50 by jwe]
jwe
parents: 1086
diff changeset
744
20918
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
745 octave_value origin_arg = (nargin == 3) ? args(2) : octave_value (-1.0);
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
746
21127
df7891224709 maint: Use "return ovl (...)" in DEFUN macros.
Rik <rik@octave.org>
parents: 21126
diff changeset
747 return ovl (os.seek (args(1), origin_arg));
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
748 }
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
749
1957
0e6a0a271998 [project @ 1996-02-15 01:08:15 by jwe]
jwe
parents: 1884
diff changeset
750 DEFUN (ftell, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
751 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
752 @deftypefn {} {@var{pos} =} ftell (@var{fid})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
753 Return the position of the file pointer as the number of characters from the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
754 beginning of the file specified by file descriptor @var{fid}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
755 @seealso{fseek, frewind, feof, fopen}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
756 @end deftypefn */)
1181
b2b7c5264ac2 [project @ 1995-03-28 23:51:50 by jwe]
jwe
parents: 1086
diff changeset
757 {
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
758 if (args.length () != 1)
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
759 print_usage ();
506
0f388340e607 [project @ 1994-07-09 06:10:34 by jwe]
jwe
parents: 497
diff changeset
760
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
761 octave_stream os = octave_stream_list::lookup (args(0), "ftell");
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
762
21127
df7891224709 maint: Use "return ovl (...)" in DEFUN macros.
Rik <rik@octave.org>
parents: 21126
diff changeset
763 return ovl (os.tell ());
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
764 }
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
765
3737
b736f8b8f0a1 [project @ 2000-11-16 18:01:08 by jwe]
jwe
parents: 3569
diff changeset
766 DEFUN (fprintf, args, nargout,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
767 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
768 @deftypefn {} {} fprintf (@var{fid}, @var{template}, @dots{})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
769 @deftypefnx {} {} fprintf (@var{template}, @dots{})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
770 @deftypefnx {} {@var{numbytes} =} fprintf (@dots{})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
771 This function is equivalent to @code{printf}, except that the output is
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
772 written to the file descriptor @var{fid} instead of @code{stdout}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
773
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
774 If @var{fid} is omitted, the output is written to @code{stdout} making the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
775 function exactly equivalent to @code{printf}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
776
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
777 The optional output returns the number of bytes written to the file.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
778
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
779 Implementation Note: For compatibility with @sc{matlab}, escape sequences in
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
780 the template string (e.g., @qcode{"@xbackslashchar{}n"} => newline) are
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
781 expanded even when the template string is defined with single quotes.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
782 @seealso{fputs, fdisp, fwrite, fscanf, printf, sprintf, fopen}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
783 @end deftypefn */)
1181
b2b7c5264ac2 [project @ 1995-03-28 23:51:50 by jwe]
jwe
parents: 1086
diff changeset
784 {
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4388
diff changeset
785 static std::string who = "fprintf";
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4388
diff changeset
786
1181
b2b7c5264ac2 [project @ 1995-03-28 23:51:50 by jwe]
jwe
parents: 1086
diff changeset
787 int nargin = args.length ();
b2b7c5264ac2 [project @ 1995-03-28 23:51:50 by jwe]
jwe
parents: 1086
diff changeset
788
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
789 if (! (nargin > 1 || (nargin > 0 && args(0).is_string ())))
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
790 print_usage ();
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
791
20918
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
792 int result;
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
793
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
794 octave_stream os;
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
795 int fmt_n = 0;
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
796
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
797 if (args(0).is_string ())
20918
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
798 os = octave_stream_list::lookup (1, who);
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
799 else
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
800 {
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
801 fmt_n = 1;
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
802 os = octave_stream_list::lookup (args(0), who);
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
803 }
2873
c3f7ed3874af [project @ 1997-04-23 04:01:02 by jwe]
jwe
parents: 2847
diff changeset
804
20918
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
805 if (! args(fmt_n).is_string ())
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
806 error ("%s: format TEMPLATE must be a string", who.c_str ());
1181
b2b7c5264ac2 [project @ 1995-03-28 23:51:50 by jwe]
jwe
parents: 1086
diff changeset
807
20918
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
808 octave_value_list tmp_args;
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
809
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
810 if (nargin > 1 + fmt_n)
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
811 {
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
812 tmp_args.resize (nargin-fmt_n-1, octave_value ());
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
813
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
814 for (int i = fmt_n + 1; i < nargin; i++)
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
815 tmp_args(i-fmt_n-1) = args(i);
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
816 }
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
817
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
818 result = os.printf (args(fmt_n), tmp_args, who);
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
819
4715
7bed0ef1a6ad [project @ 2004-01-22 22:10:28 by jwe]
jwe
parents: 4649
diff changeset
820 if (nargout > 0)
21127
df7891224709 maint: Use "return ovl (...)" in DEFUN macros.
Rik <rik@octave.org>
parents: 21126
diff changeset
821 return ovl (result);
20918
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
822 else
20941
a4f5da7c5463 maint: Replace "octave_value_list ()" with "ovl ()".
Rik <rik@octave.org>
parents: 20940
diff changeset
823 return ovl ();
1181
b2b7c5264ac2 [project @ 1995-03-28 23:51:50 by jwe]
jwe
parents: 1086
diff changeset
824 }
b2b7c5264ac2 [project @ 1995-03-28 23:51:50 by jwe]
jwe
parents: 1086
diff changeset
825
21634
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
826 DEFUN (printf, args, nargout,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
827 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
828 @deftypefn {} {} printf (@var{template}, @dots{})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
829 Print optional arguments under the control of the template string
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
830 @var{template} to the stream @code{stdout} and return the number of
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
831 characters printed.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
832 @ifclear OCTAVE_MANUAL
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
833
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
834 See the Formatted Output section of the GNU Octave manual for a
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
835 complete description of the syntax of the template string.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
836 @end ifclear
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
837
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
838 Implementation Note: For compatibility with @sc{matlab}, escape sequences in
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
839 the template string (e.g., @qcode{"@xbackslashchar{}n"} => newline) are
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
840 expanded even when the template string is defined with single quotes.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
841 @seealso{fprintf, sprintf, scanf}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
842 @end deftypefn */)
21634
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
843 {
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
844 static std::string who = "printf";
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
845
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
846 int nargin = args.length ();
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
847
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
848 if (nargin == 0)
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
849 print_usage ();
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
850
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
851 int result;
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
852
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
853 if (! args(0).is_string ())
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
854 error ("%s: format TEMPLATE must be a string", who.c_str ());
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
855
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
856 octave_value_list tmp_args;
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
857
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
858 if (nargin > 1)
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
859 {
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
860 tmp_args.resize (nargin-1, octave_value ());
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
861
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
862 for (int i = 1; i < nargin; i++)
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
863 tmp_args(i-1) = args(i);
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
864 }
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
865
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
866 result = stdout_stream.printf (args(0), tmp_args, who);
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
867
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
868 if (nargout > 0)
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
869 return ovl (result);
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
870 else
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
871 return ovl ();
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
872 }
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
873
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
874 DEFUN (fputs, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
875 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
876 @deftypefn {} {} fputs (@var{fid}, @var{string})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
877 @deftypefnx {} {@var{status} =} fputs (@var{fid}, @var{string})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
878 Write the string @var{string} to the file with file descriptor @var{fid}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
879
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
880 The string is written to the file with no additional formatting. Use
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
881 @code{fdisp} instead to automatically append a newline character appropriate
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
882 for the local machine.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
883
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
884 Return a non-negative number on success or EOF on error.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
885 @seealso{fdisp, fprintf, fwrite, fopen}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
886 @end deftypefn */)
21634
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
887 {
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
888 static std::string who = "fputs";
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
889
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
890 if (args.length () != 2)
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
891 print_usage ();
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
892
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
893 octave_stream os = octave_stream_list::lookup (args(0), who);
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
894
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
895 return ovl (os.puts (args(1), who));
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
896 }
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
897
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
898 DEFUN (puts, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
899 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
900 @deftypefn {} {} puts (@var{string})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
901 @deftypefnx {} {@var{status} =} puts (@var{string})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
902 Write a string to the standard output with no formatting.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
903
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
904 The string is written verbatim to the standard output. Use @code{disp} to
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
905 automatically append a newline character appropriate for the local machine.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
906
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
907 Return a non-negative number on success and EOF on error.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
908 @seealso{fputs, disp}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
909 @end deftypefn */)
21634
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
910 {
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
911 static std::string who = "puts";
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
912
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
913 if (args.length () != 1)
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
914 print_usage ();
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
915
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
916 return ovl (stdout_stream.puts (args(0), who));
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
917 }
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
918
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
919 DEFUN (sprintf, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
920 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
921 @deftypefn {} {} sprintf (@var{template}, @dots{})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
922 This is like @code{printf}, except that the output is returned as a
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
923 string.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
924
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
925 Unlike the C library function, which requires you to provide a suitably
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
926 sized string as an argument, Octave's @code{sprintf} function returns the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
927 string, automatically sized to hold all of the items converted.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
928
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
929 Implementation Note: For compatibility with @sc{matlab}, escape sequences in
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
930 the template string (e.g., @qcode{"@xbackslashchar{}n"} => newline) are
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
931 expanded even when the template string is defined with single quotes.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
932 @seealso{printf, fprintf, sscanf}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
933 @end deftypefn */)
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
934 {
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4388
diff changeset
935 static std::string who = "sprintf";
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4388
diff changeset
936
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
937 int nargin = args.length ();
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
938
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
939 if (nargin == 0)
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
940 print_usage ();
2116
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
941
21506
128703a29388 avoid some duplicate code in file-io functions
John W. Eaton <jwe@octave.org>
parents: 21504
diff changeset
942 // We don't use octave_ostrstream::create here because need direct
128703a29388 avoid some duplicate code in file-io functions
John W. Eaton <jwe@octave.org>
parents: 21504
diff changeset
943 // access to the OSTR object so that we can extract a string object
128703a29388 avoid some duplicate code in file-io functions
John W. Eaton <jwe@octave.org>
parents: 21504
diff changeset
944 // from it to return.
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
945 octave_ostrstream *ostr = new octave_ostrstream ();
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
946
21506
128703a29388 avoid some duplicate code in file-io functions
John W. Eaton <jwe@octave.org>
parents: 21504
diff changeset
947 // The octave_stream destructor will delete OSTR for us.
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
948 octave_stream os (ostr);
628
aecbe369233b [project @ 1994-08-19 13:27:51 by jwe]
jwe
parents: 610
diff changeset
949
20898
8da80da1ac37 maint: Use ovl() more places in the code.
Rik <rik@octave.org>
parents: 20884
diff changeset
950 if (! os.is_valid ())
8da80da1ac37 maint: Use ovl() more places in the code.
Rik <rik@octave.org>
parents: 20884
diff changeset
951 error ("%s: unable to create output buffer", who.c_str ());
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
952
20898
8da80da1ac37 maint: Use ovl() more places in the code.
Rik <rik@octave.org>
parents: 20884
diff changeset
953 octave_value fmt_arg = args(0);
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
954
20898
8da80da1ac37 maint: Use ovl() more places in the code.
Rik <rik@octave.org>
parents: 20884
diff changeset
955 if (! fmt_arg.is_string ())
8da80da1ac37 maint: Use ovl() more places in the code.
Rik <rik@octave.org>
parents: 20884
diff changeset
956 error ("%s: format TEMPLATE must be a string", who.c_str ());
628
aecbe369233b [project @ 1994-08-19 13:27:51 by jwe]
jwe
parents: 610
diff changeset
957
20898
8da80da1ac37 maint: Use ovl() more places in the code.
Rik <rik@octave.org>
parents: 20884
diff changeset
958 octave_value_list retval (3);
19727
3c3b9fce7d2b improve compatibility of sprintf function.
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
959
20898
8da80da1ac37 maint: Use ovl() more places in the code.
Rik <rik@octave.org>
parents: 20884
diff changeset
960 octave_value_list tmp_args;
8da80da1ac37 maint: Use ovl() more places in the code.
Rik <rik@octave.org>
parents: 20884
diff changeset
961 if (nargin > 1)
8da80da1ac37 maint: Use ovl() more places in the code.
Rik <rik@octave.org>
parents: 20884
diff changeset
962 {
8da80da1ac37 maint: Use ovl() more places in the code.
Rik <rik@octave.org>
parents: 20884
diff changeset
963 tmp_args.resize (nargin-1, octave_value ());
19727
3c3b9fce7d2b improve compatibility of sprintf function.
John W. Eaton <jwe@octave.org>
parents: 19697
diff changeset
964
20898
8da80da1ac37 maint: Use ovl() more places in the code.
Rik <rik@octave.org>
parents: 20884
diff changeset
965 for (int i = 1; i < nargin; i++)
8da80da1ac37 maint: Use ovl() more places in the code.
Rik <rik@octave.org>
parents: 20884
diff changeset
966 tmp_args(i-1) = args(i);
8da80da1ac37 maint: Use ovl() more places in the code.
Rik <rik@octave.org>
parents: 20884
diff changeset
967 }
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
968
20898
8da80da1ac37 maint: Use ovl() more places in the code.
Rik <rik@octave.org>
parents: 20884
diff changeset
969 // NOTE: Call to os.error must precede next call to ostr which might reset it.
8da80da1ac37 maint: Use ovl() more places in the code.
Rik <rik@octave.org>
parents: 20884
diff changeset
970 retval(2) = os.printf (fmt_arg, tmp_args, who);
8da80da1ac37 maint: Use ovl() more places in the code.
Rik <rik@octave.org>
parents: 20884
diff changeset
971 retval(1) = os.error ();
8da80da1ac37 maint: Use ovl() more places in the code.
Rik <rik@octave.org>
parents: 20884
diff changeset
972
8da80da1ac37 maint: Use ovl() more places in the code.
Rik <rik@octave.org>
parents: 20884
diff changeset
973 std::string result = ostr->str ();
8da80da1ac37 maint: Use ovl() more places in the code.
Rik <rik@octave.org>
parents: 20884
diff changeset
974 char type = fmt_arg.is_sq_string () ? '\'' : '"';
8da80da1ac37 maint: Use ovl() more places in the code.
Rik <rik@octave.org>
parents: 20884
diff changeset
975
8da80da1ac37 maint: Use ovl() more places in the code.
Rik <rik@octave.org>
parents: 20884
diff changeset
976 retval(0) = (result.empty () ? octave_value (charMatrix (1, 0), type)
8da80da1ac37 maint: Use ovl() more places in the code.
Rik <rik@octave.org>
parents: 20884
diff changeset
977 : octave_value (result, type));
1
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
978
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
979 return retval;
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
980 }
78fd87e624cb [project @ 1993-08-08 01:13:40 by jwe]
jwe
parents:
diff changeset
981
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
982 DEFUN (fscanf, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
983 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
984 @deftypefn {} {[@var{val}, @var{count}, @var{errmsg}] =} fscanf (@var{fid}, @var{template}, @var{size})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
985 @deftypefnx {} {[@var{v1}, @var{v2}, @dots{}, @var{count}, @var{errmsg}] =} fscanf (@var{fid}, @var{template}, "C")
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
986 In the first form, read from @var{fid} according to @var{template},
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
987 returning the result in the matrix @var{val}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
988
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
989 The optional argument @var{size} specifies the amount of data to read
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
990 and may be one of
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
991
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
992 @table @code
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
993 @item Inf
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
994 Read as much as possible, returning a column vector.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
995
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
996 @item @var{nr}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
997 Read up to @var{nr} elements, returning a column vector.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
998
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
999 @item [@var{nr}, Inf]
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1000 Read as much as possible, returning a matrix with @var{nr} rows. If the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1001 number of elements read is not an exact multiple of @var{nr}, the last
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1002 column is padded with zeros.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1003
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1004 @item [@var{nr}, @var{nc}]
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1005 Read up to @code{@var{nr} * @var{nc}} elements, returning a matrix with
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1006 @var{nr} rows. If the number of elements read is not an exact multiple
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1007 of @var{nr}, the last column is padded with zeros.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1008 @end table
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1009
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1010 @noindent
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1011 If @var{size} is omitted, a value of @code{Inf} is assumed.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1012
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1013 A string is returned if @var{template} specifies only character conversions.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1014
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1015 The number of items successfully read is returned in @var{count}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1016
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1017 If an error occurs, @var{errmsg} contains a system-dependent error message.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1018
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1019 In the second form, read from @var{fid} according to @var{template},
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1020 with each conversion specifier in @var{template} corresponding to a
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1021 single scalar return value. This form is more ``C-like'', and also
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1022 compatible with previous versions of Octave. The number of successful
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1023 conversions is returned in @var{count}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1024 @ifclear OCTAVE_MANUAL
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1025
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1026 See the Formatted Input section of the GNU Octave manual for a
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1027 complete description of the syntax of the template string.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1028 @end ifclear
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1029 @seealso{fgets, fgetl, fread, scanf, sscanf, fopen}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1030 @end deftypefn */)
1181
b2b7c5264ac2 [project @ 1995-03-28 23:51:50 by jwe]
jwe
parents: 1086
diff changeset
1031 {
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4388
diff changeset
1032 static std::string who = "fscanf";
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4388
diff changeset
1033
1181
b2b7c5264ac2 [project @ 1995-03-28 23:51:50 by jwe]
jwe
parents: 1086
diff changeset
1034 int nargin = args.length ();
b2b7c5264ac2 [project @ 1995-03-28 23:51:50 by jwe]
jwe
parents: 1086
diff changeset
1035
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
1036 if (nargin < 2 || nargin > 3)
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
1037 print_usage ();
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
1038
20918
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
1039 octave_value_list retval;
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
1040
21506
128703a29388 avoid some duplicate code in file-io functions
John W. Eaton <jwe@octave.org>
parents: 21504
diff changeset
1041 octave_stream os = octave_stream_list::lookup (args(0), who);
128703a29388 avoid some duplicate code in file-io functions
John W. Eaton <jwe@octave.org>
parents: 21504
diff changeset
1042
128703a29388 avoid some duplicate code in file-io functions
John W. Eaton <jwe@octave.org>
parents: 21504
diff changeset
1043 if (! args(1).is_string ())
128703a29388 avoid some duplicate code in file-io functions
John W. Eaton <jwe@octave.org>
parents: 21504
diff changeset
1044 error ("%s: format TEMPLATE must be a string", who.c_str ());
128703a29388 avoid some duplicate code in file-io functions
John W. Eaton <jwe@octave.org>
parents: 21504
diff changeset
1045
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2189
diff changeset
1046 if (nargin == 3 && args(2).is_string ())
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
1047 {
20918
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
1048 retval = ovl (os.oscanf (args(1), who));
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
1049 }
1181
b2b7c5264ac2 [project @ 1995-03-28 23:51:50 by jwe]
jwe
parents: 1086
diff changeset
1050 else
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2189
diff changeset
1051 {
20918
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
1052 octave_idx_type count = 0;
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2189
diff changeset
1053
20918
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
1054 Array<double> size = (nargin == 3)
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
1055 ? args(2).vector_value ()
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
1056 : Array<double> (dim_vector (1, 1),
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
1057 lo_ieee_inf_value ());
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2189
diff changeset
1058
20918
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
1059 octave_value tmp = os.scanf (args(1), size, count, who);
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
1060
20918
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
1061 retval = ovl (tmp, count, os.error ());
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2189
diff changeset
1062 }
1181
b2b7c5264ac2 [project @ 1995-03-28 23:51:50 by jwe]
jwe
parents: 1086
diff changeset
1063
b2b7c5264ac2 [project @ 1995-03-28 23:51:50 by jwe]
jwe
parents: 1086
diff changeset
1064 return retval;
b2b7c5264ac2 [project @ 1995-03-28 23:51:50 by jwe]
jwe
parents: 1086
diff changeset
1065 }
b2b7c5264ac2 [project @ 1995-03-28 23:51:50 by jwe]
jwe
parents: 1086
diff changeset
1066
13194
3e1871badab9 allow sscanf to accept character arrays with more than one row
John W. Eaton <jwe@octave.org>
parents: 12998
diff changeset
1067 static std::string
21507
2cdbae31c022 improve compatibility of textscan for character arrays
John W. Eaton <jwe@octave.org>
parents: 21506
diff changeset
1068 get_scan_string_data (const octave_value& val, const std::string& who)
13194
3e1871badab9 allow sscanf to accept character arrays with more than one row
John W. Eaton <jwe@octave.org>
parents: 12998
diff changeset
1069 {
3e1871badab9 allow sscanf to accept character arrays with more than one row
John W. Eaton <jwe@octave.org>
parents: 12998
diff changeset
1070 std::string retval;
3e1871badab9 allow sscanf to accept character arrays with more than one row
John W. Eaton <jwe@octave.org>
parents: 12998
diff changeset
1071
20962
3aa293be0e8d maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents: 20956
diff changeset
1072 if (! val.is_string ())
21507
2cdbae31c022 improve compatibility of textscan for character arrays
John W. Eaton <jwe@octave.org>
parents: 21506
diff changeset
1073 error ("%s: argument STRING must be a string", who.c_str ());
13194
3e1871badab9 allow sscanf to accept character arrays with more than one row
John W. Eaton <jwe@octave.org>
parents: 12998
diff changeset
1074
20962
3aa293be0e8d maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents: 20956
diff changeset
1075 octave_value tmp = val.reshape (dim_vector (1, val.numel ()));
3aa293be0e8d maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents: 20956
diff changeset
1076
3aa293be0e8d maint: Invert simple conditionals in if/else/error paradigm.
Rik <rik@octave.org>
parents: 20956
diff changeset
1077 retval = tmp.string_value ();
13194
3e1871badab9 allow sscanf to accept character arrays with more than one row
John W. Eaton <jwe@octave.org>
parents: 12998
diff changeset
1078
3e1871badab9 allow sscanf to accept character arrays with more than one row
John W. Eaton <jwe@octave.org>
parents: 12998
diff changeset
1079 return retval;
3e1871badab9 allow sscanf to accept character arrays with more than one row
John W. Eaton <jwe@octave.org>
parents: 12998
diff changeset
1080 }
3e1871badab9 allow sscanf to accept character arrays with more than one row
John W. Eaton <jwe@octave.org>
parents: 12998
diff changeset
1081
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
1082 DEFUN (sscanf, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1083 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1084 @deftypefn {} {[@var{val}, @var{count}, @var{errmsg}, @var{pos}] =} sscanf (@var{string}, @var{template}, @var{size})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1085 @deftypefnx {} {[@var{v1}, @var{v2}, @dots{}, @var{count}, @var{errmsg}] =} sscanf (@var{string}, @var{template}, "C")
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1086 This is like @code{fscanf}, except that the characters are taken from the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1087 string @var{string} instead of from a stream.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1088
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1089 Reaching the end of the string is treated as an end-of-file condition. In
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1090 addition to the values returned by @code{fscanf}, the index of the next
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1091 character to be read is returned in @var{pos}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1092 @seealso{fscanf, scanf, sprintf}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1093 @end deftypefn */)
444
ba637cc5c5f3 [project @ 1994-06-02 17:15:07 by jwe]
jwe
parents: 372
diff changeset
1094 {
4468
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4388
diff changeset
1095 static std::string who = "sscanf";
efb6301dae80 [project @ 2003-07-15 19:18:20 by jwe]
jwe
parents: 4388
diff changeset
1096
506
0f388340e607 [project @ 1994-07-09 06:10:34 by jwe]
jwe
parents: 497
diff changeset
1097 int nargin = args.length ();
0f388340e607 [project @ 1994-07-09 06:10:34 by jwe]
jwe
parents: 497
diff changeset
1098
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
1099 if (nargin < 2 || nargin > 3)
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
1100 print_usage ();
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
1101
20918
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
1102 octave_value_list retval;
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
1103
21507
2cdbae31c022 improve compatibility of textscan for character arrays
John W. Eaton <jwe@octave.org>
parents: 21506
diff changeset
1104 std::string data = get_scan_string_data (args(0), who);
21506
128703a29388 avoid some duplicate code in file-io functions
John W. Eaton <jwe@octave.org>
parents: 21504
diff changeset
1105
128703a29388 avoid some duplicate code in file-io functions
John W. Eaton <jwe@octave.org>
parents: 21504
diff changeset
1106 octave_stream os = octave_istrstream::create (data);
128703a29388 avoid some duplicate code in file-io functions
John W. Eaton <jwe@octave.org>
parents: 21504
diff changeset
1107
128703a29388 avoid some duplicate code in file-io functions
John W. Eaton <jwe@octave.org>
parents: 21504
diff changeset
1108 if (! os.is_valid ())
128703a29388 avoid some duplicate code in file-io functions
John W. Eaton <jwe@octave.org>
parents: 21504
diff changeset
1109 error ("%s: unable to create temporary input buffer", who.c_str ());
128703a29388 avoid some duplicate code in file-io functions
John W. Eaton <jwe@octave.org>
parents: 21504
diff changeset
1110
128703a29388 avoid some duplicate code in file-io functions
John W. Eaton <jwe@octave.org>
parents: 21504
diff changeset
1111 if (! args(1).is_string ())
128703a29388 avoid some duplicate code in file-io functions
John W. Eaton <jwe@octave.org>
parents: 21504
diff changeset
1112 error ("%s: format TEMPLATE must be a string", who.c_str ());
128703a29388 avoid some duplicate code in file-io functions
John W. Eaton <jwe@octave.org>
parents: 21504
diff changeset
1113
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2189
diff changeset
1114 if (nargin == 3 && args(2).is_string ())
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
1115 {
20918
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
1116 retval = ovl (os.oscanf (args(1), who));
444
ba637cc5c5f3 [project @ 1994-06-02 17:15:07 by jwe]
jwe
parents: 372
diff changeset
1117 }
ba637cc5c5f3 [project @ 1994-06-02 17:15:07 by jwe]
jwe
parents: 372
diff changeset
1118 else
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2189
diff changeset
1119 {
20918
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
1120 octave_idx_type count = 0;
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2189
diff changeset
1121
20918
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
1122 Array<double> size = (nargin == 3) ? args(2).vector_value ()
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
1123 : Array<double> (dim_vector (1, 1),
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
1124 lo_ieee_inf_value ());
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
1125
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
1126 octave_value tmp = os.scanf (args(1), size, count, who);
20561
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
1127
20918
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
1128 // FIXME: is this the right thing to do?
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
1129 // Extract error message first, because getting
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
1130 // position will clear it.
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
1131 std::string errmsg = os.error ();
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
1132
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
1133 retval = ovl (tmp, count, errmsg,
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
1134 (os.eof () ? data.length () : os.tell ()) + 1);
2215
ab0e1fd337f5 [project @ 1996-05-16 15:56:40 by jwe]
jwe
parents: 2189
diff changeset
1135 }
444
ba637cc5c5f3 [project @ 1994-06-02 17:15:07 by jwe]
jwe
parents: 372
diff changeset
1136
ba637cc5c5f3 [project @ 1994-06-02 17:15:07 by jwe]
jwe
parents: 372
diff changeset
1137 return retval;
ba637cc5c5f3 [project @ 1994-06-02 17:15:07 by jwe]
jwe
parents: 372
diff changeset
1138 }
ba637cc5c5f3 [project @ 1994-06-02 17:15:07 by jwe]
jwe
parents: 372
diff changeset
1139
21634
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
1140 DEFUN (scanf, args, nargout,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1141 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1142 @deftypefn {} {[@var{val}, @var{count}, @var{errmsg}] =} scanf (@var{template}, @var{size})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1143 @deftypefnx {} {[@var{v1}, @var{v2}, @dots{}, @var{count}, @var{errmsg}]] =} scanf (@var{template}, "C")
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1144 This is equivalent to calling @code{fscanf} with @var{fid} = @code{stdin}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1145
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1146 It is currently not useful to call @code{scanf} in interactive programs.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1147 @seealso{fscanf, sscanf, printf}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1148 @end deftypefn */)
21634
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
1149 {
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
1150 int nargin = args.length ();
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
1151
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
1152 octave_value_list tmp_args (nargin+1, octave_value ());
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
1153
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
1154 tmp_args (0) = 0.0;
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
1155 for (int i = 0; i < nargin; i++)
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
1156 tmp_args(i+1) = args(i);
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
1157
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
1158 return Ffscanf (tmp_args, nargout);
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
1159 }
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
1160
21504
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1161 static octave_value_list
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1162 textscan_internal (const std::string& who, const octave_value_list& args)
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1163 {
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1164 if (args.length () < 1)
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1165 print_usage (who);
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1166
21507
2cdbae31c022 improve compatibility of textscan for character arrays
John W. Eaton <jwe@octave.org>
parents: 21506
diff changeset
1167 octave_stream os;
2cdbae31c022 improve compatibility of textscan for character arrays
John W. Eaton <jwe@octave.org>
parents: 21506
diff changeset
1168
2cdbae31c022 improve compatibility of textscan for character arrays
John W. Eaton <jwe@octave.org>
parents: 21506
diff changeset
1169 if (args(0).is_string ())
2cdbae31c022 improve compatibility of textscan for character arrays
John W. Eaton <jwe@octave.org>
parents: 21506
diff changeset
1170 {
2cdbae31c022 improve compatibility of textscan for character arrays
John W. Eaton <jwe@octave.org>
parents: 21506
diff changeset
1171 std::string data = get_scan_string_data (args(0), who);
2cdbae31c022 improve compatibility of textscan for character arrays
John W. Eaton <jwe@octave.org>
parents: 21506
diff changeset
1172
2cdbae31c022 improve compatibility of textscan for character arrays
John W. Eaton <jwe@octave.org>
parents: 21506
diff changeset
1173 os = octave_istrstream::create (data);
2cdbae31c022 improve compatibility of textscan for character arrays
John W. Eaton <jwe@octave.org>
parents: 21506
diff changeset
1174
2cdbae31c022 improve compatibility of textscan for character arrays
John W. Eaton <jwe@octave.org>
parents: 21506
diff changeset
1175 if (! os.is_valid ())
2cdbae31c022 improve compatibility of textscan for character arrays
John W. Eaton <jwe@octave.org>
parents: 21506
diff changeset
1176 error ("%s: unable to create temporary input buffer", who.c_str ());
2cdbae31c022 improve compatibility of textscan for character arrays
John W. Eaton <jwe@octave.org>
parents: 21506
diff changeset
1177 }
2cdbae31c022 improve compatibility of textscan for character arrays
John W. Eaton <jwe@octave.org>
parents: 21506
diff changeset
1178 else
2cdbae31c022 improve compatibility of textscan for character arrays
John W. Eaton <jwe@octave.org>
parents: 21506
diff changeset
1179 os =octave_stream_list::lookup (args(0), who);
21504
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1180
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1181 int nskip = 1;
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1182
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1183 std::string fmt;
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1184
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1185 if (args.length () == 1)
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1186 {
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1187 // ommited format = %f. explicit "" = width from file
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1188 fmt = "%f";
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1189 }
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1190 else if (args(1).is_string ())
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1191 {
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1192 fmt = args(1).string_value ();
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1193
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1194 if (args(1).is_sq_string ())
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1195 fmt = do_string_escapes (fmt);
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1196
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1197 nskip++;
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1198 }
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1199 else
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1200 error ("%s: FORMAT must be a string", who.c_str ());
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1201
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1202 octave_idx_type ntimes = -1;
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1203
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1204 if (args.length () > 2)
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1205 {
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1206 if (args(2).is_numeric_type ())
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1207 {
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1208 ntimes = args(2).idx_type_value ();
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1209
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1210 if (ntimes < args(2).double_value ())
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1211 error ("%s: REPEAT = %g is too large",
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1212 who.c_str (), args(2).double_value ());
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1213
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1214 nskip++;
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1215 }
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1216 }
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1217
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1218 octave_value_list options = args.splice (0, nskip);
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1219
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1220 octave_idx_type count = 0;
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1221
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1222 octave_value result = os.textscan (fmt, ntimes, options, who, count);
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1223
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1224 std::string errmsg = os.error ();
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1225
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1226 return ovl (result, count, errmsg);
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1227 }
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1228
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1229 DEFUN (textscan, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1230 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1231 @deftypefn {} {@var{C} =} textscan (@var{fid}, @var{format})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1232 @deftypefnx {} {@var{C} =} textscan (@var{fid}, @var{format}, @var{repeat})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1233 @deftypefnx {} {@var{C} =} textscan (@var{fid}, @var{format}, @var{param}, @var{value}, @dots{})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1234 @deftypefnx {} {@var{C} =} textscan (@var{fid}, @var{format}, @var{repeat}, @var{param}, @var{value}, @dots{})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1235 @deftypefnx {} {@var{C} =} textscan (@var{str}, @dots{})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1236 @deftypefnx {} {[@var{C}, @var{position}, @var{errmsg}] =} textscan (@dots{})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1237 Read data from a text file or string.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1238
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1239 The string @var{str} or file associated with @var{fid} is read from and
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1240 parsed according to @var{format}. The function is an extension of
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1241 @code{strread} and @code{textread}. Differences include: the ability to
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1242 read from either a file or a string, additional options, and additional
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1243 format specifiers.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1244
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1245 The input is interpreted as a sequence of words, delimiters (such as
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1246 whitespace), and literals. The characters that form delimiters and
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1247 whitespace are determined by the options. The format consists of format
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1248 specifiers interspersed between literals. In the format, whitespace forms
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1249 a delimiter between consecutive literals, but is otherwise ignored.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1250
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1251 The output @var{C} is a cell array where the number of columns is determined
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1252 by the number of format specifiers.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1253
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1254 The first word of the input is matched to the first specifier of the format
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1255 and placed in the first column of the output; the second is matched to the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1256 second specifier and placed in the second column and so forth. If there
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1257 are more words than specifiers then the process is repeated until all words
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1258 have been processed or the limit imposed by @var{repeat} has been met (see
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1259 below).
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1260
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1261 The string @var{format} describes how the words in @var{str} should be
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1262 parsed. As in @var{fscanf}, any (non-whitespace) text in the format that is
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1263 not one of these specifiers is considered a literal. If there is a literal
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1264 between two format specifiers then that same literal must appear in the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1265 input stream between the matching words.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1266
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1267 The following specifiers are valid:
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1268
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1269 @table @code
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1270 @item %f
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1271 @itemx %f64
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1272 @itemx %n
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1273 The word is parsed as a number and converted to double.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1274
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1275 @item %f32
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1276 The word is parsed as a number and converted to single (float).
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1277
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1278 @item %d
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1279 @itemx %d8
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1280 @itemx %d16
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1281 @itemx %d32
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1282 @itemx %d64
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1283 The word is parsed as a number and converted to int8, int16, int32, or
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1284 int64. If no size is specified then int32 is used.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1285
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1286 @item %u
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1287 @itemx %u8
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1288 @itemx %u16
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1289 @itemx %u32
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1290 @itemx %u64
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1291 The word is parsed as a number and converted to uint8, uint16, uint32, or
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1292 uint64. If no size is specified then uint32 is used.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1293
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1294 @item %s
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1295 The word is parsed as a string ending at the last character before
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1296 whitespace, an end-of-line, or a delimiter specified in the options.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1297
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1298 @item %q
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1299 The word is parsed as a "quoted string".
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1300 If the first character of the string is a double quote (") then the string
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1301 includes everything until a matching double quote---including whitespace,
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1302 delimiters, and end-of-line characters. If a pair of consecutive double
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1303 quotes appears in the input, it is replaced in the output by a single
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1304 double quote. For examples, the input "He said ""Hello""" would
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1305 return the value 'He said "Hello"'.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1306
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1307 @item %c
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1308 The next character of the input is read.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1309 This includes delimiters, whitespace, and end-of-line characters.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1310
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1311 @item %[@dots{}]
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1312 @itemx %[^@dots{}]
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1313 In the first form, the word consists of the longest run consisting of only
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1314 characters between the brackets. Ranges of characters can be specified by
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1315 a hyphen; for example, %[0-9a-zA-Z] matches all alphanumeric characters (if
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1316 the underlying character set is ASCII). Since @sc{matlab} treats hyphens
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1317 literally, this expansion only applies to alphanumeric characters. To
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1318 include '-' in the set, it should appear first or last in the brackets; to
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1319 include ']', it should be the first character. If the first character is
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1320 '^' then the word consists of characters @strong{not} listed.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1321
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1322 @item %N@dots{}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1323 For %s, %c %d, %f, %n, %u, an optional width can be specified as %Ns, etc.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1324 where N is an integer > 1. For %c, this causes exactly N characters to be
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1325 read instead of a single character. For the other specifiers, it is an
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1326 upper bound on the number of characters read; normal delimiters can cause
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1327 fewer characters to be read. For complex numbers, this limit applies to
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1328 the real and imaginary components individually. For %f and %n, format
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1329 specifiers like %N.Mf are allowed, where M is an upper bound on number of
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1330 characters after the decimal point to be considered; subsequent digits are
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1331 skipped. For example, the specifier %8.2f would read 12.345e6 as 1.234e7.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1332
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1333 @item %*@dots{}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1334 The word specified by the remainder of the conversion specifier is skipped.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1335
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1336 @item literals
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1337 In addition the format may contain literal character strings; these will be
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1338 skipped during reading. If the input string does not match this literal,
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1339 the processing terminates.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1340 @end table
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1341
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1342 Parsed words corresponding to the first specifier are returned in the first
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1343 output argument and likewise for the rest of the specifiers.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1344
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1345 By default, if there is only one input argument, @var{format} is @t{"%f"}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1346 This means that numbers are read from the input into a single column vector.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1347 If @var{format} is explicitly empty (@qcode{""}) then textscan will
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1348 return data in a number of columns matching the number of fields on the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1349 first data line of the input. Either of these is suitable only when the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1350 input is exclusively numeric.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1351
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1352 For example, the string
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1353
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1354 @smallexample
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1355 @group
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1356 @var{str} = "\
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1357 Bunny Bugs 5.5\n\
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1358 Duck Daffy -7.5e-5\n\
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1359 Penguin Tux 6"
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1360 @end group
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1361 @end smallexample
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1362
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1363 @noindent
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1364 can be read using
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1365
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1366 @example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1367 @var{a} = textscan (@var{str}, "%s %s %f");
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1368 @end example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1369
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1370 The optional numeric argument @var{repeat} can be used for limiting the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1371 number of items read:
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1372
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1373 @table @asis
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1374 @item -1
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1375 Read all of the string or file until the end (default).
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1376
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1377 @item N
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1378 Read until the first of two conditions occurs: 1) the format has been
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1379 processed N times, or 2) N lines of the input have been processed. Zero
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1380 (0) is an acceptable value for @var{repeat}. Currently, end-of-line
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1381 characters inside %q, %c, and %[@dots{}]$ conversions do not contribute to
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1382 the line count. This is incompatible with @sc{matlab} and may change in
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1383 future.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1384 @end table
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1385
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1386 The behavior of @code{textscan} can be changed via property/value pairs.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1387 The following properties are recognized:
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1388
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1389 @table @asis
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1390 @item @qcode{"BufSize"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1391 This specifies the number of bytes to use for the internal buffer.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1392 A modest speed improvement may be obtained by setting this to a large value
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1393 when reading a large file, especially if the input contains long strings.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1394 The default is 4096, or a value dependent on @var{n} if that is specified.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1395
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1396 @item @qcode{"CollectOutput"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1397 A value of 1 or true instructs @code{textscan} to concatenate consecutive
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1398 columns of the same class in the output cell array. A value of 0 or false
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1399 (default) leaves output in distinct columns.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1400
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1401 @item @qcode{"CommentStyle"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1402 Specify parts of the input which are considered comments and will be
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1403 skipped. @var{value} is the comment style and can be either (1) A string
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1404 or 1x1 cell string, to skip everything to the right of it; (2) A cell array
21992
03c692adf563 maint: strip trailing whitespace from files.
John W. Eaton <jwe@octave.org>
parents: 21991
diff changeset
1405 of two strings, to skip everything between the first and second strings.
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1406 Comments are only parsed where whitespace is accepted and do not act as
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1407 delimiters.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1408
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1409 @item @qcode{"Delimiter"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1410 If @var{value} is a string, any character in @var{value} will be used to
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1411 split the input into words. If @var{value} is a cell array of strings,
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1412 any string in the array will be used to split the input into words.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1413 (default value = any whitespace.)
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1414
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1415 @item @qcode{"EmptyValue"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1416 Value to return for empty numeric values in non-whitespace delimited data.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1417 The default is NaN@. When the data type does not support NaN (int32 for
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1418 example), then the default is zero.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1419
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1420 @item @qcode{"EndOfLine"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1421 @var{value} can be either an emtpy or one character specifying the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1422 end-of-line character, or the pair
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1423 @qcode{"@xbackslashchar{}r@xbackslashchar{}n"} (CRLF).
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1424 In the latter case, any of
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1425 @qcode{"@xbackslashchar{}r"}, @qcode{"@xbackslashchar{}n"} or
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1426 @qcode{"@xbackslashchar{}r@xbackslashchar{}n"} is counted as a (single)
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1427 newline. If no value is given,
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1428 @qcode{"@xbackslashchar{}r@xbackslashchar{}n"} is used.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1429 @c If set to "" (empty string) EOLs are ignored as delimiters and added
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1430 @c to whitespace.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1431
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1432 @c When reading from a character string, optional input argument @var{n}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1433 @c specifies the number of times @var{format} should be used (i.e., to limit
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1434 @c the amount of data read).
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1435 @c When reading from file, @var{n} specifies the number of data lines to read;
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1436 @c in this sense it differs slightly from the format repeat count in strread.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1437
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1438 @item @qcode{"HeaderLines"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1439 The first @var{value} number of lines of @var{fid} are skipped. Note that
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1440 this does not refer to the first non-comment lines, but the first lines of
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1441 any type.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1442
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1443 @item @qcode{"MultipleDelimsAsOne"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1444 If @var{value} is nonzero, treat a series of consecutive delimiters,
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1445 without whitespace in between, as a single delimiter. Consecutive
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1446 delimiter series need not be vertically aligned. Without this option, a
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1447 single delimiter before the end of the line does not cause the line to be
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1448 considered to end with an empty value, but a single delimiter at the start
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1449 of a line causes the line to be considered to start with an empty value.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1450
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1451 @item @qcode{"TreatAsEmpty"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1452 Treat single occurrences (surrounded by delimiters or whitespace) of the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1453 string(s) in @var{value} as missing values.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1454
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1455 @item @qcode{"ReturnOnError"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1456 If set to numerical 1 or true, return normally as soon as an error is
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1457 encountered, such as trying to read a string using @qcode{%f}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1458 If set to 0 or false, return an error and no data.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1459
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1460 @item @qcode{"Whitespace"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1461 Any character in @var{value} will be interpreted as whitespace and trimmed;
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1462 The default value for whitespace is
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1463 @c Note: the next line specifically has a newline which generates a space
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1464 @c in the output of qcode, but keeps the next line < 80 characters.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1465 @qcode{"
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1466 @xbackslashchar{}b@xbackslashchar{}r@xbackslashchar{}n@xbackslashchar{}t"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1467 (note the space). Unless whitespace is set to @qcode{""} (empty) AND at
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1468 least one @qcode{"%s"} format conversion specifier is supplied, a space is
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1469 always part of whitespace.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1470
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1471 @end table
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1472
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1473 When the number of words in @var{str} or @var{fid} doesn't match an exact
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1474 multiple of the number of format conversion specifiers, @code{textscan}'s
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1475 behavior depends on whether the last character of the string or file is an
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1476 end-of-line as specified by the @code{EndOfLine} option:
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1477
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1478 @table @asis
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1479 @item last character = end-of-line
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1480 Data columns are padded with empty fields, NaN or 0 (for integer fields) so
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1481 that all columns have equal length
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1482
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1483 @item last character is not end-of-line
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1484 Data columns are not padded; @code{textscan} returns columns of unequal
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1485 length
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1486 @end table
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1487
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1488
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1489 The second output @var{position} provides the location, in characters
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1490 from the beginning of the file or string, where processing stopped.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1491
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1492 @seealso{dlmread, fscanf, load, strread, textread}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1493 @end deftypefn */)
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1494 {
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1495 static std::string who = "textscan";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1496
21504
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1497 return textscan_internal (who, args);
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1498 }
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1499
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1500 DEFUN (__textscan__, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1501 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1502 @deftypefn {} {@var{C} =} __textscan__ (@var{who}, @dots{})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1503 Like @code{textscan} but accept additional argument @var{who} to use
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1504 as the name of the function when reporting errors.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
1505 @end deftypefn */)
21504
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1506 {
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1507 if (args.length () == 0)
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1508 print_usage ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1509
21504
65d1daa7d7a7 new function __textscan__
John W. Eaton <jwe@octave.org>
parents: 21503
diff changeset
1510 return textscan_internal (args(0).string_value (), args.splice (0, 1));
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1511 }
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
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: 21457
diff changeset
1513 /*
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1514 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1515 %! str = "1, 2, 3, 4\n 5, , , 8\n 9, 10, 11, 12";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1516 %! fmtstr = "%f %d %f %s";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1517 %! c = textscan (str, fmtstr, 2, "delimiter", ",", "emptyvalue", -Inf);
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1518 %! assert (c{1}, [1;5]);
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1519 %! assert (c{3}, [3; -Inf]);
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1520 %! assert (iscellstr (c{4}));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
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: 21457
diff changeset
1522 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1523 %! b = [10:10:100];
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1524 %! b = [b; 8*b/5];
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1525 %! str = sprintf ("%g miles/hr = %g kilometers/hr\n", b);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1526 %! fmt = "%f miles/hr = %f kilometers/hr";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1527 %! c = textscan (str, fmt);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1528 %! assert (c{1}, b(1,:)', 1e-5);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1529 %! assert (c{2}, b(2,:)', 1e-5);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
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: 21457
diff changeset
1531 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1532 %! str = "13, -, NA, str1, -25\r\n// Middle line\r\n36, na, 05, str3, 6";
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1533 %! c = textscan (str, "%d %n %f %s %n", "delimiter", ",",
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1534 %! "treatAsEmpty", {"NA", "na", "-"}, "commentStyle", "//");
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1535 %! assert (c{1}, int32 ([13; 36]));
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1536 %! assert (c{2}, [NaN; NaN]);
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1537 %! assert (c{3}, [NaN; 5]);
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1538 %! assert (c{4}, {"str1"; "str3"});
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1539 %! assert (c{5}, [-25; 6]);
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1540
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1541 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1542 %! str = "Km:10 = hhhBjjj miles16hour\r\n";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1543 %! str = [str "Km:15 = hhhJjjj miles241hour\r\n"];
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1544 %! str = [str "Km:2 = hhhRjjj miles3hour\r\n"];
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1545 %! str = [str "Km:25 = hhhZ\r\n"];
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1546 %! fmt = "Km:%d = hhh%1sjjj miles%dhour";
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1547 %! c = textscan (str, fmt, "delimiter", " ");
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1548 %! assert (c{1}', int32 ([10, 15, 2, 25]));
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1549 %! assert (c{2}', {'B' 'J' 'R' 'Z'});
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1550 %! assert (c{3}', int32 ([16, 241, 3, 0]));
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1551
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1552 ## Test with default EndOfLine parameter
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1553 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1554 %! c = textscan ("L1\nL2", "%s");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1555 %! assert (c{:}, {"L1"; "L2"});
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1556
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1557 ## Test with EndofLine parameter set to "" (empty) - newline should be in word
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1558 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1559 %! c = textscan ("L1\nL2", "%s", "endofline", "");
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1560 %! assert (int8 ([c{:}{:}]), int8 ([76, 49, 10, 76, 50]));
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
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: 21457
diff changeset
1562 ### Matlab fails this test. A literal after a conversion is not a delimiter
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1563 #%!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1564 #%! ## No delimiters at all besides EOL. Skip fields, even empty fields
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1565 #%! str = "Text1Text2Text\nTextText4Text\nText57Text";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1566 #%! c = textscan (str, "Text%*dText%dText");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1567 #%! assert (c{1}, int32 ([2; 4; 0]));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
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: 21457
diff changeset
1569 ## CollectOutput test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1570 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1571 %! b = [10:10:100];
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1572 %! b = [b; 8*b/5; 8*b*1000/5];
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1573 %! str = sprintf ("%g miles/hr = %g (%g) kilometers (meters)/hr\n", b);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1574 %! fmt = "%f miles%s %s %f (%f) kilometers %*s";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1575 %! c = textscan (str, fmt, "collectoutput", 1);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1576 %! assert (size (c{3}), [10, 2]);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1577 %! assert (size (c{2}), [10, 2]);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1578
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1579 ## CollectOutput test with uneven column length files
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1580 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1581 %! b = [10:10:100];
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1582 %! b = [b; 8*b/5; 8*b*1000/5];
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1583 %! str = sprintf ("%g miles/hr = %g (%g) kilometers (meters)/hr\n", b);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1584 %! str = [str "110 miles/hr"];
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1585 %! fmt = "%f miles%s %s %f (%f) kilometers %*s";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1586 %! c = textscan (str, fmt, "collectoutput", 1);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1587 %! assert (size (c{1}), [11, 1]);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1588 %! assert (size (c{3}), [11, 2]);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1589 %! assert (size (c{2}), [11, 2]);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1590 %! assert (c{3}(end), NaN);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1591 %! assert (c{2}{11, 1}, "/hr");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1592 %! assert (isempty (c{2}{11, 2}), true);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1593
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1594 ## Double quoted string
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1595 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1596 %! str = 'First "the second called ""the middle""" third';
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1597 %! fmt = "%q";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1598 %! c = textscan (str, fmt);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1599 %! assert (c{1}, {"First"; 'the second called "the middle"'; "third"});
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
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: 21457
diff changeset
1601 ## Arbitrary character
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1602 %!test
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1603 %! c = textscan ("a first, \n second, third", "%s %c %11c", "delimiter", " ,");
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1604 %! assert (c{1}, {"a"; "ond"});
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1605 %! assert (c{2}, {"f"; "t"});
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1606 %! assert (c{3}, {"irst, \n sec"; "hird"});
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1607
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1608 ## Field width and non-standard delimiters
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1609 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1610 %! str = "12;34;123456789;7";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1611 %! c = textscan (str, "%4d %4d", "delimiter", ";", "collectOutput", 1);
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1612 %! assert (c, {[12, 34; 1234, 5678; 9, 7]});
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1613
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1614 ## Field width and non-standard delimiters (2)
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1615 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1616 %! str = "12;34;123456789;7";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1617 %! c = textscan (str, "%4f %f", "delimiter", ";", "collectOutput", 1);
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1618 %! assert (c, {[12, 34; 1234, 56789; 7, NaN]});
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1619
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1620 ## Ignore trailing delimiter, but use leading one
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1621 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1622 %! str = "12.234e+2,34, \n12345.789-9876j,78\n,10|3";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1623 %! c = textscan (str, "%10.2f %f", "delimiter", ",", "collectOutput", 1,
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1624 %! "expChars", "e|");
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1625 %! assert (c, {[1223, 34; 12345.79-9876j, 78; NaN, 10000]}, 1e-6);
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1626
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1627 ## Multi-character delimiter
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1628 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1629 %! str = "99end2 space88gap 4564";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1630 %! c = textscan (str, "%d %s", "delimiter", {"end", "gap", "space"});
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1631 %! assert (c{1}, int32 ([99; 88]));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1632 %! assert (c{2}, {"2 "; "4564"});
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1633
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1634 ## FIXME: Following two tests still fail (4/13/2016)?
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1635 ### Delimiters as part of literals, and following literals
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1636 #%!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1637 #%! str = "12 R&D & 7";
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1638 #%! c = textscan (str, "%f R&D %f", "delimiter", "&", "collectOutput", 1,
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1639 #%! "EmptyValue", -99);
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1640 #%! assert (c, {[12, -99; 7, -99]});
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1641 #
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1642 ### Delimiters as part of literals, and before literals
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1643 #%!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1644 #%! str = "12 & R&D 7";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1645 #%! c = textscan (str, "%f R&D %f", "delimiter", "&", "collectOutput", 1);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1646 #%! assert (c, {[12 7]});
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1647
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1648 ## Check number of lines read, not number of passes through format string
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1649 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1650 %! f = tempname ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1651 %! fid = fopen (f, "w+");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1652 %! fprintf (fid, "1\n2\n3\n4\n5\n6");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1653 %! fseek (fid, 0, "bof");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1654 %! c = textscan (fid, "%f %f", 2);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1655 %! E = feof (fid);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1656 %! fclose (fid);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1657 %! unlink (f);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1658 %! assert (c, {1, 2});
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1659 %! assert (! E);
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1660
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1661 ## Check number of lines read, not number of passes through format string
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1662 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1663 %! f = tempname ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1664 %! fid = fopen (f, "w+");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1665 %! fprintf (fid, "1\r\n2\r3\n4\r\n5\n6");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1666 %! fseek (fid, 0, "bof");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1667 %! c = textscan (fid, "%f %f", 4);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1668 %! fclose (fid);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1669 %! unlink (f);
21580
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21566
diff changeset
1670 %! assert (c, {[1;3], [2;4]});
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1671
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1672 ## Check number of lines read, with multiple delimiters
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1673 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1674 %! f = tempname ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1675 %! fid = fopen (f, "w+");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1676 %! fprintf (fid, "1-\r\n-2\r3-\n-4\r\n5\n6");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1677 %! fseek (fid, 0, "bof");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1678 %! c = textscan (fid, "%f %f", 4, "delimiter", "-", "multipleDelimsAsOne", 1);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1679 %! fclose (fid);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1680 %! unlink (f);
21580
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21566
diff changeset
1681 %! assert (c, {[1;3], [2;4]});
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1682
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1683 ## Check ReturnOnError
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1684 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1685 %! f = tempname ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1686 %! fid = fopen (f, "w+");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1687 %! str = "1 2 3\n4 s 6";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1688 %! fprintf (fid, str);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1689 %! fseek (fid, 0, "bof");
21622
fed1111e1899 textscan: eliminate ReturnOnError="continue" mode (bug #47538)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21621
diff changeset
1690 %! c = textscan (fid, "%f %f %f", "ReturnOnError", 1);
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1691 %! fseek (fid, 0, "bof");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1692 %! fclose (fid);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1693 %! unlink (f);
21622
fed1111e1899 textscan: eliminate ReturnOnError="continue" mode (bug #47538)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21621
diff changeset
1694 %! u = textscan (str, "%f %f %f", "ReturnOnError", 1);
fed1111e1899 textscan: eliminate ReturnOnError="continue" mode (bug #47538)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21621
diff changeset
1695 %! assert (c, {[1;4], [2], [3]});
fed1111e1899 textscan: eliminate ReturnOnError="continue" mode (bug #47538)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21621
diff changeset
1696 %! assert (u, {[1;4], [2], [3]});
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1697
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1698 %! ## Check ReturnOnError (2)
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1699 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1700 %! f = tempname ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1701 %! fid = fopen (f, "w+");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1702 %! str = "1 2 3\n4 s 6\n";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1703 %! fprintf (fid, str);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1704 %! fseek (fid, 0, "bof");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1705 %! c = textscan (fid, "%f %f %f", "ReturnOnError", 1);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1706 %! fseek (fid, 0, "bof");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1707 %! fclose (fid);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1708 %! unlink (f);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1709 %! u = textscan (str, "%f %f %f", "ReturnOnError", 1);
21580
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21566
diff changeset
1710 %! assert (c, {[1;4], 2, 3});
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21566
diff changeset
1711 %! assert (u, {[1;4], 2, 3});
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1712
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1713 %!error <Read error in field 2 of row 2>
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1714 %! textscan ("1 2 3\n4 s 6", "%f %f %f", "ReturnOnError", 0);
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1715
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1716 ## Check ReturnOnError (3)
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1717 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1718 %! f = tempname ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1719 %! fid = fopen (f, "w+");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1720 %! fprintf (fid, "1 s 3\n4 5 6");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1721 %! fseek (fid, 0, "bof");
21622
fed1111e1899 textscan: eliminate ReturnOnError="continue" mode (bug #47538)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21621
diff changeset
1722 %! c = textscan (fid, "", "ReturnOnError", 1);
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1723 %! fseek (fid, 0, "bof");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1724 %! fclose (fid);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1725 %! unlink (f);
21622
fed1111e1899 textscan: eliminate ReturnOnError="continue" mode (bug #47538)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21621
diff changeset
1726 %! assert (c, {1});
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1727
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1728 ## Check ReturnOnError with empty fields
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1729 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1730 %! c = textscan ("1,,3\n4,5,6", "", "Delimiter", ",", "ReturnOnError", 1);
21580
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21566
diff changeset
1731 %! assert (c, {[1;4], [NaN;5], [3;6]});
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1732
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1733 ## Check ReturnOnError with empty fields (2)
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1734 %!test
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1735 %! c = textscan ("1,,3\n4,5,6", "%f %f %f", "Delimiter", ",",
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1736 %! "ReturnOnError", 1);
21580
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21566
diff changeset
1737 %! assert (c, {[1;4], [NaN;5], [3;6]});
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1738
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1739 ## Check ReturnOnError in first column
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1740 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1741 %! c = textscan ("1 2 3\ns 5 6", "", "ReturnOnError", 1);
21580
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21566
diff changeset
1742 %! assert (c, {1, 2, 3});
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1743
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1744 ## FIXME: This test fails (4/14/16)
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1745 ### Test incomplete first data line
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1746 #%!test
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1747 #%! R = textscan (['Empty1' char(10)], 'Empty%d %f');
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1748 #%! assert (R{1}, int32 (1));
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1749 #%! assert (isempty (R{2}), true);
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1750
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1751 ## bug #37023
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1752 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1753 %! data = textscan (" 1. 1 \n 2 3\n", '%f %f');
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1754 %! assert (data{1}, [1; 2], 1e-15);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1755 %! assert (data{2}, [1; 3], 1e-15);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1756
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1757 ## Whitespace test (bug #37333) using delimiter ";"
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1758 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1759 %! tc{1, 1} = "C:/code;";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1760 %! tc{1, end+1} = "C:/code/meas;";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1761 %! tc{1, end+1} = " C:/code/sim;";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1762 %! tc{1, end+1} = "C:/code/utils;";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1763 %! string = [tc{:}];
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1764 %! c = textscan (string, "%s", "delimiter", ";");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1765 %! for k = 1:max (numel (c{1}), numel (tc))
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1766 %! lh = c{1}{k};
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1767 %! rh = tc{k};
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1768 %! rh(rh == ";") = "";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1769 %! rh = strtrim (rh);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1770 %! assert (strcmp (lh, rh));
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1771 %! endfor
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1772
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1773 ## Whitespace test (bug #37333), adding multipleDelimsAsOne true arg
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1774 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1775 %! tc{1, 1} = "C:/code;";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1776 %! tc{1, end+1} = " C:/code/meas;";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1777 %! tc{1, end+1} = "C:/code/sim;;";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1778 %! tc{1, end+1} = "C:/code/utils;";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1779 %! string = [tc{:}];
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1780 %! c = textscan (string, "%s", "delimiter", ";", "multipleDelimsAsOne", 1);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1781 %! for k = 1:max (numel (c{1}), numel (tc))
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1782 %! lh = c{1}{k};
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1783 %! rh = tc{k};
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1784 %! rh(rh == ";") = "";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1785 %! rh = strtrim (rh);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1786 %! assert (strcmp (lh, rh));
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1787 %! endfor
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1788
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1789 ## Whitespace test (bug #37333), adding multipleDelimsAsOne false arg
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1790 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1791 %! tc{1, 1} = "C:/code;";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1792 %! tc{1, end+1} = " C:/code/meas;";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1793 %! tc{1, end+1} = "C:/code/sim;;";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1794 %! tc{1, end+1} = "";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1795 %! tc{1, end+1} = "C:/code/utils;";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1796 %! string = [tc{:}];
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1797 %! c = textscan (string, "%s", "delimiter", ";", "multipleDelimsAsOne", 0);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1798 %! for k = 1:max (numel (c{1}), numel (tc))
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1799 %! lh = c{1}{k};
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1800 %! rh = tc{k};
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1801 %! rh(rh == ";") = "";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1802 %! rh = strtrim (rh);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1803 %! assert (strcmp (lh, rh));
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1804 %! endfor
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1805
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1806 ## Whitespace test (bug #37333) whitespace "" arg
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1807 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1808 %! tc{1, 1} = "C:/code;";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1809 %! tc{1, end+1} = " C:/code/meas;";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1810 %! tc{1, end+1} = "C:/code/sim;";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1811 %! tc{1, end+1} = "C:/code/utils;";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1812 %! string = [tc{:}];
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1813 %! c = textscan (string, "%s", "delimiter", ";", "whitespace", "");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1814 %! for k = 1:max (numel (c{1}), numel (tc))
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1815 %! lh = c{1}{k};
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1816 %! rh = tc{k};
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1817 %! rh(rh == ";") = "";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1818 %! assert (strcmp (lh, rh));
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1819 %! endfor
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1820
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1821 ## Whitespace test (bug #37333), whitespace " " arg
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1822 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1823 %! tc{1, 1} = "C:/code;";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1824 %! tc{1, end+1} = " C:/code/meas;";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1825 %! tc{1, end+1} = "C:/code/sim;";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1826 %! tc{1, end+1} = "C:/code/utils;";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1827 %! string = [tc{:}];
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1828 %! c = textscan (string, "%s", "delimiter", ";", "whitespace", " ");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1829 %! for k = 1:max (numel (c{1}), numel (tc))
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1830 %! lh = c{1}{k};
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1831 %! rh = tc{k};
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1832 %! rh(rh == ";") = "";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1833 %! rh = strtrim (rh);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1834 %! assert (strcmp (lh, rh));
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1835 %! endfor
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1836
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1837 ## Tests reading with empty format, should return proper nr of columns
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1838 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1839 %! f = tempname ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1840 %! fid = fopen (f, "w+");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1841 %! fprintf (fid, " 1 2 3 4\n5 6 7 8");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1842 %! fseek (fid, 0, "bof");
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1843 %! C = textscan (fid, "");
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1844 %! E = feof (fid);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1845 %! fclose (fid);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1846 %! unlink (f);
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1847 %! assert (C{1}, [1 ; 5], 1e-6);
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1848 %! assert (C{2}, [2 ; 6], 1e-6);
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1849 %! assert (C{3}, [3 ; 7], 1e-6);
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1850 %! assert (C{4}, [4 ; 8], 1e-6);
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1851 %! assert (E);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1852
21621
fe0a6de805e4 textscan: Leave file read position correct at exit (bug #47671)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21581
diff changeset
1853 ## Test leaving the file at the correct position on exit
fe0a6de805e4 textscan: Leave file read position correct at exit (bug #47671)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21581
diff changeset
1854 %!test
fe0a6de805e4 textscan: Leave file read position correct at exit (bug #47671)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21581
diff changeset
1855 %! f = tempname ();
fe0a6de805e4 textscan: Leave file read position correct at exit (bug #47671)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21581
diff changeset
1856 %! fid = fopen (f, "w+");
fe0a6de805e4 textscan: Leave file read position correct at exit (bug #47671)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21581
diff changeset
1857 %! fprintf (fid, "1,2\n3,4\n");
fe0a6de805e4 textscan: Leave file read position correct at exit (bug #47671)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21581
diff changeset
1858 %! fseek (fid, 0, "bof");
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1859 %! C = textscan (fid, "%s %f", 2, "Delimiter", ",");
21621
fe0a6de805e4 textscan: Leave file read position correct at exit (bug #47671)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21581
diff changeset
1860 %! E = ftell (fid);
fe0a6de805e4 textscan: Leave file read position correct at exit (bug #47671)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21581
diff changeset
1861 %! fclose (fid);
fe0a6de805e4 textscan: Leave file read position correct at exit (bug #47671)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21581
diff changeset
1862 %! unlink (f);
fe0a6de805e4 textscan: Leave file read position correct at exit (bug #47671)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21581
diff changeset
1863 %! assert (E, 8);
fe0a6de805e4 textscan: Leave file read position correct at exit (bug #47671)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21581
diff changeset
1864
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1865 ## Tests reading with empty format; empty fields & incomplete lower row
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1866 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1867 %! f = tempname ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1868 %! fid = fopen (f, "w+");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1869 %! fprintf (fid, " ,2,,4\n5,6");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1870 %! fseek (fid, 0, "bof");
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1871 %! C = textscan (fid, "", "delimiter", ",", "EmptyValue", 999,
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1872 %! "CollectOutput" , 1);
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1873 %! fclose (fid);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1874 %! unlink (f);
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1875 %! assert (C{1}, [999, 2, 999, 4; 5, 6, 999, 999], 1e-6);
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1876
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1877 ## Error message tests
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1878
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1879 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1880 %! f = tempname ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1881 %! fid = fopen (f, "w+");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1882 %! msg1 = "textscan: 1 parameters given, but only 0 values";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1883 %! try
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1884 %! C = textscan (fid, "", "headerlines");
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1885 %! end_try_catch;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1886 %! assert (!feof (fid));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1887 %! fclose (fid);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1888 %! unlink (f);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1889 %! assert (msg1, lasterr);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1890
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1891 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1892 %! f = tempname ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1893 %! fid = fopen (f, "w+");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1894 %! msg1 = "textscan: HeaderLines must be numeric";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1895 %! try
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1896 %! C = textscan (fid, "", "headerlines", "hh");
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1897 %! end_try_catch;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1898 %! fclose (fid);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1899 %! unlink (f);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1900 %! assert (msg1, lasterr);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1901
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1902 ## Skip headerlines
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1903 %!test
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1904 %! C = textscan ("field 1 field2\n 1 2\n3 4", "", "headerlines", 1,
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1905 %! "collectOutput", 1);
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1906 %! assert (C, {[1 2; 3 4]});
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1907
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1908 ## Skip headerlines with non-default EOL
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1909 %!test
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1910 %! C = textscan ("field 1 field2\r 1 2\r3 4", "", "headerlines", 2,
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1911 %! "collectOutput", 1, "EndOfLine", '\r');
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1912 %! assert (C, {[3 4]});
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1913
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1914 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1915 %! f = tempname ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1916 %! fid = fopen (f, "w+");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1917 %! fprintf (fid,"some_string");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1918 %! fseek (fid, 0, "bof");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1919 %! msg1 = "textscan: EndOfLine must be at most one character or '\\r\\n'";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1920 %! try
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1921 %! C = textscan (fid, "%f", "EndOfLine", "\n\r");
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1922 %! end_try_catch;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1923 %! fclose (fid);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1924 %! unlink (f);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1925 %! assert (msg1, lasterr);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1926
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1927 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1928 %! f = tempname ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1929 %! fid = fopen (f, "w+");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1930 %! fprintf (fid,"some_string");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1931 %! fseek (fid, 0, "bof");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1932 %! msg1 = "textscan: EndOfLine must be at most one character or '\\r\\n'";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1933 %! try
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1934 %! C = textscan (fid, "%f", "EndOfLine", 33);
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1935 %! end_try_catch;
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1936 %! fclose (fid);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1937 %! unlink (f);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1938 %! assert (msg1, lasterr);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
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: 21457
diff changeset
1940 ## Bug #41824
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1941 %!assert (textscan ("123", "", "whitespace", " "){:}, 123);
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1942
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1943 ## Bug #42343-1, just test supplied emptyvalue
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1944 %!assert (textscan (",NaN", "", "delimiter", "," ,"emptyValue" ,Inf),
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1945 %! {Inf, NaN})
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1946
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1947 ## Bug #42343-2, test padding with supplied emptyvalue
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1948 %!test
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1949 %! c = textscan (",1,,4\nInf, ,NaN\n", "", "delimiter", ",",
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1950 %! "emptyvalue", -10);
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1951 %! assert (cell2mat (c), [-10, 1, -10, 4; Inf, -10, NaN, -10]);
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1952
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1953 ## Bug #42528
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1954 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1955 %! assert (textscan ("1i", ""){1}, 0+1i);
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1956 %! C = textscan ("3, 2-4i, NaN\n -i, 1, 23.4+2.2i\n 1+1 1+1j", "",
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1957 %! "delimiter", ",");
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1958 %! assert (cell2mat (C), [3+0i, 2-4i, NaN+0i; 0-i, 1+0i, 23.4+2.2i; 1 1 1+1i]);
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
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: 21457
diff changeset
1960 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1961 %! ## TreatAsEmpty
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1962 %! C = textscan ("1,2,3,NN,5,6\n", "%d%d%d%f", "delimiter", ",",
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1963 %! "TreatAsEmpty", "NN");
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1964 %! assert (C{3}(1), int32 (3));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1965 %! assert (C{4}(1), NaN);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
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: 21457
diff changeset
1967 ## MultipleDelimsAsOne
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1968 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1969 %! str = "11, 12, 13,, 15\n21,, 23, 24, 25\n,, 33, 34, 35\n";
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1970 %! C = textscan (str, "%f %f %f %f", "delimiter", ",",
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1971 %! "multipledelimsasone", 1, "endofline", "\n");
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1972 %! assert (C{1}', [11, 21, 33]);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1973 %! assert (C{2}', [12, 23, 34]);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1974 %! assert (C{3}', [13, 24, 35]);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1975 %! assert (C{4}', [15, 25, NaN]);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1976
21551
990c6c31a684 Accept escape sequences in single quoted strings for "delimiter" (bug #47539)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21548
diff changeset
1977 ## Single-quoted escape sequences
990c6c31a684 Accept escape sequences in single quoted strings for "delimiter" (bug #47539)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21548
diff changeset
1978 %!test
990c6c31a684 Accept escape sequences in single quoted strings for "delimiter" (bug #47539)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21548
diff changeset
1979 %! str = "11\t12\t13\r21\t22\t23";
990c6c31a684 Accept escape sequences in single quoted strings for "delimiter" (bug #47539)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21548
diff changeset
1980 %! c = textscan (str, "", "delimiter", '\t', "EndOfLine", '\r');
990c6c31a684 Accept escape sequences in single quoted strings for "delimiter" (bug #47539)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21548
diff changeset
1981 %! assert (c{1}', [11, 21]);
990c6c31a684 Accept escape sequences in single quoted strings for "delimiter" (bug #47539)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21548
diff changeset
1982 %! assert (c{2}', [12, 22]);
990c6c31a684 Accept escape sequences in single quoted strings for "delimiter" (bug #47539)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21548
diff changeset
1983 %! assert (c{3}', [13, 23]);
990c6c31a684 Accept escape sequences in single quoted strings for "delimiter" (bug #47539)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21548
diff changeset
1984
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1985 ## Bug #44750
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1986 %!test
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1987 %! c = textscan ("/home/foo/", "%s", "delimiter", "/",
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1988 %! "MultipleDelimsAsOne", 1);
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1989 %! assert (c{1}, {"home"; "foo"});
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1990
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1991 ## FIXME: Test still fails (4/13/2016)?
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
1992 ## Allow cuddling %sliteral, but warn it is ambiguous
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1993 #%!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1994 #%! C = textscan ("abcxyz51\nxyz83\n##xyz101", "%s xyz %d");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1995 #%! assert (C{1}([1 3]), {"abc"; "##"});
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1996 #%! assert (isempty (C{1}{2}), true);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1997 #%! assert (C{2}, int32 ([51; 83; 101]));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1998 ### Literals are not delimiters.
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
1999
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2000 ## Test for false positives in check for non-supported format specifiers
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2001 %!test
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2002 %! c = textscan ("Total: 32.5 % (of cm values)",
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2003 %! "Total: %f %% (of cm values)");
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2004 %! assert (c{1}, 32.5, 1e-5);
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
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: 21457
diff changeset
2006 ## Test various forms of string format specifiers (bug #45712)
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2007 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2008 %! str = "14 :1 z:2 z:3 z:5 z:11";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2009 %! C = textscan (str, "%f %s %*s %3s %*3s %f", "delimiter", ":");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2010 %! assert (C, {14, {"1 z"}, {"3 z"}, 11});
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2011
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2012 ## Bit width, fixed width conversion specifiers
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2013 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2014 %! str2 = "123456789012345 ";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2015 %! str2 = [str2 str2 str2 str2 str2 str2 str2 str2];
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2016 %! str2 = [str2 "123456789.01234 1234567890.1234 12345.678901234 12345.678901234"];
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2017 %! pttrn = "%3u8%*s %5u16%*s %10u32%*s %15u64 %3d8%*s %5d16%*s %10d32%*s %15d64 %9f32%*s %14f64%*s %10.2f32%*s %12.2f64%*s";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2018 %! C = textscan (str2, pttrn, "delimiter", " ");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2019 %! assert (C{1}, uint8 (123));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2020 %! assert (C{2}, uint16 (12345));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2021 %! assert (C{3}, uint32 (1234567890));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2022 %! assert (C{4}, uint64 (123456789012345));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2023 %! assert (C{5}, int8 (123));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2024 %! assert (C{6}, int16 (12345));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2025 %! assert (C{7}, int32 (1234567890));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2026 %! assert (C{8}, int64 (123456789012345));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2027 %! assert (C{9}, single (123456789), 1e-12);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2028 %! assert (C{10}, double (1234567890.123), 1e-15);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2029 %! assert (C{11}, single (12345.68), 1e-5);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2030 %! assert (C{12}, double (12345.68), 1e-11);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2031
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2032 ## Bit width, fixed width conv. specifiers -- check the right amount is left
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2033 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2034 %! str2 = "123456789012345 ";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2035 %! str2 = [str2 str2 "123456789.01234"];
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2036 %! pttrn = "%3u8 %5u16 %10u32 %3d8 %5d16 %10d32 %9f32 %9f";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2037 %! C = textscan (str2, pttrn, "delimiter", " ");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2038 %! assert (C{1}, uint8 (123));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2039 %! assert (C{2}, uint16 (45678));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2040 %! assert (C{3}, uint32 (9012345));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2041 %! assert (C{4}, int8 (123));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2042 %! assert (C{5}, int16 (45678));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2043 %! assert (C{6}, int32 (9012345));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2044 %! assert (C{7}, single (123456789), 1e-12);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2045 %! assert (C{8}, double (0.01234), 1e-12);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2046
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2047 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2048 %! C = textscan ("123.123", "%2f %3f %3f");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2049 %! assert (C{1}, 12);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2050 %! assert (C{2}, 3.1, 1e-11);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2051 %! assert (C{3}, 23);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
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: 21457
diff changeset
2053 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2054 %! C = textscan ("123.123", "%3f %3f %3f");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2055 %! assert (C{1}, 123);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2056 %! assert (C{2}, 0.12, 1e-11);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2057 %! assert (C{3}, 3);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
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: 21457
diff changeset
2059 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2060 %! C = textscan ("123.123", "%4f %3f");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2061 %! assert (C{1}, 123);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2062 %! assert (C{2}, 123);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2063
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2064 ## field width interrupts exponent. (Matlab incorrectly gives [12, 2e12])
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2065 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2066 %! assert (textscan ("12e12", "%4f"), {[120; 2]});
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2067 %! assert (textscan ("12e+12", "%5f"), {[120; 2]});
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2068 %! assert (textscan ("125e-12","%6f"), {[12.5; 2]});
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2069
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2070 ## %[] tests
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2071 ## Plain [..] and *[..]
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2072 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2073 %! ar = "abcdefguvwxAny\nacegxyzTrailing\nJunk";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2074 %! C = textscan (ar, "%[abcdefg] %*[uvwxyz] %s");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2075 %! assert (C{1}, {"abcdefg"; "aceg"; ""});
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2076 %! assert (C{2}, {"Any"; "Trailing"; "Junk"});
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2077
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2078 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2079 %! assert (textscan ("A2 B2 C3", "%*[ABC]%d", 3), {int32([2; 2; 3])});
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2080
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2081 ## [^..] and *[^..]
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2082 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2083 %! br = "abcdefguvwx1Any\nacegxyz2Trailing\n3Junk";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2084 %! C = textscan (br, "%[abcdefg] %*[^0123456789] %s");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2085 %! assert (C{1}, {"abcdefg"; "aceg"; ""});
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2086 %! assert (C{2}, {"1Any"; "2Trailing"; "3Junk"});
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2087
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2088 ## [..] and [^..] containing delimiters
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2089 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2090 %! cr = "ab cd efguv wx1Any\na ce gx yz2Trailing\n 3Junk";
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2091 %! C = textscan (cr, "%[ abcdefg] %*[^0123456789] %s", "delimiter", " \n",
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2092 %! "whitespace", "");
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2093 %! assert (C{1}, {"ab cd efg"; "a ce g"; " "});
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2094 %! assert (C{2}, {"1Any"; "2Trailing"; "3Junk"});
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2095
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2096 ## Bug #36464
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2097 %!assert (textscan ("1 2 3 4 5 6", "%*n%n%*[^\n]"){1}, 2);
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2098
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2099 ## test %[]] and %[^]]
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2100 %!test
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2101 %! assert (textscan ("345]", "%*[123456]%[]]"){1}{1}, "]");
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2102 %! assert (textscan ("345]", "%*[^]]%s"){1}{1}, "]");
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2103
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2104 ## Test that "-i" checks the next two characters
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2105 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2106 %! C = textscan ("-i -in -inf -infinity", "%f %f%s %f %f %s");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2107 %! assert (C, {-i, -i, {"n"}, -Inf, -Inf, {"inity"}});
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2108
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2109 ## Again for "+i", this time with custom parser
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2110 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2111 %! C = textscan ("+i +in +inf +infinity", "%f %f%s %f %f %s", "ExpChars", "eE");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2112 %! assert (C, {i, i, {"n"}, Inf, Inf, {"inity"}});
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2113
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2114 ## Check single quoted format interprets control sequences
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2115 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2116 %! C = textscan ("1 2\t3 4", '%f %[^\t] %f %f');
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2117 %! assert (C, {1, {"2"}, 3, 4});
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2118
21532
936ab0fca2f7 Textscan returns emptyVal for rows with literal but no conversion (bug #47458)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21507
diff changeset
2119 %% Check a non-empty line with no valid conversion registers empytValue
936ab0fca2f7 Textscan returns emptyVal for rows with literal but no conversion (bug #47458)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21507
diff changeset
2120 %!test
936ab0fca2f7 Textscan returns emptyVal for rows with literal but no conversion (bug #47458)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21507
diff changeset
2121 %! C = textscan ("Empty\n", "Empty%f %f");
21580
ecce63c99c3f maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents: 21566
diff changeset
2122 %! assert (C, { NaN, NaN });
21532
936ab0fca2f7 Textscan returns emptyVal for rows with literal but no conversion (bug #47458)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21507
diff changeset
2123
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2124 ## Check overflow and underflow of integer types
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2125 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2126 %! a = "-1e90 ";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2127 %! b = "1e90 ";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2128 %! fmt = "%d8 %d16 %d32 %d64 %u8 %u16 %u32 %u64 ";
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2129 %! C = textscan ([a a a a a a a a b b b b b b b b], fmt);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2130 %! assert (C{1}, int8 ([-128; 127]));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2131 %! assert (C{2}, int16 ([-32768; 32767]));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2132 %! assert (C{3}, int32 ([-2147483648; 2147483647]));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2133 %! assert (C{4}, int64 ([-9223372036854775808; 9223372036854775807]));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2134 %! assert (C{5}, uint8 ([0; 255]));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2135 %! assert (C{6}, uint16 ([0; 65535]));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2136 %! assert (C{7}, uint32 ([0; 4294967295]));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2137 %! assert (C{8}, uint64 ([0; 18446744073709551615]));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2138
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2139 ## Tests from Matlab (does The MathWorks have any copyright over the input?)
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2140 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2141 %! f = tempname ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2142 %! fid = fopen (f, "w+");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2143 %! fprintf (fid,"09/12/2005 Level1 12.34 45 1.23e10 inf Nan Yes 5.1+3i\n");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2144 %! fprintf (fid,"10/12/2005 Level2 23.54 60 9e19 -inf 0.001 No 2.2-.5i\n");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2145 %! fprintf (fid,"11/12/2005 Level3 34.90 12 2e5 10 100 No 3.1+.1i\n");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2146 %! fseek (fid, 0, "bof");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2147 %! C = textscan (fid,"%s %s %f32 %d8 %u %f %f %s %f");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2148 %! %assert (C{1}, {"09/12/2005";"10/12/2005";"11/12/2005"});
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2149 %! assert (C{2}, {"Level1";"Level2";"Level3"});
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2150 %! assert (C{3}, [single(12.34);single(23.54);single(34.90)]);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2151 %! assert (C{4}, [int8(45);int8(60);int8(12)]);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2152 %! assert (C{5}, [uint32(4294967295);uint32(4294967295);uint32(200000)]);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2153 %! assert (C{6}, [inf;-inf;10]);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2154 %! assert (C{7}, [NaN;0.001;100], eps);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2155 %! assert (C{8}, {"Yes";"No";"No"});
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2156 %! assert (C{9}, [5.1+3i;2.2-0.5i;3.1+0.1i]);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2157 %! fseek (fid, 0, "bof");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2158 %! C = textscan (fid,"%s Level%d %f32 %d8 %u %f %f %s %f");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2159 %! assert (C{2}, [int32(1);int32(2);int32(3)]);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2160 %! assert (C{3}, [single(12.34);single(23.54);single(34.90)]);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2161 %! fseek (fid, 0, "bof");
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2162 %! C = textscan (fid, '%s %*[^\n]');
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2163 %! fclose (fid);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2164 %! unlink (f);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2165 %! assert (C, {{"09/12/2005";"10/12/2005";"11/12/2005"}});
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2166
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2167 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2168 %! f = tempname ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2169 %! fid = fopen (f, "w+");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2170 %! fprintf (fid,"1, 2, 3, 4, , 6\n");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2171 %! fprintf (fid,"7, 8, 9, , 11, 12\n");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2172 %! fseek (fid, 0, "bof");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2173 %! C = textscan (fid,"%f %f %f %f %u8 %f", "Delimiter",",","EmptyValue",-Inf);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2174 %! fclose (fid);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2175 %! unlink (f);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2176 %! assert (C{4}, [4; -Inf]);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2177 %! assert (C{5}, uint8 ([0; 11]));
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
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: 21457
diff changeset
2179 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2180 %! f = tempname ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2181 %! fid = fopen (f, "w+");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2182 %! fprintf (fid,"abc, 2, NA, 3, 4\n");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2183 %! fprintf (fid,"// Comment Here\n");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2184 %! fprintf (fid,"def, na, 5, 6, 7\n");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2185 %! fseek (fid, 0, "bof");
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2186 %! C = textscan (fid, "%s %n %n %n %n", "Delimiter", ",",
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2187 %! "TreatAsEmpty", {"NA","na"}, "CommentStyle", "//");
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2188 %! fclose (fid);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2189 %! unlink (f);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2190 %! assert (C{1}, {"abc";"def"});
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2191 %! assert (C{2}, [2; NaN]);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2192 %! assert (C{3}, [NaN; 5]);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2193 %! assert (C{4}, [3; 6]);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2194 %! assert (C{5}, [4; 7]);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2195
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2196 ## FIXME: Almost passes. Second return value is {"/"}. Tested 4/14/16.
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2197 ### Test start of comment as string
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2198 #%!test
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2199 #%! c = textscan ("1 / 2 // 3", "%n %s %u8", "CommentStyle", {"//"});
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2200 #%! assert (c(1), {1, "/", 2});
21507
2cdbae31c022 improve compatibility of textscan for character arrays
John W. Eaton <jwe@octave.org>
parents: 21506
diff changeset
2201
2cdbae31c022 improve compatibility of textscan for character arrays
John W. Eaton <jwe@octave.org>
parents: 21506
diff changeset
2202 %!assert (textscan (["1 2 3 4"; "5 6 7 8"], "%f"), {[15; 26; 37; 48]})
21566
02826fb0f261 textscan: Fix bug in exponent parsing
Mike Miller <mtmiller@octave.org>
parents: 21551
diff changeset
2203
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2204 ## Check for delimiter after exponent
21566
02826fb0f261 textscan: Fix bug in exponent parsing
Mike Miller <mtmiller@octave.org>
parents: 21551
diff changeset
2205 %!assert (textscan ("1e-3|42", "%f", "delimiter", "|"), {[1e-3; 42]})
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2206 */
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2207
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2208 // These tests have end-comment sequences, so can't just be in a comment
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2209 #if 0
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2210 ## Test unfinished comment
21502
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2211 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2212 %! c = textscan ("1 2 /* half comment", "%n %u8", "CommentStyle", {"/*", "*/"});
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2213 %! assert (c, {1, 2});
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2214
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2215 ## Test reading from a real file
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2216 %!test
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2217 %! f = tempname ();
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2218 %! fid = fopen (f, "w+");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2219 %! d = rand (1, 4);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2220 %! fprintf (fid, " %f %f /* comment */ %f %f ", d);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2221 %! fseek (fid, 0, "bof");
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2222 %! A = textscan (fid, "%f %f", "CommentStyle", {"/*", "*/"});
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2223 %! E = feof (fid);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2224 %! fclose (fid);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2225 %! unlink (f);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2226 %! assert (A{1}, [d(1); d(3)], 1e-6);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2227 %! assert (A{2}, [d(2); d(4)], 1e-6);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2228 %! assert (E);
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2229 #endif
7a19c5678f91 move textscan class to oct-stream.cc and textscan function to file-io.cc
John W. Eaton <jwe@octave.org>
parents: 21457
diff changeset
2230
21625
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2231 /*
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2232 ## Test input validation
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2233 %!error textscan ()
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2234 %!error textscan (single (40))
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2235 %!error textscan ({40})
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2236 %!error <must be a string> textscan ("Hello World", 2)
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2237 %!error <at most one character or>
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2238 %! textscan ("Hello World", "%s", "EndOfLine", 3);
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2239 %!error <'%z' is not a valid format specifier> textscan ("1.0", "%z")
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2240 %!error <no valid format conversion specifiers> textscan ("1.0", "foo")
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2241 */
3cadca91e390 Clean up BIST tests for textscan.
Rik <rik@octave.org>
parents: 21622
diff changeset
2242
2116
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
2243 static octave_value
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
2244 do_fread (octave_stream& os, const octave_value& size_arg,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10259
diff changeset
2245 const octave_value& prec_arg, const octave_value& skip_arg,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10259
diff changeset
2246 const octave_value& arch_arg, octave_idx_type& count)
2116
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
2247 {
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
2248 count = -1;
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
2249
20703
85e5efae848a eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20700
diff changeset
2250 Array<double> size = size_arg.xvector_value ("fread: invalid SIZE specified");
85e5efae848a eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20700
diff changeset
2251
85e5efae848a eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20700
diff changeset
2252 std::string prec = prec_arg.xstring_value ("fread: PRECISION must be a string");
85e5efae848a eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20700
diff changeset
2253
85e5efae848a eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20700
diff changeset
2254 int block_size = 1;
85e5efae848a eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20700
diff changeset
2255 oct_data_conv::data_type input_type;
85e5efae848a eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20700
diff changeset
2256 oct_data_conv::data_type output_type;
85e5efae848a eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20700
diff changeset
2257
20741
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20713
diff changeset
2258 try
2116
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
2259 {
20741
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20713
diff changeset
2260 oct_data_conv::string_to_data_type (prec, block_size,
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20713
diff changeset
2261 input_type, output_type);
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20713
diff changeset
2262 }
20785
b6f2909e7f94 always throw exception after debugging with debug_on_error
John W. Eaton <jwe@octave.org>
parents: 20764
diff changeset
2263 catch (octave_execution_exception& e)
20741
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20713
diff changeset
2264 {
20756
200ae1d650b7 propagate octave_execution_exception objects through try/catch blocks
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2265 error (e, "fread: invalid PRECISION specified");
20741
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20713
diff changeset
2266 }
4944
44046bbaa52c [project @ 2004-08-31 05:30:46 by jwe]
jwe
parents: 4933
diff changeset
2267
20747
ee2743bd07a8 eliminate various compiler warnings
John W. Eaton <jwe@octave.org>
parents: 20741
diff changeset
2268 int skip = 0;
3202
44d82b369c78 [project @ 1998-10-29 20:27:57 by jwe]
jwe
parents: 3180
diff changeset
2269
20741
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20713
diff changeset
2270 try
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20713
diff changeset
2271 {
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20713
diff changeset
2272 skip = skip_arg.int_value (true);
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20713
diff changeset
2273 }
20785
b6f2909e7f94 always throw exception after debugging with debug_on_error
John W. Eaton <jwe@octave.org>
parents: 20764
diff changeset
2274 catch (octave_execution_exception& e)
20741
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20713
diff changeset
2275 {
20756
200ae1d650b7 propagate octave_execution_exception objects through try/catch blocks
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2276 error (e, "fread: SKIP must be an integer");
20741
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20713
diff changeset
2277 }
2116
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
2278
20741
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20713
diff changeset
2279 std::string arch = arch_arg.xstring_value ("fread: ARCH architecture type must be a string");
2116
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
2280
21739
c4ab2e54f100 use namespace for oct_mach_info class
John W. Eaton <jwe@octave.org>
parents: 21736
diff changeset
2281 octave::mach_info::float_format flt_fmt
c4ab2e54f100 use namespace for oct_mach_info class
John W. Eaton <jwe@octave.org>
parents: 21736
diff changeset
2282 = octave::mach_info::string_to_float_format (arch);
20741
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20713
diff changeset
2283
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20713
diff changeset
2284 return os.read (size, block_size, input_type, output_type, skip,
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20713
diff changeset
2285 flt_fmt, count);
2116
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
2286 }
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
2287
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
2288 DEFUN (fread, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2289 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2290 @deftypefn {} {@var{val} =} fread (@var{fid})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2291 @deftypefnx {} {@var{val} =} fread (@var{fid}, @var{size})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2292 @deftypefnx {} {@var{val} =} fread (@var{fid}, @var{size}, @var{precision})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2293 @deftypefnx {} {@var{val} =} fread (@var{fid}, @var{size}, @var{precision}, @var{skip})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2294 @deftypefnx {} {@var{val} =} fread (@var{fid}, @var{size}, @var{precision}, @var{skip}, @var{arch})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2295 @deftypefnx {} {[@var{val}, @var{count}] =} fread (@dots{})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2296 Read binary data from the file specified by the file descriptor @var{fid}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2297
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2298 The optional argument @var{size} specifies the amount of data to read
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2299 and may be one of
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2300
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2301 @table @code
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2302 @item Inf
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2303 Read as much as possible, returning a column vector.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2304
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2305 @item @var{nr}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2306 Read up to @var{nr} elements, returning a column vector.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2307
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2308 @item [@var{nr}, Inf]
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2309 Read as much as possible, returning a matrix with @var{nr} rows. If the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2310 number of elements read is not an exact multiple of @var{nr}, the last
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2311 column is padded with zeros.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2312
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2313 @item [@var{nr}, @var{nc}]
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2314 Read up to @code{@var{nr} * @var{nc}} elements, returning a matrix with
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2315 @var{nr} rows. If the number of elements read is not an exact multiple
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2316 of @var{nr}, the last column is padded with zeros.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2317 @end table
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2318
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2319 @noindent
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2320 If @var{size} is omitted, a value of @code{Inf} is assumed.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2321
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2322 The optional argument @var{precision} is a string specifying the type of
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2323 data to read and may be one of
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2324
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2325 @table @asis
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2326 @item @qcode{"schar"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2327 @itemx @qcode{"signed char"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2328 Signed character.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2329
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2330 @item @qcode{"uchar"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2331 @itemx @qcode{"unsigned char"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2332 Unsigned character.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2333
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2334 @item @qcode{"int8"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2335 @itemx @qcode{"integer*1"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2336
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2337 8-bit signed integer.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2338
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2339 @item @qcode{"int16"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2340 @itemx @qcode{"integer*2"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2341 16-bit signed integer.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2342
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2343 @item @qcode{"int32"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2344 @itemx @qcode{"integer*4"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2345 32-bit signed integer.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2346
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2347 @item @qcode{"int64"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2348 @itemx @qcode{"integer*8"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2349 64-bit signed integer.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2350
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2351 @item @qcode{"uint8"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2352 8-bit unsigned integer.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2353
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2354 @item @qcode{"uint16"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2355 16-bit unsigned integer.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2356
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2357 @item @qcode{"uint32"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2358 32-bit unsigned integer.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2359
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2360 @item @qcode{"uint64"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2361 64-bit unsigned integer.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2362
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2363 @item @qcode{"single"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2364 @itemx @qcode{"float32"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2365 @itemx @qcode{"real*4"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2366 32-bit floating point number.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2367
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2368 @item @qcode{"double"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2369 @itemx @qcode{"float64"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2370 @itemx @qcode{"real*8"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2371 64-bit floating point number.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2372
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2373 @item @qcode{"char"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2374 @itemx @qcode{"char*1"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2375 Single character.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2376
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2377 @item @qcode{"short"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2378 Short integer (size is platform dependent).
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2379
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2380 @item @qcode{"int"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2381 Integer (size is platform dependent).
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2382
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2383 @item @qcode{"long"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2384 Long integer (size is platform dependent).
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2385
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2386 @item @qcode{"ushort"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2387 @itemx @qcode{"unsigned short"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2388 Unsigned short integer (size is platform dependent).
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2389
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2390 @item @qcode{"uint"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2391 @itemx @qcode{"unsigned int"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2392 Unsigned integer (size is platform dependent).
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2393
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2394 @item @qcode{"ulong"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2395 @itemx @qcode{"unsigned long"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2396 Unsigned long integer (size is platform dependent).
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2397
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2398 @item @qcode{"float"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2399 Single precision floating point number (size is platform dependent).
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2400 @end table
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2401
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2402 @noindent
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2403 The default precision is @qcode{"uchar"}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2404
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2405 The @var{precision} argument may also specify an optional repeat
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2406 count. For example, @samp{32*single} causes @code{fread} to read
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2407 a block of 32 single precision floating point numbers. Reading in
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2408 blocks is useful in combination with the @var{skip} argument.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2409
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2410 The @var{precision} argument may also specify a type conversion.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2411 For example, @samp{int16=>int32} causes @code{fread} to read 16-bit
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2412 integer values and return an array of 32-bit integer values. By
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2413 default, @code{fread} returns a double precision array. The special
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2414 form @samp{*TYPE} is shorthand for @samp{TYPE=>TYPE}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2415
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2416 The conversion and repeat counts may be combined. For example, the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2417 specification @samp{32*single=>single} causes @code{fread} to read
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2418 blocks of single precision floating point values and return an array
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2419 of single precision values instead of the default array of double
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2420 precision values.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2421
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2422 The optional argument @var{skip} specifies the number of bytes to skip
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2423 after each element (or block of elements) is read. If it is not
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2424 specified, a value of 0 is assumed. If the final block read is not
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2425 complete, the final skip is omitted. For example,
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2426
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2427 @example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2428 fread (f, 10, "3*single=>single", 8)
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2429 @end example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2430
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2431 @noindent
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2432 will omit the final 8-byte skip because the last read will not be
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2433 a complete block of 3 values.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2434
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2435 The optional argument @var{arch} is a string specifying the data format
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2436 for the file. Valid values are
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2437
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2438 @table @asis
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2439 @item @qcode{"native"} or @qcode{"n"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2440 The format of the current machine.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2441
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2442 @item @qcode{"ieee-be"} or @qcode{"b"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2443 IEEE big endian.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2444
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2445 @item @qcode{"ieee-le"} or @qcode{"l"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2446 IEEE little endian.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2447 @end table
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2448
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2449 If no @var{arch} is given the value used in the call to @code{fopen} which
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2450 created the file descriptor is used. Otherwise, the value specified with
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2451 @code{fread} overrides that of @code{fopen} and determines the data format.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2452
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2453 The output argument @var{val} contains the data read from the file.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2454
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2455 The optional return value @var{count} contains the number of elements read.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2456 @seealso{fwrite, fgets, fgetl, fscanf, fopen}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2457 @end deftypefn */)
529
7ea224e713cd [project @ 1994-07-20 18:54:27 by jwe]
jwe
parents: 506
diff changeset
2458 {
2116
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
2459 int nargin = args.length ();
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
2460
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2461 if (nargin < 1 || nargin > 5)
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2462 print_usage ();
2116
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
2463
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2464 octave_stream os = octave_stream_list::lookup (args(0), "fread");
2116
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
2465
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2466 octave_value size = lo_ieee_inf_value ();
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2467 octave_value prec = "uchar";
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2468 octave_value skip = 0;
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2469 octave_value arch = "unknown";
2116
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
2470
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2471 int idx = 1;
2116
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
2472
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2473 if (nargin > idx && ! args(idx).is_string ())
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2474 size = args(idx++);
2116
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
2475
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2476 if (nargin > idx)
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2477 prec = args(idx++);
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2478
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2479 if (nargin > idx)
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2480 skip = args(idx++);
4257
e556870ce8f0 [project @ 2003-01-02 03:14:41 by jwe]
jwe
parents: 4254
diff changeset
2481
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2482 if (nargin > idx)
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2483 arch = args(idx++);
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2484 else if (skip.is_string ())
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2485 {
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2486 arch = skip;
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2487 skip = 0;
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2488 }
2116
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
2489
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2490 octave_idx_type count = -1;
20561
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
2491
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2492 octave_value tmp = do_fread (os, size, prec, skip, arch, count);
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2493
20918
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
2494 return ovl (tmp, count);
529
7ea224e713cd [project @ 1994-07-20 18:54:27 by jwe]
jwe
parents: 506
diff changeset
2495 }
7ea224e713cd [project @ 1994-07-20 18:54:27 by jwe]
jwe
parents: 506
diff changeset
2496
2116
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
2497 static int
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
2498 do_fwrite (octave_stream& os, const octave_value& data,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10259
diff changeset
2499 const octave_value& prec_arg, const octave_value& skip_arg,
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10259
diff changeset
2500 const octave_value& arch_arg)
2116
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
2501 {
20700
68e3a747ca02 rename octave_value value extractors that accept error message args
John W. Eaton <jwe@octave.org>
parents: 20687
diff changeset
2502 std::string prec = prec_arg.xstring_value ("fwrite: PRECISION must be a string");
20581
fd0efcdb3718 use new string_value method to handle value extraction errors
John W. Eaton <jwe@octave.org>
parents: 20561
diff changeset
2503
fd0efcdb3718 use new string_value method to handle value extraction errors
John W. Eaton <jwe@octave.org>
parents: 20561
diff changeset
2504 int block_size = 1;
fd0efcdb3718 use new string_value method to handle value extraction errors
John W. Eaton <jwe@octave.org>
parents: 20561
diff changeset
2505 oct_data_conv::data_type output_type;
2116
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
2506
20741
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20713
diff changeset
2507 try
20581
fd0efcdb3718 use new string_value method to handle value extraction errors
John W. Eaton <jwe@octave.org>
parents: 20561
diff changeset
2508 {
20741
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20713
diff changeset
2509 oct_data_conv::string_to_data_type (prec, block_size, output_type);
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20713
diff changeset
2510 }
20785
b6f2909e7f94 always throw exception after debugging with debug_on_error
John W. Eaton <jwe@octave.org>
parents: 20764
diff changeset
2511 catch (octave_execution_exception& e)
20741
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20713
diff changeset
2512 {
20756
200ae1d650b7 propagate octave_execution_exception objects through try/catch blocks
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2513 error (e, "fwrite: invalid PRECISION specified");
20741
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20713
diff changeset
2514 }
2116
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
2515
20747
ee2743bd07a8 eliminate various compiler warnings
John W. Eaton <jwe@octave.org>
parents: 20741
diff changeset
2516 int skip = 0;
3202
44d82b369c78 [project @ 1998-10-29 20:27:57 by jwe]
jwe
parents: 3180
diff changeset
2517
20741
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20713
diff changeset
2518 try
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20713
diff changeset
2519 {
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20713
diff changeset
2520 skip = skip_arg.int_value (true);
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20713
diff changeset
2521 }
20785
b6f2909e7f94 always throw exception after debugging with debug_on_error
John W. Eaton <jwe@octave.org>
parents: 20764
diff changeset
2522 catch (octave_execution_exception& e)
20741
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20713
diff changeset
2523 {
20756
200ae1d650b7 propagate octave_execution_exception objects through try/catch blocks
John W. Eaton <jwe@octave.org>
parents: 20747
diff changeset
2524 error (e, "fwrite: SKIP must be an integer");
20741
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20713
diff changeset
2525 }
2116
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
2526
20741
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20713
diff changeset
2527 std::string arch = arch_arg.xstring_value ("fwrite: ARCH architecture type must be a string");
2116
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
2528
21739
c4ab2e54f100 use namespace for oct_mach_info class
John W. Eaton <jwe@octave.org>
parents: 21736
diff changeset
2529 octave::mach_info::float_format flt_fmt
c4ab2e54f100 use namespace for oct_mach_info class
John W. Eaton <jwe@octave.org>
parents: 21736
diff changeset
2530 = octave::mach_info::string_to_float_format (arch);
20741
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20713
diff changeset
2531
a5ab31b52ae8 eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20713
diff changeset
2532 return os.write (data, block_size, output_type, skip, flt_fmt);
2116
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
2533 }
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
2534
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
2535 DEFUN (fwrite, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2536 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2537 @deftypefn {} {} fwrite (@var{fid}, @var{data})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2538 @deftypefnx {} {} fwrite (@var{fid}, @var{data}, @var{precision})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2539 @deftypefnx {} {} fwrite (@var{fid}, @var{data}, @var{precision}, @var{skip})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2540 @deftypefnx {} {} fwrite (@var{fid}, @var{data}, @var{precision}, @var{skip}, @var{arch})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2541 @deftypefnx {} {@var{count} =} fwrite (@dots{})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2542 Write data in binary form to the file specified by the file descriptor
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2543 @var{fid}, returning the number of values @var{count} successfully written
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2544 to the file.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2545
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2546 The argument @var{data} is a matrix of values that are to be written to
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2547 the file. The values are extracted in column-major order.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2548
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2549 The remaining arguments @var{precision}, @var{skip}, and @var{arch} are
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2550 optional, and are interpreted as described for @code{fread}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2551
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2552 The behavior of @code{fwrite} is undefined if the values in @var{data}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2553 are too large to fit in the specified precision.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2554 @seealso{fread, fputs, fprintf, fopen}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2555 @end deftypefn */)
1181
b2b7c5264ac2 [project @ 1995-03-28 23:51:50 by jwe]
jwe
parents: 1086
diff changeset
2556 {
2116
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
2557 int nargin = args.length ();
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
2558
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2559 if (nargin < 2 || nargin > 5)
5823
080c08b192d8 [project @ 2006-05-19 05:32:17 by jwe]
jwe
parents: 5775
diff changeset
2560 print_usage ();
2116
99658f9b74c8 [project @ 1996-05-10 07:20:15 by jwe]
jwe
parents: 2095
diff changeset
2561
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2562 octave_stream os = octave_stream_list::lookup (args(0), "fwrite");
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2563
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2564 octave_value prec = "uchar";
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2565 octave_value skip = 0;
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2566 octave_value arch = "unknown";
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2567
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2568 int idx = 1;
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2569
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2570 octave_value data = args(idx++);
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2571
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2572 if (nargin > idx)
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2573 prec = args(idx++);
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2574
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2575 if (nargin > idx)
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2576 skip = args(idx++);
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2577
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2578 if (nargin > idx)
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2579 arch = args(idx++);
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2580 else if (skip.is_string ())
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2581 {
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2582 arch = skip;
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2583 skip = 0;
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2584 }
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2585
21127
df7891224709 maint: Use "return ovl (...)" in DEFUN macros.
Rik <rik@octave.org>
parents: 21126
diff changeset
2586 return ovl (do_fwrite (os, data, prec, skip, arch));
1181
b2b7c5264ac2 [project @ 1995-03-28 23:51:50 by jwe]
jwe
parents: 1086
diff changeset
2587 }
b2b7c5264ac2 [project @ 1995-03-28 23:51:50 by jwe]
jwe
parents: 1086
diff changeset
2588
5906
bdf16e1564e1 [project @ 2006-07-27 03:07:14 by jwe]
jwe
parents: 5823
diff changeset
2589 DEFUNX ("feof", Ffeof, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2590 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2591 @deftypefn {} {@var{status} =} feof (@var{fid})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2592 Return 1 if an end-of-file condition has been encountered for the file
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2593 specified by file descriptor @var{fid} and 0 otherwise.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2594
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2595 Note that @code{feof} will only return 1 if the end of the file has already
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2596 been encountered, not if the next read operation will result in an
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2597 end-of-file condition.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2598 @seealso{fread, frewind, fseek, fclear, fopen}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2599 @end deftypefn */)
529
7ea224e713cd [project @ 1994-07-20 18:54:27 by jwe]
jwe
parents: 506
diff changeset
2600 {
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2601 if (args.length () != 1)
5823
080c08b192d8 [project @ 2006-05-19 05:32:17 by jwe]
jwe
parents: 5775
diff changeset
2602 print_usage ();
444
ba637cc5c5f3 [project @ 1994-06-02 17:15:07 by jwe]
jwe
parents: 372
diff changeset
2603
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2604 octave_stream os = octave_stream_list::lookup (args(0), "feof");
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2605
21127
df7891224709 maint: Use "return ovl (...)" in DEFUN macros.
Rik <rik@octave.org>
parents: 21126
diff changeset
2606 return ovl (os.eof () ? 1.0 : 0.0);
444
ba637cc5c5f3 [project @ 1994-06-02 17:15:07 by jwe]
jwe
parents: 372
diff changeset
2607 }
ba637cc5c5f3 [project @ 1994-06-02 17:15:07 by jwe]
jwe
parents: 372
diff changeset
2608
5906
bdf16e1564e1 [project @ 2006-07-27 03:07:14 by jwe]
jwe
parents: 5823
diff changeset
2609 DEFUNX ("ferror", Fferror, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2610 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2611 @deftypefn {} {@var{msg} =} ferror (@var{fid})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2612 @deftypefnx {} {[@var{msg}, @var{err}] =} ferror (@var{fid})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2613 @deftypefnx {} {[@var{dots}] =} ferror (@var{fid}, "clear")
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2614 Query the error status of the stream specified by file descriptor @var{fid}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2615
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2616 If an error condition exists then return a string @var{msg} describing the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2617 error. Otherwise, return an empty string @qcode{""}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2618
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2619 The second input @qcode{"clear"} is optional. If supplied, the error
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2620 state on the stream will be cleared.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2621
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2622 The optional second output is a numeric indication of the error status.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2623 @var{err} is 1 if an error condition has been encountered and 0 otherwise.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2624
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2625 Note that @code{ferror} indicates if an error has already occurred, not
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2626 whether the next operation will result in an error condition.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2627 @seealso{fclear, fopen}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2628 @end deftypefn */)
1230
92609e161b29 [project @ 1995-04-10 01:08:57 by jwe]
jwe
parents: 1181
diff changeset
2629 {
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
2630 int nargin = args.length ();
1230
92609e161b29 [project @ 1995-04-10 01:08:57 by jwe]
jwe
parents: 1181
diff changeset
2631
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2632 if (nargin < 1 || nargin > 2)
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2633 print_usage ();
1230
92609e161b29 [project @ 1995-04-10 01:08:57 by jwe]
jwe
parents: 1181
diff changeset
2634
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2635 octave_stream os = octave_stream_list::lookup (args(0), "ferror");
1230
92609e161b29 [project @ 1995-04-10 01:08:57 by jwe]
jwe
parents: 1181
diff changeset
2636
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2637 bool clear = false;
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
2638
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2639 if (nargin == 2)
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2640 {
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2641 std::string opt = args(1).string_value ();
20561
c41595061186 eliminate more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents: 20457
diff changeset
2642
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2643 clear = (opt == "clear");
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2644 }
1755
3a9462b655f1 [project @ 1996-01-22 04:47:22 by jwe]
jwe
parents: 1750
diff changeset
2645
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2646 int error_number = 0;
1230
92609e161b29 [project @ 1995-04-10 01:08:57 by jwe]
jwe
parents: 1181
diff changeset
2647
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2648 std::string error_message = os.error (clear, error_number);
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2649
20918
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
2650 return ovl (error_message, error_number);
1230
92609e161b29 [project @ 1995-04-10 01:08:57 by jwe]
jwe
parents: 1181
diff changeset
2651 }
92609e161b29 [project @ 1995-04-10 01:08:57 by jwe]
jwe
parents: 1181
diff changeset
2652
18477
aa7ca90ce746 avoid problems if popen is a macro
John W. Eaton <jwe@octave.org>
parents: 18040
diff changeset
2653 DEFUNX ("popen", Fpopen, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2654 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2655 @deftypefn {} {@var{fid} =} popen (@var{command}, @var{mode})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2656 Start a process and create a pipe.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2657
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2658 The name of the command to run is given by @var{command}. The argument
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2659 @var{mode} may be
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2660
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2661 @table @code
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2662 @item @qcode{"r"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2663 The pipe will be connected to the standard output of the process, and
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2664 open for reading.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2665
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2666 @item @qcode{"w"}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2667 The pipe will be connected to the standard input of the process, and
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2668 open for writing.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2669 @end table
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2670
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2671 The file identifier corresponding to the input or output stream of the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2672 process is returned in @var{fid}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2673
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2674 For example:
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2675
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2676 @example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2677 @group
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2678 fid = popen ("ls -ltr / | tail -3", "r");
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2679 while (ischar (s = fgets (fid)))
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2680 fputs (stdout, s);
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2681 endwhile
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2682
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2683 @print{} drwxr-xr-x 33 root root 3072 Feb 15 13:28 etc
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2684 @print{} drwxr-xr-x 3 root root 1024 Feb 15 13:28 lib
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2685 @print{} drwxrwxrwt 15 root root 2048 Feb 17 14:53 tmp
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2686 @end group
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2687 @end example
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2688 @seealso{popen2}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2689 @end deftypefn */)
1230
92609e161b29 [project @ 1995-04-10 01:08:57 by jwe]
jwe
parents: 1181
diff changeset
2690 {
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2691 if (args.length () != 2)
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2692 print_usage ();
1230
92609e161b29 [project @ 1995-04-10 01:08:57 by jwe]
jwe
parents: 1181
diff changeset
2693
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2694 std::string name = args(0).xstring_value ("popen: COMMAND must be a string");
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2695 std::string mode = args(1).xstring_value ("popen: MODE must be a string");
1230
92609e161b29 [project @ 1995-04-10 01:08:57 by jwe]
jwe
parents: 1181
diff changeset
2696
20918
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
2697 octave_value retval;
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
2698
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2699 if (mode == "r")
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2700 {
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2701 octave_stream ips = octave_iprocstream::create (name);
1230
92609e161b29 [project @ 1995-04-10 01:08:57 by jwe]
jwe
parents: 1181
diff changeset
2702
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2703 retval = octave_stream_list::insert (ips);
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2704 }
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2705 else if (mode == "w")
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2706 {
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2707 octave_stream ops = octave_oprocstream::create (name);
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2708
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2709 retval = octave_stream_list::insert (ops);
2095
36903d507b0e [project @ 1996-04-28 09:00:07 by jwe]
jwe
parents: 1996
diff changeset
2710 }
1230
92609e161b29 [project @ 1995-04-10 01:08:57 by jwe]
jwe
parents: 1181
diff changeset
2711 else
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2712 error ("popen: invalid MODE specified");
1230
92609e161b29 [project @ 1995-04-10 01:08:57 by jwe]
jwe
parents: 1181
diff changeset
2713
92609e161b29 [project @ 1995-04-10 01:08:57 by jwe]
jwe
parents: 1181
diff changeset
2714 return retval;
92609e161b29 [project @ 1995-04-10 01:08:57 by jwe]
jwe
parents: 1181
diff changeset
2715 }
92609e161b29 [project @ 1995-04-10 01:08:57 by jwe]
jwe
parents: 1181
diff changeset
2716
16308
122c1a7a3004 avoid trouble if pclose is a macro
John W. Eaton <jwe@octave.org>
parents: 16099
diff changeset
2717 DEFUNX ("pclose", Fpclose, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2718 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2719 @deftypefn {} {} pclose (@var{fid})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2720 Close a file identifier that was opened by @code{popen}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2721
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2722 The function @code{fclose} may also be used for the same purpose.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2723 @seealso{fclose, popen}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2724 @end deftypefn */)
1230
92609e161b29 [project @ 1995-04-10 01:08:57 by jwe]
jwe
parents: 1181
diff changeset
2725 {
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2726 if (args.length () != 1)
5823
080c08b192d8 [project @ 2006-05-19 05:32:17 by jwe]
jwe
parents: 5775
diff changeset
2727 print_usage ();
1379
e29697484908 [project @ 1995-09-12 00:41:58 by jwe]
jwe
parents: 1377
diff changeset
2728
21127
df7891224709 maint: Use "return ovl (...)" in DEFUN macros.
Rik <rik@octave.org>
parents: 21126
diff changeset
2729 return ovl (octave_stream_list::remove (args(0), "pclose"));
1379
e29697484908 [project @ 1995-09-12 00:41:58 by jwe]
jwe
parents: 1377
diff changeset
2730 }
e29697484908 [project @ 1995-09-12 00:41:58 by jwe]
jwe
parents: 1377
diff changeset
2731
19278
6ca096827123 Use tempname() rather than tmpnam() in core Octave.
Rik <rik@octave.org>
parents: 19268
diff changeset
2732 DEFUN (tempname, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2733 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2734 @deftypefn {} {@var{fname} =} tempname ()
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2735 @deftypefnx {} {@var{fname} =} tempname (@var{dir})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2736 @deftypefnx {} {@var{fname} =} tempname (@var{dir}, @var{prefix})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2737 Return a unique temporary filename as a string.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2738
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2739 If @var{prefix} is omitted, a value of @qcode{"oct-"} is used.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2740
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2741 If @var{dir} is also omitted, the default directory for temporary files
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2742 (@code{P_tmpdir}) is used. If @var{dir} is provided, it must exist,
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2743 otherwise the default directory for temporary files is used.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2744
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2745 Programming Note: Because the named file is not opened by @code{tempname},
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2746 it is possible, though relatively unlikely, that it will not be available
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2747 by the time your program attempts to open it. If this is a concern,
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2748 see @code{tmpfile}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2749 @seealso{mkstemp, tempdir, P_tmpdir, tmpfile}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2750 @end deftypefn */)
1802
8173b1fa052d [project @ 1996-01-29 06:10:18 by jwe]
jwe
parents: 1799
diff changeset
2751 {
20812
d9ca869ca124 maint: Clean-up more instances of print_usage().
Rik <rik@octave.org>
parents: 20805
diff changeset
2752 int nargin = args.length ();
2936
87cb7614257d [project @ 1997-05-06 05:46:00 by jwe]
jwe
parents: 2926
diff changeset
2753
20812
d9ca869ca124 maint: Clean-up more instances of print_usage().
Rik <rik@octave.org>
parents: 20805
diff changeset
2754 if (nargin > 2)
5823
080c08b192d8 [project @ 2006-05-19 05:32:17 by jwe]
jwe
parents: 5775
diff changeset
2755 print_usage ();
1802
8173b1fa052d [project @ 1996-01-29 06:10:18 by jwe]
jwe
parents: 1799
diff changeset
2756
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2757 std::string dir;
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2758
20812
d9ca869ca124 maint: Clean-up more instances of print_usage().
Rik <rik@octave.org>
parents: 20805
diff changeset
2759 if (nargin > 0)
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2760 dir = args(0).xstring_value ("tempname: DIR must be a string");
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2761
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2762 std::string pfx ("oct-");
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2763
20812
d9ca869ca124 maint: Clean-up more instances of print_usage().
Rik <rik@octave.org>
parents: 20805
diff changeset
2764 if (nargin > 1)
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2765 pfx = args(1).xstring_value ("tempname: PREFIX must be a string");
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2766
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
2767 return ovl (octave::sys::tempnam (dir, pfx));
1802
8173b1fa052d [project @ 1996-01-29 06:10:18 by jwe]
jwe
parents: 1799
diff changeset
2768 }
8173b1fa052d [project @ 1996-01-29 06:10:18 by jwe]
jwe
parents: 1799
diff changeset
2769
19420
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2770 /*
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2771 %!test
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2772 %! if (ispc ())
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2773 %! envname = "TMP";
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2774 %! else
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2775 %! envname = "TMPDIR";
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2776 %! endif
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2777 %! envdir = getenv (envname);
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2778 %! unsetenv (envname);
21457
05ffef4ebf62 tempname: Fix failing BIST test when P_tmpdir has trailing slash
Mike Miller <mtmiller@octave.org>
parents: 21301
diff changeset
2779 %! ## Strip trailing file separators from P_tmpdir
05ffef4ebf62 tempname: Fix failing BIST test when P_tmpdir has trailing slash
Mike Miller <mtmiller@octave.org>
parents: 21301
diff changeset
2780 %! def_tmpdir = P_tmpdir;
05ffef4ebf62 tempname: Fix failing BIST test when P_tmpdir has trailing slash
Mike Miller <mtmiller@octave.org>
parents: 21301
diff changeset
2781 %! while (length (def_tmpdir) > 2 && strfind (filesep ("all"), def_tmpdir(end)))
05ffef4ebf62 tempname: Fix failing BIST test when P_tmpdir has trailing slash
Mike Miller <mtmiller@octave.org>
parents: 21301
diff changeset
2782 %! def_tmpdir(end) = [];
05ffef4ebf62 tempname: Fix failing BIST test when P_tmpdir has trailing slash
Mike Miller <mtmiller@octave.org>
parents: 21301
diff changeset
2783 %! endwhile
19420
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2784 %! unwind_protect
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2785 %! ## Test 0-argument form
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2786 %! fname = tempname ();
19597
db92e7e28e1f strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 19420
diff changeset
2787 %! [tmpdir, tmpfname] = fileparts (fname);
21457
05ffef4ebf62 tempname: Fix failing BIST test when P_tmpdir has trailing slash
Mike Miller <mtmiller@octave.org>
parents: 21301
diff changeset
2788 %! assert (tmpdir, def_tmpdir);
19420
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2789 %! assert (tmpfname (1:4), "oct-");
19597
db92e7e28e1f strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 19420
diff changeset
2790 %! ## Test 1-argument form
21457
05ffef4ebf62 tempname: Fix failing BIST test when P_tmpdir has trailing slash
Mike Miller <mtmiller@octave.org>
parents: 21301
diff changeset
2791 %! tmp_tmpdir = [def_tmpdir filesep() substr(tmpfname, -5)];
19420
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2792 %! mkdir (tmp_tmpdir) || error ("Unable to create tmp dir");
21457
05ffef4ebf62 tempname: Fix failing BIST test when P_tmpdir has trailing slash
Mike Miller <mtmiller@octave.org>
parents: 21301
diff changeset
2793 %! setenv (envname, def_tmpdir);
19420
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2794 %! fname = tempname (tmp_tmpdir);
19597
db92e7e28e1f strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 19420
diff changeset
2795 %! [tmpdir, tmpfname] = fileparts (fname);
19420
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2796 %! assert (tmpdir, tmp_tmpdir);
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2797 %! assert (tmpfname (1:4), "oct-");
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2798 %! ## Test 1-argument form w/null tmpdir
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2799 %! fname = tempname ("");
19597
db92e7e28e1f strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 19420
diff changeset
2800 %! [tmpdir, tmpfname] = fileparts (fname);
21457
05ffef4ebf62 tempname: Fix failing BIST test when P_tmpdir has trailing slash
Mike Miller <mtmiller@octave.org>
parents: 21301
diff changeset
2801 %! assert (tmpdir, def_tmpdir);
19420
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2802 %! assert (tmpfname (1:4), "oct-");
19597
db92e7e28e1f strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 19420
diff changeset
2803 %! ## Test 2-argument form
19420
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2804 %! fname = tempname (tmp_tmpdir, "pfx-");
19597
db92e7e28e1f strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 19420
diff changeset
2805 %! [tmpdir, tmpfname] = fileparts (fname);
19420
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2806 %! assert (tmpdir, tmp_tmpdir);
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2807 %! assert (tmpfname (1:4), "pfx-");
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2808 %! ## Test 2-argument form w/null prefix
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2809 %! fname = tempname (tmp_tmpdir, "");
19597
db92e7e28e1f strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 19420
diff changeset
2810 %! [tmpdir, tmpfname] = fileparts (fname);
19420
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2811 %! assert (tmpdir, tmp_tmpdir);
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2812 %! assert (tmpfname (1:4), "file");
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2813 %! unwind_protect_cleanup
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2814 %! rmdir (tmp_tmpdir);
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2815 %! if (isempty (envdir))
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2816 %! unsetenv (envname);
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2817 %! else
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2818 %! setenv (envname, envdir);
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2819 %! endif
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2820 %! end_unwind_protect
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2821 */
82f2a3437e02 Fix tempname so it prioritizes user input dir rather than TMPDIR (bug #43844).
Rik <rik@octave.org>
parents: 19403
diff changeset
2822
4326
1cae4472c624 [project @ 2003-02-15 23:14:47 by jwe]
jwe
parents: 4267
diff changeset
2823 DEFUN (tmpfile, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2824 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2825 @deftypefn {} {[@var{fid}, @var{msg}] =} tmpfile ()
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2826 Return the file ID corresponding to a new temporary file with a unique
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2827 name.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2828
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2829 The file is opened in binary read/write (@qcode{"w+b"}) mode and will be
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2830 deleted automatically when it is closed or when Octave exits.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2831
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2832 If successful, @var{fid} is a valid file ID and @var{msg} is an empty
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2833 string. Otherwise, @var{fid} is -1 and @var{msg} contains a
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2834 system-dependent error message.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2835 @seealso{tempname, mkstemp, tempdir, P_tmpdir}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2836 @end deftypefn */)
4326
1cae4472c624 [project @ 2003-02-15 23:14:47 by jwe]
jwe
parents: 4267
diff changeset
2837 {
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2838 if (args.length () != 0)
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2839 print_usage ();
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2840
20918
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
2841 octave_value_list retval;
4326
1cae4472c624 [project @ 2003-02-15 23:14:47 by jwe]
jwe
parents: 4267
diff changeset
2842
21991
80659e58609f provide wrapper for tmpfile (bug #48337)
John W. Eaton <jwe@octave.org>
parents: 21966
diff changeset
2843 FILE *fid = octave_tmpfile_wrapper ();
4326
1cae4472c624 [project @ 2003-02-15 23:14:47 by jwe]
jwe
parents: 4267
diff changeset
2844
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2845 if (fid)
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2846 {
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2847 std::string nm;
4326
1cae4472c624 [project @ 2003-02-15 23:14:47 by jwe]
jwe
parents: 4267
diff changeset
2848
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2849 std::ios::openmode md = fopen_mode_to_ios_mode ("w+b");
4326
1cae4472c624 [project @ 2003-02-15 23:14:47 by jwe]
jwe
parents: 4267
diff changeset
2850
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2851 octave_stream s = octave_stdiostream::create (nm, fid, md);
4326
1cae4472c624 [project @ 2003-02-15 23:14:47 by jwe]
jwe
parents: 4267
diff changeset
2852
20918
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
2853 if (! s)
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2854 error ("tmpfile: failed to create octave_stdiostream object");
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2855
20918
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
2856 retval = ovl (octave_stream_list::insert (s), "");
4326
1cae4472c624 [project @ 2003-02-15 23:14:47 by jwe]
jwe
parents: 4267
diff changeset
2857 }
1cae4472c624 [project @ 2003-02-15 23:14:47 by jwe]
jwe
parents: 4267
diff changeset
2858 else
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2859 {
21942
aab79a1885cc limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents: 21919
diff changeset
2860 retval = ovl (-1, std::strerror (errno));
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2861 }
4326
1cae4472c624 [project @ 2003-02-15 23:14:47 by jwe]
jwe
parents: 4267
diff changeset
2862
1cae4472c624 [project @ 2003-02-15 23:14:47 by jwe]
jwe
parents: 4267
diff changeset
2863 return retval;
1cae4472c624 [project @ 2003-02-15 23:14:47 by jwe]
jwe
parents: 4267
diff changeset
2864 }
1cae4472c624 [project @ 2003-02-15 23:14:47 by jwe]
jwe
parents: 4267
diff changeset
2865
1cae4472c624 [project @ 2003-02-15 23:14:47 by jwe]
jwe
parents: 4267
diff changeset
2866 DEFUN (mkstemp, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2867 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2868 @deftypefn {} {[@var{fid}, @var{name}, @var{msg}] =} mkstemp ("@var{template}")
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2869 @deftypefnx {} {[@var{fid}, @var{name}, @var{msg}] =} mkstemp ("@var{template}", @var{delete})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2870 Return the file descriptor @var{fid} corresponding to a new temporary file
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2871 with a unique name created from @var{template}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2872
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2873 The last six characters of @var{template} must be @qcode{"XXXXXX"} and
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2874 these are replaced with a string that makes the filename unique. The file
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2875 is then created with mode read/write and permissions that are system
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2876 dependent (on GNU/Linux systems, the permissions will be 0600 for versions
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2877 of glibc 2.0.7 and later). The file is opened in binary mode and with the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2878 @w{@code{O_EXCL}} flag.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2879
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2880 If the optional argument @var{delete} is supplied and is true, the file will
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2881 be deleted automatically when Octave exits.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2882
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2883 If successful, @var{fid} is a valid file ID, @var{name} is the name of the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2884 file, and @var{msg} is an empty string. Otherwise, @var{fid} is -1,
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2885 @var{name} is empty, and @var{msg} contains a system-dependent error
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2886 message.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2887 @seealso{tempname, tempdir, P_tmpdir, tmpfile, fopen}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2888 @end deftypefn */)
4326
1cae4472c624 [project @ 2003-02-15 23:14:47 by jwe]
jwe
parents: 4267
diff changeset
2889 {
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2890 int nargin = args.length ();
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2891
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2892 if (nargin < 1 || nargin > 2)
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2893 print_usage ();
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2894
20884
f1b2a2dbc0e1 2015 Code Sprint: use ovl () in C++ files.
José Luis García Pallero <jgpallero@gmail.com>
parents: 20861
diff changeset
2895 std::string tmpl8 = args(0).xstring_value ("mkstemp: TEMPLATE argument must be a string");
4326
1cae4472c624 [project @ 2003-02-15 23:14:47 by jwe]
jwe
parents: 4267
diff changeset
2896
20884
f1b2a2dbc0e1 2015 Code Sprint: use ovl () in C++ files.
José Luis García Pallero <jgpallero@gmail.com>
parents: 20861
diff changeset
2897 octave_value_list retval = ovl (-1, "", "");
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2898
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2899 OCTAVE_LOCAL_BUFFER (char, tmp, tmpl8.size () + 1);
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2900 strcpy (tmp, tmpl8.c_str ());
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2901
21888
00f1249f2483 create wrappers for some gnulib functions
John W. Eaton <jwe@octave.org>
parents: 21852
diff changeset
2902 int fd = octave_mkostemp_wrapper (tmp);
4326
1cae4472c624 [project @ 2003-02-15 23:14:47 by jwe]
jwe
parents: 4267
diff changeset
2903
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2904 if (fd < 0)
4326
1cae4472c624 [project @ 2003-02-15 23:14:47 by jwe]
jwe
parents: 4267
diff changeset
2905 {
20884
f1b2a2dbc0e1 2015 Code Sprint: use ovl () in C++ files.
José Luis García Pallero <jgpallero@gmail.com>
parents: 20861
diff changeset
2906 retval(0) = fd;
21942
aab79a1885cc limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents: 21919
diff changeset
2907 retval(2) = std::strerror (errno);
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2908 }
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2909 else
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2910 {
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2911 const char *fopen_mode = "w+b";
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2912
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2913 FILE *fid = fdopen (fd, fopen_mode);
20581
fd0efcdb3718 use new string_value method to handle value extraction errors
John W. Eaton <jwe@octave.org>
parents: 20561
diff changeset
2914
20884
f1b2a2dbc0e1 2015 Code Sprint: use ovl () in C++ files.
José Luis García Pallero <jgpallero@gmail.com>
parents: 20861
diff changeset
2915 if (! fid)
f1b2a2dbc0e1 2015 Code Sprint: use ovl () in C++ files.
José Luis García Pallero <jgpallero@gmail.com>
parents: 20861
diff changeset
2916 {
f1b2a2dbc0e1 2015 Code Sprint: use ovl () in C++ files.
José Luis García Pallero <jgpallero@gmail.com>
parents: 20861
diff changeset
2917 retval(0) = -1;
21942
aab79a1885cc limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents: 21919
diff changeset
2918 retval(2) = std::strerror (errno);
20884
f1b2a2dbc0e1 2015 Code Sprint: use ovl () in C++ files.
José Luis García Pallero <jgpallero@gmail.com>
parents: 20861
diff changeset
2919 }
f1b2a2dbc0e1 2015 Code Sprint: use ovl () in C++ files.
José Luis García Pallero <jgpallero@gmail.com>
parents: 20861
diff changeset
2920 else
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2921 {
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2922 std::string nm = tmp;
20581
fd0efcdb3718 use new string_value method to handle value extraction errors
John W. Eaton <jwe@octave.org>
parents: 20561
diff changeset
2923
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2924 std::ios::openmode md = fopen_mode_to_ios_mode (fopen_mode);
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2925
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2926 octave_stream s = octave_stdiostream::create (nm, fid, md);
20581
fd0efcdb3718 use new string_value method to handle value extraction errors
John W. Eaton <jwe@octave.org>
parents: 20561
diff changeset
2927
20884
f1b2a2dbc0e1 2015 Code Sprint: use ovl () in C++ files.
José Luis García Pallero <jgpallero@gmail.com>
parents: 20861
diff changeset
2928 if (! s)
f1b2a2dbc0e1 2015 Code Sprint: use ovl () in C++ files.
José Luis García Pallero <jgpallero@gmail.com>
parents: 20861
diff changeset
2929 error ("mkstemp: failed to create octave_stdiostream object");
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2930
20884
f1b2a2dbc0e1 2015 Code Sprint: use ovl () in C++ files.
José Luis García Pallero <jgpallero@gmail.com>
parents: 20861
diff changeset
2931 retval(0) = octave_stream_list::insert (s);
f1b2a2dbc0e1 2015 Code Sprint: use ovl () in C++ files.
José Luis García Pallero <jgpallero@gmail.com>
parents: 20861
diff changeset
2932 retval(1) = nm;
f1b2a2dbc0e1 2015 Code Sprint: use ovl () in C++ files.
José Luis García Pallero <jgpallero@gmail.com>
parents: 20861
diff changeset
2933
f1b2a2dbc0e1 2015 Code Sprint: use ovl () in C++ files.
José Luis García Pallero <jgpallero@gmail.com>
parents: 20861
diff changeset
2934 if (nargin == 2 && args(1).is_true ())
f1b2a2dbc0e1 2015 Code Sprint: use ovl () in C++ files.
José Luis García Pallero <jgpallero@gmail.com>
parents: 20861
diff changeset
2935 mark_for_deletion (nm);
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10259
diff changeset
2936 }
4326
1cae4472c624 [project @ 2003-02-15 23:14:47 by jwe]
jwe
parents: 4267
diff changeset
2937 }
1cae4472c624 [project @ 2003-02-15 23:14:47 by jwe]
jwe
parents: 4267
diff changeset
2938
1cae4472c624 [project @ 2003-02-15 23:14:47 by jwe]
jwe
parents: 4267
diff changeset
2939 return retval;
1cae4472c624 [project @ 2003-02-15 23:14:47 by jwe]
jwe
parents: 4267
diff changeset
2940 }
1cae4472c624 [project @ 2003-02-15 23:14:47 by jwe]
jwe
parents: 4267
diff changeset
2941
20200
d9f35ceff9e1 Change mkfifo to use an octal argument for MODE (bug #45054).
Rik <rik@octave.org>
parents: 20198
diff changeset
2942 // FIXME: This routine also exists verbatim in syscalls.cc.
d9f35ceff9e1 Change mkfifo to use an octal argument for MODE (bug #45054).
Rik <rik@octave.org>
parents: 20198
diff changeset
2943 // Maybe change to be a general utility routine.
1400
209ec8f4db00 [project @ 1995-09-15 04:08:08 by jwe]
jwe
parents: 1381
diff changeset
2944 static int
209ec8f4db00 [project @ 1995-09-15 04:08:08 by jwe]
jwe
parents: 1381
diff changeset
2945 convert (int x, int ibase, int obase)
209ec8f4db00 [project @ 1995-09-15 04:08:08 by jwe]
jwe
parents: 1381
diff changeset
2946 {
209ec8f4db00 [project @ 1995-09-15 04:08:08 by jwe]
jwe
parents: 1381
diff changeset
2947 int retval = 0;
209ec8f4db00 [project @ 1995-09-15 04:08:08 by jwe]
jwe
parents: 1381
diff changeset
2948
209ec8f4db00 [project @ 1995-09-15 04:08:08 by jwe]
jwe
parents: 1381
diff changeset
2949 int tmp = x % obase;
209ec8f4db00 [project @ 1995-09-15 04:08:08 by jwe]
jwe
parents: 1381
diff changeset
2950
209ec8f4db00 [project @ 1995-09-15 04:08:08 by jwe]
jwe
parents: 1381
diff changeset
2951 if (tmp > ibase - 1)
20428
7ac907da9fba Use error() rather than ::error() unless explicitly required.
Rik <rik@octave.org>
parents: 20200
diff changeset
2952 error ("umask: invalid digit");
21055
5e00ed38a58b maint: Replace if/error/else paradigm with just if/error.
Rik <rik@octave.org>
parents: 20962
diff changeset
2953
5e00ed38a58b maint: Replace if/error/else paradigm with just if/error.
Rik <rik@octave.org>
parents: 20962
diff changeset
2954 retval = tmp;
5e00ed38a58b maint: Replace if/error/else paradigm with just if/error.
Rik <rik@octave.org>
parents: 20962
diff changeset
2955 int mult = ibase;
5e00ed38a58b maint: Replace if/error/else paradigm with just if/error.
Rik <rik@octave.org>
parents: 20962
diff changeset
2956 while ((x = (x - tmp) / obase))
1400
209ec8f4db00 [project @ 1995-09-15 04:08:08 by jwe]
jwe
parents: 1381
diff changeset
2957 {
21055
5e00ed38a58b maint: Replace if/error/else paradigm with just if/error.
Rik <rik@octave.org>
parents: 20962
diff changeset
2958 tmp = x % obase;
5e00ed38a58b maint: Replace if/error/else paradigm with just if/error.
Rik <rik@octave.org>
parents: 20962
diff changeset
2959
5e00ed38a58b maint: Replace if/error/else paradigm with just if/error.
Rik <rik@octave.org>
parents: 20962
diff changeset
2960 if (tmp > ibase - 1)
5e00ed38a58b maint: Replace if/error/else paradigm with just if/error.
Rik <rik@octave.org>
parents: 20962
diff changeset
2961 error ("umask: invalid digit");
5e00ed38a58b maint: Replace if/error/else paradigm with just if/error.
Rik <rik@octave.org>
parents: 20962
diff changeset
2962
5e00ed38a58b maint: Replace if/error/else paradigm with just if/error.
Rik <rik@octave.org>
parents: 20962
diff changeset
2963 retval += mult * tmp;
5e00ed38a58b maint: Replace if/error/else paradigm with just if/error.
Rik <rik@octave.org>
parents: 20962
diff changeset
2964 mult *= ibase;
1400
209ec8f4db00 [project @ 1995-09-15 04:08:08 by jwe]
jwe
parents: 1381
diff changeset
2965 }
209ec8f4db00 [project @ 1995-09-15 04:08:08 by jwe]
jwe
parents: 1381
diff changeset
2966
209ec8f4db00 [project @ 1995-09-15 04:08:08 by jwe]
jwe
parents: 1381
diff changeset
2967 return retval;
209ec8f4db00 [project @ 1995-09-15 04:08:08 by jwe]
jwe
parents: 1381
diff changeset
2968 }
209ec8f4db00 [project @ 1995-09-15 04:08:08 by jwe]
jwe
parents: 1381
diff changeset
2969
10197
4d433bd2d4dc attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents: 10182
diff changeset
2970 DEFUNX ("umask", Fumask, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2971 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2972 @deftypefn {} {} umask (@var{mask})
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2973 Set the permission mask for file creation.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2974
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2975 The parameter @var{mask} is an integer, interpreted as an octal number.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2976
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2977 If successful, returns the previous value of the mask (as an integer to be
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2978 interpreted as an octal number); otherwise an error message is printed.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2979
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2980 The permission mask is a UNIX concept used when creating new objects on a
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2981 file system such as files, directories, or named FIFOs. The object to be
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2982 created has base permissions in an octal number @var{mode} which are
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2983 modified according to the octal value of @var{mask}. The final permissions
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2984 for the new object are @code{@var{mode} - @var{mask}}.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2985 @seealso{fopen, mkdir, mkfifo}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
2986 @end deftypefn */)
1400
209ec8f4db00 [project @ 1995-09-15 04:08:08 by jwe]
jwe
parents: 1381
diff changeset
2987 {
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2988 if (args.length () != 1)
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2989 print_usage ();
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2990
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2991 int mask = args(0).xint_value ("umask: MASK must be an integer");
1400
209ec8f4db00 [project @ 1995-09-15 04:08:08 by jwe]
jwe
parents: 1381
diff changeset
2992
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2993 if (mask < 0)
20918
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
2994 error ("umask: MASK must be a positive integer value");
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
2995
20918
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
2996 int oct_mask = convert (mask, 8, 10);
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
2997
21733
cb0fdd941d84 use namespace for system file_ops class
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
2998 int status = convert (octave::sys::umask (oct_mask), 10, 8);
1400
209ec8f4db00 [project @ 1995-09-15 04:08:08 by jwe]
jwe
parents: 1381
diff changeset
2999
209ec8f4db00 [project @ 1995-09-15 04:08:08 by jwe]
jwe
parents: 1381
diff changeset
3000 if (status >= 0)
20918
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
3001 return ovl (status);
6f0bd96f93c0 maint: Use new C++ archetype in more files.
Rik <rik@octave.org>
parents: 20898
diff changeset
3002 else
20941
a4f5da7c5463 maint: Replace "octave_value_list ()" with "ovl ()".
Rik <rik@octave.org>
parents: 20940
diff changeset
3003 return ovl ();
1400
209ec8f4db00 [project @ 1995-09-15 04:08:08 by jwe]
jwe
parents: 1381
diff changeset
3004 }
209ec8f4db00 [project @ 1995-09-15 04:08:08 by jwe]
jwe
parents: 1381
diff changeset
3005
5749
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3006 static octave_value
6483
d00da2148c53 [project @ 2007-04-04 02:58:48 by jwe]
jwe
parents: 6456
diff changeset
3007 const_value (const char *, const octave_value_list& args, int val)
2189
d01936305d99 [project @ 1996-05-14 03:31:57 by jwe]
jwe
parents: 2148
diff changeset
3008 {
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
3009 if (args.length () != 0)
5823
080c08b192d8 [project @ 2006-05-19 05:32:17 by jwe]
jwe
parents: 5775
diff changeset
3010 print_usage ();
5749
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3011
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
3012 return octave_value (val);
5749
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3013 }
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3014
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3015 DEFUNX ("P_tmpdir", FP_tmpdir, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3016 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3017 @deftypefn {} {} P_tmpdir ()
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3018 Return the name of the host system's @strong{default} directory for
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3019 temporary files.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3020
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3021 Programming Note: The value returned by @code{P_tmpdir} is always the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3022 default location. This value may not agree with that returned from
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3023 @code{tempdir} if the user has overridden the default with the @env{TMPDIR}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3024 environment variable.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3025 @seealso{tempdir, tempname, mkstemp, tmpfile}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3026 @end deftypefn */)
5749
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3027 {
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
3028 if (args.length () != 0)
5823
080c08b192d8 [project @ 2006-05-19 05:32:17 by jwe]
jwe
parents: 5775
diff changeset
3029 print_usage ();
5749
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3030
21127
df7891224709 maint: Use "return ovl (...)" in DEFUN macros.
Rik <rik@octave.org>
parents: 21126
diff changeset
3031 return ovl (get_P_tmpdir ());
5749
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3032 }
2341
6ee55c0f25a9 [project @ 1996-07-24 10:33:09 by jwe]
jwe
parents: 2324
diff changeset
3033
5749
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3034 // NOTE: the values of SEEK_SET, SEEK_CUR, and SEEK_END have to be
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3035 // this way for Matlab compatibility.
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3036
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3037 DEFUNX ("SEEK_SET", FSEEK_SET, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3038 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3039 @deftypefn {} {} SEEK_SET ()
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3040 @deftypefnx {} {} SEEK_CUR ()
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3041 @deftypefnx {} {} SEEK_END ()
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3042 Return the numerical value to pass to @code{fseek} to perform one of the
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3043 following actions:
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3044
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3045 @table @code
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3046 @item SEEK_SET
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3047 Position file relative to the beginning.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3048
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3049 @item SEEK_CUR
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3050 Position file relative to the current position.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3051
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3052 @item SEEK_END
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3053 Position file relative to the end.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3054 @end table
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3055 @seealso{fseek}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3056 @end deftypefn */)
5749
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3057 {
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3058 return const_value ("SEEK_SET", args, -1);
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3059 }
2189
d01936305d99 [project @ 1996-05-14 03:31:57 by jwe]
jwe
parents: 2148
diff changeset
3060
5749
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3061 DEFUNX ("SEEK_CUR", FSEEK_CUR, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3062 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3063 @deftypefn {} {} SEEK_CUR ()
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3064 Return the numerical value to pass to @code{fseek} to
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3065 position the file pointer relative to the current position.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3066 @seealso{SEEK_SET, SEEK_END}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3067 @end deftypefn */)
5749
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3068 {
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3069 return const_value ("SEEK_CUR", args, 0);
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3070 }
2189
d01936305d99 [project @ 1996-05-14 03:31:57 by jwe]
jwe
parents: 2148
diff changeset
3071
5749
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3072 DEFUNX ("SEEK_END", FSEEK_END, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3073 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3074 @deftypefn {} {} SEEK_END ()
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3075 Return the numerical value to pass to @code{fseek} to
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3076 position the file pointer relative to the end of the file.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3077 @seealso{SEEK_SET, SEEK_CUR}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3078 @end deftypefn */)
5749
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3079 {
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3080 return const_value ("SEEK_END", args, 1);
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3081 }
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3082
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3083 static octave_value
6483
d00da2148c53 [project @ 2007-04-04 02:58:48 by jwe]
jwe
parents: 6456
diff changeset
3084 const_value (const char *, const octave_value_list& args,
10315
57a59eae83cc untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents: 10259
diff changeset
3085 const octave_value& val)
5749
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3086 {
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
3087 if (args.length () != 0)
5823
080c08b192d8 [project @ 2006-05-19 05:32:17 by jwe]
jwe
parents: 5775
diff changeset
3088 print_usage ();
5749
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3089
20805
2d6ddb2b157c eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents: 20785
diff changeset
3090 return octave_value (val);
5749
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3091 }
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3092
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3093 DEFUNX ("stdin", Fstdin, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3094 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3095 @deftypefn {} {} stdin ()
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3096 Return the numeric value corresponding to the standard input stream.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3097
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3098 When Octave is used interactively, stdin is filtered through the command
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3099 line editing functions.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3100 @seealso{stdout, stderr}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3101 @end deftypefn */)
5749
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3102 {
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3103 return const_value ("stdin", args, stdin_file);
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3104 }
2189
d01936305d99 [project @ 1996-05-14 03:31:57 by jwe]
jwe
parents: 2148
diff changeset
3105
5749
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3106 DEFUNX ("stdout", Fstdout, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3107 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3108 @deftypefn {} {} stdout ()
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3109 Return the numeric value corresponding to the standard output stream.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3110
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3111 Data written to the standard output is normally filtered through the pager.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3112 @seealso{stdin, stderr}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3113 @end deftypefn */)
5749
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3114 {
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3115 return const_value ("stdout", args, stdout_file);
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3116 }
2189
d01936305d99 [project @ 1996-05-14 03:31:57 by jwe]
jwe
parents: 2148
diff changeset
3117
5749
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3118 DEFUNX ("stderr", Fstderr, args, ,
21966
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3119 doc: /* -*- texinfo -*-
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3120 @deftypefn {} {} stderr ()
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3121 Return the numeric value corresponding to the standard error stream.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3122
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3123 Even if paging is turned on, the standard error is not sent to the pager.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3124 It is useful for error messages and prompts.
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3125 @seealso{stdin, stdout}
112b20240c87 move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents: 21942
diff changeset
3126 @end deftypefn */)
5749
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3127 {
26beffa9f66f [project @ 2006-04-07 20:35:07 by jwe]
jwe
parents: 5653
diff changeset
3128 return const_value ("stderr", args, stderr_file);
2189
d01936305d99 [project @ 1996-05-14 03:31:57 by jwe]
jwe
parents: 2148
diff changeset
3129 }