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