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