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