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