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