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