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 |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
2095
|
23 // Originally written by John C. Campbell <jcc@bevo.che.wisc.edu> |
1230
|
24 // |
2095
|
25 // Thomas Baier <baier@ci.tuwien.ac.at> added the original versions of |
|
26 // the following functions: |
1230
|
27 // |
2095
|
28 // popen |
|
29 // pclose |
|
30 // execute (now popen2.m) |
|
31 // sync_system (now merged with system) |
|
32 // async_system (now merged with system) |
1230
|
33 |
2095
|
34 // Completely rewritten by John W. Eaton <jwe@bevo.che.wisc.edu>, |
|
35 // April 1996. |
1
|
36 |
240
|
37 #ifdef HAVE_CONFIG_H |
1230
|
38 #include <config.h> |
1
|
39 #endif |
|
40 |
2095
|
41 #include <climits> |
1343
|
42 |
2095
|
43 #include <iostream.h> |
1350
|
44 |
|
45 #ifdef HAVE_UNISTD_H |
2442
|
46 #ifdef HAVE_SYS_TYPES_H |
1230
|
47 #include <sys/types.h> |
2442
|
48 #endif |
1
|
49 #include <unistd.h> |
1343
|
50 #endif |
1350
|
51 |
2926
|
52 #include "file-ops.h" |
|
53 |
1352
|
54 #include "defun.h" |
1
|
55 #include "error.h" |
2116
|
56 #include "lo-ieee.h" |
2095
|
57 #include "oct-fstrm.h" |
|
58 #include "oct-iostrm.h" |
1377
|
59 #include "oct-map.h" |
1750
|
60 #include "oct-obj.h" |
2095
|
61 #include "oct-prcstrm.h" |
|
62 #include "oct-stream.h" |
|
63 #include "oct-strstrm.h" |
1
|
64 #include "pager.h" |
444
|
65 #include "sysdep.h" |
1352
|
66 #include "utils.h" |
2370
|
67 #include "variables.h" |
1583
|
68 |
2902
|
69 static octave_value stdin_file; |
|
70 static octave_value stdout_file; |
|
71 static octave_value stderr_file; |
|
72 |
1
|
73 void |
164
|
74 initialize_file_io (void) |
1
|
75 { |
2116
|
76 octave_istream *stdin_stream |
|
77 = new octave_istream (&cin, "stdin"); |
|
78 |
|
79 // This uses octave_stdout (see pager.h), not cout so that Octave's |
|
80 // standard output stream will pass through the pager. |
|
81 |
|
82 octave_ostream *stdout_stream |
|
83 = new octave_ostream (&octave_stdout, "stdout"); |
|
84 |
|
85 octave_ostream *stderr_stream |
|
86 = new octave_ostream (&cerr, "stderr"); |
1
|
87 |
2902
|
88 stdin_file = octave_stream_list::insert (stdin_stream); |
|
89 stdout_file = octave_stream_list::insert (stdout_stream); |
|
90 stderr_file = octave_stream_list::insert (stderr_stream); |
1
|
91 } |
|
92 |
2095
|
93 void |
|
94 close_files (void) |
1
|
95 { |
2095
|
96 octave_stream_list::clear (); |
|
97 } |
636
|
98 |
2095
|
99 static void |
|
100 gripe_invalid_file_id (const char *fcn) |
|
101 { |
|
102 ::error ("%s: invalid file id", fcn); |
1
|
103 } |
|
104 |
2095
|
105 static int |
|
106 fopen_mode_to_ios_mode (const string& mode) |
1
|
107 { |
2095
|
108 int retval = 0; |
896
|
109 |
2095
|
110 if (! mode.empty ()) |
368
|
111 { |
2095
|
112 // Could probably be faster, but does it really matter? |
1766
|
113 |
2095
|
114 if (mode == "r") |
|
115 retval = ios::in; |
|
116 else if (mode == "w") |
|
117 retval = ios::out | ios::trunc; |
|
118 else if (mode == "a") |
|
119 retval = ios::out | ios::app; |
|
120 else if (mode == "r+") |
|
121 retval = ios::in | ios::out; |
|
122 else if (mode == "w+") |
|
123 retval = ios::in | ios::out | ios::trunc; |
|
124 else if (mode == "a+") |
|
125 retval = ios::in | ios::out | ios::app; |
|
126 else if (mode == "rb") |
|
127 retval = ios::in | ios::bin; |
|
128 else if (mode == "wb") |
|
129 retval = ios::out | ios::trunc | ios::bin; |
|
130 else if (mode == "ab") |
|
131 retval = ios::out | ios::app | ios::bin; |
|
132 else if (mode == "r+b") |
|
133 retval = ios::in | ios::out | ios::bin; |
|
134 else if (mode == "w+b") |
|
135 retval = ios::in | ios::out | ios::trunc | ios::bin; |
|
136 else if (mode == "a+b") |
|
137 retval = ios::in | ios::out | ios::app | ios::bin; |
368
|
138 else |
2095
|
139 ::error ("invalid mode specified"); |
1
|
140 } |
|
141 |
|
142 return retval; |
|
143 } |
|
144 |
1957
|
145 DEFUN (fclose, args, , |
2116
|
146 "fclose (FILENUM): close a file") |
529
|
147 { |
2095
|
148 double retval = -1.0; |
529
|
149 |
|
150 int nargin = args.length (); |
|
151 |
2095
|
152 if (nargin == 1) |
|
153 { |
2800
|
154 retval = static_cast<double> (octave_stream_list::remove (args(0))); |
1
|
155 |
2095
|
156 if (retval < 0) |
|
157 gripe_invalid_file_id ("fclose"); |
1
|
158 } |
|
159 else |
2095
|
160 print_usage ("fclose"); |
1
|
161 |
|
162 return retval; |
|
163 } |
|
164 |
1957
|
165 DEFUN (fflush, args, , |
2116
|
166 "fflush (FILENUM): flush buffered data to output file") |
1181
|
167 { |
2095
|
168 double retval = -1.0; |
1181
|
169 |
|
170 int nargin = args.length (); |
|
171 |
2095
|
172 if (nargin == 1) |
|
173 { |
2609
|
174 // XXX FIXME XXX -- any way to avoid special case for stdout? |
|
175 |
|
176 int fid = octave_stream_list::get_file_number (args (0)); |
|
177 |
|
178 if (fid == 1) |
|
179 { |
|
180 flush_octave_stdout (); |
2095
|
181 |
2609
|
182 retval = 0.0; |
|
183 } |
2095
|
184 else |
2609
|
185 { |
|
186 octave_stream *os = octave_stream_list::lookup (fid); |
|
187 |
|
188 if (os) |
2800
|
189 retval = static_cast<double> (os->flush ()); |
2609
|
190 else |
|
191 gripe_invalid_file_id ("fflush"); |
|
192 } |
2095
|
193 } |
|
194 else |
1181
|
195 print_usage ("fflush"); |
|
196 |
|
197 return retval; |
|
198 } |
|
199 |
2095
|
200 DEFUN (fgetl, args, , |
2599
|
201 "[STRING, LENGTH] = fgetl (FILENUM [, LENGTH])\n\ |
1339
|
202 \n\ |
|
203 read a string from a file") |
|
204 { |
2599
|
205 octave_value_list retval; |
|
206 |
|
207 retval(1) = 0.0; |
|
208 retval(0) = -1.0; |
1339
|
209 |
|
210 int nargin = args.length (); |
|
211 |
|
212 if (nargin == 1 || nargin == 2) |
2095
|
213 { |
|
214 octave_stream *os = octave_stream_list::lookup (args(0)); |
|
215 |
|
216 if (os) |
|
217 { |
|
218 octave_value len_arg = (nargin == 2) |
2800
|
219 ? args(1) : octave_value (static_cast<double> (INT_MAX)); |
2095
|
220 |
|
221 bool err = false; |
|
222 |
|
223 string tmp = os->getl (len_arg, err); |
|
224 |
|
225 if (! err) |
2599
|
226 { |
2800
|
227 retval(1) = static_cast<double> (tmp.length ()); |
2599
|
228 retval(0) = tmp; |
|
229 } |
2095
|
230 } |
|
231 else |
|
232 gripe_invalid_file_id ("fgetl"); |
|
233 } |
1339
|
234 else |
|
235 print_usage ("fgetl"); |
|
236 |
|
237 return retval; |
|
238 } |
|
239 |
2095
|
240 DEFUN (fgets, args, , |
2599
|
241 "[STRING, LENGTH] = fgets (FILENUM [, LENGTH])\n\ |
529
|
242 \n\ |
1181
|
243 read a string from a file") |
529
|
244 { |
2599
|
245 octave_value_list retval; |
|
246 |
|
247 retval(1) = 0.0; |
|
248 retval(0) = -1.0; |
529
|
249 |
|
250 int nargin = args.length (); |
|
251 |
1338
|
252 if (nargin == 1 || nargin == 2) |
2095
|
253 { |
|
254 octave_stream *os = octave_stream_list::lookup (args(0)); |
|
255 |
|
256 if (os) |
|
257 { |
|
258 octave_value len_arg = (nargin == 2) |
2800
|
259 ? args(1) : octave_value (static_cast<double> (INT_MAX)); |
2095
|
260 |
|
261 bool err = false; |
|
262 |
|
263 string tmp = os->gets (len_arg, err); |
|
264 |
|
265 if (! err) |
2599
|
266 { |
2800
|
267 retval(1) = static_cast<double> (tmp.length ()); |
2599
|
268 retval(0) = tmp; |
|
269 } |
2095
|
270 } |
|
271 else |
|
272 gripe_invalid_file_id ("fgets"); |
|
273 } |
1338
|
274 else |
1181
|
275 print_usage ("fgets"); |
529
|
276 |
|
277 return retval; |
|
278 } |
|
279 |
2095
|
280 static octave_base_stream * |
|
281 do_stream_open (const string& name, const string& mode, |
|
282 const string& arch, int& fid) |
1
|
283 { |
2095
|
284 octave_base_stream *retval = 0; |
1
|
285 |
2095
|
286 fid = -1; |
1
|
287 |
2095
|
288 int md = fopen_mode_to_ios_mode (mode); |
1
|
289 |
2095
|
290 if (! error_state) |
|
291 { |
2318
|
292 oct_mach_info::float_format flt_fmt = |
|
293 oct_mach_info::string_to_float_format (arch); |
1
|
294 |
2095
|
295 if (! error_state) |
2318
|
296 retval = new octave_fstream (name, md, flt_fmt); |
1
|
297 } |
|
298 |
2095
|
299 return retval; |
|
300 } |
1
|
301 |
2095
|
302 static octave_base_stream * |
|
303 do_stream_open (const octave_value& tc_name, const octave_value& tc_mode, |
|
304 const octave_value& tc_arch, const char *fcn, int& fid) |
|
305 { |
|
306 octave_base_stream *retval = 0; |
|
307 |
|
308 fid = -1; |
|
309 |
|
310 string name = tc_name.string_value (); |
|
311 |
|
312 if (! error_state) |
1
|
313 { |
2095
|
314 string mode = tc_mode.string_value (); |
|
315 |
|
316 if (! error_state) |
|
317 { |
|
318 string arch = tc_arch.string_value (); |
1
|
319 |
2095
|
320 if (! error_state) |
|
321 retval = do_stream_open (name, mode, arch, fid); |
|
322 else |
|
323 ::error ("%s: architecture type must be a string", fcn); |
|
324 } |
|
325 else |
|
326 ::error ("%s: file mode must be a string", fcn); |
1
|
327 } |
2095
|
328 else |
|
329 ::error ("%s: file name must be a string", fcn); |
1
|
330 |
|
331 return retval; |
|
332 } |
|
333 |
1957
|
334 DEFUN (fopen, args, , |
2318
|
335 "[FILENUM, ERRMSG] = fopen (FILENAME, MODE [, ARCH]): open a file\n\ |
1181
|
336 \n\ |
2318
|
337 FILENAME is a string specifying the name of the file.\n\ |
|
338 \n\ |
|
339 MODE is a string specifying whether the file should be opened for\n\ |
|
340 reading, writing, or both. Valid values for MODE include:\n\ |
1181
|
341 \n\ |
2095
|
342 r : open text file for reading\n\ |
|
343 w : open text file for writing; discard previous contents if any\n\ |
|
344 a : append; open or create text file for writing at end of file\n\ |
|
345 r+ : open text file for update (i.e., reading and writing)\n\ |
|
346 w+ : create text file for update; discard previous contents if any\n\ |
|
347 a+ : append; open or create text file for update, writing at end\n\ |
|
348 \n\ |
2318
|
349 Update mode permits reading from and writing to the same file.\n\ |
|
350 \n\ |
3263
|
351 If MODE is missing, a value of \"r\" is assumed.\n\ |
|
352 \n\ |
2318
|
353 ARCH is a string specifying the default data format for the file.\n\ |
|
354 Valid values for ARCH are:\n\ |
|
355 \n\ |
|
356 native -- the format of the current machine (this is the default)\n\ |
|
357 ieee-le -- IEEE big endian\n\ |
|
358 ieee-be -- IEEE little endian\n\ |
|
359 vaxd -- VAX D floating format\n\ |
|
360 vaxg -- VAX G floating format\n\ |
2324
|
361 cray -- Cray floating format\n\ |
2318
|
362 \n\ |
|
363 however, conversions are currently only supported for ieee-be, and\n\ |
|
364 ieee-le formats.\n\ |
|
365 \n\ |
|
366 \n\ |
|
367 FILENUM is a number that can be used to refer to the open file.\n\ |
|
368 If fopen fails, FILENUM is set to -1 and ERRMSG contains a\n\ |
|
369 system-dependent error message") |
529
|
370 { |
2599
|
371 octave_value_list retval; |
|
372 |
|
373 retval(0) = -1.0; |
529
|
374 |
|
375 int nargin = args.length (); |
|
376 |
2095
|
377 if (nargin == 1) |
|
378 { |
3263
|
379 if (args(0).is_string ()) |
|
380 { |
|
381 // If there is only one argument and it is a string but it |
|
382 // is not the string "all", we assume it is a file to open |
|
383 // with MODE = "r". To open a file called "all", you have |
|
384 // to supply more than one argument. |
|
385 |
|
386 if (args(0).string_value () == "all") |
|
387 return octave_stream_list::open_file_numbers (); |
|
388 } |
2095
|
389 else |
|
390 { |
|
391 string_vector tmp = octave_stream_list::get_info (args(0)); |
529
|
392 |
2095
|
393 if (! error_state) |
|
394 { |
|
395 retval(2) = tmp(2); |
|
396 retval(1) = tmp(1); |
|
397 retval(0) = tmp(0); |
|
398 } |
3263
|
399 |
|
400 return retval; |
2432
|
401 } |
1
|
402 } |
|
403 |
2095
|
404 if (nargin > 0 && nargin < 4) |
|
405 { |
|
406 octave_value mode = (nargin == 2 || nargin == 3) |
|
407 ? args(1) : octave_value ("r"); |
|
408 |
|
409 octave_value arch = (nargin == 3) |
|
410 ? args(2) : octave_value ("native"); |
|
411 |
|
412 int fid = -1; |
|
413 |
|
414 octave_base_stream *os |
|
415 = do_stream_open (args(0), mode, arch, "fopen", fid); |
|
416 |
|
417 if (os) |
|
418 { |
|
419 if (os->ok () && ! error_state) |
|
420 { |
|
421 retval(1) = ""; |
2902
|
422 retval(0) = octave_stream_list::insert (os); |
2095
|
423 } |
|
424 else |
|
425 { |
3162
|
426 int error_number = 0; |
|
427 |
|
428 retval(1) = os->error (false, error_number); |
2095
|
429 retval(0) = -1.0; |
|
430 } |
|
431 } |
|
432 else |
|
433 ::error ("fopen: internal error"); |
|
434 } |
|
435 else |
|
436 print_usage ("fopen"); |
1
|
437 |
|
438 return retval; |
|
439 } |
|
440 |
1957
|
441 DEFUN (freport, args, , |
1181
|
442 "freport (): list open files and their status") |
|
443 { |
2095
|
444 octave_value_list retval; |
1181
|
445 |
|
446 int nargin = args.length (); |
|
447 |
|
448 if (nargin > 0) |
|
449 warning ("freport: ignoring extra arguments"); |
|
450 |
2095
|
451 octave_stdout << octave_stream_list::list_open_files (); |
1181
|
452 |
|
453 return retval; |
|
454 } |
|
455 |
1957
|
456 DEFUN (frewind, args, , |
2116
|
457 "frewind (FILENUM): set file position at beginning of file") |
529
|
458 { |
2095
|
459 double retval = -1.0; |
1
|
460 |
506
|
461 int nargin = args.length (); |
|
462 |
2095
|
463 if (nargin == 1) |
1086
|
464 { |
2095
|
465 octave_stream *os = octave_stream_list::lookup (args(0)); |
636
|
466 |
2095
|
467 if (os) |
2800
|
468 retval = static_cast<double> (os->rewind ()); |
1
|
469 else |
2095
|
470 gripe_invalid_file_id ("frewind"); |
1
|
471 } |
|
472 else |
2095
|
473 print_usage ("frewind"); |
1
|
474 |
|
475 return retval; |
|
476 } |
|
477 |
1957
|
478 DEFUN (fseek, args, , |
2116
|
479 "fseek (FILENUM, OFFSET [, ORIGIN])\n\ |
1181
|
480 \n\ |
2341
|
481 set file position for reading or writing.\n\ |
|
482 \n\ |
|
483 ORIGIN may be one of:\n\ |
|
484 \n\ |
|
485 SEEK_SET : offset is relative to the beginning of the file (default)\n\ |
|
486 SEEK_CUR : offset is relative to the current position\n\ |
|
487 SEEK_END : offset is relative to the end of the file") |
529
|
488 { |
2095
|
489 double retval = -1.0; |
529
|
490 |
|
491 int nargin = args.length (); |
|
492 |
2095
|
493 if (nargin == 2 || nargin == 3) |
|
494 { |
|
495 octave_stream *os = octave_stream_list::lookup (args(0)); |
1181
|
496 |
2095
|
497 if (os) |
|
498 { |
|
499 octave_value origin_arg = (nargin == 3) |
2341
|
500 ? args(2) : octave_value (-1.0); |
1
|
501 |
2800
|
502 retval = static_cast<double> (os->seek (args(1), origin_arg)); |
2095
|
503 } |
|
504 else |
|
505 ::error ("fseek: invalid file id"); |
368
|
506 } |
2095
|
507 else |
|
508 print_usage ("fseek"); |
1
|
509 |
|
510 return retval; |
|
511 } |
|
512 |
1957
|
513 DEFUN (ftell, args, , |
2116
|
514 "POSITION = ftell (FILENUM): returns the current file position") |
1181
|
515 { |
2095
|
516 double retval = -1.0; |
1
|
517 |
506
|
518 int nargin = args.length (); |
|
519 |
2095
|
520 if (nargin == 1) |
1
|
521 { |
2095
|
522 octave_stream *os = octave_stream_list::lookup (args(0)); |
1
|
523 |
2095
|
524 if (os) |
2800
|
525 retval = static_cast<double> (os->tell ()); |
2095
|
526 else |
|
527 gripe_invalid_file_id ("ftell"); |
1
|
528 } |
|
529 else |
2095
|
530 print_usage ("ftell"); |
1
|
531 |
|
532 return retval; |
|
533 } |
|
534 |
1957
|
535 DEFUN (fprintf, args, , |
2116
|
536 "fprintf (FILENUM, FORMAT, ...)") |
1181
|
537 { |
2095
|
538 double retval = -1.0; |
1181
|
539 |
|
540 int nargin = args.length (); |
|
541 |
2875
|
542 if (nargin > 1 || (nargin > 0 && args(0).is_string ())) |
2095
|
543 { |
2873
|
544 octave_stream *os = 0; |
|
545 int fmt_n = 0; |
|
546 |
|
547 if (args(0).is_string ()) |
|
548 os = octave_stream_list::lookup (1); |
|
549 else |
|
550 { |
|
551 fmt_n = 1; |
|
552 os = octave_stream_list::lookup (args(0)); |
|
553 } |
2095
|
554 |
|
555 if (os) |
|
556 { |
2873
|
557 if (args(fmt_n).is_string ()) |
2095
|
558 { |
2873
|
559 string fmt = args(fmt_n).string_value (); |
2095
|
560 |
|
561 octave_value_list tmp_args; |
|
562 |
2873
|
563 if (nargin > 1 + fmt_n) |
2095
|
564 { |
2873
|
565 tmp_args.resize (nargin-fmt_n-1, octave_value ()); |
2095
|
566 |
2873
|
567 for (int i = fmt_n + 1; i < nargin; i++) |
|
568 tmp_args(i-fmt_n-1) = args(i); |
2095
|
569 } |
|
570 |
|
571 retval = os->printf (fmt, tmp_args); |
|
572 } |
|
573 else |
|
574 ::error ("fprintf: format must be a string"); |
|
575 } |
|
576 else |
|
577 gripe_invalid_file_id ("fprintf"); |
|
578 } |
|
579 else |
1181
|
580 print_usage ("fprintf"); |
|
581 |
|
582 return retval; |
|
583 } |
|
584 |
2095
|
585 DEFUN (fputs, args, , |
2116
|
586 "fputs (FILENUM, STRING)") |
1181
|
587 { |
2095
|
588 double retval = -1.0; |
1181
|
589 |
|
590 int nargin = args.length (); |
|
591 |
2095
|
592 if (nargin == 2) |
|
593 { |
|
594 octave_stream *os = octave_stream_list::lookup (args(0)); |
1181
|
595 |
2095
|
596 if (os) |
|
597 retval = os->puts (args(1)); |
|
598 else |
|
599 gripe_invalid_file_id ("fputs"); |
|
600 } |
1181
|
601 else |
2095
|
602 print_usage ("fputs"); |
1181
|
603 |
|
604 return retval; |
|
605 } |
|
606 |
2095
|
607 DEFUN (sprintf, args, , |
|
608 "[s, errmsg, status] = sprintf (FORMAT, ...)") |
1
|
609 { |
2095
|
610 octave_value_list retval; |
1
|
611 |
2095
|
612 int nargin = args.length (); |
1
|
613 |
2095
|
614 if (nargin > 0) |
1
|
615 { |
2116
|
616 retval(2) = -1.0; |
|
617 retval(1) = "unknown error"; |
|
618 retval(0) = ""; |
|
619 |
2095
|
620 octave_ostrstream ostr; |
1
|
621 |
2148
|
622 octave_stream os (&ostr, true); |
628
|
623 |
2095
|
624 if (os) |
|
625 { |
|
626 if (args(0).is_string ()) |
|
627 { |
|
628 string fmt = args(0).string_value (); |
628
|
629 |
2095
|
630 octave_value_list tmp_args; |
1
|
631 |
2095
|
632 if (nargin > 1) |
|
633 { |
|
634 tmp_args.resize (nargin-1, octave_value ()); |
1
|
635 |
2095
|
636 for (int i = 1; i < nargin; i++) |
|
637 tmp_args(i-1) = args(i); |
|
638 } |
628
|
639 |
2825
|
640 retval(2) = static_cast<double> (os.printf (fmt, tmp_args)); |
2095
|
641 retval(1) = os.error (); |
|
642 char *tmp = ostr.str (); |
|
643 retval(0) = tmp; |
|
644 delete [] tmp; |
|
645 } |
|
646 else |
|
647 ::error ("sprintf: format must be a string"); |
|
648 } |
|
649 else |
|
650 ::error ("sprintf: unable to create output buffer"); |
1
|
651 } |
|
652 else |
2095
|
653 print_usage ("sprintf"); |
1
|
654 |
|
655 return retval; |
|
656 } |
|
657 |
2095
|
658 DEFUN (fscanf, args, , |
2122
|
659 "[A, COUNT] = fscanf (FILENUM, FORMAT [, SIZE])\n\ |
|
660 \n\ |
|
661 Read from FILENUM according to FORMAT, returning the result in the\n\ |
|
662 matrix A. SIZE is optional. If present, it can be one of\n\ |
|
663 \n\ |
|
664 Inf : read as much as possible, returning a column vector\n\ |
|
665 (unless doing all character conversions, in which case a\n\ |
|
666 string is returned)\n\ |
3243
|
667 NR : read up to NR elements, returning a column vector\n\ |
2122
|
668 [NR, NC] : read up to NR x NC elements, returning a matrix with NR rows\n\ |
3243
|
669 [NR, Inf] : read as much as possible, returning a matrix with NR rows\n\ |
2122
|
670 \n\ |
|
671 If it is omitted, a value of Inf is assumed.\n\ |
|
672 \n\ |
2215
|
673 The number of items successfully read is returned in COUNT.\n\ |
|
674 \n\ |
|
675 [A, B, ...] = fscanf (FILENUM, FORMAT, \"C\")\n\ |
|
676 \n\ |
|
677 Read from FILENUM according to FORMAT, with each conversion specifier\n\ |
|
678 in FORMAT corresponding to a single scalar return value. This form is\n\ |
|
679 more `C-like', and also compatible with previous versions of Octave") |
1181
|
680 { |
2095
|
681 octave_value_list retval; |
1181
|
682 |
|
683 int nargin = args.length (); |
|
684 |
2215
|
685 if (nargin == 3 && args(2).is_string ()) |
2095
|
686 { |
|
687 octave_stream *os = octave_stream_list::lookup (args(0)); |
1181
|
688 |
2095
|
689 if (os) |
|
690 { |
|
691 if (args(1).is_string ()) |
|
692 { |
|
693 string fmt = args(1).string_value (); |
1181
|
694 |
2215
|
695 retval = os->oscanf (fmt); |
2095
|
696 } |
|
697 else |
|
698 ::error ("fscanf: format must be a string"); |
|
699 } |
|
700 else |
|
701 gripe_invalid_file_id ("fscanf"); |
|
702 } |
1181
|
703 else |
2215
|
704 { |
|
705 retval (1) = 0.0; |
|
706 retval (0) = Matrix (); |
|
707 |
|
708 if (nargin == 2 || nargin == 3) |
|
709 { |
|
710 octave_stream *os = octave_stream_list::lookup (args(0)); |
|
711 |
|
712 if (os) |
|
713 { |
|
714 if (args(1).is_string ()) |
|
715 { |
|
716 string fmt = args(1).string_value (); |
|
717 |
|
718 int count = 0; |
|
719 |
|
720 Matrix size = (nargin == 3) |
|
721 ? args(2).matrix_value () : Matrix (1, 1, octave_Inf); |
|
722 |
|
723 if (! error_state) |
|
724 { |
|
725 octave_value tmp = os->scanf (fmt, size, count); |
|
726 |
2800
|
727 retval(1) = static_cast<double> (count); |
2215
|
728 retval(0) = tmp; |
|
729 } |
|
730 } |
|
731 else |
|
732 ::error ("fscanf: format must be a string"); |
|
733 } |
|
734 else |
|
735 gripe_invalid_file_id ("fscanf"); |
|
736 } |
|
737 else |
|
738 print_usage ("fscanf"); |
|
739 } |
1181
|
740 |
|
741 return retval; |
|
742 } |
|
743 |
2095
|
744 DEFUN (sscanf, args, , |
2122
|
745 "[A, COUNT, ERRMSG, INDEX] = sscanf (STRING, FORMAT, SIZE)\n\ |
|
746 \n\ |
2215
|
747 Read from STRING according to FORMAT, returning the result in the\n\ |
2122
|
748 matrix A. SIZE is optional. If present, it can be one of\n\ |
|
749 \n\ |
|
750 Inf : read as much as possible, returning a column vector\n\ |
|
751 (unless doing all character conversions, in which case a\n\ |
|
752 string is returned)\n\ |
3243
|
753 NR : read up to NR elements, returning a column vector\n\ |
2122
|
754 [NR, NC] : read up to NR x NC elements, returning a matrix with NR rows\n\ |
3243
|
755 [NR, Inf] : read as much as possible, returning a matrix with NR rows\n\ |
2122
|
756 \n\ |
|
757 If it is omitted, a value of Inf is assumed.\n\ |
|
758 \n\ |
|
759 The number of items successfully read is returned in COUNT. If an\n\ |
|
760 error occurs, ERRMSG contains the text of the corresponding error\n\ |
|
761 message. INDEX contains the index of the next character to be read\n\ |
2215
|
762 from STRING\n\ |
|
763 \n\ |
|
764 [A, B, ...] = sscanf (STRING, FORMAT, \"C\")\n\ |
|
765 \n\ |
|
766 Read from STRING according to FORMAT, with each conversion specifier\n\ |
|
767 in FORMAT corresponding to a single scalar return value. This form is\n\ |
|
768 more `C-like', and also compatible with previous versions of Octave") |
444
|
769 { |
2095
|
770 octave_value_list retval; |
444
|
771 |
506
|
772 int nargin = args.length (); |
|
773 |
2215
|
774 if (nargin == 3 && args(2).is_string ()) |
2095
|
775 { |
|
776 if (args(0).is_string ()) |
|
777 { |
|
778 string data = args(0).string_value (); |
444
|
779 |
2095
|
780 octave_istrstream istr (data); |
1358
|
781 |
2148
|
782 octave_stream os (&istr, true); |
636
|
783 |
2095
|
784 if (os) |
|
785 { |
|
786 if (args(1).is_string ()) |
|
787 { |
|
788 string fmt = args(1).string_value (); |
444
|
789 |
2215
|
790 retval = os.oscanf (fmt); |
2095
|
791 } |
|
792 else |
|
793 ::error ("sscanf: format must be a string"); |
|
794 } |
|
795 else |
|
796 ::error ("sscanf: unable to create temporary input buffer"); |
444
|
797 } |
636
|
798 else |
2095
|
799 ::error ("sscanf: first argument must be a string"); |
444
|
800 } |
|
801 else |
2215
|
802 { |
|
803 if (nargin == 2 || nargin == 3) |
|
804 { |
|
805 retval(3) = -1.0; |
|
806 retval(2) = "unknown error"; |
|
807 retval(1) = 0.0; |
|
808 retval(0) = Matrix (); |
|
809 |
|
810 if (args(0).is_string ()) |
|
811 { |
|
812 string data = args(0).string_value (); |
|
813 |
|
814 octave_istrstream istr (data); |
|
815 |
|
816 octave_stream os (&istr, true); |
|
817 |
|
818 if (os) |
|
819 { |
|
820 if (args(1).is_string ()) |
|
821 { |
|
822 string fmt = args(1).string_value (); |
|
823 |
|
824 int count = 0; |
|
825 |
|
826 Matrix size = (nargin == 3) |
|
827 ? args(2).matrix_value () : Matrix (1, 1, octave_Inf); |
|
828 |
|
829 octave_value tmp = os.scanf (fmt, size, count); |
|
830 |
|
831 // XXX FIXME XXX -- is this the right thing to do? |
|
832 // Extract error message first, because getting |
|
833 // position will clear it. |
|
834 string errmsg = os.error (); |
|
835 |
2800
|
836 retval(3) = static_cast<double> (os.tell () + 1); |
2215
|
837 retval(2) = errmsg; |
2800
|
838 retval(1) = static_cast<double> (count); |
2215
|
839 retval(0) = tmp; |
|
840 } |
|
841 else |
|
842 ::error ("sscanf: format must be a string"); |
|
843 } |
|
844 else |
|
845 ::error ("sscanf: unable to create temporary input buffer"); |
|
846 } |
|
847 else |
|
848 ::error ("sscanf: first argument must be a string"); |
|
849 } |
|
850 else |
|
851 print_usage ("sscanf"); |
|
852 } |
444
|
853 |
|
854 return retval; |
|
855 } |
|
856 |
2215
|
857 DEFUN (scanf, args, nargout, |
|
858 "scanf (FORMAT) is equivalent to fscanf (stdin, FORMAT)") |
|
859 { |
|
860 int nargin = args.length (); |
|
861 |
|
862 octave_value_list tmp_args (nargin+1, octave_value ()); |
|
863 |
|
864 tmp_args (0) = 0.0; |
|
865 for (int i = 0; i < nargin; i++) |
|
866 tmp_args (i+1) = args (i); |
|
867 |
|
868 return Ffscanf (tmp_args, nargout); |
|
869 } |
|
870 |
2116
|
871 static octave_value |
|
872 do_fread (octave_stream& os, const octave_value& size_arg, |
|
873 const octave_value& prec_arg, const octave_value& skip_arg, |
|
874 const octave_value& arch_arg, int& count) |
|
875 { |
|
876 octave_value retval; |
|
877 |
|
878 count = -1; |
|
879 |
|
880 Matrix size = size_arg.matrix_value (); |
|
881 |
|
882 if (! error_state) |
|
883 { |
|
884 string prec = prec_arg.string_value (); |
|
885 |
|
886 if (! error_state) |
|
887 { |
2318
|
888 oct_data_conv::data_type dt |
|
889 = oct_data_conv::string_to_data_type (prec); |
2116
|
890 |
|
891 if (! error_state) |
|
892 { |
3202
|
893 int skip = skip_arg.int_value (true); |
2116
|
894 |
|
895 if (! error_state) |
|
896 { |
3202
|
897 string arch = arch_arg.string_value (); |
|
898 |
|
899 if (! error_state) |
2116
|
900 { |
3202
|
901 oct_mach_info::float_format flt_fmt |
|
902 = oct_mach_info::string_to_float_format (arch); |
2116
|
903 |
|
904 if (! error_state) |
3202
|
905 retval = os.read (size, dt, skip, flt_fmt, count); |
2116
|
906 } |
|
907 else |
3202
|
908 ::error ("fread: architecture type must be a string"); |
2116
|
909 } |
|
910 else |
3202
|
911 ::error ("fread: skip must be an integer"); |
2116
|
912 } |
|
913 else |
|
914 ::error ("fread: invalid data type specified"); |
|
915 } |
|
916 else |
|
917 ::error ("fread: precision must be a string"); |
|
918 } |
|
919 else |
|
920 ::error ("fread: invalid size specified"); |
|
921 |
|
922 return retval; |
|
923 } |
|
924 |
|
925 DEFUN (fread, args, , |
|
926 "[DATA, COUNT] = fread (FILENUM [, SIZE] [, PRECISION] [, SKIP] [, ARCH])\n\ |
529
|
927 \n\ |
2318
|
928 Reads data in binary form of type PRECISION from a file.\n\ |
|
929 \n\ |
|
930 FILENUM : file number from fopen\n\ |
529
|
931 \n\ |
2318
|
932 SIZE : size specification for the data matrix\n\ |
|
933 \n\ |
|
934 PRECISION : string specifying type of data to read, valid types are\n\ |
529
|
935 \n\ |
2318
|
936 char, char*1, integer*1, int8 -- character\n\ |
|
937 schar, signed char -- signed character\n\ |
|
938 uchar, unsigned char -- unsigned character (default)\n\ |
|
939 short -- short integer\n\ |
|
940 ushort, unsigned short -- unsigned short integer\n\ |
|
941 int -- integer\n\ |
|
942 uint, unsigned int -- unsigned integer\n\ |
|
943 long -- long integer\n\ |
|
944 ulong, unsigned long -- unsigned long integer\n\ |
|
945 float, float32, real*4 -- single precision float\n\ |
|
946 double, float64, real*8 -- double precision float\n\ |
|
947 int16, integer*2 -- two byte integer\n\ |
3238
|
948 uint16 -- two byte unsigned integer\n\ |
2318
|
949 int32, integer*4 -- four byte integer\n\ |
3238
|
950 uint32 -- four byte unsigned integer\n\ |
2318
|
951 \n\ |
3180
|
952 SKIP : number of bytes to skip after each element is read\n\ |
2318
|
953 (default is 0)\n\ |
529
|
954 \n\ |
2802
|
955 ARCH : string specifying the data format for the file. Valid\n\ |
2318
|
956 values are\n\ |
|
957 \n\ |
|
958 native -- the format of the current machine (default)\n\ |
3246
|
959 ieee-be -- IEEE big endian\n\ |
|
960 ieee-le -- IEEE little endian\n\ |
2318
|
961 vaxd -- VAX D floating format\n\ |
|
962 vaxg -- VAX G floating format\n\ |
2324
|
963 cray -- Cray floating format\n\ |
2318
|
964 \n\ |
2324
|
965 however, conversions are currently only supported for\n\ |
3246
|
966 ieee-be and ieee-le formats.\n\ |
2318
|
967 \n\ |
|
968 \n\ |
|
969 DATA : matrix in which the data is stored\n\ |
|
970 \n\ |
|
971 COUNT : number of elements read") |
529
|
972 { |
2095
|
973 octave_value_list retval; |
2116
|
974 |
|
975 int nargin = args.length (); |
|
976 |
2318
|
977 if (nargin > 0 && nargin < 6) |
2116
|
978 { |
|
979 retval(1) = -1.0; |
|
980 retval(0) = Matrix (); |
|
981 |
|
982 octave_stream *os = octave_stream_list::lookup (args(0)); |
|
983 |
|
984 if (os) |
|
985 { |
|
986 octave_value size = (nargin > 1) |
|
987 ? args(1) : octave_value (octave_Inf); |
|
988 |
|
989 octave_value prec = (nargin > 2) |
|
990 ? args(2) : octave_value ("uchar"); |
|
991 |
|
992 octave_value skip = (nargin > 3) |
|
993 ? args(3) : octave_value (0.0); |
|
994 |
|
995 octave_value arch = (nargin > 4) |
2318
|
996 ? args(4) : octave_value ("unknown"); |
2116
|
997 |
|
998 int count = -1; |
|
999 |
|
1000 octave_value tmp = do_fread (*os, size, prec, skip, arch, count); |
|
1001 |
2800
|
1002 retval(1) = static_cast<double> (count); |
2116
|
1003 retval(0) = tmp; |
|
1004 } |
|
1005 else |
|
1006 gripe_invalid_file_id ("fread"); |
|
1007 } |
|
1008 else |
|
1009 print_usage ("fread"); |
|
1010 |
529
|
1011 return retval; |
|
1012 } |
|
1013 |
2116
|
1014 static int |
|
1015 do_fwrite (octave_stream& os, const octave_value& data, |
|
1016 const octave_value& prec_arg, const octave_value& skip_arg, |
|
1017 const octave_value& arch_arg) |
|
1018 { |
|
1019 int retval = -1; |
|
1020 |
|
1021 string prec = prec_arg.string_value (); |
|
1022 |
|
1023 if (! error_state) |
|
1024 { |
2318
|
1025 oct_data_conv::data_type dt |
|
1026 = oct_data_conv::string_to_data_type (prec); |
2116
|
1027 |
|
1028 if (! error_state) |
|
1029 { |
3202
|
1030 int skip = skip_arg.int_value (true); |
2116
|
1031 |
|
1032 if (! error_state) |
|
1033 { |
3202
|
1034 string arch = arch_arg.string_value (); |
|
1035 |
|
1036 if (! error_state) |
2116
|
1037 { |
3202
|
1038 oct_mach_info::float_format flt_fmt |
|
1039 = oct_mach_info::string_to_float_format (arch); |
2116
|
1040 |
|
1041 if (! error_state) |
3202
|
1042 retval = os.write (data, dt, skip, flt_fmt); |
2116
|
1043 } |
|
1044 else |
3202
|
1045 ::error ("fwrite: architecture type must be a string"); |
2116
|
1046 } |
|
1047 else |
3202
|
1048 ::error ("fwrite: skip must be an integer"); |
2116
|
1049 } |
3202
|
1050 else |
|
1051 ::error ("fwrite: invalid precision specified"); |
2116
|
1052 } |
|
1053 else |
|
1054 ::error ("fwrite: precision must be a string"); |
|
1055 |
|
1056 return retval; |
|
1057 } |
|
1058 |
|
1059 DEFUN (fwrite, args, , |
|
1060 "COUNT = fwrite (FILENUM, DATA [, PRECISION] [, SKIP] [, ARCH])\n\ |
1181
|
1061 \n\ |
2318
|
1062 Writes data to a file in binary form of size PRECISION\n\ |
|
1063 \n\ |
|
1064 FILENUM : file number from fopen\n\ |
|
1065 \n\ |
|
1066 DATA : matrix of elements to be written\n\ |
|
1067 \n\ |
3180
|
1068 PRECISION : string specifying type of data to write, valid types are\n\ |
1181
|
1069 \n\ |
2318
|
1070 char, char*1, integer*1, int8 -- character\n\ |
|
1071 schar, signed char -- signed character\n\ |
|
1072 uchar, unsigned char -- unsigned character (default)\n\ |
|
1073 short -- short integer\n\ |
|
1074 ushort, unsigned short -- unsigned short integer\n\ |
|
1075 int -- integer\n\ |
|
1076 uint, unsigned int -- unsigned integer\n\ |
|
1077 long -- long integer\n\ |
|
1078 ulong, unsigned long -- unsigned long integer\n\ |
|
1079 float, float32, real*4 -- single precision float\n\ |
|
1080 double, float64, real*8 -- double precision float\n\ |
|
1081 int16, integer*2 -- two byte integer\n\ |
3238
|
1082 uint16 -- two byte unsigned integer\n\ |
2318
|
1083 int32, integer*4 -- four byte integer\n\ |
3238
|
1084 uint32 -- four byte unsigned integer\n\ |
1181
|
1085 \n\ |
3180
|
1086 SKIP : number of bytes to skip before each element is written\n\ |
2318
|
1087 (the default is 0)\n\ |
|
1088 \n\ |
2802
|
1089 ARCH : string specifying the data format for the file. Valid\n\ |
2318
|
1090 values are\n\ |
1181
|
1091 \n\ |
2318
|
1092 native -- the format of the current machine (default)\n\ |
|
1093 ieee-le -- IEEE big endian\n\ |
|
1094 ieee-be -- IEEE little endian\n\ |
|
1095 vaxd -- VAX D floating format\n\ |
|
1096 vaxg -- VAX G floating format\n\ |
2802
|
1097 cray -- Cray floating format\n\ |
2318
|
1098 \n\ |
|
1099 however, conversions are currently only supported for ieee-be, and\n\ |
|
1100 ieee-le formats.\n\ |
|
1101 \n\ |
|
1102 COUNT : number of elements written") |
1181
|
1103 { |
2116
|
1104 octave_value retval = -1.0; |
|
1105 |
|
1106 int nargin = args.length (); |
|
1107 |
|
1108 if (nargin > 1 && nargin < 6) |
|
1109 { |
|
1110 octave_stream *os = octave_stream_list::lookup (args(0)); |
|
1111 |
|
1112 if (os) |
|
1113 { |
|
1114 octave_value data = args(1); |
2318
|
1115 |
|
1116 octave_value prec = (nargin > 2) |
|
1117 ? args(2) : octave_value ("uchar"); |
|
1118 |
|
1119 octave_value skip = (nargin > 3) |
|
1120 ? args(3) : octave_value (0.0); |
|
1121 |
|
1122 octave_value arch = (nargin > 4) |
|
1123 ? args(4) : octave_value ("unknown"); |
2116
|
1124 |
2825
|
1125 double status = do_fwrite (*os, data, prec, skip, arch); |
|
1126 |
|
1127 retval = status; |
2116
|
1128 } |
|
1129 else |
|
1130 gripe_invalid_file_id ("fwrite"); |
|
1131 } |
|
1132 else |
|
1133 print_usage ("fwrite"); |
|
1134 |
1181
|
1135 return retval; |
|
1136 } |
|
1137 |
1957
|
1138 DEFUN (feof, args, , |
2116
|
1139 "ERROR = feof (FILENUM)\n\ |
529
|
1140 \n\ |
|
1141 Returns a non zero value for an end of file condition for the\n\ |
2116
|
1142 file specified by FILENUM from fopen") |
529
|
1143 { |
2095
|
1144 double retval = -1.0; |
529
|
1145 |
|
1146 int nargin = args.length (); |
|
1147 |
2095
|
1148 if (nargin == 1) |
|
1149 { |
|
1150 octave_stream *os = octave_stream_list::lookup (args(0)); |
444
|
1151 |
2095
|
1152 if (os) |
|
1153 retval = os->eof () ? 1.0 : 0.0; |
|
1154 else |
|
1155 gripe_invalid_file_id ("feof"); |
|
1156 } |
529
|
1157 else |
2095
|
1158 print_usage ("feof"); |
444
|
1159 |
|
1160 return retval; |
|
1161 } |
|
1162 |
2095
|
1163 DEFUN (ferror, args, , |
2116
|
1164 "ERROR = ferror (FILENUM, [\"clear\"])\n\ |
2095
|
1165 \n\ |
|
1166 Returns a non zero value for an error condition on the\n\ |
2116
|
1167 file specified by FILENUM from fopen") |
1230
|
1168 { |
2095
|
1169 octave_value_list retval; |
1230
|
1170 |
2095
|
1171 int nargin = args.length (); |
1230
|
1172 |
2095
|
1173 if (nargin == 1 || nargin == 2) |
|
1174 { |
|
1175 octave_stream *os = octave_stream_list::lookup (args(0)); |
1230
|
1176 |
2095
|
1177 if (os) |
|
1178 { |
|
1179 bool clear = false; |
1230
|
1180 |
2095
|
1181 if (nargin == 2) |
|
1182 { |
|
1183 string opt = args(1).string_value (); |
|
1184 |
|
1185 if (! error_state) |
|
1186 clear = (opt == "clear"); |
|
1187 else |
|
1188 return retval; |
|
1189 } |
1755
|
1190 |
2095
|
1191 int error_number = 0; |
|
1192 |
|
1193 string error_message = os->error (clear, error_number); |
1230
|
1194 |
2800
|
1195 retval(1) = static_cast<double> (error_number); |
2095
|
1196 retval(0) = error_message; |
|
1197 } |
|
1198 else |
|
1199 gripe_invalid_file_id ("ferror"); |
1230
|
1200 } |
2095
|
1201 else |
|
1202 print_usage ("ferror"); |
1230
|
1203 |
|
1204 return retval; |
|
1205 } |
|
1206 |
1957
|
1207 DEFUN (popen, args, , |
3301
|
1208 "-*- texinfo -*-\n\ |
|
1209 @deftypefn {Built-in Function} {fid =} popen (@var{command}, @var{mode})\n\ |
|
1210 Start a process and create a pipe. The name of the command to run is\n\ |
|
1211 given by @var{command}. The file identifier corresponding to the input\n\ |
|
1212 or output stream of the process is returned in @var{fid}. The argument\n\ |
|
1213 @var{mode} may be\n\ |
|
1214 \n\ |
|
1215 @table @code\n\ |
|
1216 @item \"r\"\n\ |
|
1217 The pipe will be connected to the standard output of the process, and\n\ |
|
1218 open for reading.\n\ |
1230
|
1219 \n\ |
3301
|
1220 @item \"w\"\n\ |
|
1221 The pipe will be connected to the standard input of the process, and\n\ |
|
1222 open for writing.\n\ |
|
1223 @end table\n\ |
|
1224 \n\ |
|
1225 For example,\n\ |
1230
|
1226 \n\ |
3301
|
1227 @example\n\ |
|
1228 @group\n\ |
|
1229 fid = popen (\"ls -ltr / | tail -3\", \"r\");\n\ |
|
1230 while (isstr (s = fgets (fid)))\n\ |
|
1231 fputs (stdout, s);\n\ |
|
1232 endwhile\n\ |
|
1233 @print{} drwxr-xr-x 33 root root 3072 Feb 15 13:28 etc\n\ |
|
1234 @print{} drwxr-xr-x 3 root root 1024 Feb 15 13:28 lib\n\ |
|
1235 @print{} drwxrwxrwt 15 root root 2048 Feb 17 14:53 tmp\n\ |
|
1236 @end group\n\ |
|
1237 @end example\n\ |
|
1238 @end deftypefn") |
1230
|
1239 { |
2902
|
1240 octave_value retval = -1.0; |
1230
|
1241 |
|
1242 int nargin = args.length (); |
|
1243 |
2095
|
1244 if (nargin == 2) |
|
1245 { |
|
1246 string name = args(0).string_value (); |
1230
|
1247 |
2095
|
1248 if (! error_state) |
|
1249 { |
|
1250 string mode = args(1).string_value (); |
1230
|
1251 |
2095
|
1252 if (! error_state) |
|
1253 { |
|
1254 if (mode == "r") |
|
1255 { |
|
1256 octave_iprocstream *ips = new octave_iprocstream (name); |
1230
|
1257 |
2095
|
1258 retval = octave_stream_list::insert (ips); |
|
1259 } |
|
1260 else if (mode == "w") |
|
1261 { |
|
1262 octave_oprocstream *ops = new octave_oprocstream (name); |
1230
|
1263 |
2095
|
1264 retval = octave_stream_list::insert (ops); |
|
1265 } |
|
1266 else |
|
1267 ::error ("popen: invalid mode specified"); |
|
1268 } |
|
1269 else |
|
1270 ::error ("popen: mode must be a string"); |
|
1271 } |
|
1272 else |
|
1273 ::error ("popen: name must be a string"); |
|
1274 } |
1230
|
1275 else |
2095
|
1276 print_usage ("popen"); |
1230
|
1277 |
|
1278 return retval; |
|
1279 } |
|
1280 |
1957
|
1281 DEFUN (pclose, args, , |
3301
|
1282 "-*- texifno -*-\n\ |
|
1283 @deftypefn {Built-in Function} {} pclose (@var{fid})\n\ |
|
1284 Close a file identifier that was opened by @code{popen}. You may also\n\ |
|
1285 use @code{fclose} for the same purpose.\n\ |
|
1286 @end deftypefn") |
1230
|
1287 { |
2095
|
1288 double retval = -1.0; |
1230
|
1289 |
|
1290 int nargin = args.length (); |
|
1291 |
2095
|
1292 if (nargin == 1) |
1230
|
1293 { |
2800
|
1294 retval = static_cast<double> (octave_stream_list::remove (args(0))); |
1230
|
1295 |
2095
|
1296 if (retval < 0) |
|
1297 gripe_invalid_file_id ("pclose"); |
1377
|
1298 } |
|
1299 else |
2095
|
1300 print_usage ("pclose"); |
1379
|
1301 |
|
1302 return retval; |
|
1303 } |
|
1304 |
2458
|
1305 DEFUN (tmpnam, args, , |
2936
|
1306 "tmpnam (DIR, PREFIX)\n\ |
2237
|
1307 Return unique temporary file name.") |
1802
|
1308 { |
2095
|
1309 octave_value retval; |
1802
|
1310 |
2936
|
1311 int len = args.length (); |
|
1312 |
|
1313 if (len < 3) |
|
1314 { |
|
1315 string dir = len > 0 ? args(0).string_value () : string (); |
|
1316 string pfx = len > 1 ? args(1).string_value () : string ("oct-"); |
|
1317 |
|
1318 if (! error_state) |
|
1319 retval = file_ops::tempnam (dir, pfx); |
|
1320 } |
1802
|
1321 else |
2458
|
1322 print_usage ("tmpnam"); |
1802
|
1323 |
|
1324 return retval; |
|
1325 } |
|
1326 |
2458
|
1327 DEFALIAS (octave_tmp_file_name, tmpnam); |
|
1328 |
1400
|
1329 static int |
|
1330 convert (int x, int ibase, int obase) |
|
1331 { |
|
1332 int retval = 0; |
|
1333 |
|
1334 int tmp = x % obase; |
|
1335 |
|
1336 if (tmp > ibase - 1) |
2095
|
1337 ::error ("umask: invalid digit"); |
1400
|
1338 else |
|
1339 { |
|
1340 retval = tmp; |
|
1341 int mult = ibase; |
|
1342 while ((x = (x - tmp) / obase)) |
|
1343 { |
|
1344 tmp = x % obase; |
|
1345 if (tmp > ibase - 1) |
|
1346 { |
2095
|
1347 ::error ("umask: invalid digit"); |
1400
|
1348 break; |
|
1349 } |
|
1350 retval += mult * tmp; |
|
1351 mult *= ibase; |
|
1352 } |
|
1353 } |
|
1354 |
|
1355 return retval; |
|
1356 } |
|
1357 |
1957
|
1358 DEFUN (umask, args, , |
3301
|
1359 "-*- texinfo -*-\n\ |
|
1360 @deftypefn {Built-in Function} {} umask (@var{mask})\n\ |
|
1361 Set the permission mask for file creation. The parameter @var{mask}\n\ |
|
1362 is an integer, interpreted as an octal number. If successful,\n\ |
|
1363 returns the previous value of the mask (as an integer to be\n\ |
|
1364 interpreted as an octal number); otherwise an error message is printed.\n\ |
|
1365 @end deftypefn") |
1400
|
1366 { |
2095
|
1367 octave_value_list retval; |
1400
|
1368 |
|
1369 int status = 0; |
|
1370 |
|
1371 if (args.length () == 1) |
|
1372 { |
3202
|
1373 int mask = args(0).int_value (true); |
1400
|
1374 |
3202
|
1375 if (! error_state) |
1400
|
1376 { |
3202
|
1377 if (mask < 0) |
1400
|
1378 { |
|
1379 status = -1; |
2095
|
1380 ::error ("umask: MASK must be a positive integer value"); |
1400
|
1381 } |
|
1382 else |
|
1383 { |
|
1384 int oct_mask = convert (mask, 8, 10); |
|
1385 |
|
1386 if (! error_state) |
2926
|
1387 status = convert (file_ops::umask (oct_mask), 10, 8); |
1400
|
1388 } |
|
1389 } |
3202
|
1390 else |
|
1391 { |
|
1392 status = -1; |
|
1393 ::error ("umask: expecting integer argument"); |
|
1394 } |
1400
|
1395 } |
|
1396 else |
|
1397 print_usage ("umask"); |
|
1398 |
|
1399 if (status >= 0) |
2800
|
1400 retval(0) = static_cast<double> (status); |
1400
|
1401 |
|
1402 return retval; |
|
1403 } |
|
1404 |
2189
|
1405 void |
|
1406 symbols_of_file_io (void) |
|
1407 { |
2341
|
1408 // NOTE: the values of SEEK_SET, SEEK_CUR, and SEEK_END have to be |
|
1409 // this way for Matlab compatibility. |
|
1410 |
3141
|
1411 DEFCONST (SEEK_SET, -1.0, |
2189
|
1412 "used with fseek to position file relative to the beginning"); |
|
1413 |
3141
|
1414 DEFCONST (SEEK_CUR, 0.0, |
2189
|
1415 "used with fseek to position file relative to the current position"); |
|
1416 |
3141
|
1417 DEFCONST (SEEK_END, 1.0, |
2189
|
1418 "used with fseek to position file relative to the end"); |
|
1419 |
3141
|
1420 DEFCONSTX ("stdin", SBV_stdin, stdin_file, |
2189
|
1421 "file number of the standard input stream"); |
|
1422 |
3141
|
1423 DEFCONSTX ("stdout", SBV_stdout, stdout_file, |
2189
|
1424 "file number of the standard output stream"); |
|
1425 |
3141
|
1426 DEFCONSTX ("stderr", SBV_stderr, stderr_file, |
2189
|
1427 "file number of the standard error stream"); |
|
1428 } |
|
1429 |
444
|
1430 /* |
1
|
1431 ;;; Local Variables: *** |
|
1432 ;;; mode: C++ *** |
|
1433 ;;; End: *** |
|
1434 */ |