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