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