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