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