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