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