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