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