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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
1
|
21 |
|
22 */ |
|
23 |
2095
|
24 // Originally written by John C. Campbell <jcc@bevo.che.wisc.edu> |
1230
|
25 // |
2095
|
26 // Thomas Baier <baier@ci.tuwien.ac.at> added the original versions of |
|
27 // the following functions: |
1230
|
28 // |
2095
|
29 // popen |
|
30 // pclose |
|
31 // execute (now popen2.m) |
|
32 // sync_system (now merged with system) |
|
33 // async_system (now merged with system) |
1230
|
34 |
5041
|
35 // Completely rewritten by John W. Eaton <jwe@bevo.che.wics.edu>, |
2095
|
36 // April 1996. |
1
|
37 |
240
|
38 #ifdef HAVE_CONFIG_H |
1230
|
39 #include <config.h> |
1
|
40 #endif |
|
41 |
4797
|
42 #include <cerrno> |
2095
|
43 #include <climits> |
4797
|
44 #include <cstdio> |
1343
|
45 |
3503
|
46 #include <iostream> |
4726
|
47 #include <vector> |
1350
|
48 |
|
49 #ifdef HAVE_UNISTD_H |
2442
|
50 #ifdef HAVE_SYS_TYPES_H |
1230
|
51 #include <sys/types.h> |
2442
|
52 #endif |
1
|
53 #include <unistd.h> |
1343
|
54 #endif |
1350
|
55 |
5325
|
56 #ifdef HAVE_ZLIB_H |
|
57 #include <zlib.h> |
|
58 #endif |
|
59 |
5102
|
60 #include "error.h" |
2926
|
61 #include "file-ops.h" |
5102
|
62 #include "lo-ieee.h" |
2926
|
63 |
1352
|
64 #include "defun.h" |
2095
|
65 #include "oct-fstrm.h" |
|
66 #include "oct-iostrm.h" |
1377
|
67 #include "oct-map.h" |
1750
|
68 #include "oct-obj.h" |
2095
|
69 #include "oct-prcstrm.h" |
|
70 #include "oct-stream.h" |
|
71 #include "oct-strstrm.h" |
1
|
72 #include "pager.h" |
4649
|
73 #include "so-array.h" |
444
|
74 #include "sysdep.h" |
1352
|
75 #include "utils.h" |
2370
|
76 #include "variables.h" |
1583
|
77 |
2902
|
78 static octave_value stdin_file; |
|
79 static octave_value stdout_file; |
|
80 static octave_value stderr_file; |
|
81 |
4468
|
82 static octave_stream stdin_stream; |
|
83 static octave_stream stdout_stream; |
|
84 static octave_stream stderr_stream; |
|
85 |
1
|
86 void |
164
|
87 initialize_file_io (void) |
1
|
88 { |
4468
|
89 stdin_stream = octave_istream::create (&std::cin, "stdin"); |
2116
|
90 |
3531
|
91 // This uses octave_stdout (see pager.h), not std::cout so that Octave's |
2116
|
92 // standard output stream will pass through the pager. |
|
93 |
4468
|
94 stdout_stream = octave_ostream::create (&octave_stdout, "stdout"); |
2116
|
95 |
4468
|
96 stderr_stream = octave_ostream::create (&std::cerr, "stderr"); |
1
|
97 |
2902
|
98 stdin_file = octave_stream_list::insert (stdin_stream); |
|
99 stdout_file = octave_stream_list::insert (stdout_stream); |
|
100 stderr_file = octave_stream_list::insert (stderr_stream); |
1
|
101 } |
|
102 |
2095
|
103 void |
|
104 close_files (void) |
1
|
105 { |
2095
|
106 octave_stream_list::clear (); |
|
107 } |
636
|
108 |
5102
|
109 // List of files to delete when we exit or crash. |
|
110 // |
|
111 // XXX FIXME XXX -- this should really be static, but that causes |
|
112 // problems on some systems. |
|
113 std::stack <std::string> tmp_files; |
|
114 |
|
115 void |
|
116 mark_for_deletion (const std::string& file) |
|
117 { |
|
118 tmp_files.push (file); |
|
119 } |
|
120 |
|
121 void |
|
122 cleanup_tmp_files (void) |
|
123 { |
|
124 while (! tmp_files.empty ()) |
|
125 { |
|
126 std::string filename = tmp_files.top (); |
|
127 tmp_files.pop (); |
|
128 unlink (filename.c_str ()); |
|
129 } |
|
130 } |
|
131 |
4036
|
132 static std::ios::openmode |
5325
|
133 fopen_mode_to_ios_mode (const std::string& mode_arg) |
1
|
134 { |
4036
|
135 std::ios::openmode retval = std::ios::in; |
896
|
136 |
5325
|
137 if (! mode_arg.empty ()) |
368
|
138 { |
2095
|
139 // Could probably be faster, but does it really matter? |
1766
|
140 |
5325
|
141 std::string mode = mode_arg; |
|
142 |
|
143 size_t pos = mode.find ('z'); |
|
144 |
|
145 if (pos != NPOS) |
|
146 { |
|
147 #if defined (HAVE_ZLIB) |
|
148 mode.erase (pos, 1); |
|
149 #else |
|
150 error ("this version of Octave does not support gzipped files"); |
|
151 #endif |
|
152 } |
|
153 |
|
154 if (! error_state) |
|
155 { |
|
156 if (mode == "rt") |
|
157 retval = std::ios::in; |
|
158 else if (mode == "wt") |
|
159 retval = std::ios::out | std::ios::trunc; |
|
160 else if (mode == "at") |
|
161 retval = std::ios::out | std::ios::app; |
|
162 else if (mode == "r+t") |
|
163 retval = std::ios::in | std::ios::out; |
|
164 else if (mode == "w+t") |
|
165 retval = std::ios::in | std::ios::out | std::ios::trunc; |
|
166 else if (mode == "a+t") |
|
167 retval = std::ios::in | std::ios::out | std::ios::ate; |
|
168 else if (mode == "rb" || mode == "r") |
|
169 retval = std::ios::in | std::ios::binary; |
|
170 else if (mode == "wb" || mode == "w") |
|
171 retval = std::ios::out | std::ios::trunc | std::ios::binary; |
|
172 else if (mode == "ab" || mode == "a") |
|
173 retval = std::ios::out | std::ios::app | std::ios::binary; |
|
174 else if (mode == "r+b" || mode == "r+") |
|
175 retval = std::ios::in | std::ios::out | std::ios::binary; |
|
176 else if (mode == "w+b" || mode == "w+") |
|
177 retval = (std::ios::in | std::ios::out | std::ios::trunc |
|
178 | std::ios::binary); |
|
179 else if (mode == "a+b" || mode == "a+") |
|
180 retval = (std::ios::in | std::ios::out | std::ios::ate |
|
181 | std::ios::binary); |
|
182 else |
|
183 ::error ("invalid mode specified"); |
|
184 } |
1
|
185 } |
|
186 |
|
187 return retval; |
|
188 } |
|
189 |
4028
|
190 DEFUN (isstream, args, , |
3448
|
191 "-*- texinfo -*-\n\ |
4028
|
192 @deftypefn {Built-in Function} {} isstream (@var{x})\n\ |
3448
|
193 Return true if @var{x} is a stream object. Otherwise, return false.\n\ |
|
194 @end deftypefn") |
3340
|
195 { |
|
196 octave_value retval; |
|
197 |
|
198 if (args.length () == 1) |
|
199 retval = args(0).is_stream (); |
|
200 else |
4028
|
201 print_usage ("isstream"); |
3340
|
202 |
|
203 return retval; |
|
204 } |
|
205 |
1957
|
206 DEFUN (fclose, args, , |
3372
|
207 "-*- texinfo -*-\n\ |
|
208 @deftypefn {Built-in Function} {} fclose (@var{fid})\n\ |
4594
|
209 Closes the specified file. If successful, @code{fclose} returns 0,\n\ |
|
210 otherwise, it returns -1.\n\ |
5095
|
211 @end deftypefn\n\ |
|
212 @seealso{fopen, fseek, ftell}") |
529
|
213 { |
4233
|
214 octave_value retval = -1; |
529
|
215 |
|
216 int nargin = args.length (); |
|
217 |
2095
|
218 if (nargin == 1) |
4233
|
219 retval = octave_stream_list::remove (args(0), "fclose"); |
1
|
220 else |
2095
|
221 print_usage ("fclose"); |
1
|
222 |
|
223 return retval; |
|
224 } |
|
225 |
5144
|
226 DEFUN (fclear, args, , |
|
227 "-*- texinfo -*-\n\ |
|
228 @deftypefn {Built-in Function} {} fclear (@var{fid})\n\ |
|
229 Clear the stream state for the specified file.\n\ |
|
230 @end deftypefn") |
|
231 { |
|
232 octave_value retval; |
|
233 |
|
234 int nargin = args.length (); |
|
235 |
|
236 if (nargin == 1) |
|
237 { |
|
238 int fid = octave_stream_list::get_file_number (args (0)); |
|
239 |
|
240 octave_stream os = octave_stream_list::lookup (fid, "fclear"); |
|
241 |
|
242 if (! error_state) |
|
243 os.clearerr (); |
|
244 } |
|
245 else |
|
246 print_usage ("fclear"); |
|
247 |
|
248 return retval; |
|
249 } |
|
250 |
1957
|
251 DEFUN (fflush, args, , |
3372
|
252 "-*- texinfo -*-\n\ |
|
253 @deftypefn {Built-in Function} {} fflush (@var{fid})\n\ |
|
254 Flush output to @var{fid}. This is useful for ensuring that all\n\ |
|
255 pending output makes it to the screen before some other event occurs.\n\ |
|
256 For example, it is always a good idea to flush the standard output\n\ |
|
257 stream before calling @code{input}.\n\ |
5095
|
258 \n\ |
|
259 @code{fflush} returns 0 on success and an OS dependent error value\n\ |
|
260 (@minus{}1 on unix) on error.\n\ |
|
261 @end deftypefn\n\ |
|
262 @seealso{fopen, fclose}") |
1181
|
263 { |
4233
|
264 octave_value retval = -1; |
1181
|
265 |
|
266 int nargin = args.length (); |
|
267 |
2095
|
268 if (nargin == 1) |
|
269 { |
2609
|
270 // XXX FIXME XXX -- any way to avoid special case for stdout? |
|
271 |
|
272 int fid = octave_stream_list::get_file_number (args (0)); |
|
273 |
|
274 if (fid == 1) |
|
275 { |
|
276 flush_octave_stdout (); |
2095
|
277 |
4233
|
278 retval = 0; |
2609
|
279 } |
2095
|
280 else |
2609
|
281 { |
3341
|
282 octave_stream os = octave_stream_list::lookup (fid, "fflush"); |
2609
|
283 |
3341
|
284 if (! error_state) |
4233
|
285 retval = os.flush (); |
2609
|
286 } |
2095
|
287 } |
|
288 else |
1181
|
289 print_usage ("fflush"); |
|
290 |
|
291 return retval; |
|
292 } |
|
293 |
2095
|
294 DEFUN (fgetl, args, , |
3372
|
295 "-*- texinfo -*-\n\ |
|
296 @deftypefn {Built-in Function} {} fgetl (@var{fid}, @var{len})\n\ |
|
297 Read characters from a file, stopping after a newline, or EOF,\n\ |
|
298 or @var{len} characters have been read. The characters read, excluding\n\ |
|
299 the possible trailing newline, are returned as a string.\n\ |
1339
|
300 \n\ |
3372
|
301 If @var{len} is omitted, @code{fgetl} reads until the next newline\n\ |
|
302 character.\n\ |
|
303 \n\ |
|
304 If there are no more characters to read, @code{fgetl} returns @minus{}1.\n\ |
5095
|
305 @end deftypefn\n\ |
|
306 @seealso{fread, fscanf}") |
1339
|
307 { |
4468
|
308 static std::string who = "fgetl"; |
|
309 |
2599
|
310 octave_value_list retval; |
|
311 |
4233
|
312 retval(1) = 0; |
|
313 retval(0) = -1; |
1339
|
314 |
|
315 int nargin = args.length (); |
|
316 |
|
317 if (nargin == 1 || nargin == 2) |
2095
|
318 { |
4468
|
319 octave_stream os = octave_stream_list::lookup (args(0), who); |
2095
|
320 |
3341
|
321 if (! error_state) |
2095
|
322 { |
|
323 octave_value len_arg = (nargin == 2) |
4233
|
324 ? args(1) : octave_value (INT_MAX); |
2095
|
325 |
|
326 bool err = false; |
|
327 |
4468
|
328 std::string tmp = os.getl (len_arg, err, who); |
2095
|
329 |
|
330 if (! err) |
2599
|
331 { |
4254
|
332 retval(1) = tmp.length (); |
2599
|
333 retval(0) = tmp; |
|
334 } |
2095
|
335 } |
|
336 } |
1339
|
337 else |
4468
|
338 print_usage (who); |
1339
|
339 |
|
340 return retval; |
|
341 } |
|
342 |
2095
|
343 DEFUN (fgets, args, , |
3372
|
344 "-*- texinfo -*-\n\ |
|
345 @deftypefn {Built-in Function} {} fgets (@var{fid}, @var{len})\n\ |
|
346 Read characters from a file, stopping after a newline, or EOF,\n\ |
|
347 or @var{len} characters have been read. The characters read, including\n\ |
|
348 the possible trailing newline, are returned as a string.\n\ |
529
|
349 \n\ |
3372
|
350 If @var{len} is omitted, @code{fgets} reads until the next newline\n\ |
|
351 character.\n\ |
|
352 \n\ |
|
353 If there are no more characters to read, @code{fgets} returns @minus{}1.\n\ |
5095
|
354 @end deftypefn\n\ |
|
355 @seealso{fread, fscanf}") |
529
|
356 { |
4468
|
357 static std::string who = "fgets"; |
|
358 |
2599
|
359 octave_value_list retval; |
|
360 |
|
361 retval(1) = 0.0; |
|
362 retval(0) = -1.0; |
529
|
363 |
|
364 int nargin = args.length (); |
|
365 |
1338
|
366 if (nargin == 1 || nargin == 2) |
2095
|
367 { |
4468
|
368 octave_stream os = octave_stream_list::lookup (args(0), who); |
2095
|
369 |
3341
|
370 if (! error_state) |
2095
|
371 { |
|
372 octave_value len_arg = (nargin == 2) |
4233
|
373 ? args(1) : octave_value (INT_MAX); |
2095
|
374 |
|
375 bool err = false; |
|
376 |
4468
|
377 std::string tmp = os.gets (len_arg, err, who); |
2095
|
378 |
|
379 if (! err) |
2599
|
380 { |
4254
|
381 retval(1) = tmp.length (); |
2599
|
382 retval(0) = tmp; |
|
383 } |
2095
|
384 } |
|
385 } |
1338
|
386 else |
4468
|
387 print_usage (who); |
529
|
388 |
|
389 return retval; |
|
390 } |
|
391 |
3340
|
392 static octave_stream |
3523
|
393 do_stream_open (const std::string& name, const std::string& mode, |
|
394 const std::string& arch, int& fid) |
1
|
395 { |
3340
|
396 octave_stream retval; |
1
|
397 |
2095
|
398 fid = -1; |
1
|
399 |
4036
|
400 std::ios::openmode md = fopen_mode_to_ios_mode (mode); |
1
|
401 |
2095
|
402 if (! error_state) |
|
403 { |
2318
|
404 oct_mach_info::float_format flt_fmt = |
|
405 oct_mach_info::string_to_float_format (arch); |
1
|
406 |
4798
|
407 if (! error_state) |
|
408 { |
5325
|
409 #if defined (HAVE_ZLIB) |
|
410 std::string tmode = mode; |
|
411 |
|
412 size_t pos = tmode.find ('z'); |
4798
|
413 |
5325
|
414 if (pos != NPOS) |
|
415 { |
|
416 tmode.erase (pos, 1); |
|
417 |
|
418 gzFile fptr = ::gzopen (name.c_str (), tmode.c_str ()); |
4797
|
419 |
5325
|
420 if (fptr) |
|
421 retval = octave_zstdiostream::create (name, fptr, md, flt_fmt); |
|
422 else |
|
423 { |
|
424 using namespace std; |
|
425 retval.error (::strerror (errno)); |
|
426 } |
|
427 } |
|
428 else |
|
429 #endif |
4799
|
430 { |
5325
|
431 FILE *fptr = ::fopen (name.c_str (), mode.c_str ()); |
|
432 |
|
433 if (fptr) |
|
434 retval = octave_stdiostream::create (name, fptr, md, flt_fmt); |
|
435 else |
|
436 { |
|
437 using namespace std; |
|
438 retval.error (::strerror (errno)); |
|
439 } |
4798
|
440 } |
5325
|
441 |
4797
|
442 } |
1
|
443 } |
|
444 |
2095
|
445 return retval; |
|
446 } |
1
|
447 |
3340
|
448 static octave_stream |
2095
|
449 do_stream_open (const octave_value& tc_name, const octave_value& tc_mode, |
|
450 const octave_value& tc_arch, const char *fcn, int& fid) |
|
451 { |
3340
|
452 octave_stream retval; |
2095
|
453 |
|
454 fid = -1; |
|
455 |
3523
|
456 std::string name = tc_name.string_value (); |
2095
|
457 |
|
458 if (! error_state) |
1
|
459 { |
3523
|
460 std::string mode = tc_mode.string_value (); |
2095
|
461 |
|
462 if (! error_state) |
|
463 { |
3523
|
464 std::string arch = tc_arch.string_value (); |
1
|
465 |
2095
|
466 if (! error_state) |
|
467 retval = do_stream_open (name, mode, arch, fid); |
|
468 else |
|
469 ::error ("%s: architecture type must be a string", fcn); |
|
470 } |
|
471 else |
|
472 ::error ("%s: file mode must be a string", fcn); |
1
|
473 } |
2095
|
474 else |
|
475 ::error ("%s: file name must be a string", fcn); |
1
|
476 |
|
477 return retval; |
|
478 } |
|
479 |
1957
|
480 DEFUN (fopen, args, , |
3372
|
481 "-*- texinfo -*-\n\ |
|
482 @deftypefn {Built-in Function} {[@var{fid}, @var{msg}] =} fopen (@var{name}, @var{mode}, @var{arch})\n\ |
|
483 @deftypefnx {Built-in Function} {@var{fid_list} =} fopen (\"all\")\n\ |
|
484 @deftypefnx {Built-in Function} {@var{file} =} fopen (@var{fid})\n\ |
|
485 The first form of the @code{fopen} function opens the named file with\n\ |
|
486 the specified mode (read-write, read-only, etc.) and architecture\n\ |
|
487 interpretation (IEEE big endian, IEEE little endian, etc.), and returns\n\ |
|
488 an integer value that may be used to refer to the file later. If an\n\ |
|
489 error occurs, @var{fid} is set to @minus{}1 and @var{msg} contains the\n\ |
|
490 corresponding system error message. The @var{mode} is a one or two\n\ |
|
491 character string that specifies whether the file is to be opened for\n\ |
|
492 reading, writing, or both.\n\ |
1181
|
493 \n\ |
3372
|
494 The second form of the @code{fopen} function returns a vector of file ids\n\ |
|
495 corresponding to all the currently open files, excluding the\n\ |
|
496 @code{stdin}, @code{stdout}, and @code{stderr} streams.\n\ |
2318
|
497 \n\ |
3372
|
498 The third form of the @code{fopen} function returns the name of a\n\ |
|
499 currently open file given its file id.\n\ |
1181
|
500 \n\ |
3372
|
501 For example,\n\ |
|
502 \n\ |
|
503 @example\n\ |
|
504 myfile = fopen (\"splat.dat\", \"r\", \"ieee-le\");\n\ |
|
505 @end example\n\ |
2095
|
506 \n\ |
3372
|
507 @noindent\n\ |
|
508 opens the file @file{splat.dat} for reading. If necessary, binary\n\ |
|
509 numeric values will be read assuming they are stored in IEEE format with\n\ |
|
510 the least significant bit first, and then converted to the native\n\ |
|
511 representation.\n\ |
2318
|
512 \n\ |
3372
|
513 Opening a file that is already open simply opens it again and returns a\n\ |
|
514 separate file id. It is not an error to open a file several times,\n\ |
|
515 though writing to the same file through several different file ids may\n\ |
|
516 produce unexpected results.\n\ |
|
517 \n\ |
|
518 The possible values @samp{mode} may have are\n\ |
|
519 \n\ |
|
520 @table @asis\n\ |
|
521 @item @samp{r}\n\ |
|
522 Open a file for reading.\n\ |
3263
|
523 \n\ |
3372
|
524 @item @samp{w}\n\ |
|
525 Open a file for writing. The previous contents are discared.\n\ |
|
526 \n\ |
|
527 @item @samp{a}\n\ |
|
528 Open or create a file for writing at the end of the file.\n\ |
|
529 \n\ |
|
530 @item @samp{r+}\n\ |
|
531 Open an existing file for reading and writing.\n\ |
|
532 \n\ |
|
533 @item @samp{w+}\n\ |
|
534 Open a file for reading or writing. The previous contents are\n\ |
|
535 discarded.\n\ |
|
536 \n\ |
|
537 @item @samp{a+}\n\ |
|
538 Open or create a file for reading or writing at the end of the\n\ |
|
539 file.\n\ |
|
540 @end table\n\ |
|
541 \n\ |
4865
|
542 Append a \"t\" to the mode string to open the file in text mode or a\n\ |
|
543 \"b\" to open in binary mode. On Windows and Macintosh systems, text\n\ |
|
544 mode reading and writing automatically converts linefeeds to the\n\ |
|
545 appropriate line end character for the system (carriage-return linefeed\n\ |
|
546 on Windows, carriage-returnn on Macintosh). The default if no mode is\n\ |
|
547 specified is binary mode.\n\ |
|
548 \n\ |
5325
|
549 Additionally, you may append a \"z\" to the mode string to open a\n\ |
|
550 gzipped file for reading or writing. For this to be successful, you\n\ |
|
551 must also open the file in binary mode.\n\ |
|
552 \n\ |
3372
|
553 The parameter @var{arch} is a string specifying the default data format\n\ |
|
554 for the file. Valid values for @var{arch} are:\n\ |
2318
|
555 \n\ |
3372
|
556 @table @asis\n\ |
|
557 @samp{native}\n\ |
|
558 The format of the current machine (this is the default).\n\ |
|
559 \n\ |
4082
|
560 @samp{ieee-be}\n\ |
3372
|
561 IEEE big endian format.\n\ |
|
562 \n\ |
4082
|
563 @samp{ieee-le}\n\ |
3372
|
564 IEEE little endian format.\n\ |
2318
|
565 \n\ |
3372
|
566 @samp{vaxd}\n\ |
|
567 VAX D floating format.\n\ |
2318
|
568 \n\ |
3372
|
569 @samp{vaxg}\n\ |
|
570 VAX G floating format.\n\ |
2318
|
571 \n\ |
3372
|
572 @samp{cray}\n\ |
|
573 Cray floating format.\n\ |
|
574 @end table\n\ |
|
575 \n\ |
|
576 @noindent\n\ |
|
577 however, conversions are currently only supported for @samp{native}\n\ |
|
578 @samp{ieee-be}, and @samp{ieee-le} formats.\n\ |
5095
|
579 @end deftypefn\n\ |
|
580 @seealso{fclose, fread, fseek}") |
529
|
581 { |
2599
|
582 octave_value_list retval; |
|
583 |
|
584 retval(0) = -1.0; |
529
|
585 |
|
586 int nargin = args.length (); |
|
587 |
2095
|
588 if (nargin == 1) |
|
589 { |
3263
|
590 if (args(0).is_string ()) |
|
591 { |
|
592 // If there is only one argument and it is a string but it |
|
593 // is not the string "all", we assume it is a file to open |
|
594 // with MODE = "r". To open a file called "all", you have |
|
595 // to supply more than one argument. |
|
596 |
|
597 if (args(0).string_value () == "all") |
|
598 return octave_stream_list::open_file_numbers (); |
|
599 } |
2095
|
600 else |
|
601 { |
|
602 string_vector tmp = octave_stream_list::get_info (args(0)); |
529
|
603 |
2095
|
604 if (! error_state) |
|
605 { |
|
606 retval(2) = tmp(2); |
|
607 retval(1) = tmp(1); |
|
608 retval(0) = tmp(0); |
|
609 } |
3263
|
610 |
|
611 return retval; |
2432
|
612 } |
1
|
613 } |
|
614 |
2095
|
615 if (nargin > 0 && nargin < 4) |
|
616 { |
|
617 octave_value mode = (nargin == 2 || nargin == 3) |
|
618 ? args(1) : octave_value ("r"); |
|
619 |
|
620 octave_value arch = (nargin == 3) |
|
621 ? args(2) : octave_value ("native"); |
|
622 |
|
623 int fid = -1; |
|
624 |
3340
|
625 octave_stream os = do_stream_open (args(0), mode, arch, "fopen", fid); |
2095
|
626 |
3340
|
627 if (os.is_valid ()) |
2095
|
628 { |
3340
|
629 if (os && ! error_state) |
2095
|
630 { |
|
631 retval(1) = ""; |
2902
|
632 retval(0) = octave_stream_list::insert (os); |
2095
|
633 } |
|
634 else |
|
635 { |
3162
|
636 int error_number = 0; |
|
637 |
3340
|
638 retval(1) = os.error (false, error_number); |
2095
|
639 retval(0) = -1.0; |
|
640 } |
|
641 } |
|
642 else |
3340
|
643 error ("fopen: internal error"); |
2095
|
644 } |
|
645 else |
|
646 print_usage ("fopen"); |
1
|
647 |
|
648 return retval; |
|
649 } |
|
650 |
1957
|
651 DEFUN (freport, args, , |
3372
|
652 "-*- texinfo -*-\n\ |
|
653 @deftypefn {Built-in Function} {} freport ()\n\ |
|
654 Print a list of which files have been opened, and whether they are open\n\ |
|
655 for reading, writing, or both. For example,\n\ |
|
656 \n\ |
|
657 @example\n\ |
|
658 @group\n\ |
|
659 freport ()\n\ |
|
660 \n\ |
|
661 @print{} number mode name\n\ |
|
662 @print{} \n\ |
|
663 @print{} 0 r stdin\n\ |
|
664 @print{} 1 w stdout\n\ |
|
665 @print{} 2 w stderr\n\ |
|
666 @print{} 3 r myfile\n\ |
|
667 @end group\n\ |
|
668 @end example\n\ |
|
669 @end deftypefn") |
1181
|
670 { |
2095
|
671 octave_value_list retval; |
1181
|
672 |
|
673 int nargin = args.length (); |
|
674 |
|
675 if (nargin > 0) |
|
676 warning ("freport: ignoring extra arguments"); |
|
677 |
2095
|
678 octave_stdout << octave_stream_list::list_open_files (); |
1181
|
679 |
|
680 return retval; |
|
681 } |
|
682 |
4715
|
683 DEFUN (frewind, args, nargout, |
3372
|
684 "-*- texinfo -*-\n\ |
|
685 @deftypefn {Built-in Function} {} frewind (@var{fid})\n\ |
|
686 Move the file pointer to the beginning of the file @var{fid}, returning\n\ |
4715
|
687 0 for success, and -1 if an error was encountered. It is equivalent to\n\ |
3372
|
688 @code{fseek (@var{fid}, 0, SEEK_SET)}.\n\ |
|
689 @end deftypefn") |
529
|
690 { |
4715
|
691 octave_value retval; |
|
692 |
|
693 int result = -1; |
1
|
694 |
506
|
695 int nargin = args.length (); |
|
696 |
2095
|
697 if (nargin == 1) |
1086
|
698 { |
3341
|
699 octave_stream os = octave_stream_list::lookup (args(0), "frewind"); |
636
|
700 |
3341
|
701 if (! error_state) |
4715
|
702 result = os.rewind (); |
1
|
703 } |
|
704 else |
2095
|
705 print_usage ("frewind"); |
1
|
706 |
4715
|
707 if (nargout > 0) |
|
708 retval = result; |
|
709 |
1
|
710 return retval; |
|
711 } |
|
712 |
1957
|
713 DEFUN (fseek, args, , |
3372
|
714 "-*- texinfo -*-\n\ |
|
715 @deftypefn {Built-in Function} {} fseek (@var{fid}, @var{offset}, @var{origin})\n\ |
5095
|
716 Set the file pointer to any location within the file @var{fid}.\n\ |
|
717 \n\ |
|
718 The pointer is positioned @var{offset} characters from the @var{origin},\n\ |
3372
|
719 which may be one of the predefined variables @code{SEEK_CUR} (current\n\ |
|
720 position), @code{SEEK_SET} (beginning), or @code{SEEK_END} (end of\n\ |
5095
|
721 file) or strings \"cof\", \"bof\" or \"eof\". If @var{origin} is omitted,\n\ |
|
722 @code{SEEK_SET} is assumed. The offset must be zero, or a value returned\n\ |
|
723 by @code{ftell} (in which case @var{origin} must be @code{SEEK_SET}.\n\ |
|
724 \n\ |
|
725 Return 0 on success and -1 on error.\n\ |
|
726 @end deftypefn\n\ |
|
727 @seealso{ftell, fopen, fclose}") |
529
|
728 { |
4233
|
729 octave_value retval = -1; |
529
|
730 |
|
731 int nargin = args.length (); |
|
732 |
2095
|
733 if (nargin == 2 || nargin == 3) |
|
734 { |
3341
|
735 octave_stream os = octave_stream_list::lookup (args(0), "fseek"); |
1181
|
736 |
3341
|
737 if (! error_state) |
2095
|
738 { |
|
739 octave_value origin_arg = (nargin == 3) |
2341
|
740 ? args(2) : octave_value (-1.0); |
1
|
741 |
4233
|
742 retval = os.seek (args(1), origin_arg); |
2095
|
743 } |
368
|
744 } |
2095
|
745 else |
|
746 print_usage ("fseek"); |
1
|
747 |
|
748 return retval; |
|
749 } |
|
750 |
1957
|
751 DEFUN (ftell, args, , |
3372
|
752 "-*- texinfo -*-\n\ |
|
753 @deftypefn {Built-in Function} {} ftell (@var{fid})\n\ |
|
754 Return the position of the file pointer as the number of characters\n\ |
|
755 from the beginning of the file @var{fid}.\n\ |
5095
|
756 @end deftypefn\n\ |
|
757 @seealso{fseek, fopen, fclose}") |
1181
|
758 { |
4797
|
759 octave_value retval = -1; |
1
|
760 |
506
|
761 int nargin = args.length (); |
|
762 |
2095
|
763 if (nargin == 1) |
1
|
764 { |
3341
|
765 octave_stream os = octave_stream_list::lookup (args(0), "ftell"); |
1
|
766 |
3341
|
767 if (! error_state) |
4797
|
768 retval = os.tell (); |
1
|
769 } |
|
770 else |
2095
|
771 print_usage ("ftell"); |
1
|
772 |
|
773 return retval; |
|
774 } |
|
775 |
3737
|
776 DEFUN (fprintf, args, nargout, |
3372
|
777 "-*- texinfo -*-\n\ |
|
778 @deftypefn {Built-in Function} {} fprintf (@var{fid}, @var{template}, @dots{})\n\ |
|
779 This function is just like @code{printf}, except that the output is\n\ |
|
780 written to the stream @var{fid} instead of @code{stdout}.\n\ |
5095
|
781 @end deftypefn\n\ |
|
782 @seealso{printf, sprintf, fread, fscanf, fopen, fclose}") |
1181
|
783 { |
4468
|
784 static std::string who = "fprintf"; |
|
785 |
4715
|
786 octave_value retval; |
|
787 |
|
788 int result = -1; |
4468
|
789 |
1181
|
790 int nargin = args.length (); |
|
791 |
2875
|
792 if (nargin > 1 || (nargin > 0 && args(0).is_string ())) |
2095
|
793 { |
3340
|
794 octave_stream os; |
2873
|
795 int fmt_n = 0; |
|
796 |
3737
|
797 if (args(0).is_string ()) |
|
798 { |
4468
|
799 os = octave_stream_list::lookup (1, who); |
3737
|
800 } |
2873
|
801 else |
|
802 { |
|
803 fmt_n = 1; |
4468
|
804 os = octave_stream_list::lookup (args(0), who); |
2873
|
805 } |
2095
|
806 |
3341
|
807 if (! error_state) |
2095
|
808 { |
2873
|
809 if (args(fmt_n).is_string ()) |
2095
|
810 { |
|
811 octave_value_list tmp_args; |
|
812 |
2873
|
813 if (nargin > 1 + fmt_n) |
2095
|
814 { |
2873
|
815 tmp_args.resize (nargin-fmt_n-1, octave_value ()); |
2095
|
816 |
2873
|
817 for (int i = fmt_n + 1; i < nargin; i++) |
|
818 tmp_args(i-fmt_n-1) = args(i); |
2095
|
819 } |
|
820 |
5279
|
821 result = os.printf (args(fmt_n), tmp_args, who); |
2095
|
822 } |
|
823 else |
4468
|
824 ::error ("%s: format must be a string", who.c_str ()); |
2095
|
825 } |
|
826 } |
|
827 else |
4468
|
828 print_usage (who); |
1181
|
829 |
4715
|
830 if (nargout > 0) |
|
831 retval = result; |
|
832 |
|
833 return retval; |
1181
|
834 } |
|
835 |
4715
|
836 DEFUN (printf, args, nargout, |
4468
|
837 "-*- texinfo -*-\n\ |
5095
|
838 @deftypefn {Built-in Function} {} printf (@var{template}, @dots{})\n\ |
4468
|
839 Print optional arguments under the control of the template string\n\ |
|
840 @var{template} to the stream @code{stdout}.\n\ |
5095
|
841 \n\ |
|
842 Return the number of characters printed.\n\ |
4468
|
843 @end deftypefn\n\ |
5095
|
844 @seealso{fprintf, sprintf, scanf}") |
4468
|
845 { |
|
846 static std::string who = "printf"; |
|
847 |
4715
|
848 octave_value retval; |
|
849 |
|
850 int result = -1; |
4468
|
851 |
|
852 int nargin = args.length (); |
|
853 |
|
854 if (nargin > 0) |
|
855 { |
|
856 if (args(0).is_string ()) |
|
857 { |
|
858 octave_value_list tmp_args; |
|
859 |
|
860 if (nargin > 1) |
|
861 { |
|
862 tmp_args.resize (nargin-1, octave_value ()); |
|
863 |
|
864 for (int i = 1; i < nargin; i++) |
|
865 tmp_args(i-1) = args(i); |
|
866 } |
|
867 |
5279
|
868 result = stdout_stream.printf (args(0), tmp_args, who); |
4468
|
869 } |
|
870 else |
|
871 ::error ("%s: format must be a string", who.c_str ()); |
|
872 } |
|
873 else |
|
874 print_usage (who); |
|
875 |
4715
|
876 if (nargout > 0) |
|
877 retval = result; |
|
878 |
4468
|
879 return retval; |
|
880 } |
|
881 |
2095
|
882 DEFUN (fputs, args, , |
3372
|
883 "-*- texinfo -*-\n\ |
|
884 @deftypefn {Built-in Function} {} fputs (@var{fid}, @var{string})\n\ |
|
885 Write a string to a file with no formatting.\n\ |
5095
|
886 \n\ |
|
887 Return a non-negative number on success and EOF on error.\n\ |
3372
|
888 @end deftypefn") |
1181
|
889 { |
4468
|
890 static std::string who = "fputs"; |
|
891 |
4233
|
892 octave_value retval = -1; |
1181
|
893 |
|
894 int nargin = args.length (); |
|
895 |
2095
|
896 if (nargin == 2) |
|
897 { |
4468
|
898 octave_stream os = octave_stream_list::lookup (args(0), who); |
1181
|
899 |
3341
|
900 if (! error_state) |
4468
|
901 retval = os.puts (args(1), who); |
2095
|
902 } |
1181
|
903 else |
4468
|
904 print_usage (who); |
|
905 |
|
906 return retval; |
|
907 } |
|
908 |
|
909 DEFUN (puts, args, , |
|
910 "-*- texinfo -*-\n\ |
|
911 @deftypefn {Built-in Function} {} puts (@var{string})\n\ |
|
912 Write a string to the standard output with no formatting.\n\ |
5095
|
913 \n\ |
|
914 Return a non-negative number on success and EOF on error.\n\ |
4468
|
915 @end deftypefn") |
|
916 { |
|
917 static std::string who = "puts"; |
|
918 |
|
919 octave_value retval = -1; |
|
920 |
|
921 if (args.length () == 1) |
|
922 retval = stdout_stream.puts (args(0), who); |
|
923 else |
|
924 print_usage (who); |
1181
|
925 |
|
926 return retval; |
|
927 } |
|
928 |
2095
|
929 DEFUN (sprintf, args, , |
3372
|
930 "-*- texinfo -*-\n\ |
|
931 @deftypefn {Built-in Function} {} sprintf (@var{template}, @dots{})\n\ |
|
932 This is like @code{printf}, except that the output is returned as a\n\ |
|
933 string. Unlike the C library function, which requires you to provide a\n\ |
|
934 suitably sized string as an argument, Octave's @code{sprintf} function\n\ |
|
935 returns the string, automatically sized to hold all of the items\n\ |
|
936 converted.\n\ |
5095
|
937 @end deftypefn\n\ |
|
938 @seealso{printf, fprintf, sscanf}") |
1
|
939 { |
4468
|
940 static std::string who = "sprintf"; |
|
941 |
2095
|
942 octave_value_list retval; |
1
|
943 |
2095
|
944 int nargin = args.length (); |
1
|
945 |
2095
|
946 if (nargin > 0) |
1
|
947 { |
2116
|
948 retval(2) = -1.0; |
|
949 retval(1) = "unknown error"; |
|
950 retval(0) = ""; |
|
951 |
3340
|
952 octave_ostrstream *ostr = new octave_ostrstream (); |
1
|
953 |
3340
|
954 octave_stream os (ostr); |
628
|
955 |
3340
|
956 if (os.is_valid ()) |
2095
|
957 { |
|
958 if (args(0).is_string ()) |
|
959 { |
|
960 octave_value_list tmp_args; |
1
|
961 |
2095
|
962 if (nargin > 1) |
|
963 { |
|
964 tmp_args.resize (nargin-1, octave_value ()); |
1
|
965 |
2095
|
966 for (int i = 1; i < nargin; i++) |
|
967 tmp_args(i-1) = args(i); |
|
968 } |
628
|
969 |
5279
|
970 retval(2) = os.printf (args(0), tmp_args, who); |
2095
|
971 retval(1) = os.error (); |
3340
|
972 retval(0) = ostr->str (); |
2095
|
973 } |
|
974 else |
4468
|
975 ::error ("%s: format must be a string", who.c_str ()); |
2095
|
976 } |
|
977 else |
4468
|
978 ::error ("%s: unable to create output buffer", who.c_str ()); |
1
|
979 } |
|
980 else |
4468
|
981 print_usage (who); |
1
|
982 |
|
983 return retval; |
|
984 } |
|
985 |
2095
|
986 DEFUN (fscanf, args, , |
3372
|
987 "-*- texinfo -*-\n\ |
|
988 @deftypefn {Built-in Function} {[@var{val}, @var{count}] =} fscanf (@var{fid}, @var{template}, @var{size})\n\ |
3491
|
989 @deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}, @var{count}] = } fscanf (@var{fid}, @var{template}, \"C\")\n\ |
3372
|
990 In the first form, read from @var{fid} according to @var{template},\n\ |
|
991 returning the result in the matrix @var{val}.\n\ |
2122
|
992 \n\ |
3372
|
993 The optional argument @var{size} specifies the amount of data to read\n\ |
|
994 and may be one of\n\ |
|
995 \n\ |
|
996 @table @code\n\ |
|
997 @item Inf\n\ |
|
998 Read as much as possible, returning a column vector.\n\ |
|
999 \n\ |
|
1000 @item @var{nr}\n\ |
|
1001 Read up to @var{nr} elements, returning a column vector.\n\ |
2122
|
1002 \n\ |
3372
|
1003 @item [@var{nr}, Inf]\n\ |
|
1004 Read as much as possible, returning a matrix with @var{nr} rows. If the\n\ |
|
1005 number of elements read is not an exact multiple of @var{nr}, the last\n\ |
|
1006 column is padded with zeros.\n\ |
|
1007 \n\ |
|
1008 @item [@var{nr}, @var{nc}]\n\ |
|
1009 Read up to @code{@var{nr} * @var{nc}} elements, returning a matrix with\n\ |
|
1010 @var{nr} rows. If the number of elements read is not an exact multiple\n\ |
|
1011 of @var{nr}, the last column is padded with zeros.\n\ |
|
1012 @end table\n\ |
2122
|
1013 \n\ |
3372
|
1014 @noindent\n\ |
|
1015 If @var{size} is omitted, a value of @code{Inf} is assumed.\n\ |
2122
|
1016 \n\ |
3372
|
1017 A string is returned if @var{template} specifies only character\n\ |
|
1018 conversions.\n\ |
2215
|
1019 \n\ |
3372
|
1020 The number of items successfully read is returned in @var{count}.\n\ |
2215
|
1021 \n\ |
3372
|
1022 In the second form, read from @var{fid} according to @var{template},\n\ |
|
1023 with each conversion specifier in @var{template} corresponding to a\n\ |
|
1024 single scalar return value. This form is more `C-like', and also\n\ |
3491
|
1025 compatible with previous versions of Octave. The number of successful\n\ |
|
1026 conversions is returned in @var{count}\n\ |
5095
|
1027 @end deftypefn\n\ |
|
1028 @seealso{scanf, sscanf, fread, fprintf}") |
1181
|
1029 { |
4468
|
1030 static std::string who = "fscanf"; |
|
1031 |
2095
|
1032 octave_value_list retval; |
1181
|
1033 |
|
1034 int nargin = args.length (); |
|
1035 |
2215
|
1036 if (nargin == 3 && args(2).is_string ()) |
2095
|
1037 { |
4468
|
1038 octave_stream os = octave_stream_list::lookup (args(0), who); |
1181
|
1039 |
3341
|
1040 if (! error_state) |
2095
|
1041 { |
|
1042 if (args(1).is_string ()) |
5279
|
1043 retval = os.oscanf (args(1), who); |
2095
|
1044 else |
4468
|
1045 ::error ("%s: format must be a string", who.c_str ()); |
2095
|
1046 } |
|
1047 } |
1181
|
1048 else |
2215
|
1049 { |
|
1050 retval (1) = 0.0; |
|
1051 retval (0) = Matrix (); |
|
1052 |
|
1053 if (nargin == 2 || nargin == 3) |
|
1054 { |
4468
|
1055 octave_stream os = octave_stream_list::lookup (args(0), who); |
2215
|
1056 |
3342
|
1057 if (! error_state) |
2215
|
1058 { |
|
1059 if (args(1).is_string ()) |
|
1060 { |
5275
|
1061 octave_idx_type count = 0; |
2215
|
1062 |
3810
|
1063 Array<double> size = (nargin == 3) |
4102
|
1064 ? args(2).vector_value () |
|
1065 : Array<double> (1, lo_ieee_inf_value ()); |
2215
|
1066 |
|
1067 if (! error_state) |
|
1068 { |
5279
|
1069 octave_value tmp = os.scanf (args(1), size, count, who); |
2215
|
1070 |
4233
|
1071 retval(1) = count; |
2215
|
1072 retval(0) = tmp; |
|
1073 } |
|
1074 } |
|
1075 else |
4468
|
1076 ::error ("%s: format must be a string", who.c_str ()); |
2215
|
1077 } |
|
1078 } |
|
1079 else |
4468
|
1080 print_usage (who); |
2215
|
1081 } |
1181
|
1082 |
|
1083 return retval; |
|
1084 } |
|
1085 |
2095
|
1086 DEFUN (sscanf, args, , |
3372
|
1087 "-*- texinfo -*-\n\ |
|
1088 @deftypefn {Built-in Function} {[@var{val}, @var{count}] =} sscanf (@var{string}, @var{template}, @var{size})\n\ |
3491
|
1089 @deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}, @var{count}] = } sscanf (@var{string}, @var{template}, \"C\")\n\ |
3372
|
1090 This is like @code{fscanf}, except that the characters are taken from the\n\ |
|
1091 string @var{string} instead of from a stream. Reaching the end of the\n\ |
|
1092 string is treated as an end-of-file condition.\n\ |
5095
|
1093 @end deftypefn\n\ |
|
1094 @seealso{fscanf, scanf, sprintf}") |
444
|
1095 { |
4468
|
1096 static std::string who = "sscanf"; |
|
1097 |
2095
|
1098 octave_value_list retval; |
444
|
1099 |
506
|
1100 int nargin = args.length (); |
|
1101 |
2215
|
1102 if (nargin == 3 && args(2).is_string ()) |
2095
|
1103 { |
|
1104 if (args(0).is_string ()) |
|
1105 { |
3523
|
1106 std::string data = args(0).string_value (); |
444
|
1107 |
3340
|
1108 octave_stream os = octave_istrstream::create (data); |
1358
|
1109 |
3340
|
1110 if (os.is_valid ()) |
2095
|
1111 { |
|
1112 if (args(1).is_string ()) |
5279
|
1113 retval = os.oscanf (args(1), who); |
2095
|
1114 else |
4468
|
1115 ::error ("%s: format must be a string", who.c_str ()); |
2095
|
1116 } |
|
1117 else |
4468
|
1118 ::error ("%s: unable to create temporary input buffer", |
|
1119 who.c_str ()); |
444
|
1120 } |
636
|
1121 else |
4468
|
1122 ::error ("%s: first argument must be a string", who.c_str ()); |
444
|
1123 } |
|
1124 else |
2215
|
1125 { |
|
1126 if (nargin == 2 || nargin == 3) |
|
1127 { |
|
1128 retval(3) = -1.0; |
|
1129 retval(2) = "unknown error"; |
|
1130 retval(1) = 0.0; |
|
1131 retval(0) = Matrix (); |
|
1132 |
|
1133 if (args(0).is_string ()) |
|
1134 { |
3523
|
1135 std::string data = args(0).string_value (); |
2215
|
1136 |
3340
|
1137 octave_stream os = octave_istrstream::create (data); |
2215
|
1138 |
3340
|
1139 if (os.is_valid ()) |
2215
|
1140 { |
|
1141 if (args(1).is_string ()) |
|
1142 { |
5275
|
1143 octave_idx_type count = 0; |
2215
|
1144 |
3810
|
1145 Array<double> size = (nargin == 3) |
|
1146 ? args(2).vector_value () |
4102
|
1147 : Array<double> (1, lo_ieee_inf_value ()); |
2215
|
1148 |
5279
|
1149 octave_value tmp = os.scanf (args(1), size, count, who); |
2215
|
1150 |
|
1151 // XXX FIXME XXX -- is this the right thing to do? |
|
1152 // Extract error message first, because getting |
|
1153 // position will clear it. |
3523
|
1154 std::string errmsg = os.error (); |
2215
|
1155 |
4254
|
1156 retval(3) = os.tell () + 1; |
2215
|
1157 retval(2) = errmsg; |
4233
|
1158 retval(1) = count; |
2215
|
1159 retval(0) = tmp; |
|
1160 } |
|
1161 else |
4468
|
1162 ::error ("%s: format must be a string", who.c_str ()); |
2215
|
1163 } |
|
1164 else |
4468
|
1165 ::error ("%s: unable to create temporary input buffer", |
|
1166 who.c_str ()); |
2215
|
1167 } |
|
1168 else |
4468
|
1169 ::error ("%s: first argument must be a string", who.c_str ()); |
2215
|
1170 } |
|
1171 else |
4468
|
1172 print_usage (who); |
2215
|
1173 } |
444
|
1174 |
|
1175 return retval; |
|
1176 } |
|
1177 |
2215
|
1178 DEFUN (scanf, args, nargout, |
3372
|
1179 "-*- texinfo -*-\n\ |
|
1180 @deftypefn {Built-in Function} {[@var{val}, @var{count}] =} scanf (@var{template}, @var{size})\n\ |
3491
|
1181 @deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}, @var{count}]] = } scanf (@var{template}, \"C\")\n\ |
3372
|
1182 This is equivalent to calling @code{fscanf} with @var{fid} = @code{stdin}.\n\ |
|
1183 \n\ |
|
1184 It is currently not useful to call @code{scanf} in interactive\n\ |
|
1185 programs.\n\ |
5095
|
1186 @end deftypefn\n\ |
|
1187 @seealso{fscanf, sscanf, printf}") |
2215
|
1188 { |
|
1189 int nargin = args.length (); |
|
1190 |
|
1191 octave_value_list tmp_args (nargin+1, octave_value ()); |
|
1192 |
|
1193 tmp_args (0) = 0.0; |
|
1194 for (int i = 0; i < nargin; i++) |
|
1195 tmp_args (i+1) = args (i); |
|
1196 |
|
1197 return Ffscanf (tmp_args, nargout); |
|
1198 } |
|
1199 |
2116
|
1200 static octave_value |
|
1201 do_fread (octave_stream& os, const octave_value& size_arg, |
|
1202 const octave_value& prec_arg, const octave_value& skip_arg, |
5275
|
1203 const octave_value& arch_arg, octave_idx_type& count) |
2116
|
1204 { |
|
1205 octave_value retval; |
|
1206 |
|
1207 count = -1; |
|
1208 |
3810
|
1209 Array<double> size = size_arg.vector_value (); |
2116
|
1210 |
|
1211 if (! error_state) |
|
1212 { |
3523
|
1213 std::string prec = prec_arg.string_value (); |
2116
|
1214 |
|
1215 if (! error_state) |
|
1216 { |
4944
|
1217 int block_size = 1; |
|
1218 oct_data_conv::data_type input_type; |
|
1219 oct_data_conv::data_type output_type; |
|
1220 |
|
1221 oct_data_conv::string_to_data_type (prec, block_size, |
|
1222 input_type, output_type); |
2116
|
1223 |
|
1224 if (! error_state) |
|
1225 { |
3202
|
1226 int skip = skip_arg.int_value (true); |
2116
|
1227 |
|
1228 if (! error_state) |
|
1229 { |
3523
|
1230 std::string arch = arch_arg.string_value (); |
3202
|
1231 |
|
1232 if (! error_state) |
2116
|
1233 { |
3202
|
1234 oct_mach_info::float_format flt_fmt |
|
1235 = oct_mach_info::string_to_float_format (arch); |
2116
|
1236 |
|
1237 if (! error_state) |
4944
|
1238 retval = os.read (size, block_size, input_type, |
|
1239 output_type, skip, flt_fmt, count); |
2116
|
1240 } |
|
1241 else |
3202
|
1242 ::error ("fread: architecture type must be a string"); |
2116
|
1243 } |
|
1244 else |
3202
|
1245 ::error ("fread: skip must be an integer"); |
2116
|
1246 } |
|
1247 else |
|
1248 ::error ("fread: invalid data type specified"); |
|
1249 } |
|
1250 else |
|
1251 ::error ("fread: precision must be a string"); |
|
1252 } |
|
1253 else |
|
1254 ::error ("fread: invalid size specified"); |
|
1255 |
|
1256 return retval; |
|
1257 } |
|
1258 |
|
1259 DEFUN (fread, args, , |
3372
|
1260 "-*- texinfo -*-\n\ |
|
1261 @deftypefn {Built-in Function} {[@var{val}, @var{count}] =} fread (@var{fid}, @var{size}, @var{precision}, @var{skip}, @var{arch})\n\ |
|
1262 Read binary data of type @var{precision} from the specified file ID\n\ |
|
1263 @var{fid}.\n\ |
|
1264 \n\ |
|
1265 The optional argument @var{size} specifies the amount of data to read\n\ |
|
1266 and may be one of\n\ |
|
1267 \n\ |
|
1268 @table @code\n\ |
|
1269 @item Inf\n\ |
|
1270 Read as much as possible, returning a column vector.\n\ |
529
|
1271 \n\ |
3372
|
1272 @item @var{nr}\n\ |
|
1273 Read up to @var{nr} elements, returning a column vector.\n\ |
|
1274 \n\ |
|
1275 @item [@var{nr}, Inf]\n\ |
|
1276 Read as much as possible, returning a matrix with @var{nr} rows. If the\n\ |
|
1277 number of elements read is not an exact multiple of @var{nr}, the last\n\ |
|
1278 column is padded with zeros.\n\ |
|
1279 \n\ |
|
1280 @item [@var{nr}, @var{nc}]\n\ |
|
1281 Read up to @code{@var{nr} * @var{nc}} elements, returning a matrix with\n\ |
|
1282 @var{nr} rows. If the number of elements read is not an exact multiple\n\ |
|
1283 of @var{nr}, the last column is padded with zeros.\n\ |
|
1284 @end table\n\ |
|
1285 \n\ |
|
1286 @noindent\n\ |
|
1287 If @var{size} is omitted, a value of @code{Inf} is assumed.\n\ |
2318
|
1288 \n\ |
3372
|
1289 The optional argument @var{precision} is a string specifying the type of\n\ |
|
1290 data to read and may be one of\n\ |
|
1291 \n\ |
|
1292 @table @code\n\ |
4944
|
1293 @item \"schar\"\n\ |
|
1294 @itemx \"signed char\"\n\ |
3372
|
1295 Signed character.\n\ |
529
|
1296 \n\ |
4944
|
1297 @item \"uchar\"\n\ |
|
1298 @itemx \"unsigned char\"\n\ |
3372
|
1299 Unsigned character.\n\ |
|
1300 \n\ |
4944
|
1301 @item \"int8\"\n\ |
|
1302 @itemx \"integer*1\"\n\ |
|
1303 \n\ |
|
1304 8-bit signed integer.\n\ |
2318
|
1305 \n\ |
4944
|
1306 @item \"int16\"\n\ |
|
1307 @itemx \"integer*2\"\n\ |
|
1308 16-bit signed integer.\n\ |
3372
|
1309 \n\ |
4944
|
1310 @item \"int32\"\n\ |
|
1311 @itemx \"integer*4\"\n\ |
|
1312 32-bit signed integer.\n\ |
3372
|
1313 \n\ |
4944
|
1314 @item \"int64\"\n\ |
|
1315 @itemx \"integer*8\"\n\ |
|
1316 64-bit signed integer.\n\ |
|
1317 \n\ |
|
1318 @item \"uint8\"\n\ |
|
1319 8-bit unsigned integer.\n\ |
529
|
1320 \n\ |
4944
|
1321 @item \"uint16\"\n\ |
|
1322 16-bit unsigned integer.\n\ |
3372
|
1323 \n\ |
4944
|
1324 @item \"uint32\"\n\ |
|
1325 32-bit unsigned integer.\n\ |
3372
|
1326 \n\ |
4944
|
1327 @item \"uint64\"\n\ |
|
1328 64-bit unsigned integer.\n\ |
|
1329 \n\ |
|
1330 @item \"single\"\n\ |
3372
|
1331 @itemx \"float32\"\n\ |
|
1332 @itemx \"real*4\"\n\ |
4944
|
1333 32-bit floating point number.\n\ |
3372
|
1334 \n\ |
|
1335 @item \"double\"\n\ |
|
1336 @itemx \"float64\"\n\ |
|
1337 @itemx \"real*8\"\n\ |
4944
|
1338 64-bit floating point number.\n\ |
|
1339 \n\ |
|
1340 @item \"char\"\n\ |
|
1341 @itemx \"char*1\"\n\ |
|
1342 Single character.\n\ |
3372
|
1343 \n\ |
4944
|
1344 @item \"short\"\n\ |
|
1345 Short integer (size is platform dependent).\n\ |
|
1346 \n\ |
|
1347 @item \"int\"\n\ |
|
1348 Integer (size is platform dependent).\n\ |
|
1349 \n\ |
|
1350 @item \"long\"\n\ |
|
1351 Long integer (size is platform dependent).\n\ |
3372
|
1352 \n\ |
4944
|
1353 @item \"ushort\"\n\ |
|
1354 @itemx \"unsigned short\"\n\ |
|
1355 Unsigned short integer (size is platform dependent).\n\ |
4610
|
1356 \n\ |
4944
|
1357 @item \"uint\"\n\ |
|
1358 @itemx \"unsigned int\"\n\ |
|
1359 Unsigned integer (size is platform dependent).\n\ |
4610
|
1360 \n\ |
4944
|
1361 @item \"ulong\"\n\ |
|
1362 @itemx \"unsigned long\"\n\ |
|
1363 Unsigned long integer (size is platform dependent).\n\ |
|
1364 \n\ |
|
1365 @item \"float\"\n\ |
|
1366 Single precision floating point number (size is platform dependent).\n\ |
3372
|
1367 @end table\n\ |
|
1368 \n\ |
|
1369 @noindent\n\ |
|
1370 The default precision is @code{\"uchar\"}.\n\ |
2318
|
1371 \n\ |
4944
|
1372 The @var{precision} argument may also specify an optional repeat\n\ |
|
1373 count. For example, @samp{32*single} causes @code{fread} to read\n\ |
|
1374 a block of 32 single precision floating point numbers. Reading in\n\ |
|
1375 blocks is useful in combination with the @var{skip} argument.\n\ |
|
1376 \n\ |
|
1377 The @var{precision} argument may also specify a type conversion.\n\ |
|
1378 For example, @samp{int16=>int32} causes @code{fread} to read 16-bit\n\ |
|
1379 integer values and return an array of 32-bit integer values. By\n\ |
|
1380 default, @code{fread} returns a double precision array. The special\n\ |
|
1381 form @samp{*TYPE} is shorthand for @samp{TYPE=>TYPE}.\n\ |
|
1382 \n\ |
|
1383 The conversion and repeat counts may be combined. For example,\n\ |
|
1384 @samp{32*single=>single} causes @code{fread} to read blocks of single\n\ |
|
1385 precision floating point values and return an array of single precision\n\ |
|
1386 values instead of the default array of double precision values.\n\ |
|
1387 \n\ |
3372
|
1388 The optional argument @var{skip} specifies the number of bytes to skip\n\ |
4944
|
1389 after each element (or block of elements) is read. If it is not\n\ |
|
1390 specified, a value of 0 is assumed. If the final block read is not\n\ |
|
1391 complete, the final skip is omitted. For example,\n\ |
|
1392 \n\ |
|
1393 @example\n\ |
|
1394 fread (f, 10, \"3*single=>single\", 8)\n\ |
|
1395 @end example\n\ |
|
1396 \n\ |
|
1397 @noindent\n\ |
|
1398 will omit the final 8-byte skip because the last read will not be\n\ |
|
1399 a complete block of 3 values.\n\ |
3372
|
1400 \n\ |
|
1401 The optional argument @var{arch} is a string specifying the data format\n\ |
|
1402 for the file. Valid values are\n\ |
529
|
1403 \n\ |
3372
|
1404 @table @code\n\ |
|
1405 @item \"native\"\n\ |
|
1406 The format of the current machine.\n\ |
|
1407 \n\ |
4546
|
1408 @item \"ieee-be\"\n\ |
3372
|
1409 IEEE big endian.\n\ |
|
1410 \n\ |
4546
|
1411 @item \"ieee-le\"\n\ |
3372
|
1412 IEEE little endian.\n\ |
2318
|
1413 \n\ |
3372
|
1414 @item \"vaxd\"\n\ |
|
1415 VAX D floating format.\n\ |
|
1416 \n\ |
|
1417 @item \"vaxg\"\n\ |
|
1418 VAX G floating format.\n\ |
2318
|
1419 \n\ |
3372
|
1420 @item \"cray\"\n\ |
|
1421 Cray floating format.\n\ |
|
1422 @end table\n\ |
2318
|
1423 \n\ |
3372
|
1424 @noindent\n\ |
|
1425 Conversions are currently only supported for @code{\"ieee-be\"} and\n\ |
|
1426 @code{\"ieee-le\"} formats.\n\ |
2318
|
1427 \n\ |
3372
|
1428 The data read from the file is returned in @var{val}, and the number of\n\ |
|
1429 values read is returned in @code{count}\n\ |
5095
|
1430 @end deftypefn\n\ |
|
1431 @seealso{fwrite, fopen, fclose}") |
529
|
1432 { |
2095
|
1433 octave_value_list retval; |
2116
|
1434 |
|
1435 int nargin = args.length (); |
|
1436 |
2318
|
1437 if (nargin > 0 && nargin < 6) |
2116
|
1438 { |
|
1439 retval(1) = -1.0; |
|
1440 retval(0) = Matrix (); |
|
1441 |
3341
|
1442 octave_stream os = octave_stream_list::lookup (args(0), "fread"); |
2116
|
1443 |
3341
|
1444 if (! error_state) |
2116
|
1445 { |
4257
|
1446 octave_value size = lo_ieee_inf_value (); |
|
1447 octave_value prec = "uchar"; |
|
1448 octave_value skip = 0; |
|
1449 octave_value arch = "unknown"; |
2116
|
1450 |
4257
|
1451 int idx = 1; |
2116
|
1452 |
4257
|
1453 if (nargin > 1 && ! args(idx).is_string ()) |
|
1454 size = args(idx++); |
|
1455 |
|
1456 if (nargin > idx) |
|
1457 prec = args(idx++); |
2116
|
1458 |
4257
|
1459 if (nargin > idx) |
|
1460 skip = args(idx++); |
|
1461 |
|
1462 if (nargin > idx) |
|
1463 arch = args(idx++); |
2116
|
1464 |
5275
|
1465 octave_idx_type count = -1; |
2116
|
1466 |
3340
|
1467 octave_value tmp = do_fread (os, size, prec, skip, arch, count); |
2116
|
1468 |
4233
|
1469 retval(1) = count; |
2116
|
1470 retval(0) = tmp; |
|
1471 } |
|
1472 } |
|
1473 else |
|
1474 print_usage ("fread"); |
|
1475 |
529
|
1476 return retval; |
|
1477 } |
|
1478 |
2116
|
1479 static int |
|
1480 do_fwrite (octave_stream& os, const octave_value& data, |
|
1481 const octave_value& prec_arg, const octave_value& skip_arg, |
|
1482 const octave_value& arch_arg) |
|
1483 { |
|
1484 int retval = -1; |
|
1485 |
3523
|
1486 std::string prec = prec_arg.string_value (); |
2116
|
1487 |
|
1488 if (! error_state) |
|
1489 { |
4944
|
1490 int block_size = 1; |
|
1491 oct_data_conv::data_type output_type; |
|
1492 |
|
1493 oct_data_conv::string_to_data_type (prec, block_size, output_type); |
2116
|
1494 |
|
1495 if (! error_state) |
|
1496 { |
3202
|
1497 int skip = skip_arg.int_value (true); |
2116
|
1498 |
|
1499 if (! error_state) |
|
1500 { |
3523
|
1501 std::string arch = arch_arg.string_value (); |
3202
|
1502 |
|
1503 if (! error_state) |
2116
|
1504 { |
3202
|
1505 oct_mach_info::float_format flt_fmt |
|
1506 = oct_mach_info::string_to_float_format (arch); |
2116
|
1507 |
|
1508 if (! error_state) |
4944
|
1509 retval = os.write (data, block_size, output_type, |
|
1510 skip, flt_fmt); |
2116
|
1511 } |
|
1512 else |
3202
|
1513 ::error ("fwrite: architecture type must be a string"); |
2116
|
1514 } |
|
1515 else |
3202
|
1516 ::error ("fwrite: skip must be an integer"); |
2116
|
1517 } |
3202
|
1518 else |
|
1519 ::error ("fwrite: invalid precision specified"); |
2116
|
1520 } |
|
1521 else |
|
1522 ::error ("fwrite: precision must be a string"); |
|
1523 |
|
1524 return retval; |
|
1525 } |
|
1526 |
|
1527 DEFUN (fwrite, args, , |
3372
|
1528 "-*- texinfo -*-\n\ |
|
1529 @deftypefn {Built-in Function} {@var{count} =} fwrite (@var{fid}, @var{data}, @var{precision}, @var{skip}, @var{arch})\n\ |
|
1530 Write data in binary form of type @var{precision} to the specified file\n\ |
|
1531 ID @var{fid}, returning the number of values successfully written to the\n\ |
|
1532 file.\n\ |
1181
|
1533 \n\ |
3372
|
1534 The argument @var{data} is a matrix of values that are to be written to\n\ |
|
1535 the file. The values are extracted in column-major order.\n\ |
1181
|
1536 \n\ |
3372
|
1537 The remaining arguments @var{precision}, @var{skip}, and @var{arch} are\n\ |
|
1538 optional, and are interpreted as described for @code{fread}.\n\ |
1181
|
1539 \n\ |
3372
|
1540 The behavior of @code{fwrite} is undefined if the values in @var{data}\n\ |
|
1541 are too large to fit in the specified precision.\n\ |
5095
|
1542 @end deftypefn\n\ |
|
1543 @seealso{fread, fopen, fclose}") |
1181
|
1544 { |
4233
|
1545 octave_value retval = -1; |
2116
|
1546 |
|
1547 int nargin = args.length (); |
|
1548 |
|
1549 if (nargin > 1 && nargin < 6) |
|
1550 { |
3341
|
1551 octave_stream os = octave_stream_list::lookup (args(0), "fwrite"); |
2116
|
1552 |
3341
|
1553 if (! error_state) |
2116
|
1554 { |
|
1555 octave_value data = args(1); |
2318
|
1556 |
|
1557 octave_value prec = (nargin > 2) |
|
1558 ? args(2) : octave_value ("uchar"); |
|
1559 |
|
1560 octave_value skip = (nargin > 3) |
|
1561 ? args(3) : octave_value (0.0); |
|
1562 |
|
1563 octave_value arch = (nargin > 4) |
|
1564 ? args(4) : octave_value ("unknown"); |
2116
|
1565 |
3340
|
1566 double status = do_fwrite (os, data, prec, skip, arch); |
2825
|
1567 |
|
1568 retval = status; |
2116
|
1569 } |
|
1570 } |
|
1571 else |
|
1572 print_usage ("fwrite"); |
|
1573 |
1181
|
1574 return retval; |
|
1575 } |
|
1576 |
1957
|
1577 DEFUN (feof, args, , |
3372
|
1578 "-*- texinfo -*-\n\ |
|
1579 @deftypefn {Built-in Function} {} feof (@var{fid})\n\ |
|
1580 Return 1 if an end-of-file condition has been encountered for a given\n\ |
|
1581 file and 0 otherwise. Note that it will only return 1 if the end of the\n\ |
|
1582 file has already been encountered, not if the next read operation will\n\ |
|
1583 result in an end-of-file condition.\n\ |
5095
|
1584 @end deftypefn\n\ |
|
1585 @seealso{fread, fopen, fclose}") |
529
|
1586 { |
4233
|
1587 octave_value retval = -1; |
529
|
1588 |
|
1589 int nargin = args.length (); |
|
1590 |
2095
|
1591 if (nargin == 1) |
|
1592 { |
3341
|
1593 octave_stream os = octave_stream_list::lookup (args(0), "feof"); |
444
|
1594 |
3341
|
1595 if (! error_state) |
3340
|
1596 retval = os.eof () ? 1.0 : 0.0; |
2095
|
1597 } |
529
|
1598 else |
2095
|
1599 print_usage ("feof"); |
444
|
1600 |
|
1601 return retval; |
|
1602 } |
|
1603 |
2095
|
1604 DEFUN (ferror, args, , |
3372
|
1605 "-*- texinfo -*-\n\ |
|
1606 @deftypefn {Built-in Function} {} ferror (@var{fid})\n\ |
|
1607 Return 1 if an error condition has been encountered for a given file\n\ |
|
1608 and 0 otherwise. Note that it will only return 1 if an error has\n\ |
|
1609 already been encountered, not if the next operation will result in an\n\ |
|
1610 error condition.\n\ |
|
1611 @end deftypefn") |
1230
|
1612 { |
2095
|
1613 octave_value_list retval; |
1230
|
1614 |
2095
|
1615 int nargin = args.length (); |
1230
|
1616 |
2095
|
1617 if (nargin == 1 || nargin == 2) |
|
1618 { |
3341
|
1619 octave_stream os = octave_stream_list::lookup (args(0), "ferror"); |
1230
|
1620 |
3341
|
1621 if (! error_state) |
2095
|
1622 { |
|
1623 bool clear = false; |
1230
|
1624 |
2095
|
1625 if (nargin == 2) |
|
1626 { |
3523
|
1627 std::string opt = args(1).string_value (); |
2095
|
1628 |
|
1629 if (! error_state) |
|
1630 clear = (opt == "clear"); |
|
1631 else |
|
1632 return retval; |
|
1633 } |
1755
|
1634 |
2095
|
1635 int error_number = 0; |
|
1636 |
3523
|
1637 std::string error_message = os.error (clear, error_number); |
1230
|
1638 |
4233
|
1639 retval(1) = error_number; |
2095
|
1640 retval(0) = error_message; |
|
1641 } |
1230
|
1642 } |
2095
|
1643 else |
|
1644 print_usage ("ferror"); |
1230
|
1645 |
|
1646 return retval; |
|
1647 } |
|
1648 |
1957
|
1649 DEFUN (popen, args, , |
3301
|
1650 "-*- texinfo -*-\n\ |
4326
|
1651 @deftypefn {Built-in Function} {@var{fid} =} popen (@var{command}, @var{mode})\n\ |
3301
|
1652 Start a process and create a pipe. The name of the command to run is\n\ |
|
1653 given by @var{command}. The file identifier corresponding to the input\n\ |
|
1654 or output stream of the process is returned in @var{fid}. The argument\n\ |
|
1655 @var{mode} may be\n\ |
|
1656 \n\ |
|
1657 @table @code\n\ |
|
1658 @item \"r\"\n\ |
|
1659 The pipe will be connected to the standard output of the process, and\n\ |
|
1660 open for reading.\n\ |
1230
|
1661 \n\ |
3301
|
1662 @item \"w\"\n\ |
|
1663 The pipe will be connected to the standard input of the process, and\n\ |
|
1664 open for writing.\n\ |
|
1665 @end table\n\ |
|
1666 \n\ |
|
1667 For example,\n\ |
1230
|
1668 \n\ |
3301
|
1669 @example\n\ |
|
1670 @group\n\ |
|
1671 fid = popen (\"ls -ltr / | tail -3\", \"r\");\n\ |
|
1672 while (isstr (s = fgets (fid)))\n\ |
|
1673 fputs (stdout, s);\n\ |
|
1674 endwhile\n\ |
|
1675 @print{} drwxr-xr-x 33 root root 3072 Feb 15 13:28 etc\n\ |
|
1676 @print{} drwxr-xr-x 3 root root 1024 Feb 15 13:28 lib\n\ |
|
1677 @print{} drwxrwxrwt 15 root root 2048 Feb 17 14:53 tmp\n\ |
|
1678 @end group\n\ |
|
1679 @end example\n\ |
|
1680 @end deftypefn") |
1230
|
1681 { |
4233
|
1682 octave_value retval = -1; |
1230
|
1683 |
|
1684 int nargin = args.length (); |
|
1685 |
2095
|
1686 if (nargin == 2) |
|
1687 { |
3523
|
1688 std::string name = args(0).string_value (); |
1230
|
1689 |
2095
|
1690 if (! error_state) |
|
1691 { |
3523
|
1692 std::string mode = args(1).string_value (); |
1230
|
1693 |
2095
|
1694 if (! error_state) |
|
1695 { |
|
1696 if (mode == "r") |
|
1697 { |
3340
|
1698 octave_stream ips = octave_iprocstream::create (name); |
1230
|
1699 |
2095
|
1700 retval = octave_stream_list::insert (ips); |
|
1701 } |
|
1702 else if (mode == "w") |
|
1703 { |
3340
|
1704 octave_stream ops = octave_oprocstream::create (name); |
1230
|
1705 |
2095
|
1706 retval = octave_stream_list::insert (ops); |
|
1707 } |
|
1708 else |
|
1709 ::error ("popen: invalid mode specified"); |
|
1710 } |
|
1711 else |
|
1712 ::error ("popen: mode must be a string"); |
|
1713 } |
|
1714 else |
|
1715 ::error ("popen: name must be a string"); |
|
1716 } |
1230
|
1717 else |
2095
|
1718 print_usage ("popen"); |
1230
|
1719 |
|
1720 return retval; |
|
1721 } |
|
1722 |
1957
|
1723 DEFUN (pclose, args, , |
3381
|
1724 "-*- texinfo -*-\n\ |
3301
|
1725 @deftypefn {Built-in Function} {} pclose (@var{fid})\n\ |
|
1726 Close a file identifier that was opened by @code{popen}. You may also\n\ |
|
1727 use @code{fclose} for the same purpose.\n\ |
|
1728 @end deftypefn") |
1230
|
1729 { |
4233
|
1730 octave_value retval = -1; |
1230
|
1731 |
|
1732 int nargin = args.length (); |
|
1733 |
2095
|
1734 if (nargin == 1) |
4233
|
1735 retval = octave_stream_list::remove (args(0), "pclose"); |
1377
|
1736 else |
2095
|
1737 print_usage ("pclose"); |
1379
|
1738 |
|
1739 return retval; |
|
1740 } |
|
1741 |
2458
|
1742 DEFUN (tmpnam, args, , |
3372
|
1743 "-*- texinfo -*-\n\ |
4267
|
1744 @deftypefn {Built-in Function} {} tmpnam (@var{dir}, @var{prefix})\n\ |
3372
|
1745 Return a unique temporary file name as a string.\n\ |
|
1746 \n\ |
4267
|
1747 If @var{prefix} is omitted, a value of @code{\"oct-\"} is used.\n\ |
|
1748 If @var{dir} is also omitted, the default directory for temporary files\n\ |
|
1749 is used. If @var{dir} is provided, it must exist, otherwise the default\n\ |
|
1750 directory for temporary files is used. Since the named file is not\n\ |
|
1751 opened, by @code{tmpnam}, it is possible (though relatively unlikely)\n\ |
|
1752 that it will not be available by the time your program attempts to open it.\n\ |
4326
|
1753 @end deftypefn\n\ |
|
1754 @seealso{tmpfile, mkstemp, and P_tmpdir}") |
1802
|
1755 { |
2095
|
1756 octave_value retval; |
1802
|
1757 |
2936
|
1758 int len = args.length (); |
|
1759 |
|
1760 if (len < 3) |
|
1761 { |
3523
|
1762 std::string dir = len > 0 ? args(0).string_value () : std::string (); |
2936
|
1763 |
|
1764 if (! error_state) |
4267
|
1765 { |
|
1766 std::string pfx |
|
1767 = len > 1 ? args(1).string_value () : std::string ("oct-"); |
|
1768 |
|
1769 if (! error_state) |
|
1770 retval = file_ops::tempnam (dir, pfx); |
|
1771 else |
|
1772 ::error ("expecting second argument to be a string"); |
|
1773 } |
|
1774 else |
|
1775 ::error ("expecting first argument to be a string"); |
2936
|
1776 } |
1802
|
1777 else |
2458
|
1778 print_usage ("tmpnam"); |
1802
|
1779 |
|
1780 return retval; |
|
1781 } |
|
1782 |
2458
|
1783 DEFALIAS (octave_tmp_file_name, tmpnam); |
|
1784 |
4326
|
1785 DEFUN (tmpfile, args, , |
|
1786 "-*- texinfo -*-\n\ |
|
1787 @deftypefn {Built-in Function} {[@var{fid}, @var{msg}] =} tmpfile ()\n\ |
|
1788 Return the file ID corresponding to a new temporary file with a unique\n\ |
|
1789 name. The file is opened in binary read/write (@code{\"w+b\"}) mode.\n\ |
|
1790 The file will be deleted automatically when it is closed or when Octave\n\ |
|
1791 exits.\n\ |
|
1792 \n\ |
|
1793 If successful, @var{fid} is a valid file ID and @var{msg} is an empty\n\ |
|
1794 string. Otherwise, @var{fid} is -1 and @var{msg} contains a\n\ |
|
1795 system-dependent error message.\n\ |
|
1796 @end deftypefn\n\ |
|
1797 @seealso{tmpnam, mkstemp, and P_tmpdir}") |
|
1798 { |
|
1799 octave_value_list retval; |
|
1800 |
|
1801 retval(1) = std::string (); |
|
1802 retval(0) = -1; |
|
1803 |
|
1804 int nargin = args.length (); |
|
1805 |
|
1806 if (nargin == 0) |
|
1807 { |
|
1808 FILE *fid = tmpfile (); |
|
1809 |
|
1810 if (fid) |
|
1811 { |
|
1812 std::string nm; |
|
1813 |
|
1814 std::ios::openmode md = fopen_mode_to_ios_mode ("w+b"); |
|
1815 |
4327
|
1816 octave_stream s = octave_stdiostream::create (nm, fid, md); |
4326
|
1817 |
|
1818 if (s) |
|
1819 retval(0) = octave_stream_list::insert (s); |
|
1820 else |
4327
|
1821 error ("tmpfile: failed to create octave_stdiostream object"); |
4326
|
1822 |
|
1823 } |
|
1824 else |
|
1825 { |
|
1826 using namespace std; |
|
1827 retval(1) = ::strerror (errno); |
|
1828 retval(0) = -1; |
|
1829 } |
|
1830 } |
|
1831 else |
|
1832 print_usage ("tmpfile"); |
|
1833 |
|
1834 return retval; |
|
1835 } |
|
1836 |
|
1837 DEFUN (mkstemp, args, , |
|
1838 "-*- texinfo -*-\n\ |
5109
|
1839 @deftypefn {Built-in Function} {[@var{fid}, @var{name}, @var{msg}] =} mkstemp (@var{template}, @var{delete})\n\ |
4326
|
1840 Return the file ID corresponding to a new temporary file with a unique\n\ |
|
1841 name created from @var{template}. The last six characters of @var{template}\n\ |
5016
|
1842 must be @code{XXXXXX} and these are replaced with a string that makes the\n\ |
4326
|
1843 filename unique. The file is then created with mode read/write and\n\ |
|
1844 permissions that are system dependent (on GNU/Linux systems, the permissions\n\ |
|
1845 will be 0600 for versions of glibc 2.0.7 and later). The file is opened\n\ |
|
1846 with the @code{O_EXCL} flag.\n\ |
|
1847 \n\ |
|
1848 If the optional argument @var{delete} is supplied and is true,\n\ |
|
1849 the file will be deleted automatically when Octave exits, or when\n\ |
|
1850 the function @code{purge_tmp_files} is called.\n\ |
|
1851 \n\ |
|
1852 If successful, @var{fid} is a valid file ID, @var{name} is the name of\n\ |
|
1853 the file, and and @var{msg} is an empty string. Otherwise, @var{fid}\n\ |
|
1854 is -1, @var{name} is empty, and @var{msg} contains a system-dependent\n\ |
|
1855 error message.\n\ |
|
1856 @end deftypefn\n\ |
|
1857 @seealso{tmpfile, tmpnam, and P_tmpdir}") |
|
1858 { |
|
1859 octave_value_list retval; |
|
1860 |
|
1861 retval(2) = std::string (); |
|
1862 retval(1) = std::string (); |
|
1863 retval(0) = -1; |
|
1864 |
|
1865 #if defined (HAVE_MKSTEMP) |
|
1866 |
|
1867 int nargin = args.length (); |
|
1868 |
|
1869 if (nargin == 1 || nargin == 2) |
|
1870 { |
|
1871 std::string tmpl8 = args(0).string_value (); |
|
1872 |
|
1873 if (! error_state) |
|
1874 { |
4354
|
1875 OCTAVE_LOCAL_BUFFER (char, tmp, tmpl8.size () + 1); |
|
1876 strcpy (tmp, tmpl8.c_str ()); |
4326
|
1877 |
|
1878 int fd = mkstemp (tmp); |
|
1879 |
|
1880 if (fd < 0) |
|
1881 { |
|
1882 using namespace std; |
5109
|
1883 retval(2) = ::strerror (errno); |
4326
|
1884 retval(0) = fd; |
|
1885 } |
|
1886 else |
|
1887 { |
|
1888 const char *fopen_mode = "w+"; |
|
1889 |
|
1890 FILE *fid = fdopen (fd, fopen_mode); |
|
1891 |
|
1892 if (fid) |
|
1893 { |
|
1894 std::string nm = tmp; |
|
1895 |
|
1896 std::ios::openmode md = fopen_mode_to_ios_mode (fopen_mode); |
|
1897 |
4327
|
1898 octave_stream s = octave_stdiostream::create (nm, fid, md); |
4326
|
1899 |
|
1900 if (s) |
|
1901 { |
|
1902 retval(1) = nm; |
|
1903 retval(0) = octave_stream_list::insert (s); |
|
1904 |
|
1905 if (nargin == 2) |
|
1906 mark_for_deletion (nm); |
|
1907 } |
|
1908 else |
4327
|
1909 error ("mkstemp: failed to create octave_stdiostream object"); |
4326
|
1910 } |
|
1911 else |
|
1912 { |
|
1913 using namespace std; |
5109
|
1914 retval(2) = ::strerror (errno); |
4326
|
1915 retval(0) = -1; |
|
1916 } |
|
1917 } |
|
1918 } |
|
1919 else |
|
1920 error ("mkstemp: expecting string as first argument"); |
|
1921 } |
|
1922 else |
|
1923 print_usage ("mkstemp"); |
|
1924 |
|
1925 #else |
|
1926 retval(2) = "mkstemp: not supported on this sytem"; |
|
1927 #endif |
|
1928 |
|
1929 return retval; |
|
1930 } |
|
1931 |
1400
|
1932 static int |
|
1933 convert (int x, int ibase, int obase) |
|
1934 { |
|
1935 int retval = 0; |
|
1936 |
|
1937 int tmp = x % obase; |
|
1938 |
|
1939 if (tmp > ibase - 1) |
2095
|
1940 ::error ("umask: invalid digit"); |
1400
|
1941 else |
|
1942 { |
|
1943 retval = tmp; |
|
1944 int mult = ibase; |
|
1945 while ((x = (x - tmp) / obase)) |
|
1946 { |
|
1947 tmp = x % obase; |
|
1948 if (tmp > ibase - 1) |
|
1949 { |
2095
|
1950 ::error ("umask: invalid digit"); |
1400
|
1951 break; |
|
1952 } |
|
1953 retval += mult * tmp; |
|
1954 mult *= ibase; |
|
1955 } |
|
1956 } |
|
1957 |
|
1958 return retval; |
|
1959 } |
|
1960 |
1957
|
1961 DEFUN (umask, args, , |
3301
|
1962 "-*- texinfo -*-\n\ |
|
1963 @deftypefn {Built-in Function} {} umask (@var{mask})\n\ |
|
1964 Set the permission mask for file creation. The parameter @var{mask}\n\ |
4715
|
1965 is an integer, interpreted as an octal number. If successful,\n\ |
|
1966 returns the previous value of the mask (as an integer to be\n\ |
|
1967 interpreted as an octal number); otherwise an error message is printed.\n\ |
3301
|
1968 @end deftypefn") |
1400
|
1969 { |
2095
|
1970 octave_value_list retval; |
1400
|
1971 |
|
1972 int status = 0; |
|
1973 |
|
1974 if (args.length () == 1) |
|
1975 { |
3202
|
1976 int mask = args(0).int_value (true); |
1400
|
1977 |
3202
|
1978 if (! error_state) |
1400
|
1979 { |
3202
|
1980 if (mask < 0) |
1400
|
1981 { |
|
1982 status = -1; |
2095
|
1983 ::error ("umask: MASK must be a positive integer value"); |
1400
|
1984 } |
|
1985 else |
|
1986 { |
|
1987 int oct_mask = convert (mask, 8, 10); |
|
1988 |
|
1989 if (! error_state) |
2926
|
1990 status = convert (file_ops::umask (oct_mask), 10, 8); |
1400
|
1991 } |
|
1992 } |
3202
|
1993 else |
|
1994 { |
|
1995 status = -1; |
|
1996 ::error ("umask: expecting integer argument"); |
|
1997 } |
1400
|
1998 } |
|
1999 else |
|
2000 print_usage ("umask"); |
|
2001 |
|
2002 if (status >= 0) |
4233
|
2003 retval(0) = status; |
1400
|
2004 |
|
2005 return retval; |
|
2006 } |
|
2007 |
2189
|
2008 void |
|
2009 symbols_of_file_io (void) |
|
2010 { |
4267
|
2011 #if ! defined (P_tmpdir) |
|
2012 #define P_tmpdir "/tmp" |
|
2013 #endif |
|
2014 |
|
2015 DEFCONSTX ("P_tmpdir", SBV_P_tmpdir, P_tmpdir, |
|
2016 "-*- texinfo -*-\n\ |
|
2017 @defvr {Built-in Variable} P_tmpdir\n\ |
|
2018 The default name of the directory for temporary files on this system.\n\ |
5095
|
2019 The value of this variable is system dependent.\n\ |
4267
|
2020 @end defvr"); |
|
2021 |
2341
|
2022 // NOTE: the values of SEEK_SET, SEEK_CUR, and SEEK_END have to be |
|
2023 // this way for Matlab compatibility. |
|
2024 |
4233
|
2025 DEFCONSTX ("SEEK_SET", SBV_SEEK_SET, -1, |
3372
|
2026 "-*- texinfo -*-\n\ |
|
2027 @defvr {Built-in Variable} SEEK_SET\n\ |
|
2028 @defvrx {Built-in Variable} SEEK_CUR\n\ |
|
2029 @defvrx {Built-in Variable} SEEK_END\n\ |
|
2030 These variables may be used as the optional third argument for the\n\ |
|
2031 function @code{fseek}.\n\ |
|
2032 \n\ |
|
2033 @table @code\n\ |
|
2034 @item SEEK_SET\n\ |
|
2035 Position file relative to the beginning.\n\ |
|
2036 \n\ |
|
2037 @item SEEK_CUR\n\ |
|
2038 Position file relative to the current position.\n\ |
|
2039 \n\ |
|
2040 @item SEEK_END\n\ |
|
2041 used with fseek to position file relative to the end.\n\ |
|
2042 @end table\n\ |
|
2043 @end defvr"); |
2189
|
2044 |
4233
|
2045 DEFCONSTX ("SEEK_CUR", SBV_SEEK_CUR, 0, |
3458
|
2046 "-*- texinfo -*-\n\ |
|
2047 @defvr {Built-in Variable} SEEK_CUR\n\ |
|
2048 See SEEK_SET.\n\ |
|
2049 @end defvr"); |
2189
|
2050 |
4233
|
2051 DEFCONSTX ("SEEK_END", SBV_SEEK_END, 1, |
3458
|
2052 "-*- texinfo -*-\n\ |
|
2053 @defvr {Built-in Variable} SEEK_END\n\ |
|
2054 See SEEK_SET.\n\ |
|
2055 @end defvr"); |
2189
|
2056 |
3141
|
2057 DEFCONSTX ("stdin", SBV_stdin, stdin_file, |
3372
|
2058 "-*- texinfo -*-\n\ |
|
2059 @defvr {Built-in Variable} stdin\n\ |
|
2060 The standard input stream (file id 0). When Octave is used\n\ |
|
2061 interactively, this is filtered through the command line editing\n\ |
|
2062 functions.\n\ |
|
2063 @end defvr"); |
2189
|
2064 |
3141
|
2065 DEFCONSTX ("stdout", SBV_stdout, stdout_file, |
3372
|
2066 "-*- texinfo -*-\n\ |
|
2067 @defvr {Built-in Variable} stdout\n\ |
|
2068 The standard output stream (file id 1). Data written to the\n\ |
|
2069 standard output is normally filtered through the pager.\n\ |
|
2070 @end defvr"); |
2189
|
2071 |
3141
|
2072 DEFCONSTX ("stderr", SBV_stderr, stderr_file, |
3372
|
2073 "-*- texinfo -*-\n\ |
|
2074 @defvr {Built-in Variable} stderr\n\ |
|
2075 The standard error stream (file id 2). Even if paging is turned on,\n\ |
|
2076 the standard error is not sent to the pager. It is useful for error\n\ |
|
2077 messages and prompts.\n\ |
|
2078 @end defvr"); |
|
2079 |
2189
|
2080 } |
|
2081 |
444
|
2082 /* |
1
|
2083 ;;; Local Variables: *** |
|
2084 ;;; mode: C++ *** |
|
2085 ;;; End: *** |
|
2086 */ |