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