Mercurial > hg > octave-lyh
annotate src/file-io.cc @ 14138:72c96de7a403 stable
maint: update copyright notices for 2012
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 02 Jan 2012 14:25:41 -0500 |
parents | cb15c5185b6a |
children | 2ced2f59f523 |
rev | line source |
---|---|
1 | 1 /* |
2 | |
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
13958
diff
changeset
|
3 Copyright (C) 1993-2012 John W. Eaton |
1 | 4 |
5 This file is part of Octave. | |
6 | |
7 Octave is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
1 | 11 |
12 Octave is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
1 | 20 |
21 */ | |
22 | |
2095 | 23 // Originally written by John C. Campbell <jcc@bevo.che.wisc.edu> |
1230 | 24 // |
2095 | 25 // Thomas Baier <baier@ci.tuwien.ac.at> added the original versions of |
26 // the following functions: | |
1230 | 27 // |
2095 | 28 // popen |
29 // pclose | |
30 // execute (now popen2.m) | |
31 // sync_system (now merged with system) | |
32 // async_system (now merged with system) | |
1230 | 33 |
9322 | 34 // Extensively revised by John W. Eaton <jwe@octave.org>, |
2095 | 35 // April 1996. |
1 | 36 |
240 | 37 #ifdef HAVE_CONFIG_H |
1230 | 38 #include <config.h> |
1 | 39 #endif |
40 | |
4797 | 41 #include <cerrno> |
2095 | 42 #include <climits> |
4797 | 43 #include <cstdio> |
1343 | 44 |
3503 | 45 #include <iostream> |
7336 | 46 #include <stack> |
4726 | 47 #include <vector> |
1350 | 48 |
13958
cb15c5185b6a
mkstemp: open file in binary mode (bug #33669)
John W. Eaton <jwe@octave.org>
parents:
13929
diff
changeset
|
49 #include <fcntl.h> |
1230 | 50 #include <sys/types.h> |
1 | 51 #include <unistd.h> |
1350 | 52 |
5325 | 53 #ifdef HAVE_ZLIB_H |
54 #include <zlib.h> | |
55 #endif | |
56 | |
5102 | 57 #include "error.h" |
2926 | 58 #include "file-ops.h" |
6159 | 59 #include "file-stat.h" |
5102 | 60 #include "lo-ieee.h" |
6159 | 61 #include "oct-env.h" |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
62 #include "oct-locbuf.h" |
2926 | 63 |
1352 | 64 #include "defun.h" |
6108 | 65 #include "file-io.h" |
6159 | 66 #include "load-path.h" |
2095 | 67 #include "oct-fstrm.h" |
68 #include "oct-iostrm.h" | |
1377 | 69 #include "oct-map.h" |
1750 | 70 #include "oct-obj.h" |
2095 | 71 #include "oct-prcstrm.h" |
72 #include "oct-stream.h" | |
73 #include "oct-strstrm.h" | |
1 | 74 #include "pager.h" |
444 | 75 #include "sysdep.h" |
1352 | 76 #include "utils.h" |
2370 | 77 #include "variables.h" |
1583 | 78 |
2902 | 79 static octave_value stdin_file; |
80 static octave_value stdout_file; | |
81 static octave_value stderr_file; | |
82 | |
4468 | 83 static octave_stream stdin_stream; |
84 static octave_stream stdout_stream; | |
85 static octave_stream stderr_stream; | |
86 | |
1 | 87 void |
164 | 88 initialize_file_io (void) |
1 | 89 { |
4468 | 90 stdin_stream = octave_istream::create (&std::cin, "stdin"); |
2116 | 91 |
3531 | 92 // This uses octave_stdout (see pager.h), not std::cout so that Octave's |
2116 | 93 // standard output stream will pass through the pager. |
94 | |
4468 | 95 stdout_stream = octave_ostream::create (&octave_stdout, "stdout"); |
2116 | 96 |
4468 | 97 stderr_stream = octave_ostream::create (&std::cerr, "stderr"); |
1 | 98 |
2902 | 99 stdin_file = octave_stream_list::insert (stdin_stream); |
100 stdout_file = octave_stream_list::insert (stdout_stream); | |
101 stderr_file = octave_stream_list::insert (stderr_stream); | |
1 | 102 } |
103 | |
2095 | 104 void |
105 close_files (void) | |
1 | 106 { |
2095 | 107 octave_stream_list::clear (); |
108 } | |
636 | 109 |
5102 | 110 // List of files to delete when we exit or crash. |
111 // | |
5775 | 112 // FIXME -- this should really be static, but that causes |
5102 | 113 // problems on some systems. |
114 std::stack <std::string> tmp_files; | |
115 | |
116 void | |
117 mark_for_deletion (const std::string& file) | |
118 { | |
119 tmp_files.push (file); | |
120 } | |
121 | |
122 void | |
123 cleanup_tmp_files (void) | |
124 { | |
125 while (! tmp_files.empty ()) | |
126 { | |
127 std::string filename = tmp_files.top (); | |
128 tmp_files.pop (); | |
10411 | 129 gnulib::unlink (filename.c_str ()); |
5102 | 130 } |
131 } | |
132 | |
4036 | 133 static std::ios::openmode |
5325 | 134 fopen_mode_to_ios_mode (const std::string& mode_arg) |
1 | 135 { |
4036 | 136 std::ios::openmode retval = std::ios::in; |
896 | 137 |
5325 | 138 if (! mode_arg.empty ()) |
368 | 139 { |
2095 | 140 // Could probably be faster, but does it really matter? |
1766 | 141 |
5325 | 142 std::string mode = mode_arg; |
143 | |
7078 | 144 // 'W' and 'R' are accepted as 'w' and 'r', but we warn about |
145 // them because Matlab says they perform "automatic flushing" | |
146 // but we don't know precisely what action that implies. | |
147 | |
148 size_t pos = mode.find ('W'); | |
149 | |
8021 | 150 if (pos != std::string::npos) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
151 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
152 warning ("fopen: treating mode \"W\" as equivalent to \"w\""); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
153 mode[pos] = 'w'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
154 } |
7078 | 155 |
156 pos = mode.find ('R'); | |
157 | |
8021 | 158 if (pos != std::string::npos) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
159 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
160 warning ("fopen: treating mode \"R\" as equivalent to \"r\""); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
161 mode[pos] = 'r'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
162 } |
7078 | 163 |
164 pos = mode.find ('z'); | |
5325 | 165 |
8021 | 166 if (pos != std::string::npos) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
167 { |
5325 | 168 #if defined (HAVE_ZLIB) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
169 mode.erase (pos, 1); |
5325 | 170 #else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
171 error ("this version of Octave does not support gzipped files"); |
5325 | 172 #endif |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
173 } |
5325 | 174 |
175 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
176 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
177 if (mode == "rt") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
178 retval = std::ios::in; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
179 else if (mode == "wt") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
180 retval = std::ios::out | std::ios::trunc; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
181 else if (mode == "at") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
182 retval = std::ios::out | std::ios::app; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
183 else if (mode == "r+t" || mode == "rt+") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
184 retval = std::ios::in | std::ios::out; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
185 else if (mode == "w+t" || mode == "wt+") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
186 retval = std::ios::in | std::ios::out | std::ios::trunc; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
187 else if (mode == "a+t" || mode == "at+") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
188 retval = std::ios::in | std::ios::out | std::ios::app; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
189 else if (mode == "rb" || mode == "r") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
190 retval = std::ios::in | std::ios::binary; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
191 else if (mode == "wb" || mode == "w") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
192 retval = std::ios::out | std::ios::trunc | std::ios::binary; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
193 else if (mode == "ab" || mode == "a") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
194 retval = std::ios::out | std::ios::app | std::ios::binary; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
195 else if (mode == "r+b" || mode == "rb+" || mode == "r+") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
196 retval = std::ios::in | std::ios::out | std::ios::binary; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
197 else if (mode == "w+b" || mode == "wb+" || mode == "w+") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
198 retval = (std::ios::in | std::ios::out | std::ios::trunc |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
199 | std::ios::binary); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
200 else if (mode == "a+b" || mode == "ab+" || mode == "a+") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
201 retval = (std::ios::in | std::ios::out | std::ios::app |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
202 | std::ios::binary); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
203 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
204 ::error ("invalid mode specified"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
205 } |
1 | 206 } |
207 | |
208 return retval; | |
209 } | |
210 | |
1957 | 211 DEFUN (fclose, args, , |
3372 | 212 "-*- texinfo -*-\n\ |
12575
d0b799dafede
Grammarcheck files for 3.4.1 release.
Rik <octave@nomad.inbox5.com>
parents:
12552
diff
changeset
|
213 @deftypefn {Built-in Function} {} fclose (@var{fid})\n\ |
12552
1cfa3d9adf0a
Document the "all" parameter to fclose.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12546
diff
changeset
|
214 @deftypefnx {Built-in Function} {} fclose (\"all\")\n\ |
11572
7d6d8c1e471f
Grammarcheck Texinfo for files in src directory.
Rik <octave@nomad.inbox5.com>
parents:
11570
diff
changeset
|
215 Close the specified file. If successful, @code{fclose} returns 0,\n\ |
12575
d0b799dafede
Grammarcheck files for 3.4.1 release.
Rik <octave@nomad.inbox5.com>
parents:
12552
diff
changeset
|
216 otherwise, it returns -1. The second form of the @code{fclose} call closes\n\ |
12552
1cfa3d9adf0a
Document the "all" parameter to fclose.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12546
diff
changeset
|
217 all open files except @code{stdout}, @code{stderr}, and @code{stdin}.\n\ |
5642 | 218 @seealso{fopen, fseek, ftell}\n\ |
219 @end deftypefn") | |
529 | 220 { |
4233 | 221 octave_value retval = -1; |
529 | 222 |
223 int nargin = args.length (); | |
224 | |
2095 | 225 if (nargin == 1) |
4233 | 226 retval = octave_stream_list::remove (args(0), "fclose"); |
1 | 227 else |
5823 | 228 print_usage (); |
1 | 229 |
230 return retval; | |
231 } | |
232 | |
5144 | 233 DEFUN (fclear, args, , |
234 "-*- texinfo -*-\n\ | |
235 @deftypefn {Built-in Function} {} fclear (@var{fid})\n\ | |
236 Clear the stream state for the specified file.\n\ | |
237 @end deftypefn") | |
238 { | |
239 octave_value retval; | |
240 | |
241 int nargin = args.length (); | |
242 | |
243 if (nargin == 1) | |
244 { | |
245 int fid = octave_stream_list::get_file_number (args (0)); | |
246 | |
247 octave_stream os = octave_stream_list::lookup (fid, "fclear"); | |
248 | |
249 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
250 os.clearerr (); |
5144 | 251 } |
252 else | |
5823 | 253 print_usage (); |
5144 | 254 |
255 return retval; | |
256 } | |
257 | |
1957 | 258 DEFUN (fflush, args, , |
3372 | 259 "-*- texinfo -*-\n\ |
260 @deftypefn {Built-in Function} {} fflush (@var{fid})\n\ | |
261 Flush output to @var{fid}. This is useful for ensuring that all\n\ | |
262 pending output makes it to the screen before some other event occurs.\n\ | |
263 For example, it is always a good idea to flush the standard output\n\ | |
264 stream before calling @code{input}.\n\ | |
5095 | 265 \n\ |
266 @code{fflush} returns 0 on success and an OS dependent error value\n\ | |
10791
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10411
diff
changeset
|
267 (@minus{}1 on Unix) on error.\n\ |
5642 | 268 @seealso{fopen, fclose}\n\ |
269 @end deftypefn") | |
1181 | 270 { |
4233 | 271 octave_value retval = -1; |
1181 | 272 |
273 int nargin = args.length (); | |
274 | |
2095 | 275 if (nargin == 1) |
276 { | |
5775 | 277 // FIXME -- any way to avoid special case for stdout? |
2609 | 278 |
279 int fid = octave_stream_list::get_file_number (args (0)); | |
280 | |
281 if (fid == 1) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
282 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
283 flush_octave_stdout (); |
2095 | 284 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
285 retval = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
286 } |
2095 | 287 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
288 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
289 octave_stream os = octave_stream_list::lookup (fid, "fflush"); |
2609 | 290 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
291 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
292 retval = os.flush (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
293 } |
2095 | 294 } |
295 else | |
5823 | 296 print_usage (); |
1181 | 297 |
298 return retval; | |
299 } | |
300 | |
2095 | 301 DEFUN (fgetl, args, , |
3372 | 302 "-*- texinfo -*-\n\ |
303 @deftypefn {Built-in Function} {} fgetl (@var{fid}, @var{len})\n\ | |
304 Read characters from a file, stopping after a newline, or EOF,\n\ | |
305 or @var{len} characters have been read. The characters read, excluding\n\ | |
306 the possible trailing newline, are returned as a string.\n\ | |
1339 | 307 \n\ |
3372 | 308 If @var{len} is omitted, @code{fgetl} reads until the next newline\n\ |
309 character.\n\ | |
310 \n\ | |
311 If there are no more characters to read, @code{fgetl} returns @minus{}1.\n\ | |
5642 | 312 @seealso{fread, fscanf}\n\ |
313 @end deftypefn") | |
1339 | 314 { |
4468 | 315 static std::string who = "fgetl"; |
316 | |
2599 | 317 octave_value_list retval; |
318 | |
4233 | 319 retval(1) = 0; |
320 retval(0) = -1; | |
1339 | 321 |
322 int nargin = args.length (); | |
323 | |
324 if (nargin == 1 || nargin == 2) | |
2095 | 325 { |
4468 | 326 octave_stream os = octave_stream_list::lookup (args(0), who); |
2095 | 327 |
3341 | 328 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
329 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
330 octave_value len_arg = (nargin == 2) ? args(1) : octave_value (); |
2095 | 331 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
332 bool err = false; |
2095 | 333 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
334 std::string tmp = os.getl (len_arg, err, who); |
2095 | 335 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
336 if (! (error_state || err)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
337 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
338 retval(1) = tmp.length (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
339 retval(0) = tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
340 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
341 } |
2095 | 342 } |
1339 | 343 else |
5823 | 344 print_usage (); |
1339 | 345 |
346 return retval; | |
347 } | |
348 | |
2095 | 349 DEFUN (fgets, args, , |
3372 | 350 "-*- texinfo -*-\n\ |
12546
39ca02387a32
Improve docstrings for a number of functions.
Rik <octave@nomad.inbox5.com>
parents:
12483
diff
changeset
|
351 @deftypefn {Built-in Function} {} fgets (@var{fid})\n\ |
39ca02387a32
Improve docstrings for a number of functions.
Rik <octave@nomad.inbox5.com>
parents:
12483
diff
changeset
|
352 @deftypefnx {Built-in Function} {} fgets (@var{fid}, @var{len})\n\ |
3372 | 353 Read characters from a file, stopping after a newline, or EOF,\n\ |
354 or @var{len} characters have been read. The characters read, including\n\ | |
355 the possible trailing newline, are returned as a string.\n\ | |
529 | 356 \n\ |
3372 | 357 If @var{len} is omitted, @code{fgets} reads until the next newline\n\ |
358 character.\n\ | |
359 \n\ | |
360 If there are no more characters to read, @code{fgets} returns @minus{}1.\n\ | |
7781 | 361 @seealso{fputs, fopen, fread, fscanf}\n\ |
5642 | 362 @end deftypefn") |
529 | 363 { |
4468 | 364 static std::string who = "fgets"; |
365 | |
2599 | 366 octave_value_list retval; |
367 | |
368 retval(1) = 0.0; | |
369 retval(0) = -1.0; | |
529 | 370 |
371 int nargin = args.length (); | |
372 | |
1338 | 373 if (nargin == 1 || nargin == 2) |
2095 | 374 { |
4468 | 375 octave_stream os = octave_stream_list::lookup (args(0), who); |
2095 | 376 |
3341 | 377 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
378 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
379 octave_value len_arg = (nargin == 2) ? args(1) : octave_value (); |
2095 | 380 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
381 bool err = false; |
2095 | 382 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
383 std::string tmp = os.gets (len_arg, err, who); |
2095 | 384 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
385 if (! (error_state || err)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
386 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
387 retval(1) = tmp.length (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
388 retval(0) = tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
389 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
390 } |
2095 | 391 } |
1338 | 392 else |
5823 | 393 print_usage (); |
529 | 394 |
395 return retval; | |
396 } | |
397 | |
9701 | 398 DEFUN (fskipl, args, , |
399 "-*- texinfo -*-\n\ | |
400 @deftypefn {Built-in Function} {} fskipl (@var{fid}, @var{count})\n\ | |
11572
7d6d8c1e471f
Grammarcheck Texinfo for files in src directory.
Rik <octave@nomad.inbox5.com>
parents:
11570
diff
changeset
|
401 Skip a given number of lines, i.e., discards characters until an end-of-line\n\ |
9701 | 402 is met exactly @var{count}-times, or end-of-file occurs.\n\ |
403 Returns the number of lines skipped (end-of-line sequences encountered).\n\ | |
404 If @var{count} is omitted, it defaults to 1. @var{count} may also be\n\ | |
405 @code{Inf}, in which case lines are skipped to the end of file.\n\ | |
406 This form is suitable for counting lines in a file.\n\ | |
407 @seealso{fgetl, fgets}\n\ | |
408 @end deftypefn") | |
409 { | |
410 static std::string who = "fskipl"; | |
411 | |
412 octave_value retval; | |
413 | |
414 int nargin = args.length (); | |
415 | |
416 if (nargin == 1 || nargin == 2) | |
417 { | |
418 octave_stream os = octave_stream_list::lookup (args(0), who); | |
419 | |
420 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
421 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
422 octave_value count_arg = (nargin == 2) ? args(1) : octave_value (); |
9701 | 423 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
424 bool err = false; |
9701 | 425 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
426 long tmp = os.skipl (count_arg, err, who); |
9701 | 427 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
428 if (! (error_state || err)) |
9701 | 429 retval = tmp; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
430 } |
9701 | 431 } |
432 else | |
433 print_usage (); | |
434 | |
435 return retval; | |
436 } | |
437 | |
438 | |
3340 | 439 static octave_stream |
3523 | 440 do_stream_open (const std::string& name, const std::string& mode, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
441 const std::string& arch, int& fid) |
1 | 442 { |
3340 | 443 octave_stream retval; |
1 | 444 |
2095 | 445 fid = -1; |
1 | 446 |
4036 | 447 std::ios::openmode md = fopen_mode_to_ios_mode (mode); |
1 | 448 |
2095 | 449 if (! error_state) |
450 { | |
2318 | 451 oct_mach_info::float_format flt_fmt = |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
452 oct_mach_info::string_to_float_format (arch); |
1 | 453 |
4798 | 454 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
455 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
456 std::string fname = file_ops::tilde_expand (name); |
6159 | 457 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
458 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
|
459 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
460 if (! (md & std::ios::out |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
461 || octave_env::absolute_pathname (fname) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
462 || octave_env::rooted_relative_pathname (fname))) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
463 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
464 if (! fs.exists ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
465 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
466 std::string tmp |
10250 | 467 = octave_env::make_absolute (load_path::find_file (fname)); |
6159 | 468 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
469 if (! tmp.empty ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
470 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
471 warning_with_id ("Octave:fopen-file-in-path", |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
472 "fopen: file found in load path"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
473 fname = tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
474 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
475 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
476 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
477 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
478 if (! fs.is_dir ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
479 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
480 std::string tmode = mode; |
6405 | 481 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
482 // Use binary mode if 't' is not specified, but don't add |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
483 // 'b' if it is already present. |
6405 | 484 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
485 size_t bpos = tmode.find ('b'); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
486 size_t tpos = tmode.find ('t'); |
7730
b68e44c90afe
file-io.cc (do_stream_open): return -1 for directories
John W. Eaton <jwe@octave.org>
parents:
7708
diff
changeset
|
487 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
488 if (bpos == std::string::npos && tpos == std::string::npos) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
489 tmode += 'b'; |
6405 | 490 |
6905 | 491 #if defined (HAVE_ZLIB) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
492 size_t pos = tmode.find ('z'); |
4798 | 493 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
494 if (pos != std::string::npos) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
495 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
496 tmode.erase (pos, 1); |
7730
b68e44c90afe
file-io.cc (do_stream_open): return -1 for directories
John W. Eaton <jwe@octave.org>
parents:
7708
diff
changeset
|
497 |
12941
9a498efac5f1
use gnulib::fseek and gnulib::fopen
John W. Eaton <jwe@octave.org>
parents:
12918
diff
changeset
|
498 FILE *fptr = gnulib::fopen (fname.c_str (), tmode.c_str ()); |
11004
594adb99a25e
cache file id in octave_tstdiostream class
John W. Eaton <jwe@octave.org>
parents:
10840
diff
changeset
|
499 |
11081
f9284142a060
avoid another use of ::fileno
John W. Eaton <jwe@octave.org>
parents:
11004
diff
changeset
|
500 int fd = fileno (fptr); |
11004
594adb99a25e
cache file id in octave_tstdiostream class
John W. Eaton <jwe@octave.org>
parents:
10840
diff
changeset
|
501 |
594adb99a25e
cache file id in octave_tstdiostream class
John W. Eaton <jwe@octave.org>
parents:
10840
diff
changeset
|
502 gzFile gzf = ::gzdopen (fd, tmode.c_str ()); |
5325 | 503 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
504 if (fptr) |
11004
594adb99a25e
cache file id in octave_tstdiostream class
John W. Eaton <jwe@octave.org>
parents:
10840
diff
changeset
|
505 retval = octave_zstdiostream::create (fname, gzf, fd, |
594adb99a25e
cache file id in octave_tstdiostream class
John W. Eaton <jwe@octave.org>
parents:
10840
diff
changeset
|
506 md, flt_fmt); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
507 else |
10411 | 508 retval.error (gnulib::strerror (errno)); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
509 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
510 else |
5325 | 511 #endif |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
512 { |
12941
9a498efac5f1
use gnulib::fseek and gnulib::fopen
John W. Eaton <jwe@octave.org>
parents:
12918
diff
changeset
|
513 FILE *fptr = gnulib::fopen (fname.c_str (), tmode.c_str ()); |
5325 | 514 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
515 retval = octave_stdiostream::create (fname, fptr, md, flt_fmt); |
5370 | 516 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
517 if (! fptr) |
10411 | 518 retval.error (gnulib::strerror (errno)); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
519 } |
7730
b68e44c90afe
file-io.cc (do_stream_open): return -1 for directories
John W. Eaton <jwe@octave.org>
parents:
7708
diff
changeset
|
520 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
521 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
522 } |
1 | 523 } |
524 | |
2095 | 525 return retval; |
526 } | |
1 | 527 |
3340 | 528 static octave_stream |
2095 | 529 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
|
530 const octave_value& tc_arch, const char *fcn, int& fid) |
2095 | 531 { |
3340 | 532 octave_stream retval; |
2095 | 533 |
534 fid = -1; | |
535 | |
3523 | 536 std::string name = tc_name.string_value (); |
2095 | 537 |
538 if (! error_state) | |
1 | 539 { |
3523 | 540 std::string mode = tc_mode.string_value (); |
2095 | 541 |
542 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
543 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
544 std::string arch = tc_arch.string_value (); |
1 | 545 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
546 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
547 retval = do_stream_open (name, mode, arch, fid); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
548 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
549 ::error ("%s: architecture type must be a string", fcn); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
550 } |
2095 | 551 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
552 ::error ("%s: file mode must be a string", fcn); |
1 | 553 } |
2095 | 554 else |
555 ::error ("%s: file name must be a string", fcn); | |
1 | 556 |
557 return retval; | |
558 } | |
559 | |
11146
69b2f237060e
file-io.cc (Ffopen): argument parsing tweak
John W. Eaton <jwe@octave.org>
parents:
11081
diff
changeset
|
560 DEFUN (fopen, args, nargout, |
3372 | 561 "-*- texinfo -*-\n\ |
10840 | 562 @deftypefn {Built-in Function} {[@var{fid}, @var{msg}] =} fopen (@var{name}, @var{mode}, @var{arch})\n\ |
3372 | 563 @deftypefnx {Built-in Function} {@var{fid_list} =} fopen (\"all\")\n\ |
5353 | 564 @deftypefnx {Built-in Function} {[@var{file}, @var{mode}, @var{arch}] =} fopen (@var{fid})\n\ |
3372 | 565 The first form of the @code{fopen} function opens the named file with\n\ |
566 the specified mode (read-write, read-only, etc.) and architecture\n\ | |
567 interpretation (IEEE big endian, IEEE little endian, etc.), and returns\n\ | |
568 an integer value that may be used to refer to the file later. If an\n\ | |
569 error occurs, @var{fid} is set to @minus{}1 and @var{msg} contains the\n\ | |
570 corresponding system error message. The @var{mode} is a one or two\n\ | |
571 character string that specifies whether the file is to be opened for\n\ | |
572 reading, writing, or both.\n\ | |
1181 | 573 \n\ |
3372 | 574 The second form of the @code{fopen} function returns a vector of file ids\n\ |
575 corresponding to all the currently open files, excluding the\n\ | |
576 @code{stdin}, @code{stdout}, and @code{stderr} streams.\n\ | |
2318 | 577 \n\ |
5353 | 578 The third form of the @code{fopen} function returns information about the\n\ |
579 open file given its file id.\n\ | |
1181 | 580 \n\ |
3372 | 581 For example,\n\ |
582 \n\ | |
583 @example\n\ | |
584 myfile = fopen (\"splat.dat\", \"r\", \"ieee-le\");\n\ | |
585 @end example\n\ | |
2095 | 586 \n\ |
3372 | 587 @noindent\n\ |
588 opens the file @file{splat.dat} for reading. If necessary, binary\n\ | |
589 numeric values will be read assuming they are stored in IEEE format with\n\ | |
590 the least significant bit first, and then converted to the native\n\ | |
591 representation.\n\ | |
2318 | 592 \n\ |
3372 | 593 Opening a file that is already open simply opens it again and returns a\n\ |
594 separate file id. It is not an error to open a file several times,\n\ | |
595 though writing to the same file through several different file ids may\n\ | |
596 produce unexpected results.\n\ | |
597 \n\ | |
598 The possible values @samp{mode} may have are\n\ | |
599 \n\ | |
600 @table @asis\n\ | |
601 @item @samp{r}\n\ | |
602 Open a file for reading.\n\ | |
3263 | 603 \n\ |
3372 | 604 @item @samp{w}\n\ |
7001 | 605 Open a file for writing. The previous contents are discarded.\n\ |
3372 | 606 \n\ |
607 @item @samp{a}\n\ | |
608 Open or create a file for writing at the end of the file.\n\ | |
609 \n\ | |
610 @item @samp{r+}\n\ | |
611 Open an existing file for reading and writing.\n\ | |
612 \n\ | |
613 @item @samp{w+}\n\ | |
614 Open a file for reading or writing. The previous contents are\n\ | |
615 discarded.\n\ | |
616 \n\ | |
617 @item @samp{a+}\n\ | |
618 Open or create a file for reading or writing at the end of the\n\ | |
619 file.\n\ | |
620 @end table\n\ | |
621 \n\ | |
4865 | 622 Append a \"t\" to the mode string to open the file in text mode or a\n\ |
623 \"b\" to open in binary mode. On Windows and Macintosh systems, text\n\ | |
624 mode reading and writing automatically converts linefeeds to the\n\ | |
625 appropriate line end character for the system (carriage-return linefeed\n\ | |
6665 | 626 on Windows, carriage-return on Macintosh). The default if no mode is\n\ |
4865 | 627 specified is binary mode.\n\ |
628 \n\ | |
5325 | 629 Additionally, you may append a \"z\" to the mode string to open a\n\ |
630 gzipped file for reading or writing. For this to be successful, you\n\ | |
631 must also open the file in binary mode.\n\ | |
632 \n\ | |
3372 | 633 The parameter @var{arch} is a string specifying the default data format\n\ |
634 for the file. Valid values for @var{arch} are:\n\ | |
2318 | 635 \n\ |
3372 | 636 @table @asis\n\ |
637 @samp{native}\n\ | |
638 The format of the current machine (this is the default).\n\ | |
639 \n\ | |
4082 | 640 @samp{ieee-be}\n\ |
3372 | 641 IEEE big endian format.\n\ |
642 \n\ | |
4082 | 643 @samp{ieee-le}\n\ |
3372 | 644 IEEE little endian format.\n\ |
2318 | 645 \n\ |
3372 | 646 @samp{vaxd}\n\ |
647 VAX D floating format.\n\ | |
2318 | 648 \n\ |
3372 | 649 @samp{vaxg}\n\ |
650 VAX G floating format.\n\ | |
2318 | 651 \n\ |
3372 | 652 @samp{cray}\n\ |
653 Cray floating format.\n\ | |
654 @end table\n\ | |
655 \n\ | |
656 @noindent\n\ | |
657 however, conversions are currently only supported for @samp{native}\n\ | |
658 @samp{ieee-be}, and @samp{ieee-le} formats.\n\ | |
7781 | 659 @seealso{fclose, fgets, fputs, fread, fseek, ferror, fprintf, fscanf, ftell, fwrite}\n\ |
5642 | 660 @end deftypefn") |
529 | 661 { |
2599 | 662 octave_value_list retval; |
663 | |
664 retval(0) = -1.0; | |
529 | 665 |
666 int nargin = args.length (); | |
667 | |
2095 | 668 if (nargin == 1) |
669 { | |
12896
a19b50f6697f
Correctly allow single string input form of fopen() (Bug #33535).
Rik <octave@nomad.inbox5.com>
parents:
12775
diff
changeset
|
670 if (args(0).is_string ()) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
671 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
672 // 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
|
673 // 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
|
674 // 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
|
675 // to supply more than one argument. |
3263 | 676 |
12896
a19b50f6697f
Correctly allow single string input form of fopen() (Bug #33535).
Rik <octave@nomad.inbox5.com>
parents:
12775
diff
changeset
|
677 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
|
678 return octave_stream_list::open_file_numbers (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
679 } |
2095 | 680 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
681 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
682 string_vector tmp = octave_stream_list::get_info (args(0)); |
529 | 683 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
684 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
685 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
686 retval(2) = tmp(2); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
687 retval(1) = tmp(1); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
688 retval(0) = tmp(0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
689 } |
3263 | 690 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
691 return retval; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
692 } |
1 | 693 } |
694 | |
2095 | 695 if (nargin > 0 && nargin < 4) |
696 { | |
697 octave_value mode = (nargin == 2 || nargin == 3) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
698 ? args(1) : octave_value ("r"); |
2095 | 699 |
700 octave_value arch = (nargin == 3) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
701 ? args(2) : octave_value ("native"); |
2095 | 702 |
703 int fid = -1; | |
704 | |
3340 | 705 octave_stream os = do_stream_open (args(0), mode, arch, "fopen", fid); |
2095 | 706 |
5370 | 707 if (os && ! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
708 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
709 retval(1) = ""; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
710 retval(0) = octave_stream_list::insert (os); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
711 } |
2095 | 712 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
713 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
714 int error_number = 0; |
5370 | 715 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
716 retval(1) = os.error (false, error_number); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
717 retval(0) = -1.0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
718 } |
2095 | 719 } |
720 else | |
5823 | 721 print_usage (); |
1 | 722 |
723 return retval; | |
724 } | |
725 | |
1957 | 726 DEFUN (freport, args, , |
3372 | 727 "-*- texinfo -*-\n\ |
728 @deftypefn {Built-in Function} {} freport ()\n\ | |
729 Print a list of which files have been opened, and whether they are open\n\ | |
10840 | 730 for reading, writing, or both. For example:\n\ |
3372 | 731 \n\ |
732 @example\n\ | |
733 @group\n\ | |
734 freport ()\n\ | |
735 \n\ | |
736 @print{} number mode name\n\ | |
12642
f96b9b9f141b
doc: Periodic grammarcheck and spellcheck of documentation.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
737 @print{}\n\ |
3372 | 738 @print{} 0 r stdin\n\ |
739 @print{} 1 w stdout\n\ | |
740 @print{} 2 w stderr\n\ | |
741 @print{} 3 r myfile\n\ | |
742 @end group\n\ | |
743 @end example\n\ | |
744 @end deftypefn") | |
1181 | 745 { |
2095 | 746 octave_value_list retval; |
1181 | 747 |
748 int nargin = args.length (); | |
749 | |
750 if (nargin > 0) | |
751 warning ("freport: ignoring extra arguments"); | |
752 | |
2095 | 753 octave_stdout << octave_stream_list::list_open_files (); |
1181 | 754 |
755 return retval; | |
756 } | |
757 | |
4715 | 758 DEFUN (frewind, args, nargout, |
3372 | 759 "-*- texinfo -*-\n\ |
760 @deftypefn {Built-in Function} {} frewind (@var{fid})\n\ | |
761 Move the file pointer to the beginning of the file @var{fid}, returning\n\ | |
4715 | 762 0 for success, and -1 if an error was encountered. It is equivalent to\n\ |
3372 | 763 @code{fseek (@var{fid}, 0, SEEK_SET)}.\n\ |
764 @end deftypefn") | |
529 | 765 { |
4715 | 766 octave_value retval; |
767 | |
768 int result = -1; | |
1 | 769 |
506 | 770 int nargin = args.length (); |
771 | |
2095 | 772 if (nargin == 1) |
1086 | 773 { |
3341 | 774 octave_stream os = octave_stream_list::lookup (args(0), "frewind"); |
636 | 775 |
3341 | 776 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
777 result = os.rewind (); |
1 | 778 } |
779 else | |
5823 | 780 print_usage (); |
1 | 781 |
4715 | 782 if (nargout > 0) |
783 retval = result; | |
784 | |
1 | 785 return retval; |
786 } | |
787 | |
1957 | 788 DEFUN (fseek, args, , |
3372 | 789 "-*- texinfo -*-\n\ |
790 @deftypefn {Built-in Function} {} fseek (@var{fid}, @var{offset}, @var{origin})\n\ | |
5095 | 791 Set the file pointer to any location within the file @var{fid}.\n\ |
792 \n\ | |
793 The pointer is positioned @var{offset} characters from the @var{origin},\n\ | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
794 which may be one of the predefined variables @w{@code{SEEK_CUR}} (current\n\ |
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
795 position), @w{@code{SEEK_SET}} (beginning), or @w{@code{SEEK_END}} (end of\n\ |
9039
51dc9691f23f
Cleanup documentation files errors.texi, debug.texi, io.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
796 file) or strings \"cof\", \"bof\" or \"eof\". If @var{origin} is omitted,\n\ |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
797 @w{@code{SEEK_SET}} is assumed. The offset must be zero, or a value returned\n\ |
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
798 by @code{ftell} (in which case @var{origin} must be @w{@code{SEEK_SET}}).\n\ |
5095 | 799 \n\ |
800 Return 0 on success and -1 on error.\n\ | |
5642 | 801 @seealso{ftell, fopen, fclose}\n\ |
802 @end deftypefn") | |
529 | 803 { |
4233 | 804 octave_value retval = -1; |
529 | 805 |
806 int nargin = args.length (); | |
807 | |
2095 | 808 if (nargin == 2 || nargin == 3) |
809 { | |
3341 | 810 octave_stream os = octave_stream_list::lookup (args(0), "fseek"); |
1181 | 811 |
3341 | 812 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
813 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
814 octave_value origin_arg = (nargin == 3) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
815 ? args(2) : octave_value (-1.0); |
1 | 816 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
817 retval = os.seek (args(1), origin_arg); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
818 } |
368 | 819 } |
2095 | 820 else |
5823 | 821 print_usage (); |
1 | 822 |
823 return retval; | |
824 } | |
825 | |
1957 | 826 DEFUN (ftell, args, , |
3372 | 827 "-*- texinfo -*-\n\ |
828 @deftypefn {Built-in Function} {} ftell (@var{fid})\n\ | |
829 Return the position of the file pointer as the number of characters\n\ | |
830 from the beginning of the file @var{fid}.\n\ | |
5642 | 831 @seealso{fseek, fopen, fclose}\n\ |
832 @end deftypefn") | |
1181 | 833 { |
4797 | 834 octave_value retval = -1; |
1 | 835 |
506 | 836 int nargin = args.length (); |
837 | |
2095 | 838 if (nargin == 1) |
1 | 839 { |
3341 | 840 octave_stream os = octave_stream_list::lookup (args(0), "ftell"); |
1 | 841 |
3341 | 842 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
843 retval = os.tell (); |
1 | 844 } |
845 else | |
5823 | 846 print_usage (); |
1 | 847 |
848 return retval; | |
849 } | |
850 | |
3737 | 851 DEFUN (fprintf, args, nargout, |
3372 | 852 "-*- texinfo -*-\n\ |
853 @deftypefn {Built-in Function} {} fprintf (@var{fid}, @var{template}, @dots{})\n\ | |
854 This function is just like @code{printf}, except that the output is\n\ | |
855 written to the stream @var{fid} instead of @code{stdout}.\n\ | |
8606
0611b48a2b61
file-io.cc (Ffprintf): doc fix
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
856 If @var{fid} is omitted, the output is written to @code{stdout}.\n\ |
5642 | 857 @seealso{printf, sprintf, fread, fscanf, fopen, fclose}\n\ |
858 @end deftypefn") | |
1181 | 859 { |
4468 | 860 static std::string who = "fprintf"; |
861 | |
4715 | 862 octave_value retval; |
863 | |
864 int result = -1; | |
4468 | 865 |
1181 | 866 int nargin = args.length (); |
867 | |
2875 | 868 if (nargin > 1 || (nargin > 0 && args(0).is_string ())) |
2095 | 869 { |
3340 | 870 octave_stream os; |
2873 | 871 int fmt_n = 0; |
872 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11572
diff
changeset
|
873 if (args(0).is_string ()) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
874 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
875 os = octave_stream_list::lookup (1, who); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
876 } |
2873 | 877 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
878 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
879 fmt_n = 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
880 os = octave_stream_list::lookup (args(0), who); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
881 } |
2095 | 882 |
3341 | 883 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
884 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
885 if (args(fmt_n).is_string ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
886 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
887 octave_value_list tmp_args; |
2095 | 888 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
889 if (nargin > 1 + fmt_n) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
890 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
891 tmp_args.resize (nargin-fmt_n-1, octave_value ()); |
2095 | 892 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
893 for (int i = fmt_n + 1; i < nargin; i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
894 tmp_args(i-fmt_n-1) = args(i); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
895 } |
2095 | 896 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
897 result = os.printf (args(fmt_n), tmp_args, who); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
898 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
899 else |
12483
7a5aacf65f81
Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents:
12228
diff
changeset
|
900 ::error ("%s: format TEMPLATE must be a string", who.c_str ()); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
901 } |
2095 | 902 } |
903 else | |
5823 | 904 print_usage (); |
1181 | 905 |
4715 | 906 if (nargout > 0) |
907 retval = result; | |
908 | |
909 return retval; | |
1181 | 910 } |
911 | |
4715 | 912 DEFUN (printf, args, nargout, |
4468 | 913 "-*- texinfo -*-\n\ |
5095 | 914 @deftypefn {Built-in Function} {} printf (@var{template}, @dots{})\n\ |
4468 | 915 Print optional arguments under the control of the template string\n\ |
5653 | 916 @var{template} to the stream @code{stdout} and return the number of\n\ |
917 characters printed.\n\ | |
918 @ifclear OCTAVE_MANUAL\n\ | |
5095 | 919 \n\ |
5653 | 920 See the Formatted Output section of the GNU Octave manual for a\n\ |
921 complete description of the syntax of the template string.\n\ | |
922 @end ifclear\n\ | |
5642 | 923 @seealso{fprintf, sprintf, scanf}\n\ |
924 @end deftypefn") | |
4468 | 925 { |
926 static std::string who = "printf"; | |
927 | |
4715 | 928 octave_value retval; |
929 | |
930 int result = -1; | |
4468 | 931 |
932 int nargin = args.length (); | |
933 | |
934 if (nargin > 0) | |
935 { | |
936 if (args(0).is_string ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
937 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
938 octave_value_list tmp_args; |
4468 | 939 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
940 if (nargin > 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
941 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
942 tmp_args.resize (nargin-1, octave_value ()); |
4468 | 943 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
944 for (int i = 1; i < nargin; i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
945 tmp_args(i-1) = args(i); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
946 } |
4468 | 947 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
948 result = stdout_stream.printf (args(0), tmp_args, who); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
949 } |
4468 | 950 else |
12483
7a5aacf65f81
Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents:
12228
diff
changeset
|
951 ::error ("%s: format TEMPLATE must be a string", who.c_str ()); |
4468 | 952 } |
953 else | |
5823 | 954 print_usage (); |
4468 | 955 |
4715 | 956 if (nargout > 0) |
957 retval = result; | |
958 | |
4468 | 959 return retval; |
960 } | |
961 | |
2095 | 962 DEFUN (fputs, args, , |
3372 | 963 "-*- texinfo -*-\n\ |
964 @deftypefn {Built-in Function} {} fputs (@var{fid}, @var{string})\n\ | |
965 Write a string to a file with no formatting.\n\ | |
5095 | 966 \n\ |
967 Return a non-negative number on success and EOF on error.\n\ | |
7781 | 968 @seealso{scanf, sscanf, fread, fprintf, fgets, fscanf}\n\ |
3372 | 969 @end deftypefn") |
1181 | 970 { |
4468 | 971 static std::string who = "fputs"; |
972 | |
4233 | 973 octave_value retval = -1; |
1181 | 974 |
975 int nargin = args.length (); | |
976 | |
2095 | 977 if (nargin == 2) |
978 { | |
4468 | 979 octave_stream os = octave_stream_list::lookup (args(0), who); |
1181 | 980 |
3341 | 981 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
982 retval = os.puts (args(1), who); |
2095 | 983 } |
1181 | 984 else |
5823 | 985 print_usage (); |
4468 | 986 |
987 return retval; | |
988 } | |
989 | |
990 DEFUN (puts, args, , | |
991 "-*- texinfo -*-\n\ | |
992 @deftypefn {Built-in Function} {} puts (@var{string})\n\ | |
993 Write a string to the standard output with no formatting.\n\ | |
5095 | 994 \n\ |
995 Return a non-negative number on success and EOF on error.\n\ | |
4468 | 996 @end deftypefn") |
997 { | |
998 static std::string who = "puts"; | |
999 | |
1000 octave_value retval = -1; | |
1001 | |
1002 if (args.length () == 1) | |
1003 retval = stdout_stream.puts (args(0), who); | |
1004 else | |
5823 | 1005 print_usage (); |
1181 | 1006 |
1007 return retval; | |
1008 } | |
1009 | |
2095 | 1010 DEFUN (sprintf, args, , |
3372 | 1011 "-*- texinfo -*-\n\ |
1012 @deftypefn {Built-in Function} {} sprintf (@var{template}, @dots{})\n\ | |
1013 This is like @code{printf}, except that the output is returned as a\n\ | |
1014 string. Unlike the C library function, which requires you to provide a\n\ | |
1015 suitably sized string as an argument, Octave's @code{sprintf} function\n\ | |
1016 returns the string, automatically sized to hold all of the items\n\ | |
1017 converted.\n\ | |
5642 | 1018 @seealso{printf, fprintf, sscanf}\n\ |
1019 @end deftypefn") | |
1 | 1020 { |
4468 | 1021 static std::string who = "sprintf"; |
1022 | |
2095 | 1023 octave_value_list retval; |
1 | 1024 |
2095 | 1025 int nargin = args.length (); |
1 | 1026 |
2095 | 1027 if (nargin > 0) |
1 | 1028 { |
2116 | 1029 retval(2) = -1.0; |
1030 retval(1) = "unknown error"; | |
1031 retval(0) = ""; | |
1032 | |
3340 | 1033 octave_ostrstream *ostr = new octave_ostrstream (); |
1 | 1034 |
3340 | 1035 octave_stream os (ostr); |
628 | 1036 |
3340 | 1037 if (os.is_valid ()) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1038 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1039 octave_value fmt_arg = args(0); |
6007 | 1040 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1041 if (fmt_arg.is_string ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1042 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1043 octave_value_list tmp_args; |
1 | 1044 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1045 if (nargin > 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1046 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1047 tmp_args.resize (nargin-1, octave_value ()); |
1 | 1048 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1049 for (int i = 1; i < nargin; i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1050 tmp_args(i-1) = args(i); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1051 } |
628 | 1052 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1053 retval(2) = os.printf (fmt_arg, tmp_args, who); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1054 retval(1) = os.error (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1055 retval(0) = octave_value (ostr->str (), |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1056 fmt_arg.is_sq_string () ? '\'' : '"'); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1057 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1058 else |
12483
7a5aacf65f81
Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents:
12228
diff
changeset
|
1059 ::error ("%s: format TEMPLATE must be a string", who.c_str ()); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1060 } |
2095 | 1061 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1062 ::error ("%s: unable to create output buffer", who.c_str ()); |
1 | 1063 } |
1064 else | |
5823 | 1065 print_usage (); |
1 | 1066 |
1067 return retval; | |
1068 } | |
1069 | |
2095 | 1070 DEFUN (fscanf, args, , |
3372 | 1071 "-*- texinfo -*-\n\ |
13271
fba2cc36b762
return stream error message in scanf functions and document behavior
John W. Eaton <jwe@octave.org>
parents:
13194
diff
changeset
|
1072 @deftypefn {Built-in Function} {[@var{val}, @var{count}, @var{errmsg}] =} fscanf (@var{fid}, @var{template}, @var{size})\n\ |
fba2cc36b762
return stream error message in scanf functions and document behavior
John W. Eaton <jwe@octave.org>
parents:
13194
diff
changeset
|
1073 @deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}, @var{count}, @var{errmsg}] =} fscanf (@var{fid}, @var{template}, \"C\")\n\ |
3372 | 1074 In the first form, read from @var{fid} according to @var{template},\n\ |
1075 returning the result in the matrix @var{val}.\n\ | |
2122 | 1076 \n\ |
3372 | 1077 The optional argument @var{size} specifies the amount of data to read\n\ |
1078 and may be one of\n\ | |
1079 \n\ | |
1080 @table @code\n\ | |
1081 @item Inf\n\ | |
1082 Read as much as possible, returning a column vector.\n\ | |
1083 \n\ | |
1084 @item @var{nr}\n\ | |
1085 Read up to @var{nr} elements, returning a column vector.\n\ | |
2122 | 1086 \n\ |
3372 | 1087 @item [@var{nr}, Inf]\n\ |
1088 Read as much as possible, returning a matrix with @var{nr} rows. If the\n\ | |
1089 number of elements read is not an exact multiple of @var{nr}, the last\n\ | |
1090 column is padded with zeros.\n\ | |
1091 \n\ | |
1092 @item [@var{nr}, @var{nc}]\n\ | |
1093 Read up to @code{@var{nr} * @var{nc}} elements, returning a matrix with\n\ | |
1094 @var{nr} rows. If the number of elements read is not an exact multiple\n\ | |
1095 of @var{nr}, the last column is padded with zeros.\n\ | |
1096 @end table\n\ | |
2122 | 1097 \n\ |
3372 | 1098 @noindent\n\ |
1099 If @var{size} is omitted, a value of @code{Inf} is assumed.\n\ | |
2122 | 1100 \n\ |
3372 | 1101 A string is returned if @var{template} specifies only character\n\ |
1102 conversions.\n\ | |
2215 | 1103 \n\ |
3372 | 1104 The number of items successfully read is returned in @var{count}.\n\ |
2215 | 1105 \n\ |
13271
fba2cc36b762
return stream error message in scanf functions and document behavior
John W. Eaton <jwe@octave.org>
parents:
13194
diff
changeset
|
1106 If an error occurs, @var{errmsg} contains a system-dependent error message.\n\ |
fba2cc36b762
return stream error message in scanf functions and document behavior
John W. Eaton <jwe@octave.org>
parents:
13194
diff
changeset
|
1107 \n\ |
3372 | 1108 In the second form, read from @var{fid} according to @var{template},\n\ |
1109 with each conversion specifier in @var{template} corresponding to a\n\ | |
1110 single scalar return value. This form is more `C-like', and also\n\ | |
3491 | 1111 compatible with previous versions of Octave. The number of successful\n\ |
1112 conversions is returned in @var{count}\n\ | |
5653 | 1113 @ifclear OCTAVE_MANUAL\n\ |
1114 \n\ | |
1115 See the Formatted Input section of the GNU Octave manual for a\n\ | |
1116 complete description of the syntax of the template string.\n\ | |
1117 @end ifclear\n\ | |
7781 | 1118 @seealso{scanf, sscanf, fread, fprintf, fgets, fputs}\n\ |
5642 | 1119 @end deftypefn") |
1181 | 1120 { |
4468 | 1121 static std::string who = "fscanf"; |
1122 | |
2095 | 1123 octave_value_list retval; |
1181 | 1124 |
1125 int nargin = args.length (); | |
1126 | |
2215 | 1127 if (nargin == 3 && args(2).is_string ()) |
2095 | 1128 { |
4468 | 1129 octave_stream os = octave_stream_list::lookup (args(0), who); |
1181 | 1130 |
3341 | 1131 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1132 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1133 if (args(1).is_string ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1134 retval = os.oscanf (args(1), who); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1135 else |
12483
7a5aacf65f81
Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents:
12228
diff
changeset
|
1136 ::error ("%s: format TEMPLATE must be a string", who.c_str ()); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1137 } |
2095 | 1138 } |
1181 | 1139 else |
2215 | 1140 { |
13271
fba2cc36b762
return stream error message in scanf functions and document behavior
John W. Eaton <jwe@octave.org>
parents:
13194
diff
changeset
|
1141 retval(2) = "unknown error"; |
fba2cc36b762
return stream error message in scanf functions and document behavior
John W. Eaton <jwe@octave.org>
parents:
13194
diff
changeset
|
1142 retval(1) = 0.0; |
fba2cc36b762
return stream error message in scanf functions and document behavior
John W. Eaton <jwe@octave.org>
parents:
13194
diff
changeset
|
1143 retval(0) = Matrix (); |
2215 | 1144 |
1145 if (nargin == 2 || nargin == 3) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1146 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1147 octave_stream os = octave_stream_list::lookup (args(0), who); |
2215 | 1148 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1149 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1150 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1151 if (args(1).is_string ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1152 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1153 octave_idx_type count = 0; |
2215 | 1154 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1155 Array<double> size = (nargin == 3) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1156 ? args(2).vector_value () |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
1157 : Array<double> (dim_vector (1, 1), lo_ieee_inf_value ()); |
2215 | 1158 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1159 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1160 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1161 octave_value tmp = os.scanf (args(1), size, count, who); |
2215 | 1162 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1163 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1164 { |
13271
fba2cc36b762
return stream error message in scanf functions and document behavior
John W. Eaton <jwe@octave.org>
parents:
13194
diff
changeset
|
1165 retval(2) = os.error (); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1166 retval(1) = count; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1167 retval(0) = tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1168 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1169 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1170 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1171 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1172 ::error ("%s: format must be a string", who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1173 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1174 } |
2215 | 1175 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1176 print_usage (); |
2215 | 1177 } |
1181 | 1178 |
1179 return retval; | |
1180 } | |
1181 | |
13194
3e1871badab9
allow sscanf to accept character arrays with more than one row
John W. Eaton <jwe@octave.org>
parents:
12998
diff
changeset
|
1182 static std::string |
3e1871badab9
allow sscanf to accept character arrays with more than one row
John W. Eaton <jwe@octave.org>
parents:
12998
diff
changeset
|
1183 get_sscanf_data (const octave_value& val) |
3e1871badab9
allow sscanf to accept character arrays with more than one row
John W. Eaton <jwe@octave.org>
parents:
12998
diff
changeset
|
1184 { |
3e1871badab9
allow sscanf to accept character arrays with more than one row
John W. Eaton <jwe@octave.org>
parents:
12998
diff
changeset
|
1185 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
|
1186 |
3e1871badab9
allow sscanf to accept character arrays with more than one row
John W. Eaton <jwe@octave.org>
parents:
12998
diff
changeset
|
1187 if (val.is_string ()) |
3e1871badab9
allow sscanf to accept character arrays with more than one row
John W. Eaton <jwe@octave.org>
parents:
12998
diff
changeset
|
1188 { |
3e1871badab9
allow sscanf to accept character arrays with more than one row
John W. Eaton <jwe@octave.org>
parents:
12998
diff
changeset
|
1189 octave_value tmp = val.reshape (dim_vector (1, val.numel ())); |
3e1871badab9
allow sscanf to accept character arrays with more than one row
John W. Eaton <jwe@octave.org>
parents:
12998
diff
changeset
|
1190 |
3e1871badab9
allow sscanf to accept character arrays with more than one row
John W. Eaton <jwe@octave.org>
parents:
12998
diff
changeset
|
1191 retval = tmp.string_value (); |
3e1871badab9
allow sscanf to accept character arrays with more than one row
John W. Eaton <jwe@octave.org>
parents:
12998
diff
changeset
|
1192 } |
3e1871badab9
allow sscanf to accept character arrays with more than one row
John W. Eaton <jwe@octave.org>
parents:
12998
diff
changeset
|
1193 else |
3e1871badab9
allow sscanf to accept character arrays with more than one row
John W. Eaton <jwe@octave.org>
parents:
12998
diff
changeset
|
1194 ::error ("sscanf: argument STRING must be a string"); |
3e1871badab9
allow sscanf to accept character arrays with more than one row
John W. Eaton <jwe@octave.org>
parents:
12998
diff
changeset
|
1195 |
3e1871badab9
allow sscanf to accept character arrays with more than one row
John W. Eaton <jwe@octave.org>
parents:
12998
diff
changeset
|
1196 return retval; |
3e1871badab9
allow sscanf to accept character arrays with more than one row
John W. Eaton <jwe@octave.org>
parents:
12998
diff
changeset
|
1197 } |
3e1871badab9
allow sscanf to accept character arrays with more than one row
John W. Eaton <jwe@octave.org>
parents:
12998
diff
changeset
|
1198 |
2095 | 1199 DEFUN (sscanf, args, , |
3372 | 1200 "-*- texinfo -*-\n\ |
13271
fba2cc36b762
return stream error message in scanf functions and document behavior
John W. Eaton <jwe@octave.org>
parents:
13194
diff
changeset
|
1201 @deftypefn {Built-in Function} {[@var{val}, @var{count}, @var{errmsg}, @var{pos}] =} sscanf (@var{string}, @var{template}, @var{size})\n\ |
fba2cc36b762
return stream error message in scanf functions and document behavior
John W. Eaton <jwe@octave.org>
parents:
13194
diff
changeset
|
1202 @deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}, @var{count}, @var{errmsg}] =} sscanf (@var{string}, @var{template}, \"C\")\n\ |
3372 | 1203 This is like @code{fscanf}, except that the characters are taken from the\n\ |
1204 string @var{string} instead of from a stream. Reaching the end of the\n\ | |
12993
dfab2a8ca545
doc: update sscanf doc string to match current behavior
John W. Eaton <jwe@octave.org>
parents:
12941
diff
changeset
|
1205 string is treated as an end-of-file condition. In addition to the values\n\ |
dfab2a8ca545
doc: update sscanf doc string to match current behavior
John W. Eaton <jwe@octave.org>
parents:
12941
diff
changeset
|
1206 returned by @code{fscanf}, the index of the next character to be read\n\ |
13929
9cae456085c2
Grammarcheck of documentation before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
13271
diff
changeset
|
1207 is returned in @var{pos}.\n\ |
5642 | 1208 @seealso{fscanf, scanf, sprintf}\n\ |
1209 @end deftypefn") | |
444 | 1210 { |
4468 | 1211 static std::string who = "sscanf"; |
1212 | |
2095 | 1213 octave_value_list retval; |
444 | 1214 |
506 | 1215 int nargin = args.length (); |
1216 | |
2215 | 1217 if (nargin == 3 && args(2).is_string ()) |
2095 | 1218 { |
13194
3e1871badab9
allow sscanf to accept character arrays with more than one row
John W. Eaton <jwe@octave.org>
parents:
12998
diff
changeset
|
1219 std::string data = get_sscanf_data (args(0)); |
3e1871badab9
allow sscanf to accept character arrays with more than one row
John W. Eaton <jwe@octave.org>
parents:
12998
diff
changeset
|
1220 |
3e1871badab9
allow sscanf to accept character arrays with more than one row
John W. Eaton <jwe@octave.org>
parents:
12998
diff
changeset
|
1221 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1222 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1223 octave_stream os = octave_istrstream::create (data); |
1358 | 1224 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1225 if (os.is_valid ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1226 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1227 if (args(1).is_string ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1228 retval = os.oscanf (args(1), who); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1229 else |
12483
7a5aacf65f81
Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents:
12228
diff
changeset
|
1230 ::error ("%s: format TEMPLATE must be a string", who.c_str ()); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1231 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1232 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1233 ::error ("%s: unable to create temporary input buffer", |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1234 who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1235 } |
636 | 1236 else |
12483
7a5aacf65f81
Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents:
12228
diff
changeset
|
1237 ::error ("%s: argument STRING must be a string", who.c_str ()); |
444 | 1238 } |
1239 else | |
2215 | 1240 { |
1241 if (nargin == 2 || nargin == 3) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1242 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1243 retval(3) = -1.0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1244 retval(2) = "unknown error"; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1245 retval(1) = 0.0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1246 retval(0) = Matrix (); |
2215 | 1247 |
13194
3e1871badab9
allow sscanf to accept character arrays with more than one row
John W. Eaton <jwe@octave.org>
parents:
12998
diff
changeset
|
1248 std::string data = get_sscanf_data (args(0)); |
3e1871badab9
allow sscanf to accept character arrays with more than one row
John W. Eaton <jwe@octave.org>
parents:
12998
diff
changeset
|
1249 |
3e1871badab9
allow sscanf to accept character arrays with more than one row
John W. Eaton <jwe@octave.org>
parents:
12998
diff
changeset
|
1250 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1251 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1252 octave_stream os = octave_istrstream::create (data); |
2215 | 1253 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1254 if (os.is_valid ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1255 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1256 if (args(1).is_string ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1257 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1258 octave_idx_type count = 0; |
2215 | 1259 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1260 Array<double> size = (nargin == 3) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1261 ? args(2).vector_value () |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
1262 : Array<double> (dim_vector (1, 1), |
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
1263 lo_ieee_inf_value ()); |
2215 | 1264 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1265 octave_value tmp = os.scanf (args(1), size, count, who); |
2215 | 1266 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1267 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1268 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1269 // FIXME -- is this the right thing to do? |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1270 // Extract error message first, because getting |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1271 // position will clear it. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1272 std::string errmsg = os.error (); |
2215 | 1273 |
12997
bac0858b92ee
sscanf: correctly set output position when reading stops at end of string
John W. Eaton <jwe@octave.org>
parents:
12993
diff
changeset
|
1274 retval(3) |
bac0858b92ee
sscanf: correctly set output position when reading stops at end of string
John W. Eaton <jwe@octave.org>
parents:
12993
diff
changeset
|
1275 = (os.eof () ? data.length () : os.tell ()) + 1; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1276 retval(2) = errmsg; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1277 retval(1) = count; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1278 retval(0) = tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1279 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1280 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1281 else |
12483
7a5aacf65f81
Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents:
12228
diff
changeset
|
1282 ::error ("%s: format TEMPLATE must be a string", who.c_str ()); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1283 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1284 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1285 ::error ("%s: unable to create temporary input buffer", |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1286 who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1287 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1288 } |
2215 | 1289 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1290 print_usage (); |
2215 | 1291 } |
444 | 1292 |
1293 return retval; | |
1294 } | |
1295 | |
2215 | 1296 DEFUN (scanf, args, nargout, |
3372 | 1297 "-*- texinfo -*-\n\ |
13271
fba2cc36b762
return stream error message in scanf functions and document behavior
John W. Eaton <jwe@octave.org>
parents:
13194
diff
changeset
|
1298 @deftypefn {Built-in Function} {[@var{val}, @var{count}, @var{errmsg}] =} scanf (@var{template}, @var{size})\n\ |
fba2cc36b762
return stream error message in scanf functions and document behavior
John W. Eaton <jwe@octave.org>
parents:
13194
diff
changeset
|
1299 @deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}, @var{count}, @var{errmsg}]] =} scanf (@var{template}, \"C\")\n\ |
3372 | 1300 This is equivalent to calling @code{fscanf} with @var{fid} = @code{stdin}.\n\ |
1301 \n\ | |
1302 It is currently not useful to call @code{scanf} in interactive\n\ | |
1303 programs.\n\ | |
5642 | 1304 @seealso{fscanf, sscanf, printf}\n\ |
1305 @end deftypefn") | |
2215 | 1306 { |
1307 int nargin = args.length (); | |
1308 | |
1309 octave_value_list tmp_args (nargin+1, octave_value ()); | |
1310 | |
1311 tmp_args (0) = 0.0; | |
1312 for (int i = 0; i < nargin; i++) | |
1313 tmp_args (i+1) = args (i); | |
1314 | |
1315 return Ffscanf (tmp_args, nargout); | |
1316 } | |
1317 | |
2116 | 1318 static octave_value |
1319 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
|
1320 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
|
1321 const octave_value& arch_arg, octave_idx_type& count) |
2116 | 1322 { |
1323 octave_value retval; | |
1324 | |
1325 count = -1; | |
1326 | |
3810 | 1327 Array<double> size = size_arg.vector_value (); |
2116 | 1328 |
1329 if (! error_state) | |
1330 { | |
3523 | 1331 std::string prec = prec_arg.string_value (); |
2116 | 1332 |
1333 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1334 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1335 int block_size = 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1336 oct_data_conv::data_type input_type; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1337 oct_data_conv::data_type output_type; |
4944 | 1338 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1339 oct_data_conv::string_to_data_type (prec, block_size, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1340 input_type, output_type); |
2116 | 1341 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1342 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1343 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1344 int skip = skip_arg.int_value (true); |
2116 | 1345 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1346 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1347 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1348 std::string arch = arch_arg.string_value (); |
3202 | 1349 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1350 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1351 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1352 oct_mach_info::float_format flt_fmt |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1353 = oct_mach_info::string_to_float_format (arch); |
2116 | 1354 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1355 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1356 retval = os.read (size, block_size, input_type, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1357 output_type, skip, flt_fmt, count); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1358 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1359 else |
12483
7a5aacf65f81
Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents:
12228
diff
changeset
|
1360 ::error ("fread: ARCH architecture type must be a string"); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1361 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1362 else |
12483
7a5aacf65f81
Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents:
12228
diff
changeset
|
1363 ::error ("fread: SKIP must be an integer"); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1364 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1365 else |
12483
7a5aacf65f81
Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents:
12228
diff
changeset
|
1366 ::error ("fread: invalid PRECISION specified"); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1367 } |
2116 | 1368 else |
12483
7a5aacf65f81
Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents:
12228
diff
changeset
|
1369 ::error ("fread: PRECISION must be a string"); |
2116 | 1370 } |
1371 else | |
12483
7a5aacf65f81
Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents:
12228
diff
changeset
|
1372 ::error ("fread: invalid SIZE specified"); |
2116 | 1373 |
1374 return retval; | |
1375 } | |
1376 | |
1377 DEFUN (fread, args, , | |
3372 | 1378 "-*- texinfo -*-\n\ |
1379 @deftypefn {Built-in Function} {[@var{val}, @var{count}] =} fread (@var{fid}, @var{size}, @var{precision}, @var{skip}, @var{arch})\n\ | |
1380 Read binary data of type @var{precision} from the specified file ID\n\ | |
1381 @var{fid}.\n\ | |
1382 \n\ | |
1383 The optional argument @var{size} specifies the amount of data to read\n\ | |
1384 and may be one of\n\ | |
1385 \n\ | |
1386 @table @code\n\ | |
1387 @item Inf\n\ | |
1388 Read as much as possible, returning a column vector.\n\ | |
529 | 1389 \n\ |
3372 | 1390 @item @var{nr}\n\ |
1391 Read up to @var{nr} elements, returning a column vector.\n\ | |
1392 \n\ | |
1393 @item [@var{nr}, Inf]\n\ | |
1394 Read as much as possible, returning a matrix with @var{nr} rows. If the\n\ | |
1395 number of elements read is not an exact multiple of @var{nr}, the last\n\ | |
1396 column is padded with zeros.\n\ | |
1397 \n\ | |
1398 @item [@var{nr}, @var{nc}]\n\ | |
1399 Read up to @code{@var{nr} * @var{nc}} elements, returning a matrix with\n\ | |
1400 @var{nr} rows. If the number of elements read is not an exact multiple\n\ | |
1401 of @var{nr}, the last column is padded with zeros.\n\ | |
1402 @end table\n\ | |
1403 \n\ | |
1404 @noindent\n\ | |
1405 If @var{size} is omitted, a value of @code{Inf} is assumed.\n\ | |
2318 | 1406 \n\ |
3372 | 1407 The optional argument @var{precision} is a string specifying the type of\n\ |
1408 data to read and may be one of\n\ | |
1409 \n\ | |
11595
5ec6aa05638d
Prevent doubled quotes around @table items in Info.
Rik <octave@nomad.inbox5.com>
parents:
11586
diff
changeset
|
1410 @table @asis\n\ |
4944 | 1411 @item \"schar\"\n\ |
1412 @itemx \"signed char\"\n\ | |
3372 | 1413 Signed character.\n\ |
529 | 1414 \n\ |
4944 | 1415 @item \"uchar\"\n\ |
1416 @itemx \"unsigned char\"\n\ | |
3372 | 1417 Unsigned character.\n\ |
1418 \n\ | |
4944 | 1419 @item \"int8\"\n\ |
1420 @itemx \"integer*1\"\n\ | |
1421 \n\ | |
1422 8-bit signed integer.\n\ | |
2318 | 1423 \n\ |
4944 | 1424 @item \"int16\"\n\ |
1425 @itemx \"integer*2\"\n\ | |
1426 16-bit signed integer.\n\ | |
3372 | 1427 \n\ |
4944 | 1428 @item \"int32\"\n\ |
1429 @itemx \"integer*4\"\n\ | |
1430 32-bit signed integer.\n\ | |
3372 | 1431 \n\ |
4944 | 1432 @item \"int64\"\n\ |
1433 @itemx \"integer*8\"\n\ | |
1434 64-bit signed integer.\n\ | |
1435 \n\ | |
1436 @item \"uint8\"\n\ | |
1437 8-bit unsigned integer.\n\ | |
529 | 1438 \n\ |
4944 | 1439 @item \"uint16\"\n\ |
1440 16-bit unsigned integer.\n\ | |
3372 | 1441 \n\ |
4944 | 1442 @item \"uint32\"\n\ |
1443 32-bit unsigned integer.\n\ | |
3372 | 1444 \n\ |
4944 | 1445 @item \"uint64\"\n\ |
1446 64-bit unsigned integer.\n\ | |
1447 \n\ | |
1448 @item \"single\"\n\ | |
3372 | 1449 @itemx \"float32\"\n\ |
1450 @itemx \"real*4\"\n\ | |
4944 | 1451 32-bit floating point number.\n\ |
3372 | 1452 \n\ |
1453 @item \"double\"\n\ | |
1454 @itemx \"float64\"\n\ | |
1455 @itemx \"real*8\"\n\ | |
4944 | 1456 64-bit floating point number.\n\ |
1457 \n\ | |
1458 @item \"char\"\n\ | |
1459 @itemx \"char*1\"\n\ | |
1460 Single character.\n\ | |
3372 | 1461 \n\ |
4944 | 1462 @item \"short\"\n\ |
1463 Short integer (size is platform dependent).\n\ | |
1464 \n\ | |
1465 @item \"int\"\n\ | |
1466 Integer (size is platform dependent).\n\ | |
1467 \n\ | |
1468 @item \"long\"\n\ | |
1469 Long integer (size is platform dependent).\n\ | |
3372 | 1470 \n\ |
4944 | 1471 @item \"ushort\"\n\ |
1472 @itemx \"unsigned short\"\n\ | |
1473 Unsigned short integer (size is platform dependent).\n\ | |
4610 | 1474 \n\ |
4944 | 1475 @item \"uint\"\n\ |
1476 @itemx \"unsigned int\"\n\ | |
1477 Unsigned integer (size is platform dependent).\n\ | |
4610 | 1478 \n\ |
4944 | 1479 @item \"ulong\"\n\ |
1480 @itemx \"unsigned long\"\n\ | |
1481 Unsigned long integer (size is platform dependent).\n\ | |
1482 \n\ | |
1483 @item \"float\"\n\ | |
1484 Single precision floating point number (size is platform dependent).\n\ | |
3372 | 1485 @end table\n\ |
1486 \n\ | |
1487 @noindent\n\ | |
1488 The default precision is @code{\"uchar\"}.\n\ | |
2318 | 1489 \n\ |
4944 | 1490 The @var{precision} argument may also specify an optional repeat\n\ |
1491 count. For example, @samp{32*single} causes @code{fread} to read\n\ | |
1492 a block of 32 single precision floating point numbers. Reading in\n\ | |
1493 blocks is useful in combination with the @var{skip} argument.\n\ | |
1494 \n\ | |
1495 The @var{precision} argument may also specify a type conversion.\n\ | |
1496 For example, @samp{int16=>int32} causes @code{fread} to read 16-bit\n\ | |
1497 integer values and return an array of 32-bit integer values. By\n\ | |
1498 default, @code{fread} returns a double precision array. The special\n\ | |
1499 form @samp{*TYPE} is shorthand for @samp{TYPE=>TYPE}.\n\ | |
1500 \n\ | |
7096 | 1501 The conversion and repeat counts may be combined. For example, the\n\ |
1502 specification @samp{32*single=>single} causes @code{fread} to read\n\ | |
1503 blocks of single precision floating point values and return an array\n\ | |
1504 of single precision values instead of the default array of double\n\ | |
1505 precision values.\n\ | |
4944 | 1506 \n\ |
3372 | 1507 The optional argument @var{skip} specifies the number of bytes to skip\n\ |
4944 | 1508 after each element (or block of elements) is read. If it is not\n\ |
1509 specified, a value of 0 is assumed. If the final block read is not\n\ | |
1510 complete, the final skip is omitted. For example,\n\ | |
1511 \n\ | |
1512 @example\n\ | |
1513 fread (f, 10, \"3*single=>single\", 8)\n\ | |
1514 @end example\n\ | |
1515 \n\ | |
1516 @noindent\n\ | |
1517 will omit the final 8-byte skip because the last read will not be\n\ | |
1518 a complete block of 3 values.\n\ | |
3372 | 1519 \n\ |
1520 The optional argument @var{arch} is a string specifying the data format\n\ | |
1521 for the file. Valid values are\n\ | |
529 | 1522 \n\ |
3372 | 1523 @table @code\n\ |
1524 @item \"native\"\n\ | |
1525 The format of the current machine.\n\ | |
1526 \n\ | |
4546 | 1527 @item \"ieee-be\"\n\ |
3372 | 1528 IEEE big endian.\n\ |
1529 \n\ | |
4546 | 1530 @item \"ieee-le\"\n\ |
3372 | 1531 IEEE little endian.\n\ |
2318 | 1532 \n\ |
3372 | 1533 @item \"vaxd\"\n\ |
1534 VAX D floating format.\n\ | |
1535 \n\ | |
1536 @item \"vaxg\"\n\ | |
1537 VAX G floating format.\n\ | |
2318 | 1538 \n\ |
3372 | 1539 @item \"cray\"\n\ |
1540 Cray floating format.\n\ | |
1541 @end table\n\ | |
2318 | 1542 \n\ |
3372 | 1543 @noindent\n\ |
1544 Conversions are currently only supported for @code{\"ieee-be\"} and\n\ | |
1545 @code{\"ieee-le\"} formats.\n\ | |
2318 | 1546 \n\ |
3372 | 1547 The data read from the file is returned in @var{val}, and the number of\n\ |
1548 values read is returned in @code{count}\n\ | |
5642 | 1549 @seealso{fwrite, fopen, fclose}\n\ |
1550 @end deftypefn") | |
529 | 1551 { |
2095 | 1552 octave_value_list retval; |
2116 | 1553 |
1554 int nargin = args.length (); | |
1555 | |
2318 | 1556 if (nargin > 0 && nargin < 6) |
2116 | 1557 { |
1558 retval(1) = -1.0; | |
1559 retval(0) = Matrix (); | |
1560 | |
3341 | 1561 octave_stream os = octave_stream_list::lookup (args(0), "fread"); |
2116 | 1562 |
3341 | 1563 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1564 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1565 octave_value size = lo_ieee_inf_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1566 octave_value prec = "uchar"; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1567 octave_value skip = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1568 octave_value arch = "unknown"; |
2116 | 1569 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1570 int idx = 1; |
2116 | 1571 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1572 if (nargin > idx && ! args(idx).is_string ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1573 size = args(idx++); |
4257 | 1574 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1575 if (nargin > idx) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1576 prec = args(idx++); |
2116 | 1577 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1578 if (nargin > idx) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1579 skip = args(idx++); |
4257 | 1580 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1581 if (nargin > idx) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1582 arch = args(idx++); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1583 else if (skip.is_string ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1584 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1585 arch = skip; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1586 skip = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1587 } |
2116 | 1588 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1589 octave_idx_type count = -1; |
2116 | 1590 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1591 octave_value tmp = do_fread (os, size, prec, skip, arch, count); |
2116 | 1592 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1593 retval(1) = count; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1594 retval(0) = tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1595 } |
2116 | 1596 } |
1597 else | |
5823 | 1598 print_usage (); |
2116 | 1599 |
529 | 1600 return retval; |
1601 } | |
1602 | |
2116 | 1603 static int |
1604 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
|
1605 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
|
1606 const octave_value& arch_arg) |
2116 | 1607 { |
1608 int retval = -1; | |
1609 | |
3523 | 1610 std::string prec = prec_arg.string_value (); |
2116 | 1611 |
1612 if (! error_state) | |
1613 { | |
4944 | 1614 int block_size = 1; |
1615 oct_data_conv::data_type output_type; | |
1616 | |
1617 oct_data_conv::string_to_data_type (prec, block_size, output_type); | |
2116 | 1618 |
1619 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1620 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1621 int skip = skip_arg.int_value (true); |
2116 | 1622 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1623 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1624 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1625 std::string arch = arch_arg.string_value (); |
3202 | 1626 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1627 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1628 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1629 oct_mach_info::float_format flt_fmt |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1630 = oct_mach_info::string_to_float_format (arch); |
2116 | 1631 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1632 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1633 retval = os.write (data, block_size, output_type, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1634 skip, flt_fmt); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1635 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1636 else |
12483
7a5aacf65f81
Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents:
12228
diff
changeset
|
1637 ::error ("fwrite: ARCH architecture type must be a string"); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1638 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1639 else |
12483
7a5aacf65f81
Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents:
12228
diff
changeset
|
1640 ::error ("fwrite: SKIP must be an integer"); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1641 } |
3202 | 1642 else |
12483
7a5aacf65f81
Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents:
12228
diff
changeset
|
1643 ::error ("fwrite: invalid PRECISION specified"); |
2116 | 1644 } |
1645 else | |
12483
7a5aacf65f81
Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents:
12228
diff
changeset
|
1646 ::error ("fwrite: PRECISION must be a string"); |
2116 | 1647 |
1648 return retval; | |
1649 } | |
1650 | |
1651 DEFUN (fwrite, args, , | |
3372 | 1652 "-*- texinfo -*-\n\ |
1653 @deftypefn {Built-in Function} {@var{count} =} fwrite (@var{fid}, @var{data}, @var{precision}, @var{skip}, @var{arch})\n\ | |
1654 Write data in binary form of type @var{precision} to the specified file\n\ | |
1655 ID @var{fid}, returning the number of values successfully written to the\n\ | |
1656 file.\n\ | |
1181 | 1657 \n\ |
3372 | 1658 The argument @var{data} is a matrix of values that are to be written to\n\ |
1659 the file. The values are extracted in column-major order.\n\ | |
1181 | 1660 \n\ |
3372 | 1661 The remaining arguments @var{precision}, @var{skip}, and @var{arch} are\n\ |
1662 optional, and are interpreted as described for @code{fread}.\n\ | |
1181 | 1663 \n\ |
3372 | 1664 The behavior of @code{fwrite} is undefined if the values in @var{data}\n\ |
1665 are too large to fit in the specified precision.\n\ | |
5642 | 1666 @seealso{fread, fopen, fclose}\n\ |
1667 @end deftypefn") | |
1181 | 1668 { |
4233 | 1669 octave_value retval = -1; |
2116 | 1670 |
1671 int nargin = args.length (); | |
1672 | |
1673 if (nargin > 1 && nargin < 6) | |
1674 { | |
3341 | 1675 octave_stream os = octave_stream_list::lookup (args(0), "fwrite"); |
2116 | 1676 |
3341 | 1677 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1678 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1679 octave_value prec = "uchar"; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1680 octave_value skip = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1681 octave_value arch = "unknown"; |
2318 | 1682 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1683 int idx = 1; |
7708
b42abee70a98
fread, fwrite: allow SKIP arg to be omitted
John W. Eaton <jwe@octave.org>
parents:
7650
diff
changeset
|
1684 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1685 octave_value data = args(idx++); |
7708
b42abee70a98
fread, fwrite: allow SKIP arg to be omitted
John W. Eaton <jwe@octave.org>
parents:
7650
diff
changeset
|
1686 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1687 if (nargin > idx) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1688 prec = args(idx++); |
2318 | 1689 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1690 if (nargin > idx) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1691 skip = args(idx++); |
2318 | 1692 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1693 if (nargin > idx) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1694 arch = args(idx++); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1695 else if (skip.is_string ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1696 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1697 arch = skip; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1698 skip = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1699 } |
2116 | 1700 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1701 double status = do_fwrite (os, data, prec, skip, arch); |
2825 | 1702 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1703 retval = status; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1704 } |
2116 | 1705 } |
1706 else | |
5823 | 1707 print_usage (); |
2116 | 1708 |
1181 | 1709 return retval; |
1710 } | |
1711 | |
5906 | 1712 DEFUNX ("feof", Ffeof, args, , |
3372 | 1713 "-*- texinfo -*-\n\ |
1714 @deftypefn {Built-in Function} {} feof (@var{fid})\n\ | |
1715 Return 1 if an end-of-file condition has been encountered for a given\n\ | |
1716 file and 0 otherwise. Note that it will only return 1 if the end of the\n\ | |
1717 file has already been encountered, not if the next read operation will\n\ | |
1718 result in an end-of-file condition.\n\ | |
5642 | 1719 @seealso{fread, fopen, fclose}\n\ |
1720 @end deftypefn") | |
529 | 1721 { |
4233 | 1722 octave_value retval = -1; |
529 | 1723 |
1724 int nargin = args.length (); | |
1725 | |
2095 | 1726 if (nargin == 1) |
1727 { | |
3341 | 1728 octave_stream os = octave_stream_list::lookup (args(0), "feof"); |
444 | 1729 |
3341 | 1730 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1731 retval = os.eof () ? 1.0 : 0.0; |
2095 | 1732 } |
529 | 1733 else |
5823 | 1734 print_usage (); |
444 | 1735 |
1736 return retval; | |
1737 } | |
1738 | |
5906 | 1739 DEFUNX ("ferror", Fferror, args, , |
3372 | 1740 "-*- texinfo -*-\n\ |
9797
f569f46a1c34
update ferror doc string, take 2
John W. Eaton <jwe@octave.org>
parents:
9796
diff
changeset
|
1741 @deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} ferror (@var{fid}, \"clear\")\n\ |
f569f46a1c34
update ferror doc string, take 2
John W. Eaton <jwe@octave.org>
parents:
9796
diff
changeset
|
1742 Return 1 if an error condition has been encountered for the file ID\n\ |
f569f46a1c34
update ferror doc string, take 2
John W. Eaton <jwe@octave.org>
parents:
9796
diff
changeset
|
1743 @var{fid} and 0 otherwise. Note that it will only return 1 if an error\n\ |
f569f46a1c34
update ferror doc string, take 2
John W. Eaton <jwe@octave.org>
parents:
9796
diff
changeset
|
1744 has already been encountered, not if the next operation will result in\n\ |
f569f46a1c34
update ferror doc string, take 2
John W. Eaton <jwe@octave.org>
parents:
9796
diff
changeset
|
1745 an error condition.\n\ |
f569f46a1c34
update ferror doc string, take 2
John W. Eaton <jwe@octave.org>
parents:
9796
diff
changeset
|
1746 \n\ |
f569f46a1c34
update ferror doc string, take 2
John W. Eaton <jwe@octave.org>
parents:
9796
diff
changeset
|
1747 The second argument is optional. If it is supplied, also clear the\n\ |
3372 | 1748 error condition.\n\ |
1749 @end deftypefn") | |
1230 | 1750 { |
2095 | 1751 octave_value_list retval; |
1230 | 1752 |
2095 | 1753 int nargin = args.length (); |
1230 | 1754 |
2095 | 1755 if (nargin == 1 || nargin == 2) |
1756 { | |
3341 | 1757 octave_stream os = octave_stream_list::lookup (args(0), "ferror"); |
1230 | 1758 |
3341 | 1759 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1760 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1761 bool clear = false; |
1230 | 1762 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1763 if (nargin == 2) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1764 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1765 std::string opt = args(1).string_value (); |
2095 | 1766 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1767 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1768 clear = (opt == "clear"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1769 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1770 return retval; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1771 } |
1755 | 1772 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1773 int error_number = 0; |
2095 | 1774 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1775 std::string error_message = os.error (clear, error_number); |
1230 | 1776 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1777 retval(1) = error_number; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1778 retval(0) = error_message; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1779 } |
1230 | 1780 } |
2095 | 1781 else |
5823 | 1782 print_usage (); |
1230 | 1783 |
1784 return retval; | |
1785 } | |
1786 | |
1957 | 1787 DEFUN (popen, args, , |
3301 | 1788 "-*- texinfo -*-\n\ |
4326 | 1789 @deftypefn {Built-in Function} {@var{fid} =} popen (@var{command}, @var{mode})\n\ |
3301 | 1790 Start a process and create a pipe. The name of the command to run is\n\ |
1791 given by @var{command}. The file identifier corresponding to the input\n\ | |
1792 or output stream of the process is returned in @var{fid}. The argument\n\ | |
1793 @var{mode} may be\n\ | |
1794 \n\ | |
1795 @table @code\n\ | |
1796 @item \"r\"\n\ | |
1797 The pipe will be connected to the standard output of the process, and\n\ | |
1798 open for reading.\n\ | |
1230 | 1799 \n\ |
3301 | 1800 @item \"w\"\n\ |
1801 The pipe will be connected to the standard input of the process, and\n\ | |
1802 open for writing.\n\ | |
1803 @end table\n\ | |
1804 \n\ | |
10840 | 1805 For example:\n\ |
1230 | 1806 \n\ |
3301 | 1807 @example\n\ |
1808 @group\n\ | |
1809 fid = popen (\"ls -ltr / | tail -3\", \"r\");\n\ | |
7768
a2d9f325b65a
Use isschar instead of deprecated isstr
Rafael Laboissiere <rafael@debian.org>
parents:
7730
diff
changeset
|
1810 while (ischar (s = fgets (fid)))\n\ |
3301 | 1811 fputs (stdout, s);\n\ |
1812 endwhile\n\ | |
1813 @print{} drwxr-xr-x 33 root root 3072 Feb 15 13:28 etc\n\ | |
1814 @print{} drwxr-xr-x 3 root root 1024 Feb 15 13:28 lib\n\ | |
1815 @print{} drwxrwxrwt 15 root root 2048 Feb 17 14:53 tmp\n\ | |
1816 @end group\n\ | |
1817 @end example\n\ | |
1818 @end deftypefn") | |
1230 | 1819 { |
4233 | 1820 octave_value retval = -1; |
1230 | 1821 |
1822 int nargin = args.length (); | |
1823 | |
2095 | 1824 if (nargin == 2) |
1825 { | |
3523 | 1826 std::string name = args(0).string_value (); |
1230 | 1827 |
2095 | 1828 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1829 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1830 std::string mode = args(1).string_value (); |
1230 | 1831 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1832 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1833 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1834 if (mode == "r") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1835 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1836 octave_stream ips = octave_iprocstream::create (name); |
1230 | 1837 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1838 retval = octave_stream_list::insert (ips); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1839 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1840 else if (mode == "w") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1841 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1842 octave_stream ops = octave_oprocstream::create (name); |
1230 | 1843 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1844 retval = octave_stream_list::insert (ops); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1845 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1846 else |
12483
7a5aacf65f81
Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents:
12228
diff
changeset
|
1847 ::error ("popen: invalid MODE specified"); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1848 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1849 else |
12483
7a5aacf65f81
Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents:
12228
diff
changeset
|
1850 ::error ("popen: MODE must be a string"); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1851 } |
2095 | 1852 else |
12483
7a5aacf65f81
Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents:
12228
diff
changeset
|
1853 ::error ("popen: COMMAND must be a string"); |
2095 | 1854 } |
1230 | 1855 else |
5823 | 1856 print_usage (); |
1230 | 1857 |
1858 return retval; | |
1859 } | |
1860 | |
1957 | 1861 DEFUN (pclose, args, , |
3381 | 1862 "-*- texinfo -*-\n\ |
3301 | 1863 @deftypefn {Built-in Function} {} pclose (@var{fid})\n\ |
1864 Close a file identifier that was opened by @code{popen}. You may also\n\ | |
1865 use @code{fclose} for the same purpose.\n\ | |
1866 @end deftypefn") | |
1230 | 1867 { |
4233 | 1868 octave_value retval = -1; |
1230 | 1869 |
1870 int nargin = args.length (); | |
1871 | |
2095 | 1872 if (nargin == 1) |
4233 | 1873 retval = octave_stream_list::remove (args(0), "pclose"); |
1377 | 1874 else |
5823 | 1875 print_usage (); |
1379 | 1876 |
1877 return retval; | |
1878 } | |
1879 | |
10197
4d433bd2d4dc
attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
1880 DEFUNX ("tmpnam", Ftmpnam, args, , |
3372 | 1881 "-*- texinfo -*-\n\ |
12211
11faa69c4eaa
Add S_ISBLK and family of functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11595
diff
changeset
|
1882 @deftypefn {Built-in Function} {} tmpnam ()\n\ |
11faa69c4eaa
Add S_ISBLK and family of functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11595
diff
changeset
|
1883 @deftypefnx {Built-in Function} {} tmpnam (@var{dir})\n\ |
11faa69c4eaa
Add S_ISBLK and family of functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11595
diff
changeset
|
1884 @deftypefnx {Built-in Function} {} tmpnam (@var{dir}, @var{prefix})\n\ |
3372 | 1885 Return a unique temporary file name as a string.\n\ |
1886 \n\ | |
4267 | 1887 If @var{prefix} is omitted, a value of @code{\"oct-\"} is used.\n\ |
1888 If @var{dir} is also omitted, the default directory for temporary files\n\ | |
1889 is used. If @var{dir} is provided, it must exist, otherwise the default\n\ | |
1890 directory for temporary files is used. Since the named file is not\n\ | |
1891 opened, by @code{tmpnam}, it is possible (though relatively unlikely)\n\ | |
1892 that it will not be available by the time your program attempts to open it.\n\ | |
5642 | 1893 @seealso{tmpfile, mkstemp, P_tmpdir}\n\ |
1894 @end deftypefn") | |
1802 | 1895 { |
2095 | 1896 octave_value retval; |
1802 | 1897 |
2936 | 1898 int len = args.length (); |
1899 | |
1900 if (len < 3) | |
1901 { | |
3523 | 1902 std::string dir = len > 0 ? args(0).string_value () : std::string (); |
2936 | 1903 |
1904 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1905 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1906 std::string pfx |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1907 = len > 1 ? args(1).string_value () : std::string ("oct-"); |
4267 | 1908 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1909 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1910 retval = octave_tempnam (dir, pfx); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1911 else |
12483
7a5aacf65f81
Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents:
12228
diff
changeset
|
1912 ::error ("PREFIX must be a string"); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1913 } |
4267 | 1914 else |
12483
7a5aacf65f81
Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents:
12228
diff
changeset
|
1915 ::error ("DIR argument must be a string"); |
2936 | 1916 } |
1802 | 1917 else |
5823 | 1918 print_usage (); |
1802 | 1919 |
1920 return retval; | |
1921 } | |
1922 | |
2458 | 1923 DEFALIAS (octave_tmp_file_name, tmpnam); |
1924 | |
4326 | 1925 DEFUN (tmpfile, args, , |
1926 "-*- texinfo -*-\n\ | |
1927 @deftypefn {Built-in Function} {[@var{fid}, @var{msg}] =} tmpfile ()\n\ | |
1928 Return the file ID corresponding to a new temporary file with a unique\n\ | |
1929 name. The file is opened in binary read/write (@code{\"w+b\"}) mode.\n\ | |
1930 The file will be deleted automatically when it is closed or when Octave\n\ | |
1931 exits.\n\ | |
1932 \n\ | |
1933 If successful, @var{fid} is a valid file ID and @var{msg} is an empty\n\ | |
1934 string. Otherwise, @var{fid} is -1 and @var{msg} contains a\n\ | |
1935 system-dependent error message.\n\ | |
5642 | 1936 @seealso{tmpnam, mkstemp, P_tmpdir}\n\ |
1937 @end deftypefn") | |
4326 | 1938 { |
1939 octave_value_list retval; | |
1940 | |
1941 retval(1) = std::string (); | |
1942 retval(0) = -1; | |
1943 | |
1944 int nargin = args.length (); | |
1945 | |
1946 if (nargin == 0) | |
1947 { | |
12775 | 1948 FILE *fid = gnulib::tmpfile (); |
4326 | 1949 |
1950 if (fid) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1951 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1952 std::string nm; |
4326 | 1953 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1954 std::ios::openmode md = fopen_mode_to_ios_mode ("w+b"); |
4326 | 1955 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1956 octave_stream s = octave_stdiostream::create (nm, fid, md); |
4326 | 1957 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1958 if (s) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1959 retval(0) = octave_stream_list::insert (s); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1960 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1961 error ("tmpfile: failed to create octave_stdiostream object"); |
4326 | 1962 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1963 } |
4326 | 1964 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1965 { |
10411 | 1966 retval(1) = gnulib::strerror (errno); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1967 retval(0) = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1968 } |
4326 | 1969 } |
1970 else | |
5823 | 1971 print_usage (); |
4326 | 1972 |
1973 return retval; | |
1974 } | |
1975 | |
1976 DEFUN (mkstemp, args, , | |
1977 "-*- texinfo -*-\n\ | |
5109 | 1978 @deftypefn {Built-in Function} {[@var{fid}, @var{name}, @var{msg}] =} mkstemp (@var{template}, @var{delete})\n\ |
4326 | 1979 Return the file ID corresponding to a new temporary file with a unique\n\ |
1980 name created from @var{template}. The last six characters of @var{template}\n\ | |
5016 | 1981 must be @code{XXXXXX} and these are replaced with a string that makes the\n\ |
4326 | 1982 filename unique. The file is then created with mode read/write and\n\ |
1983 permissions that are system dependent (on GNU/Linux systems, the permissions\n\ | |
1984 will be 0600 for versions of glibc 2.0.7 and later). The file is opened\n\ | |
13958
cb15c5185b6a
mkstemp: open file in binary mode (bug #33669)
John W. Eaton <jwe@octave.org>
parents:
13929
diff
changeset
|
1985 in binary mode and with the @w{@code{O_EXCL}} flag.\n\ |
4326 | 1986 \n\ |
1987 If the optional argument @var{delete} is supplied and is true,\n\ | |
1988 the file will be deleted automatically when Octave exits, or when\n\ | |
1989 the function @code{purge_tmp_files} is called.\n\ | |
1990 \n\ | |
1991 If successful, @var{fid} is a valid file ID, @var{name} is the name of\n\ | |
7001 | 1992 the file, and @var{msg} is an empty string. Otherwise, @var{fid}\n\ |
4326 | 1993 is -1, @var{name} is empty, and @var{msg} contains a system-dependent\n\ |
1994 error message.\n\ | |
5642 | 1995 @seealso{tmpfile, tmpnam, P_tmpdir}\n\ |
1996 @end deftypefn") | |
4326 | 1997 { |
1998 octave_value_list retval; | |
1999 | |
2000 retval(2) = std::string (); | |
2001 retval(1) = std::string (); | |
2002 retval(0) = -1; | |
2003 | |
2004 int nargin = args.length (); | |
2005 | |
2006 if (nargin == 1 || nargin == 2) | |
2007 { | |
2008 std::string tmpl8 = args(0).string_value (); | |
2009 | |
2010 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2011 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2012 OCTAVE_LOCAL_BUFFER (char, tmp, tmpl8.size () + 1); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2013 strcpy (tmp, tmpl8.c_str ()); |
4326 | 2014 |
13958
cb15c5185b6a
mkstemp: open file in binary mode (bug #33669)
John W. Eaton <jwe@octave.org>
parents:
13929
diff
changeset
|
2015 int fd = gnulib::mkostemp (tmp, O_BINARY); |
4326 | 2016 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2017 if (fd < 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2018 { |
10411 | 2019 retval(2) = gnulib::strerror (errno); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2020 retval(0) = fd; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2021 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2022 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2023 { |
13958
cb15c5185b6a
mkstemp: open file in binary mode (bug #33669)
John W. Eaton <jwe@octave.org>
parents:
13929
diff
changeset
|
2024 const char *fopen_mode = "w+b"; |
4326 | 2025 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2026 FILE *fid = fdopen (fd, fopen_mode); |
4326 | 2027 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2028 if (fid) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2029 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2030 std::string nm = tmp; |
4326 | 2031 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2032 std::ios::openmode md = fopen_mode_to_ios_mode (fopen_mode); |
4326 | 2033 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2034 octave_stream s = octave_stdiostream::create (nm, fid, md); |
4326 | 2035 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2036 if (s) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2037 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2038 retval(1) = nm; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2039 retval(0) = octave_stream_list::insert (s); |
4326 | 2040 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2041 if (nargin == 2 && args(1).is_true ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2042 mark_for_deletion (nm); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2043 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2044 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2045 error ("mkstemp: failed to create octave_stdiostream object"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2046 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2047 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2048 { |
10411 | 2049 retval(2) = gnulib::strerror (errno); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2050 retval(0) = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2051 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2052 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2053 } |
4326 | 2054 else |
12483
7a5aacf65f81
Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents:
12228
diff
changeset
|
2055 error ("mkstemp: TEMPLATE argument must be a string"); |
4326 | 2056 } |
2057 else | |
5823 | 2058 print_usage (); |
4326 | 2059 |
2060 return retval; | |
2061 } | |
2062 | |
1400 | 2063 static int |
2064 convert (int x, int ibase, int obase) | |
2065 { | |
2066 int retval = 0; | |
2067 | |
2068 int tmp = x % obase; | |
2069 | |
2070 if (tmp > ibase - 1) | |
2095 | 2071 ::error ("umask: invalid digit"); |
1400 | 2072 else |
2073 { | |
2074 retval = tmp; | |
2075 int mult = ibase; | |
2076 while ((x = (x - tmp) / obase)) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2077 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2078 tmp = x % obase; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2079 if (tmp > ibase - 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2080 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2081 ::error ("umask: invalid digit"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2082 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2083 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2084 retval += mult * tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2085 mult *= ibase; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2086 } |
1400 | 2087 } |
2088 | |
2089 return retval; | |
2090 } | |
2091 | |
10197
4d433bd2d4dc
attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
2092 DEFUNX ("umask", Fumask, args, , |
3301 | 2093 "-*- texinfo -*-\n\ |
2094 @deftypefn {Built-in Function} {} umask (@var{mask})\n\ | |
2095 Set the permission mask for file creation. The parameter @var{mask}\n\ | |
4715 | 2096 is an integer, interpreted as an octal number. If successful,\n\ |
2097 returns the previous value of the mask (as an integer to be\n\ | |
2098 interpreted as an octal number); otherwise an error message is printed.\n\ | |
3301 | 2099 @end deftypefn") |
1400 | 2100 { |
2095 | 2101 octave_value_list retval; |
1400 | 2102 |
2103 int status = 0; | |
2104 | |
2105 if (args.length () == 1) | |
2106 { | |
3202 | 2107 int mask = args(0).int_value (true); |
1400 | 2108 |
3202 | 2109 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2110 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2111 if (mask < 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2112 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2113 status = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2114 ::error ("umask: MASK must be a positive integer value"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2115 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2116 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2117 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2118 int oct_mask = convert (mask, 8, 10); |
1400 | 2119 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2120 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2121 status = convert (octave_umask (oct_mask), 10, 8); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2122 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2123 } |
3202 | 2124 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2125 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2126 status = -1; |
12483
7a5aacf65f81
Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents:
12228
diff
changeset
|
2127 ::error ("umask: MASK must be an integer"); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2128 } |
1400 | 2129 } |
2130 else | |
5823 | 2131 print_usage (); |
1400 | 2132 |
2133 if (status >= 0) | |
4233 | 2134 retval(0) = status; |
1400 | 2135 |
2136 return retval; | |
2137 } | |
2138 | |
5749 | 2139 static octave_value |
6483 | 2140 const_value (const char *, const octave_value_list& args, int val) |
2189 | 2141 { |
5749 | 2142 octave_value retval; |
2143 | |
2144 int nargin = args.length (); | |
2145 | |
2146 if (nargin == 0) | |
2147 retval = val; | |
2148 else | |
5823 | 2149 print_usage (); |
5749 | 2150 |
2151 return retval; | |
2152 } | |
2153 | |
2154 DEFUNX ("P_tmpdir", FP_tmpdir, args, , | |
2155 "-*- texinfo -*-\n\ | |
2156 @deftypefn {Built-in Function} {} P_tmpdir ()\n\ | |
2157 Return the default name of the directory for temporary files on\n\ | |
2158 this system. The name of this directory is system dependent.\n\ | |
2159 @end deftypefn") | |
2160 { | |
2161 octave_value retval; | |
2162 | |
2163 int nargin = args.length (); | |
4267 | 2164 |
5749 | 2165 if (nargin == 0) |
12228
0f70c5db58c3
try to get better value than \ for P_tmpdir on Windows systems
John W. Eaton <jwe@octave.org>
parents:
12211
diff
changeset
|
2166 retval = get_P_tmpdir (); |
5749 | 2167 else |
5823 | 2168 print_usage (); |
5749 | 2169 |
2170 return retval; | |
2171 } | |
2341 | 2172 |
5749 | 2173 // NOTE: the values of SEEK_SET, SEEK_CUR, and SEEK_END have to be |
2174 // this way for Matlab compatibility. | |
2175 | |
2176 DEFUNX ("SEEK_SET", FSEEK_SET, args, , | |
2177 "-*- texinfo -*-\n\ | |
10840 | 2178 @deftypefn {Built-in Function} {} SEEK_SET ()\n\ |
5749 | 2179 @deftypefnx {Built-in Function} {} SEEK_CUR ()\n\ |
2180 @deftypefnx {Built-in Function} {} SEEK_END ()\n\ | |
2181 Return the value required to request that @code{fseek} perform\n\ | |
2182 one of the following actions:\n\ | |
3372 | 2183 @table @code\n\ |
2184 @item SEEK_SET\n\ | |
2185 Position file relative to the beginning.\n\ | |
2186 \n\ | |
2187 @item SEEK_CUR\n\ | |
2188 Position file relative to the current position.\n\ | |
2189 \n\ | |
2190 @item SEEK_END\n\ | |
5749 | 2191 Position file relative to the end.\n\ |
3372 | 2192 @end table\n\ |
5749 | 2193 @end deftypefn") |
2194 { | |
2195 return const_value ("SEEK_SET", args, -1); | |
2196 } | |
2189 | 2197 |
5749 | 2198 DEFUNX ("SEEK_CUR", FSEEK_CUR, args, , |
2199 "-*- texinfo -*-\n\ | |
2200 @deftypefn {Built-in Function} {} SEEK_CUR ()\n\ | |
3458 | 2201 See SEEK_SET.\n\ |
5749 | 2202 @end deftypefn") |
2203 { | |
2204 return const_value ("SEEK_CUR", args, 0); | |
2205 } | |
2189 | 2206 |
5749 | 2207 DEFUNX ("SEEK_END", FSEEK_END, args, , |
2208 "-*- texinfo -*-\n\ | |
2209 @deftypefn {Built-in Function} {} SEEK_END ()\n\ | |
3458 | 2210 See SEEK_SET.\n\ |
5749 | 2211 @end deftypefn") |
2212 { | |
2213 return const_value ("SEEK_END", args, 1); | |
2214 } | |
2215 | |
2216 static octave_value | |
6483 | 2217 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
|
2218 const octave_value& val) |
5749 | 2219 { |
2220 octave_value retval; | |
2221 | |
2222 int nargin = args.length (); | |
2189 | 2223 |
5749 | 2224 if (nargin == 0) |
2225 retval = val; | |
2226 else | |
5823 | 2227 print_usage (); |
5749 | 2228 |
2229 return retval; | |
2230 } | |
2231 | |
2232 DEFUNX ("stdin", Fstdin, args, , | |
2233 "-*- texinfo -*-\n\ | |
2234 @deftypefn {Built-in Function} {} stdin ()\n\ | |
2235 Return the numeric value corresponding to the standard input stream.\n\ | |
2236 When Octave is used interactively, this is filtered through the command\n\ | |
2237 line editing functions.\n\ | |
5333 | 2238 @seealso{stdout, stderr}\n\ |
5749 | 2239 @end deftypefn") |
2240 { | |
2241 return const_value ("stdin", args, stdin_file); | |
2242 } | |
2189 | 2243 |
5749 | 2244 DEFUNX ("stdout", Fstdout, args, , |
2245 "-*- texinfo -*-\n\ | |
2246 @deftypefn {Built-in Function} {} stdout ()\n\ | |
2247 Return the numeric value corresponding to the standard output stream.\n\ | |
2248 Data written to the standard output is normally filtered through the pager.\n\ | |
5333 | 2249 @seealso{stdin, stderr}\n\ |
5749 | 2250 @end deftypefn") |
2251 { | |
2252 return const_value ("stdout", args, stdout_file); | |
2253 } | |
2189 | 2254 |
5749 | 2255 DEFUNX ("stderr", Fstderr, args, , |
2256 "-*- texinfo -*-\n\ | |
2257 @deftypefn {Built-in Function} {} stderr ()\n\ | |
2258 Return the numeric value corresponding to the standard error stream.\n\ | |
2259 Even if paging is turned on, the standard error is not sent to the\n\ | |
2260 pager. It is useful for error messages and prompts.\n\ | |
5333 | 2261 @seealso{stdin, stdout}\n\ |
5749 | 2262 @end deftypefn") |
2263 { | |
2264 return const_value ("stderr", args, stderr_file); | |
2189 | 2265 } |