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