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