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