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