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