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