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