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