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