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