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 |
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 |
2875
|
528 if (nargin > 1 || (nargin > 0 && args(0).is_string ())) |
2095
|
529 { |
2873
|
530 octave_stream *os = 0; |
|
531 int fmt_n = 0; |
|
532 |
|
533 if (args(0).is_string ()) |
|
534 os = octave_stream_list::lookup (1); |
|
535 else |
|
536 { |
|
537 fmt_n = 1; |
|
538 os = octave_stream_list::lookup (args(0)); |
|
539 } |
2095
|
540 |
|
541 if (os) |
|
542 { |
2873
|
543 if (args(fmt_n).is_string ()) |
2095
|
544 { |
2873
|
545 string fmt = args(fmt_n).string_value (); |
2095
|
546 |
|
547 octave_value_list tmp_args; |
|
548 |
2873
|
549 if (nargin > 1 + fmt_n) |
2095
|
550 { |
2873
|
551 tmp_args.resize (nargin-fmt_n-1, octave_value ()); |
2095
|
552 |
2873
|
553 for (int i = fmt_n + 1; i < nargin; i++) |
|
554 tmp_args(i-fmt_n-1) = args(i); |
2095
|
555 } |
|
556 |
|
557 retval = os->printf (fmt, tmp_args); |
|
558 } |
|
559 else |
|
560 ::error ("fprintf: format must be a string"); |
|
561 } |
|
562 else |
|
563 gripe_invalid_file_id ("fprintf"); |
|
564 } |
|
565 else |
1181
|
566 print_usage ("fprintf"); |
|
567 |
|
568 return retval; |
|
569 } |
|
570 |
2095
|
571 DEFUN (fputs, args, , |
2116
|
572 "fputs (FILENUM, STRING)") |
1181
|
573 { |
2095
|
574 double retval = -1.0; |
1181
|
575 |
|
576 int nargin = args.length (); |
|
577 |
2095
|
578 if (nargin == 2) |
|
579 { |
|
580 octave_stream *os = octave_stream_list::lookup (args(0)); |
1181
|
581 |
2095
|
582 if (os) |
|
583 retval = os->puts (args(1)); |
|
584 else |
|
585 gripe_invalid_file_id ("fputs"); |
|
586 } |
1181
|
587 else |
2095
|
588 print_usage ("fputs"); |
1181
|
589 |
|
590 return retval; |
|
591 } |
|
592 |
2095
|
593 DEFUN (sprintf, args, , |
|
594 "[s, errmsg, status] = sprintf (FORMAT, ...)") |
1
|
595 { |
2095
|
596 octave_value_list retval; |
1
|
597 |
2095
|
598 int nargin = args.length (); |
1
|
599 |
2095
|
600 if (nargin > 0) |
1
|
601 { |
2116
|
602 retval(2) = -1.0; |
|
603 retval(1) = "unknown error"; |
|
604 retval(0) = ""; |
|
605 |
2095
|
606 octave_ostrstream ostr; |
1
|
607 |
2148
|
608 octave_stream os (&ostr, true); |
628
|
609 |
2095
|
610 if (os) |
|
611 { |
|
612 if (args(0).is_string ()) |
|
613 { |
|
614 string fmt = args(0).string_value (); |
628
|
615 |
2095
|
616 octave_value_list tmp_args; |
1
|
617 |
2095
|
618 if (nargin > 1) |
|
619 { |
|
620 tmp_args.resize (nargin-1, octave_value ()); |
1
|
621 |
2095
|
622 for (int i = 1; i < nargin; i++) |
|
623 tmp_args(i-1) = args(i); |
|
624 } |
628
|
625 |
2825
|
626 retval(2) = static_cast<double> (os.printf (fmt, tmp_args)); |
2095
|
627 retval(1) = os.error (); |
|
628 char *tmp = ostr.str (); |
|
629 retval(0) = tmp; |
|
630 delete [] tmp; |
|
631 } |
|
632 else |
|
633 ::error ("sprintf: format must be a string"); |
|
634 } |
|
635 else |
|
636 ::error ("sprintf: unable to create output buffer"); |
1
|
637 } |
|
638 else |
2095
|
639 print_usage ("sprintf"); |
1
|
640 |
|
641 return retval; |
|
642 } |
|
643 |
2095
|
644 DEFUN (fscanf, args, , |
2122
|
645 "[A, COUNT] = fscanf (FILENUM, FORMAT [, SIZE])\n\ |
|
646 \n\ |
|
647 Read from FILENUM according to FORMAT, returning the result in the\n\ |
|
648 matrix A. SIZE is optional. If present, it can be one of\n\ |
|
649 \n\ |
|
650 Inf : read as much as possible, returning a column vector\n\ |
|
651 (unless doing all character conversions, in which case a\n\ |
|
652 string is returned)\n\ |
|
653 NR : read as much as possible, returning a matrix with NR rows\n\ |
|
654 [NR, NC] : read up to NR x NC elements, returning a matrix with NR rows\n\ |
|
655 [NR, Inf] : same as NR\n\ |
|
656 \n\ |
|
657 If it is omitted, a value of Inf is assumed.\n\ |
|
658 \n\ |
2215
|
659 The number of items successfully read is returned in COUNT.\n\ |
|
660 \n\ |
|
661 [A, B, ...] = fscanf (FILENUM, FORMAT, \"C\")\n\ |
|
662 \n\ |
|
663 Read from FILENUM according to FORMAT, with each conversion specifier\n\ |
|
664 in FORMAT corresponding to a single scalar return value. This form is\n\ |
|
665 more `C-like', and also compatible with previous versions of Octave") |
1181
|
666 { |
2095
|
667 octave_value_list retval; |
1181
|
668 |
|
669 int nargin = args.length (); |
|
670 |
2215
|
671 if (nargin == 3 && args(2).is_string ()) |
2095
|
672 { |
|
673 octave_stream *os = octave_stream_list::lookup (args(0)); |
1181
|
674 |
2095
|
675 if (os) |
|
676 { |
|
677 if (args(1).is_string ()) |
|
678 { |
|
679 string fmt = args(1).string_value (); |
1181
|
680 |
2215
|
681 retval = os->oscanf (fmt); |
2095
|
682 } |
|
683 else |
|
684 ::error ("fscanf: format must be a string"); |
|
685 } |
|
686 else |
|
687 gripe_invalid_file_id ("fscanf"); |
|
688 } |
1181
|
689 else |
2215
|
690 { |
|
691 retval (1) = 0.0; |
|
692 retval (0) = Matrix (); |
|
693 |
|
694 if (nargin == 2 || nargin == 3) |
|
695 { |
|
696 octave_stream *os = octave_stream_list::lookup (args(0)); |
|
697 |
|
698 if (os) |
|
699 { |
|
700 if (args(1).is_string ()) |
|
701 { |
|
702 string fmt = args(1).string_value (); |
|
703 |
|
704 int count = 0; |
|
705 |
|
706 Matrix size = (nargin == 3) |
|
707 ? args(2).matrix_value () : Matrix (1, 1, octave_Inf); |
|
708 |
|
709 if (! error_state) |
|
710 { |
|
711 octave_value tmp = os->scanf (fmt, size, count); |
|
712 |
2800
|
713 retval(1) = static_cast<double> (count); |
2215
|
714 retval(0) = tmp; |
|
715 } |
|
716 } |
|
717 else |
|
718 ::error ("fscanf: format must be a string"); |
|
719 } |
|
720 else |
|
721 gripe_invalid_file_id ("fscanf"); |
|
722 } |
|
723 else |
|
724 print_usage ("fscanf"); |
|
725 } |
1181
|
726 |
|
727 return retval; |
|
728 } |
|
729 |
2095
|
730 DEFUN (sscanf, args, , |
2122
|
731 "[A, COUNT, ERRMSG, INDEX] = sscanf (STRING, FORMAT, SIZE)\n\ |
|
732 \n\ |
2215
|
733 Read from STRING according to FORMAT, returning the result in the\n\ |
2122
|
734 matrix A. SIZE is optional. If present, it can be one of\n\ |
|
735 \n\ |
|
736 Inf : read as much as possible, returning a column vector\n\ |
|
737 (unless doing all character conversions, in which case a\n\ |
|
738 string is returned)\n\ |
|
739 NR : read as much as possible, returning a matrix with NR rows\n\ |
|
740 [NR, NC] : read up to NR x NC elements, returning a matrix with NR rows\n\ |
|
741 [NR, Inf] : same as NR\n\ |
|
742 \n\ |
|
743 If it is omitted, a value of Inf is assumed.\n\ |
|
744 \n\ |
|
745 The number of items successfully read is returned in COUNT. If an\n\ |
|
746 error occurs, ERRMSG contains the text of the corresponding error\n\ |
|
747 message. INDEX contains the index of the next character to be read\n\ |
2215
|
748 from STRING\n\ |
|
749 \n\ |
|
750 [A, B, ...] = sscanf (STRING, FORMAT, \"C\")\n\ |
|
751 \n\ |
|
752 Read from STRING according to FORMAT, with each conversion specifier\n\ |
|
753 in FORMAT corresponding to a single scalar return value. This form is\n\ |
|
754 more `C-like', and also compatible with previous versions of Octave") |
444
|
755 { |
2095
|
756 octave_value_list retval; |
444
|
757 |
506
|
758 int nargin = args.length (); |
|
759 |
2215
|
760 if (nargin == 3 && args(2).is_string ()) |
2095
|
761 { |
|
762 if (args(0).is_string ()) |
|
763 { |
|
764 string data = args(0).string_value (); |
444
|
765 |
2095
|
766 octave_istrstream istr (data); |
1358
|
767 |
2148
|
768 octave_stream os (&istr, true); |
636
|
769 |
2095
|
770 if (os) |
|
771 { |
|
772 if (args(1).is_string ()) |
|
773 { |
|
774 string fmt = args(1).string_value (); |
444
|
775 |
2215
|
776 retval = os.oscanf (fmt); |
2095
|
777 } |
|
778 else |
|
779 ::error ("sscanf: format must be a string"); |
|
780 } |
|
781 else |
|
782 ::error ("sscanf: unable to create temporary input buffer"); |
444
|
783 } |
636
|
784 else |
2095
|
785 ::error ("sscanf: first argument must be a string"); |
444
|
786 } |
|
787 else |
2215
|
788 { |
|
789 if (nargin == 2 || nargin == 3) |
|
790 { |
|
791 retval(3) = -1.0; |
|
792 retval(2) = "unknown error"; |
|
793 retval(1) = 0.0; |
|
794 retval(0) = Matrix (); |
|
795 |
|
796 if (args(0).is_string ()) |
|
797 { |
|
798 string data = args(0).string_value (); |
|
799 |
|
800 octave_istrstream istr (data); |
|
801 |
|
802 octave_stream os (&istr, true); |
|
803 |
|
804 if (os) |
|
805 { |
|
806 if (args(1).is_string ()) |
|
807 { |
|
808 string fmt = args(1).string_value (); |
|
809 |
|
810 int count = 0; |
|
811 |
|
812 Matrix size = (nargin == 3) |
|
813 ? args(2).matrix_value () : Matrix (1, 1, octave_Inf); |
|
814 |
|
815 octave_value tmp = os.scanf (fmt, size, count); |
|
816 |
|
817 // XXX FIXME XXX -- is this the right thing to do? |
|
818 // Extract error message first, because getting |
|
819 // position will clear it. |
|
820 string errmsg = os.error (); |
|
821 |
2800
|
822 retval(3) = static_cast<double> (os.tell () + 1); |
2215
|
823 retval(2) = errmsg; |
2800
|
824 retval(1) = static_cast<double> (count); |
2215
|
825 retval(0) = tmp; |
|
826 } |
|
827 else |
|
828 ::error ("sscanf: format must be a string"); |
|
829 } |
|
830 else |
|
831 ::error ("sscanf: unable to create temporary input buffer"); |
|
832 } |
|
833 else |
|
834 ::error ("sscanf: first argument must be a string"); |
|
835 } |
|
836 else |
|
837 print_usage ("sscanf"); |
|
838 } |
444
|
839 |
|
840 return retval; |
|
841 } |
|
842 |
2215
|
843 DEFUN (scanf, args, nargout, |
|
844 "scanf (FORMAT) is equivalent to fscanf (stdin, FORMAT)") |
|
845 { |
|
846 int nargin = args.length (); |
|
847 |
|
848 octave_value_list tmp_args (nargin+1, octave_value ()); |
|
849 |
|
850 tmp_args (0) = 0.0; |
|
851 for (int i = 0; i < nargin; i++) |
|
852 tmp_args (i+1) = args (i); |
|
853 |
|
854 return Ffscanf (tmp_args, nargout); |
|
855 } |
|
856 |
2116
|
857 static octave_value |
|
858 do_fread (octave_stream& os, const octave_value& size_arg, |
|
859 const octave_value& prec_arg, const octave_value& skip_arg, |
|
860 const octave_value& arch_arg, int& count) |
|
861 { |
|
862 octave_value retval; |
|
863 |
|
864 count = -1; |
|
865 |
|
866 Matrix size = size_arg.matrix_value (); |
|
867 |
|
868 if (! error_state) |
|
869 { |
|
870 string prec = prec_arg.string_value (); |
|
871 |
|
872 if (! error_state) |
|
873 { |
2318
|
874 oct_data_conv::data_type dt |
|
875 = oct_data_conv::string_to_data_type (prec); |
2116
|
876 |
|
877 if (! error_state) |
|
878 { |
|
879 double dskip = skip_arg.double_value (); |
|
880 |
|
881 if (! error_state) |
|
882 { |
|
883 if (D_NINT (dskip) == dskip) |
|
884 { |
|
885 int skip = NINT (dskip); |
|
886 |
|
887 string arch = arch_arg.string_value (); |
|
888 |
|
889 if (! error_state) |
|
890 { |
2318
|
891 oct_mach_info::float_format flt_fmt |
|
892 = oct_mach_info::string_to_float_format (arch); |
2116
|
893 |
|
894 if (! error_state) |
2318
|
895 retval = os.read (size, dt, skip, flt_fmt, count); |
2116
|
896 } |
|
897 else |
|
898 ::error ("fread: architecture type must be a string"); |
|
899 } |
|
900 else |
|
901 ::error ("fread: skip must be an integer"); |
|
902 } |
|
903 else |
|
904 ::error ("fread: invalid skip specified"); |
|
905 } |
|
906 else |
|
907 ::error ("fread: invalid data type specified"); |
|
908 } |
|
909 else |
|
910 ::error ("fread: precision must be a string"); |
|
911 } |
|
912 else |
|
913 ::error ("fread: invalid size specified"); |
|
914 |
|
915 return retval; |
|
916 } |
|
917 |
|
918 DEFUN (fread, args, , |
|
919 "[DATA, COUNT] = fread (FILENUM [, SIZE] [, PRECISION] [, SKIP] [, ARCH])\n\ |
529
|
920 \n\ |
2318
|
921 Reads data in binary form of type PRECISION from a file.\n\ |
|
922 \n\ |
|
923 FILENUM : file number from fopen\n\ |
529
|
924 \n\ |
2318
|
925 SIZE : size specification for the data matrix\n\ |
|
926 \n\ |
|
927 PRECISION : string specifying type of data to read, valid types are\n\ |
529
|
928 \n\ |
2318
|
929 char, char*1, integer*1, int8 -- character\n\ |
|
930 schar, signed char -- signed character\n\ |
|
931 uchar, unsigned char -- unsigned character (default)\n\ |
|
932 short -- short integer\n\ |
|
933 ushort, unsigned short -- unsigned short integer\n\ |
|
934 int -- integer\n\ |
|
935 uint, unsigned int -- unsigned integer\n\ |
|
936 long -- long integer\n\ |
|
937 ulong, unsigned long -- unsigned long integer\n\ |
|
938 float, float32, real*4 -- single precision float\n\ |
|
939 double, float64, real*8 -- double precision float\n\ |
|
940 int16, integer*2 -- two byte integer\n\ |
|
941 int32, integer*4 -- four byte integer\n\ |
|
942 \n\ |
|
943 SKIP : number of bytes to skip before each element is read\n\ |
|
944 (default is 0)\n\ |
529
|
945 \n\ |
2802
|
946 ARCH : string specifying the data format for the file. Valid\n\ |
2318
|
947 values are\n\ |
|
948 \n\ |
|
949 native -- the format of the current machine (default)\n\ |
|
950 ieee-le -- IEEE big endian\n\ |
|
951 ieee-be -- IEEE little endian\n\ |
|
952 vaxd -- VAX D floating format\n\ |
|
953 vaxg -- VAX G floating format\n\ |
2324
|
954 cray -- Cray floating format\n\ |
2318
|
955 \n\ |
2324
|
956 however, conversions are currently only supported for\n\ |
|
957 ieee-be, and ieee-le formats.\n\ |
2318
|
958 \n\ |
|
959 \n\ |
|
960 DATA : matrix in which the data is stored\n\ |
|
961 \n\ |
|
962 COUNT : number of elements read") |
529
|
963 { |
2095
|
964 octave_value_list retval; |
2116
|
965 |
|
966 int nargin = args.length (); |
|
967 |
2318
|
968 if (nargin > 0 && nargin < 6) |
2116
|
969 { |
|
970 retval(1) = -1.0; |
|
971 retval(0) = Matrix (); |
|
972 |
|
973 octave_stream *os = octave_stream_list::lookup (args(0)); |
|
974 |
|
975 if (os) |
|
976 { |
|
977 octave_value size = (nargin > 1) |
|
978 ? args(1) : octave_value (octave_Inf); |
|
979 |
|
980 octave_value prec = (nargin > 2) |
|
981 ? args(2) : octave_value ("uchar"); |
|
982 |
|
983 octave_value skip = (nargin > 3) |
|
984 ? args(3) : octave_value (0.0); |
|
985 |
|
986 octave_value arch = (nargin > 4) |
2318
|
987 ? args(4) : octave_value ("unknown"); |
2116
|
988 |
|
989 int count = -1; |
|
990 |
|
991 octave_value tmp = do_fread (*os, size, prec, skip, arch, count); |
|
992 |
2800
|
993 retval(1) = static_cast<double> (count); |
2116
|
994 retval(0) = tmp; |
|
995 } |
|
996 else |
|
997 gripe_invalid_file_id ("fread"); |
|
998 } |
|
999 else |
|
1000 print_usage ("fread"); |
|
1001 |
529
|
1002 return retval; |
|
1003 } |
|
1004 |
2116
|
1005 static int |
|
1006 do_fwrite (octave_stream& os, const octave_value& data, |
|
1007 const octave_value& prec_arg, const octave_value& skip_arg, |
|
1008 const octave_value& arch_arg) |
|
1009 { |
|
1010 int retval = -1; |
|
1011 |
|
1012 string prec = prec_arg.string_value (); |
|
1013 |
|
1014 if (! error_state) |
|
1015 { |
2318
|
1016 oct_data_conv::data_type dt |
|
1017 = oct_data_conv::string_to_data_type (prec); |
2116
|
1018 |
|
1019 if (! error_state) |
|
1020 { |
|
1021 double dskip = skip_arg.double_value (); |
|
1022 |
|
1023 if (! error_state) |
|
1024 { |
|
1025 if (D_NINT (dskip) == dskip) |
|
1026 { |
|
1027 int skip = NINT (dskip); |
|
1028 |
|
1029 string arch = arch_arg.string_value (); |
|
1030 |
|
1031 if (! error_state) |
|
1032 { |
2318
|
1033 oct_mach_info::float_format flt_fmt |
|
1034 = oct_mach_info::string_to_float_format (arch); |
2116
|
1035 |
|
1036 if (! error_state) |
2318
|
1037 retval = os.write (data, dt, skip, flt_fmt); |
2116
|
1038 } |
|
1039 else |
|
1040 ::error ("fwrite: architecture type must be a string"); |
|
1041 } |
|
1042 else |
|
1043 ::error ("fwrite: skip must be an integer"); |
|
1044 } |
|
1045 else |
|
1046 ::error ("fwrite: invalid skip specified"); |
|
1047 } |
|
1048 } |
|
1049 else |
|
1050 ::error ("fwrite: precision must be a string"); |
|
1051 |
|
1052 return retval; |
|
1053 } |
|
1054 |
|
1055 DEFUN (fwrite, args, , |
|
1056 "COUNT = fwrite (FILENUM, DATA [, PRECISION] [, SKIP] [, ARCH])\n\ |
1181
|
1057 \n\ |
2318
|
1058 Writes data to a file in binary form of size PRECISION\n\ |
|
1059 \n\ |
|
1060 FILENUM : file number from fopen\n\ |
|
1061 \n\ |
|
1062 DATA : matrix of elements to be written\n\ |
|
1063 \n\ |
|
1064 PRECISION : string specifying type of data to read, valid types are\n\ |
1181
|
1065 \n\ |
2318
|
1066 char, char*1, integer*1, int8 -- character\n\ |
|
1067 schar, signed char -- signed character\n\ |
|
1068 uchar, unsigned char -- unsigned character (default)\n\ |
|
1069 short -- short integer\n\ |
|
1070 ushort, unsigned short -- unsigned short integer\n\ |
|
1071 int -- integer\n\ |
|
1072 uint, unsigned int -- unsigned integer\n\ |
|
1073 long -- long integer\n\ |
|
1074 ulong, unsigned long -- unsigned long integer\n\ |
|
1075 float, float32, real*4 -- single precision float\n\ |
|
1076 double, float64, real*8 -- double precision float\n\ |
|
1077 int16, integer*2 -- two byte integer\n\ |
|
1078 int32, integer*4 -- four byte integer\n\ |
1181
|
1079 \n\ |
2318
|
1080 SKIP : number of bytes to skip before each element is read\n\ |
|
1081 (the default is 0)\n\ |
|
1082 \n\ |
2802
|
1083 ARCH : string specifying the data format for the file. Valid\n\ |
2318
|
1084 values are\n\ |
1181
|
1085 \n\ |
2318
|
1086 native -- the format of the current machine (default)\n\ |
|
1087 ieee-le -- IEEE big endian\n\ |
|
1088 ieee-be -- IEEE little endian\n\ |
|
1089 vaxd -- VAX D floating format\n\ |
|
1090 vaxg -- VAX G floating format\n\ |
2802
|
1091 cray -- Cray floating format\n\ |
2318
|
1092 \n\ |
|
1093 however, conversions are currently only supported for ieee-be, and\n\ |
|
1094 ieee-le formats.\n\ |
|
1095 \n\ |
|
1096 COUNT : number of elements written") |
1181
|
1097 { |
2116
|
1098 octave_value retval = -1.0; |
|
1099 |
|
1100 int nargin = args.length (); |
|
1101 |
|
1102 if (nargin > 1 && nargin < 6) |
|
1103 { |
|
1104 octave_stream *os = octave_stream_list::lookup (args(0)); |
|
1105 |
|
1106 if (os) |
|
1107 { |
|
1108 octave_value data = args(1); |
2318
|
1109 |
|
1110 octave_value prec = (nargin > 2) |
|
1111 ? args(2) : octave_value ("uchar"); |
|
1112 |
|
1113 octave_value skip = (nargin > 3) |
|
1114 ? args(3) : octave_value (0.0); |
|
1115 |
|
1116 octave_value arch = (nargin > 4) |
|
1117 ? args(4) : octave_value ("unknown"); |
2116
|
1118 |
2825
|
1119 double status = do_fwrite (*os, data, prec, skip, arch); |
|
1120 |
|
1121 retval = status; |
2116
|
1122 } |
|
1123 else |
|
1124 gripe_invalid_file_id ("fwrite"); |
|
1125 } |
|
1126 else |
|
1127 print_usage ("fwrite"); |
|
1128 |
1181
|
1129 return retval; |
|
1130 } |
|
1131 |
1957
|
1132 DEFUN (feof, args, , |
2116
|
1133 "ERROR = feof (FILENUM)\n\ |
529
|
1134 \n\ |
|
1135 Returns a non zero value for an end of file condition for the\n\ |
2116
|
1136 file specified by FILENUM from fopen") |
529
|
1137 { |
2095
|
1138 double retval = -1.0; |
529
|
1139 |
|
1140 int nargin = args.length (); |
|
1141 |
2095
|
1142 if (nargin == 1) |
|
1143 { |
|
1144 octave_stream *os = octave_stream_list::lookup (args(0)); |
444
|
1145 |
2095
|
1146 if (os) |
|
1147 retval = os->eof () ? 1.0 : 0.0; |
|
1148 else |
|
1149 gripe_invalid_file_id ("feof"); |
|
1150 } |
529
|
1151 else |
2095
|
1152 print_usage ("feof"); |
444
|
1153 |
|
1154 return retval; |
|
1155 } |
|
1156 |
2095
|
1157 DEFUN (ferror, args, , |
2116
|
1158 "ERROR = ferror (FILENUM, [\"clear\"])\n\ |
2095
|
1159 \n\ |
|
1160 Returns a non zero value for an error condition on the\n\ |
2116
|
1161 file specified by FILENUM from fopen") |
1230
|
1162 { |
2095
|
1163 octave_value_list retval; |
1230
|
1164 |
2095
|
1165 int nargin = args.length (); |
1230
|
1166 |
2095
|
1167 if (nargin == 1 || nargin == 2) |
|
1168 { |
|
1169 octave_stream *os = octave_stream_list::lookup (args(0)); |
1230
|
1170 |
2095
|
1171 if (os) |
|
1172 { |
|
1173 bool clear = false; |
1230
|
1174 |
2095
|
1175 if (nargin == 2) |
|
1176 { |
|
1177 string opt = args(1).string_value (); |
|
1178 |
|
1179 if (! error_state) |
|
1180 clear = (opt == "clear"); |
|
1181 else |
|
1182 return retval; |
|
1183 } |
1755
|
1184 |
2095
|
1185 int error_number = 0; |
|
1186 |
|
1187 string error_message = os->error (clear, error_number); |
1230
|
1188 |
2800
|
1189 retval(1) = static_cast<double> (error_number); |
2095
|
1190 retval(0) = error_message; |
|
1191 } |
|
1192 else |
|
1193 gripe_invalid_file_id ("ferror"); |
1230
|
1194 } |
2095
|
1195 else |
|
1196 print_usage ("ferror"); |
1230
|
1197 |
|
1198 return retval; |
|
1199 } |
|
1200 |
1957
|
1201 DEFUN (popen, args, , |
1230
|
1202 "FILENUM = popen (FILENAME, MODE)\n\ |
|
1203 \n\ |
|
1204 start a process and create a pipe. Valid values for mode are:\n\ |
|
1205 \n\ |
|
1206 \"r\" : connect stdout of process to pipe\n\ |
|
1207 \"w\" : connect stdin of process to pipe") |
|
1208 { |
2095
|
1209 double retval = -1.0; |
1230
|
1210 |
|
1211 int nargin = args.length (); |
|
1212 |
2095
|
1213 if (nargin == 2) |
|
1214 { |
|
1215 string name = args(0).string_value (); |
1230
|
1216 |
2095
|
1217 if (! error_state) |
|
1218 { |
|
1219 string mode = args(1).string_value (); |
1230
|
1220 |
2095
|
1221 if (! error_state) |
|
1222 { |
|
1223 if (mode == "r") |
|
1224 { |
|
1225 octave_iprocstream *ips = new octave_iprocstream (name); |
1230
|
1226 |
2095
|
1227 retval = octave_stream_list::insert (ips); |
|
1228 } |
|
1229 else if (mode == "w") |
|
1230 { |
|
1231 octave_oprocstream *ops = new octave_oprocstream (name); |
1230
|
1232 |
2095
|
1233 retval = octave_stream_list::insert (ops); |
|
1234 } |
|
1235 else |
|
1236 ::error ("popen: invalid mode specified"); |
|
1237 } |
|
1238 else |
|
1239 ::error ("popen: mode must be a string"); |
|
1240 } |
|
1241 else |
|
1242 ::error ("popen: name must be a string"); |
|
1243 } |
1230
|
1244 else |
2095
|
1245 print_usage ("popen"); |
1230
|
1246 |
|
1247 return retval; |
|
1248 } |
|
1249 |
1957
|
1250 DEFUN (pclose, args, , |
2116
|
1251 "pclose (FILENUM)\n\ |
1230
|
1252 \n\ |
|
1253 Close a pipe and terminate the associated process") |
|
1254 { |
2095
|
1255 double retval = -1.0; |
1230
|
1256 |
|
1257 int nargin = args.length (); |
|
1258 |
2095
|
1259 if (nargin == 1) |
1230
|
1260 { |
2800
|
1261 retval = static_cast<double> (octave_stream_list::remove (args(0))); |
1230
|
1262 |
2095
|
1263 if (retval < 0) |
|
1264 gripe_invalid_file_id ("pclose"); |
1377
|
1265 } |
|
1266 else |
2095
|
1267 print_usage ("pclose"); |
1379
|
1268 |
|
1269 return retval; |
|
1270 } |
|
1271 |
2458
|
1272 DEFUN (tmpnam, args, , |
|
1273 "tmpnam ()\n\ |
2237
|
1274 Return unique temporary file name.") |
1802
|
1275 { |
2095
|
1276 octave_value retval; |
1802
|
1277 |
|
1278 if (args.length () == 0) |
|
1279 retval = oct_tempnam (); |
|
1280 else |
2458
|
1281 print_usage ("tmpnam"); |
1802
|
1282 |
|
1283 return retval; |
|
1284 } |
|
1285 |
2458
|
1286 DEFALIAS (octave_tmp_file_name, tmpnam); |
|
1287 |
1400
|
1288 static int |
|
1289 convert (int x, int ibase, int obase) |
|
1290 { |
|
1291 int retval = 0; |
|
1292 |
|
1293 int tmp = x % obase; |
|
1294 |
|
1295 if (tmp > ibase - 1) |
2095
|
1296 ::error ("umask: invalid digit"); |
1400
|
1297 else |
|
1298 { |
|
1299 retval = tmp; |
|
1300 int mult = ibase; |
|
1301 while ((x = (x - tmp) / obase)) |
|
1302 { |
|
1303 tmp = x % obase; |
|
1304 if (tmp > ibase - 1) |
|
1305 { |
2095
|
1306 ::error ("umask: invalid digit"); |
1400
|
1307 break; |
|
1308 } |
|
1309 retval += mult * tmp; |
|
1310 mult *= ibase; |
|
1311 } |
|
1312 } |
|
1313 |
|
1314 return retval; |
|
1315 } |
|
1316 |
1957
|
1317 DEFUN (umask, args, , |
1400
|
1318 "umask (MASK)\n\ |
|
1319 \n\ |
2802
|
1320 Change the file permission mask for file creation for the current\n\ |
|
1321 process. MASK is an integer, interpreted as an octal number. If\n\ |
|
1322 successful, returns the previous value of the mask (as an integer to\n\ |
|
1323 be interpreted as an octal number); otherwise an error message is\n\ |
1400
|
1324 printed.") |
|
1325 { |
2095
|
1326 octave_value_list retval; |
1400
|
1327 |
|
1328 int status = 0; |
|
1329 |
|
1330 if (args.length () == 1) |
|
1331 { |
|
1332 double dmask = args(0).double_value (); |
|
1333 |
|
1334 if (error_state) |
|
1335 { |
|
1336 status = -1; |
2095
|
1337 ::error ("umask: expecting integer argument"); |
1400
|
1338 } |
|
1339 else |
|
1340 { |
|
1341 int mask = NINT (dmask); |
|
1342 |
2800
|
1343 if (mask != dmask || mask < 0) |
1400
|
1344 { |
|
1345 status = -1; |
2095
|
1346 ::error ("umask: MASK must be a positive integer value"); |
1400
|
1347 } |
|
1348 else |
|
1349 { |
|
1350 int oct_mask = convert (mask, 8, 10); |
|
1351 |
|
1352 if (! error_state) |
1773
|
1353 status = convert (oct_umask (oct_mask), 10, 8); |
1400
|
1354 } |
|
1355 } |
|
1356 } |
|
1357 else |
|
1358 print_usage ("umask"); |
|
1359 |
|
1360 if (status >= 0) |
2800
|
1361 retval(0) = static_cast<double> (status); |
1400
|
1362 |
|
1363 return retval; |
|
1364 } |
|
1365 |
2189
|
1366 void |
|
1367 symbols_of_file_io (void) |
|
1368 { |
2341
|
1369 // NOTE: the values of SEEK_SET, SEEK_CUR, and SEEK_END have to be |
|
1370 // this way for Matlab compatibility. |
|
1371 |
|
1372 DEFCONST (SEEK_SET, -1.0, 0, 0, |
2189
|
1373 "used with fseek to position file relative to the beginning"); |
|
1374 |
2341
|
1375 DEFCONST (SEEK_CUR, 0.0, 0, 0, |
2189
|
1376 "used with fseek to position file relative to the current position"); |
|
1377 |
2341
|
1378 DEFCONST (SEEK_END, 1.0, 0, 0, |
2189
|
1379 "used with fseek to position file relative to the end"); |
|
1380 |
|
1381 DEFCONSTX ("stdin", SBV_stdin, 0.0, 0, 0, |
|
1382 "file number of the standard input stream"); |
|
1383 |
|
1384 DEFCONSTX ("stdout", SBV_stdout, 1.0, 0, 0, |
|
1385 "file number of the standard output stream"); |
|
1386 |
|
1387 DEFCONSTX ("stderr", SBV_stderr, 2.0, 0, 0, |
|
1388 "file number of the standard error stream"); |
|
1389 } |
|
1390 |
444
|
1391 /* |
1
|
1392 ;;; Local Variables: *** |
|
1393 ;;; mode: C++ *** |
|
1394 ;;; End: *** |
|
1395 */ |