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