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 { |
2873
|
691 if (args(fmt_n).is_string ()) |
2095
|
692 { |
3523
|
693 std::string fmt = args(fmt_n).string_value (); |
2095
|
694 |
|
695 octave_value_list tmp_args; |
|
696 |
2873
|
697 if (nargin > 1 + fmt_n) |
2095
|
698 { |
2873
|
699 tmp_args.resize (nargin-fmt_n-1, octave_value ()); |
2095
|
700 |
2873
|
701 for (int i = fmt_n + 1; i < nargin; i++) |
|
702 tmp_args(i-fmt_n-1) = args(i); |
2095
|
703 } |
|
704 |
4468
|
705 retval = os.printf (fmt, tmp_args, who); |
2095
|
706 } |
|
707 else |
4468
|
708 ::error ("%s: format must be a string", who.c_str ()); |
2095
|
709 } |
|
710 } |
|
711 else |
4468
|
712 print_usage (who); |
1181
|
713 |
3737
|
714 if (return_char_count) |
|
715 return retval; |
|
716 else |
|
717 return octave_value(); |
1181
|
718 } |
|
719 |
4468
|
720 DEFUN (printf, args, , |
|
721 "-*- texinfo -*-\n\ |
|
722 @deftypefn {Built-in Function} {} printf (@var{fid}, @var{template}, @dots{})\n\ |
|
723 Print optional arguments under the control of the template string\n\ |
|
724 @var{template} to the stream @code{stdout}.\n\ |
|
725 @end deftypefn\n\ |
|
726 @seealso{fprintf and sprintf}") |
|
727 { |
|
728 static std::string who = "printf"; |
|
729 |
|
730 octave_value retval = -1; |
|
731 |
|
732 int nargin = args.length (); |
|
733 |
|
734 if (nargin > 0) |
|
735 { |
|
736 if (args(0).is_string ()) |
|
737 { |
|
738 std::string fmt = args(0).string_value (); |
|
739 |
|
740 octave_value_list tmp_args; |
|
741 |
|
742 if (nargin > 1) |
|
743 { |
|
744 tmp_args.resize (nargin-1, octave_value ()); |
|
745 |
|
746 for (int i = 1; i < nargin; i++) |
|
747 tmp_args(i-1) = args(i); |
|
748 } |
|
749 |
|
750 retval = stdout_stream.printf (fmt, tmp_args, who); |
|
751 } |
|
752 else |
|
753 ::error ("%s: format must be a string", who.c_str ()); |
|
754 } |
|
755 else |
|
756 print_usage (who); |
|
757 |
|
758 return retval; |
|
759 } |
|
760 |
2095
|
761 DEFUN (fputs, args, , |
3372
|
762 "-*- texinfo -*-\n\ |
|
763 @deftypefn {Built-in Function} {} fputs (@var{fid}, @var{string})\n\ |
|
764 Write a string to a file with no formatting.\n\ |
|
765 @end deftypefn") |
1181
|
766 { |
4468
|
767 static std::string who = "fputs"; |
|
768 |
4233
|
769 octave_value retval = -1; |
1181
|
770 |
|
771 int nargin = args.length (); |
|
772 |
2095
|
773 if (nargin == 2) |
|
774 { |
4468
|
775 octave_stream os = octave_stream_list::lookup (args(0), who); |
1181
|
776 |
3341
|
777 if (! error_state) |
4468
|
778 retval = os.puts (args(1), who); |
2095
|
779 } |
1181
|
780 else |
4468
|
781 print_usage (who); |
|
782 |
|
783 return retval; |
|
784 } |
|
785 |
|
786 DEFUN (puts, args, , |
|
787 "-*- texinfo -*-\n\ |
|
788 @deftypefn {Built-in Function} {} puts (@var{string})\n\ |
|
789 Write a string to the standard output with no formatting.\n\ |
|
790 @end deftypefn") |
|
791 { |
|
792 static std::string who = "puts"; |
|
793 |
|
794 octave_value retval = -1; |
|
795 |
|
796 if (args.length () == 1) |
|
797 retval = stdout_stream.puts (args(0), who); |
|
798 else |
|
799 print_usage (who); |
1181
|
800 |
|
801 return retval; |
|
802 } |
|
803 |
2095
|
804 DEFUN (sprintf, args, , |
3372
|
805 "-*- texinfo -*-\n\ |
|
806 @deftypefn {Built-in Function} {} sprintf (@var{template}, @dots{})\n\ |
|
807 This is like @code{printf}, except that the output is returned as a\n\ |
|
808 string. Unlike the C library function, which requires you to provide a\n\ |
|
809 suitably sized string as an argument, Octave's @code{sprintf} function\n\ |
|
810 returns the string, automatically sized to hold all of the items\n\ |
|
811 converted.\n\ |
|
812 @end deftypefn") |
1
|
813 { |
4468
|
814 static std::string who = "sprintf"; |
|
815 |
2095
|
816 octave_value_list retval; |
1
|
817 |
2095
|
818 int nargin = args.length (); |
1
|
819 |
2095
|
820 if (nargin > 0) |
1
|
821 { |
2116
|
822 retval(2) = -1.0; |
|
823 retval(1) = "unknown error"; |
|
824 retval(0) = ""; |
|
825 |
3340
|
826 octave_ostrstream *ostr = new octave_ostrstream (); |
1
|
827 |
3340
|
828 octave_stream os (ostr); |
628
|
829 |
3340
|
830 if (os.is_valid ()) |
2095
|
831 { |
|
832 if (args(0).is_string ()) |
|
833 { |
3523
|
834 std::string fmt = args(0).string_value (); |
628
|
835 |
2095
|
836 octave_value_list tmp_args; |
1
|
837 |
2095
|
838 if (nargin > 1) |
|
839 { |
|
840 tmp_args.resize (nargin-1, octave_value ()); |
1
|
841 |
2095
|
842 for (int i = 1; i < nargin; i++) |
|
843 tmp_args(i-1) = args(i); |
|
844 } |
628
|
845 |
4468
|
846 retval(2) = os.printf (fmt, tmp_args, who); |
2095
|
847 retval(1) = os.error (); |
3340
|
848 retval(0) = ostr->str (); |
2095
|
849 } |
|
850 else |
4468
|
851 ::error ("%s: format must be a string", who.c_str ()); |
2095
|
852 } |
|
853 else |
4468
|
854 ::error ("%s: unable to create output buffer", who.c_str ()); |
1
|
855 } |
|
856 else |
4468
|
857 print_usage (who); |
1
|
858 |
|
859 return retval; |
|
860 } |
|
861 |
2095
|
862 DEFUN (fscanf, args, , |
3372
|
863 "-*- texinfo -*-\n\ |
|
864 @deftypefn {Built-in Function} {[@var{val}, @var{count}] =} fscanf (@var{fid}, @var{template}, @var{size})\n\ |
3491
|
865 @deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}, @var{count}] = } fscanf (@var{fid}, @var{template}, \"C\")\n\ |
3372
|
866 In the first form, read from @var{fid} according to @var{template},\n\ |
|
867 returning the result in the matrix @var{val}.\n\ |
2122
|
868 \n\ |
3372
|
869 The optional argument @var{size} specifies the amount of data to read\n\ |
|
870 and may be one of\n\ |
|
871 \n\ |
|
872 @table @code\n\ |
|
873 @item Inf\n\ |
|
874 Read as much as possible, returning a column vector.\n\ |
|
875 \n\ |
|
876 @item @var{nr}\n\ |
|
877 Read up to @var{nr} elements, returning a column vector.\n\ |
2122
|
878 \n\ |
3372
|
879 @item [@var{nr}, Inf]\n\ |
|
880 Read as much as possible, returning a matrix with @var{nr} rows. If the\n\ |
|
881 number of elements read is not an exact multiple of @var{nr}, the last\n\ |
|
882 column is padded with zeros.\n\ |
|
883 \n\ |
|
884 @item [@var{nr}, @var{nc}]\n\ |
|
885 Read up to @code{@var{nr} * @var{nc}} elements, returning a matrix with\n\ |
|
886 @var{nr} rows. If the number of elements read is not an exact multiple\n\ |
|
887 of @var{nr}, the last column is padded with zeros.\n\ |
|
888 @end table\n\ |
2122
|
889 \n\ |
3372
|
890 @noindent\n\ |
|
891 If @var{size} is omitted, a value of @code{Inf} is assumed.\n\ |
2122
|
892 \n\ |
3372
|
893 A string is returned if @var{template} specifies only character\n\ |
|
894 conversions.\n\ |
2215
|
895 \n\ |
3372
|
896 The number of items successfully read is returned in @var{count}.\n\ |
2215
|
897 \n\ |
3372
|
898 In the second form, read from @var{fid} according to @var{template},\n\ |
|
899 with each conversion specifier in @var{template} corresponding to a\n\ |
|
900 single scalar return value. This form is more `C-like', and also\n\ |
3491
|
901 compatible with previous versions of Octave. The number of successful\n\ |
|
902 conversions is returned in @var{count}\n\ |
3372
|
903 @end deftypefn") |
1181
|
904 { |
4468
|
905 static std::string who = "fscanf"; |
|
906 |
2095
|
907 octave_value_list retval; |
1181
|
908 |
|
909 int nargin = args.length (); |
|
910 |
2215
|
911 if (nargin == 3 && args(2).is_string ()) |
2095
|
912 { |
4468
|
913 octave_stream os = octave_stream_list::lookup (args(0), who); |
1181
|
914 |
3341
|
915 if (! error_state) |
2095
|
916 { |
|
917 if (args(1).is_string ()) |
|
918 { |
3523
|
919 std::string fmt = args(1).string_value (); |
1181
|
920 |
4468
|
921 retval = os.oscanf (fmt, who); |
2095
|
922 } |
|
923 else |
4468
|
924 ::error ("%s: format must be a string", who.c_str ()); |
2095
|
925 } |
|
926 } |
1181
|
927 else |
2215
|
928 { |
|
929 retval (1) = 0.0; |
|
930 retval (0) = Matrix (); |
|
931 |
|
932 if (nargin == 2 || nargin == 3) |
|
933 { |
4468
|
934 octave_stream os = octave_stream_list::lookup (args(0), who); |
2215
|
935 |
3342
|
936 if (! error_state) |
2215
|
937 { |
|
938 if (args(1).is_string ()) |
|
939 { |
3523
|
940 std::string fmt = args(1).string_value (); |
2215
|
941 |
|
942 int count = 0; |
|
943 |
3810
|
944 Array<double> size = (nargin == 3) |
4102
|
945 ? args(2).vector_value () |
|
946 : Array<double> (1, lo_ieee_inf_value ()); |
2215
|
947 |
|
948 if (! error_state) |
|
949 { |
4468
|
950 octave_value tmp = os.scanf (fmt, size, count, who); |
2215
|
951 |
4233
|
952 retval(1) = count; |
2215
|
953 retval(0) = tmp; |
|
954 } |
|
955 } |
|
956 else |
4468
|
957 ::error ("%s: format must be a string", who.c_str ()); |
2215
|
958 } |
|
959 } |
|
960 else |
4468
|
961 print_usage (who); |
2215
|
962 } |
1181
|
963 |
|
964 return retval; |
|
965 } |
|
966 |
2095
|
967 DEFUN (sscanf, args, , |
3372
|
968 "-*- texinfo -*-\n\ |
|
969 @deftypefn {Built-in Function} {[@var{val}, @var{count}] =} sscanf (@var{string}, @var{template}, @var{size})\n\ |
3491
|
970 @deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}, @var{count}] = } sscanf (@var{string}, @var{template}, \"C\")\n\ |
3372
|
971 This is like @code{fscanf}, except that the characters are taken from the\n\ |
|
972 string @var{string} instead of from a stream. Reaching the end of the\n\ |
|
973 string is treated as an end-of-file condition.\n\ |
|
974 @end deftypefn") |
444
|
975 { |
4468
|
976 static std::string who = "sscanf"; |
|
977 |
2095
|
978 octave_value_list retval; |
444
|
979 |
506
|
980 int nargin = args.length (); |
|
981 |
2215
|
982 if (nargin == 3 && args(2).is_string ()) |
2095
|
983 { |
|
984 if (args(0).is_string ()) |
|
985 { |
3523
|
986 std::string data = args(0).string_value (); |
444
|
987 |
3340
|
988 octave_stream os = octave_istrstream::create (data); |
1358
|
989 |
3340
|
990 if (os.is_valid ()) |
2095
|
991 { |
|
992 if (args(1).is_string ()) |
|
993 { |
3523
|
994 std::string fmt = args(1).string_value (); |
444
|
995 |
4468
|
996 retval = os.oscanf (fmt, who); |
2095
|
997 } |
|
998 else |
4468
|
999 ::error ("%s: format must be a string", who.c_str ()); |
2095
|
1000 } |
|
1001 else |
4468
|
1002 ::error ("%s: unable to create temporary input buffer", |
|
1003 who.c_str ()); |
444
|
1004 } |
636
|
1005 else |
4468
|
1006 ::error ("%s: first argument must be a string", who.c_str ()); |
444
|
1007 } |
|
1008 else |
2215
|
1009 { |
|
1010 if (nargin == 2 || nargin == 3) |
|
1011 { |
|
1012 retval(3) = -1.0; |
|
1013 retval(2) = "unknown error"; |
|
1014 retval(1) = 0.0; |
|
1015 retval(0) = Matrix (); |
|
1016 |
|
1017 if (args(0).is_string ()) |
|
1018 { |
3523
|
1019 std::string data = args(0).string_value (); |
2215
|
1020 |
3340
|
1021 octave_stream os = octave_istrstream::create (data); |
2215
|
1022 |
3340
|
1023 if (os.is_valid ()) |
2215
|
1024 { |
|
1025 if (args(1).is_string ()) |
|
1026 { |
3523
|
1027 std::string fmt = args(1).string_value (); |
2215
|
1028 |
|
1029 int count = 0; |
|
1030 |
3810
|
1031 Array<double> size = (nargin == 3) |
|
1032 ? args(2).vector_value () |
4102
|
1033 : Array<double> (1, lo_ieee_inf_value ()); |
2215
|
1034 |
4468
|
1035 octave_value tmp = os.scanf (fmt, size, count, who); |
2215
|
1036 |
|
1037 // XXX FIXME XXX -- is this the right thing to do? |
|
1038 // Extract error message first, because getting |
|
1039 // position will clear it. |
3523
|
1040 std::string errmsg = os.error (); |
2215
|
1041 |
4254
|
1042 retval(3) = os.tell () + 1; |
2215
|
1043 retval(2) = errmsg; |
4233
|
1044 retval(1) = count; |
2215
|
1045 retval(0) = tmp; |
|
1046 } |
|
1047 else |
4468
|
1048 ::error ("%s: format must be a string", who.c_str ()); |
2215
|
1049 } |
|
1050 else |
4468
|
1051 ::error ("%s: unable to create temporary input buffer", |
|
1052 who.c_str ()); |
2215
|
1053 } |
|
1054 else |
4468
|
1055 ::error ("%s: first argument must be a string", who.c_str ()); |
2215
|
1056 } |
|
1057 else |
4468
|
1058 print_usage (who); |
2215
|
1059 } |
444
|
1060 |
|
1061 return retval; |
|
1062 } |
|
1063 |
2215
|
1064 DEFUN (scanf, args, nargout, |
3372
|
1065 "-*- texinfo -*-\n\ |
|
1066 @deftypefn {Built-in Function} {[@var{val}, @var{count}] =} scanf (@var{template}, @var{size})\n\ |
3491
|
1067 @deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}, @var{count}]] = } scanf (@var{template}, \"C\")\n\ |
3372
|
1068 This is equivalent to calling @code{fscanf} with @var{fid} = @code{stdin}.\n\ |
|
1069 \n\ |
|
1070 It is currently not useful to call @code{scanf} in interactive\n\ |
|
1071 programs.\n\ |
|
1072 @end deftypefn") |
2215
|
1073 { |
|
1074 int nargin = args.length (); |
|
1075 |
|
1076 octave_value_list tmp_args (nargin+1, octave_value ()); |
|
1077 |
|
1078 tmp_args (0) = 0.0; |
|
1079 for (int i = 0; i < nargin; i++) |
|
1080 tmp_args (i+1) = args (i); |
|
1081 |
|
1082 return Ffscanf (tmp_args, nargout); |
|
1083 } |
|
1084 |
2116
|
1085 static octave_value |
|
1086 do_fread (octave_stream& os, const octave_value& size_arg, |
|
1087 const octave_value& prec_arg, const octave_value& skip_arg, |
|
1088 const octave_value& arch_arg, int& count) |
|
1089 { |
|
1090 octave_value retval; |
|
1091 |
|
1092 count = -1; |
|
1093 |
3810
|
1094 Array<double> size = size_arg.vector_value (); |
2116
|
1095 |
|
1096 if (! error_state) |
|
1097 { |
3523
|
1098 std::string prec = prec_arg.string_value (); |
2116
|
1099 |
|
1100 if (! error_state) |
|
1101 { |
2318
|
1102 oct_data_conv::data_type dt |
|
1103 = oct_data_conv::string_to_data_type (prec); |
2116
|
1104 |
|
1105 if (! error_state) |
|
1106 { |
3202
|
1107 int skip = skip_arg.int_value (true); |
2116
|
1108 |
|
1109 if (! error_state) |
|
1110 { |
3523
|
1111 std::string arch = arch_arg.string_value (); |
3202
|
1112 |
|
1113 if (! error_state) |
2116
|
1114 { |
3202
|
1115 oct_mach_info::float_format flt_fmt |
|
1116 = oct_mach_info::string_to_float_format (arch); |
2116
|
1117 |
|
1118 if (! error_state) |
3202
|
1119 retval = os.read (size, dt, skip, flt_fmt, count); |
2116
|
1120 } |
|
1121 else |
3202
|
1122 ::error ("fread: architecture type must be a string"); |
2116
|
1123 } |
|
1124 else |
3202
|
1125 ::error ("fread: skip must be an integer"); |
2116
|
1126 } |
|
1127 else |
|
1128 ::error ("fread: invalid data type specified"); |
|
1129 } |
|
1130 else |
|
1131 ::error ("fread: precision must be a string"); |
|
1132 } |
|
1133 else |
|
1134 ::error ("fread: invalid size specified"); |
|
1135 |
|
1136 return retval; |
|
1137 } |
|
1138 |
|
1139 DEFUN (fread, args, , |
3372
|
1140 "-*- texinfo -*-\n\ |
|
1141 @deftypefn {Built-in Function} {[@var{val}, @var{count}] =} fread (@var{fid}, @var{size}, @var{precision}, @var{skip}, @var{arch})\n\ |
|
1142 Read binary data of type @var{precision} from the specified file ID\n\ |
|
1143 @var{fid}.\n\ |
|
1144 \n\ |
|
1145 The optional argument @var{size} specifies the amount of data to read\n\ |
|
1146 and may be one of\n\ |
|
1147 \n\ |
|
1148 @table @code\n\ |
|
1149 @item Inf\n\ |
|
1150 Read as much as possible, returning a column vector.\n\ |
529
|
1151 \n\ |
3372
|
1152 @item @var{nr}\n\ |
|
1153 Read up to @var{nr} elements, returning a column vector.\n\ |
|
1154 \n\ |
|
1155 @item [@var{nr}, Inf]\n\ |
|
1156 Read as much as possible, returning a matrix with @var{nr} rows. If the\n\ |
|
1157 number of elements read is not an exact multiple of @var{nr}, the last\n\ |
|
1158 column is padded with zeros.\n\ |
|
1159 \n\ |
|
1160 @item [@var{nr}, @var{nc}]\n\ |
|
1161 Read up to @code{@var{nr} * @var{nc}} elements, returning a matrix with\n\ |
|
1162 @var{nr} rows. If the number of elements read is not an exact multiple\n\ |
|
1163 of @var{nr}, the last column is padded with zeros.\n\ |
|
1164 @end table\n\ |
|
1165 \n\ |
|
1166 @noindent\n\ |
|
1167 If @var{size} is omitted, a value of @code{Inf} is assumed.\n\ |
2318
|
1168 \n\ |
3372
|
1169 The optional argument @var{precision} is a string specifying the type of\n\ |
|
1170 data to read and may be one of\n\ |
|
1171 \n\ |
|
1172 @table @code\n\ |
|
1173 @item \"char\"\n\ |
|
1174 @itemx \"char*1\"\n\ |
|
1175 @itemx \"integer*1\"\n\ |
|
1176 @itemx \"int8\"\n\ |
|
1177 Single character.\n\ |
|
1178 \n\ |
|
1179 @item \"signed char\"\n\ |
|
1180 @itemx \"schar\"\n\ |
|
1181 Signed character.\n\ |
529
|
1182 \n\ |
3372
|
1183 @item \"unsigned char\"\n\ |
|
1184 @itemx \"uchar\"\n\ |
|
1185 Unsigned character.\n\ |
|
1186 \n\ |
|
1187 @item \"short\"\n\ |
|
1188 Short integer.\n\ |
2318
|
1189 \n\ |
3372
|
1190 @item \"unsigned short\"\n\ |
|
1191 @itemx \"ushort\"\n\ |
|
1192 Unsigned short integer.\n\ |
|
1193 \n\ |
|
1194 @item \"int\"\n\ |
|
1195 Integer.\n\ |
|
1196 \n\ |
|
1197 @item \"unsigned int\"\n\ |
|
1198 @itemx \"uint\"\n\ |
|
1199 Unsigned integer.\n\ |
529
|
1200 \n\ |
3372
|
1201 @item \"long\"\n\ |
|
1202 Long integer.\n\ |
|
1203 \n\ |
|
1204 @item \"unsigned long\"\n\ |
|
1205 @itemx \"ulong\"\n\ |
|
1206 Unsigned long integer.\n\ |
|
1207 \n\ |
|
1208 @item \"float\"\n\ |
|
1209 @itemx \"float32\"\n\ |
|
1210 @itemx \"real*4\"\n\ |
|
1211 Single precision float.\n\ |
|
1212 \n\ |
|
1213 @item \"double\"\n\ |
|
1214 @itemx \"float64\"\n\ |
|
1215 @itemx \"real*8\"\n\ |
|
1216 Double precision float.\n\ |
|
1217 \n\ |
|
1218 @item \"integer*2\"\n\ |
|
1219 @itemx \"int16\"\n\ |
|
1220 Two byte integer.\n\ |
|
1221 \n\ |
|
1222 @item \"integer*4\"\n\ |
|
1223 @itemx \"int32\"\n\ |
|
1224 Four byte integer.\n\ |
|
1225 @end table\n\ |
|
1226 \n\ |
|
1227 @noindent\n\ |
|
1228 The default precision is @code{\"uchar\"}.\n\ |
2318
|
1229 \n\ |
3372
|
1230 The optional argument @var{skip} specifies the number of bytes to skip\n\ |
4101
|
1231 after each element is read. If it is not specified, a value of 0 is\n\ |
3372
|
1232 assumed.\n\ |
|
1233 \n\ |
|
1234 The optional argument @var{arch} is a string specifying the data format\n\ |
|
1235 for the file. Valid values are\n\ |
529
|
1236 \n\ |
3372
|
1237 @table @code\n\ |
|
1238 @item \"native\"\n\ |
|
1239 The format of the current machine.\n\ |
|
1240 \n\ |
4546
|
1241 @item \"ieee-be\"\n\ |
3372
|
1242 IEEE big endian.\n\ |
|
1243 \n\ |
4546
|
1244 @item \"ieee-le\"\n\ |
3372
|
1245 IEEE little endian.\n\ |
2318
|
1246 \n\ |
3372
|
1247 @item \"vaxd\"\n\ |
|
1248 VAX D floating format.\n\ |
|
1249 \n\ |
|
1250 @item \"vaxg\"\n\ |
|
1251 VAX G floating format.\n\ |
2318
|
1252 \n\ |
3372
|
1253 @item \"cray\"\n\ |
|
1254 Cray floating format.\n\ |
|
1255 @end table\n\ |
2318
|
1256 \n\ |
3372
|
1257 @noindent\n\ |
|
1258 Conversions are currently only supported for @code{\"ieee-be\"} and\n\ |
|
1259 @code{\"ieee-le\"} formats.\n\ |
2318
|
1260 \n\ |
3372
|
1261 The data read from the file is returned in @var{val}, and the number of\n\ |
|
1262 values read is returned in @code{count}\n\ |
|
1263 @end deftypefn") |
529
|
1264 { |
2095
|
1265 octave_value_list retval; |
2116
|
1266 |
|
1267 int nargin = args.length (); |
|
1268 |
2318
|
1269 if (nargin > 0 && nargin < 6) |
2116
|
1270 { |
|
1271 retval(1) = -1.0; |
|
1272 retval(0) = Matrix (); |
|
1273 |
3341
|
1274 octave_stream os = octave_stream_list::lookup (args(0), "fread"); |
2116
|
1275 |
3341
|
1276 if (! error_state) |
2116
|
1277 { |
4257
|
1278 octave_value size = lo_ieee_inf_value (); |
|
1279 octave_value prec = "uchar"; |
|
1280 octave_value skip = 0; |
|
1281 octave_value arch = "unknown"; |
2116
|
1282 |
4257
|
1283 int idx = 1; |
2116
|
1284 |
4257
|
1285 if (nargin > 1 && ! args(idx).is_string ()) |
|
1286 size = args(idx++); |
|
1287 |
|
1288 if (nargin > idx) |
|
1289 prec = args(idx++); |
2116
|
1290 |
4257
|
1291 if (nargin > idx) |
|
1292 skip = args(idx++); |
|
1293 |
|
1294 if (nargin > idx) |
|
1295 arch = args(idx++); |
2116
|
1296 |
|
1297 int count = -1; |
|
1298 |
3340
|
1299 octave_value tmp = do_fread (os, size, prec, skip, arch, count); |
2116
|
1300 |
4233
|
1301 retval(1) = count; |
2116
|
1302 retval(0) = tmp; |
|
1303 } |
|
1304 } |
|
1305 else |
|
1306 print_usage ("fread"); |
|
1307 |
529
|
1308 return retval; |
|
1309 } |
|
1310 |
2116
|
1311 static int |
|
1312 do_fwrite (octave_stream& os, const octave_value& data, |
|
1313 const octave_value& prec_arg, const octave_value& skip_arg, |
|
1314 const octave_value& arch_arg) |
|
1315 { |
|
1316 int retval = -1; |
|
1317 |
3523
|
1318 std::string prec = prec_arg.string_value (); |
2116
|
1319 |
|
1320 if (! error_state) |
|
1321 { |
2318
|
1322 oct_data_conv::data_type dt |
|
1323 = oct_data_conv::string_to_data_type (prec); |
2116
|
1324 |
|
1325 if (! error_state) |
|
1326 { |
3202
|
1327 int skip = skip_arg.int_value (true); |
2116
|
1328 |
|
1329 if (! error_state) |
|
1330 { |
3523
|
1331 std::string arch = arch_arg.string_value (); |
3202
|
1332 |
|
1333 if (! error_state) |
2116
|
1334 { |
3202
|
1335 oct_mach_info::float_format flt_fmt |
|
1336 = oct_mach_info::string_to_float_format (arch); |
2116
|
1337 |
|
1338 if (! error_state) |
3202
|
1339 retval = os.write (data, dt, skip, flt_fmt); |
2116
|
1340 } |
|
1341 else |
3202
|
1342 ::error ("fwrite: architecture type must be a string"); |
2116
|
1343 } |
|
1344 else |
3202
|
1345 ::error ("fwrite: skip must be an integer"); |
2116
|
1346 } |
3202
|
1347 else |
|
1348 ::error ("fwrite: invalid precision specified"); |
2116
|
1349 } |
|
1350 else |
|
1351 ::error ("fwrite: precision must be a string"); |
|
1352 |
|
1353 return retval; |
|
1354 } |
|
1355 |
|
1356 DEFUN (fwrite, args, , |
3372
|
1357 "-*- texinfo -*-\n\ |
|
1358 @deftypefn {Built-in Function} {@var{count} =} fwrite (@var{fid}, @var{data}, @var{precision}, @var{skip}, @var{arch})\n\ |
|
1359 Write data in binary form of type @var{precision} to the specified file\n\ |
|
1360 ID @var{fid}, returning the number of values successfully written to the\n\ |
|
1361 file.\n\ |
1181
|
1362 \n\ |
3372
|
1363 The argument @var{data} is a matrix of values that are to be written to\n\ |
|
1364 the file. The values are extracted in column-major order.\n\ |
1181
|
1365 \n\ |
3372
|
1366 The remaining arguments @var{precision}, @var{skip}, and @var{arch} are\n\ |
|
1367 optional, and are interpreted as described for @code{fread}.\n\ |
1181
|
1368 \n\ |
3372
|
1369 The behavior of @code{fwrite} is undefined if the values in @var{data}\n\ |
|
1370 are too large to fit in the specified precision.\n\ |
|
1371 @end deftypefn") |
1181
|
1372 { |
4233
|
1373 octave_value retval = -1; |
2116
|
1374 |
|
1375 int nargin = args.length (); |
|
1376 |
|
1377 if (nargin > 1 && nargin < 6) |
|
1378 { |
3341
|
1379 octave_stream os = octave_stream_list::lookup (args(0), "fwrite"); |
2116
|
1380 |
3341
|
1381 if (! error_state) |
2116
|
1382 { |
|
1383 octave_value data = args(1); |
2318
|
1384 |
|
1385 octave_value prec = (nargin > 2) |
|
1386 ? args(2) : octave_value ("uchar"); |
|
1387 |
|
1388 octave_value skip = (nargin > 3) |
|
1389 ? args(3) : octave_value (0.0); |
|
1390 |
|
1391 octave_value arch = (nargin > 4) |
|
1392 ? args(4) : octave_value ("unknown"); |
2116
|
1393 |
3340
|
1394 double status = do_fwrite (os, data, prec, skip, arch); |
2825
|
1395 |
|
1396 retval = status; |
2116
|
1397 } |
|
1398 } |
|
1399 else |
|
1400 print_usage ("fwrite"); |
|
1401 |
1181
|
1402 return retval; |
|
1403 } |
|
1404 |
1957
|
1405 DEFUN (feof, args, , |
3372
|
1406 "-*- texinfo -*-\n\ |
|
1407 @deftypefn {Built-in Function} {} feof (@var{fid})\n\ |
|
1408 Return 1 if an end-of-file condition has been encountered for a given\n\ |
|
1409 file and 0 otherwise. Note that it will only return 1 if the end of the\n\ |
|
1410 file has already been encountered, not if the next read operation will\n\ |
|
1411 result in an end-of-file condition.\n\ |
|
1412 @end deftypefn") |
529
|
1413 { |
4233
|
1414 octave_value retval = -1; |
529
|
1415 |
|
1416 int nargin = args.length (); |
|
1417 |
2095
|
1418 if (nargin == 1) |
|
1419 { |
3341
|
1420 octave_stream os = octave_stream_list::lookup (args(0), "feof"); |
444
|
1421 |
3341
|
1422 if (! error_state) |
3340
|
1423 retval = os.eof () ? 1.0 : 0.0; |
2095
|
1424 } |
529
|
1425 else |
2095
|
1426 print_usage ("feof"); |
444
|
1427 |
|
1428 return retval; |
|
1429 } |
|
1430 |
2095
|
1431 DEFUN (ferror, args, , |
3372
|
1432 "-*- texinfo -*-\n\ |
|
1433 @deftypefn {Built-in Function} {} ferror (@var{fid})\n\ |
|
1434 Return 1 if an error condition has been encountered for a given file\n\ |
|
1435 and 0 otherwise. Note that it will only return 1 if an error has\n\ |
|
1436 already been encountered, not if the next operation will result in an\n\ |
|
1437 error condition.\n\ |
|
1438 @end deftypefn") |
1230
|
1439 { |
2095
|
1440 octave_value_list retval; |
1230
|
1441 |
2095
|
1442 int nargin = args.length (); |
1230
|
1443 |
2095
|
1444 if (nargin == 1 || nargin == 2) |
|
1445 { |
3341
|
1446 octave_stream os = octave_stream_list::lookup (args(0), "ferror"); |
1230
|
1447 |
3341
|
1448 if (! error_state) |
2095
|
1449 { |
|
1450 bool clear = false; |
1230
|
1451 |
2095
|
1452 if (nargin == 2) |
|
1453 { |
3523
|
1454 std::string opt = args(1).string_value (); |
2095
|
1455 |
|
1456 if (! error_state) |
|
1457 clear = (opt == "clear"); |
|
1458 else |
|
1459 return retval; |
|
1460 } |
1755
|
1461 |
2095
|
1462 int error_number = 0; |
|
1463 |
3523
|
1464 std::string error_message = os.error (clear, error_number); |
1230
|
1465 |
4233
|
1466 retval(1) = error_number; |
2095
|
1467 retval(0) = error_message; |
|
1468 } |
1230
|
1469 } |
2095
|
1470 else |
|
1471 print_usage ("ferror"); |
1230
|
1472 |
|
1473 return retval; |
|
1474 } |
|
1475 |
1957
|
1476 DEFUN (popen, args, , |
3301
|
1477 "-*- texinfo -*-\n\ |
4326
|
1478 @deftypefn {Built-in Function} {@var{fid} =} popen (@var{command}, @var{mode})\n\ |
3301
|
1479 Start a process and create a pipe. The name of the command to run is\n\ |
|
1480 given by @var{command}. The file identifier corresponding to the input\n\ |
|
1481 or output stream of the process is returned in @var{fid}. The argument\n\ |
|
1482 @var{mode} may be\n\ |
|
1483 \n\ |
|
1484 @table @code\n\ |
|
1485 @item \"r\"\n\ |
|
1486 The pipe will be connected to the standard output of the process, and\n\ |
|
1487 open for reading.\n\ |
1230
|
1488 \n\ |
3301
|
1489 @item \"w\"\n\ |
|
1490 The pipe will be connected to the standard input of the process, and\n\ |
|
1491 open for writing.\n\ |
|
1492 @end table\n\ |
|
1493 \n\ |
|
1494 For example,\n\ |
1230
|
1495 \n\ |
3301
|
1496 @example\n\ |
|
1497 @group\n\ |
|
1498 fid = popen (\"ls -ltr / | tail -3\", \"r\");\n\ |
|
1499 while (isstr (s = fgets (fid)))\n\ |
|
1500 fputs (stdout, s);\n\ |
|
1501 endwhile\n\ |
|
1502 @print{} drwxr-xr-x 33 root root 3072 Feb 15 13:28 etc\n\ |
|
1503 @print{} drwxr-xr-x 3 root root 1024 Feb 15 13:28 lib\n\ |
|
1504 @print{} drwxrwxrwt 15 root root 2048 Feb 17 14:53 tmp\n\ |
|
1505 @end group\n\ |
|
1506 @end example\n\ |
|
1507 @end deftypefn") |
1230
|
1508 { |
4233
|
1509 octave_value retval = -1; |
1230
|
1510 |
|
1511 int nargin = args.length (); |
|
1512 |
2095
|
1513 if (nargin == 2) |
|
1514 { |
3523
|
1515 std::string name = args(0).string_value (); |
1230
|
1516 |
2095
|
1517 if (! error_state) |
|
1518 { |
3523
|
1519 std::string mode = args(1).string_value (); |
1230
|
1520 |
2095
|
1521 if (! error_state) |
|
1522 { |
|
1523 if (mode == "r") |
|
1524 { |
3340
|
1525 octave_stream ips = octave_iprocstream::create (name); |
1230
|
1526 |
2095
|
1527 retval = octave_stream_list::insert (ips); |
|
1528 } |
|
1529 else if (mode == "w") |
|
1530 { |
3340
|
1531 octave_stream ops = octave_oprocstream::create (name); |
1230
|
1532 |
2095
|
1533 retval = octave_stream_list::insert (ops); |
|
1534 } |
|
1535 else |
|
1536 ::error ("popen: invalid mode specified"); |
|
1537 } |
|
1538 else |
|
1539 ::error ("popen: mode must be a string"); |
|
1540 } |
|
1541 else |
|
1542 ::error ("popen: name must be a string"); |
|
1543 } |
1230
|
1544 else |
2095
|
1545 print_usage ("popen"); |
1230
|
1546 |
|
1547 return retval; |
|
1548 } |
|
1549 |
1957
|
1550 DEFUN (pclose, args, , |
3381
|
1551 "-*- texinfo -*-\n\ |
3301
|
1552 @deftypefn {Built-in Function} {} pclose (@var{fid})\n\ |
|
1553 Close a file identifier that was opened by @code{popen}. You may also\n\ |
|
1554 use @code{fclose} for the same purpose.\n\ |
|
1555 @end deftypefn") |
1230
|
1556 { |
4233
|
1557 octave_value retval = -1; |
1230
|
1558 |
|
1559 int nargin = args.length (); |
|
1560 |
2095
|
1561 if (nargin == 1) |
4233
|
1562 retval = octave_stream_list::remove (args(0), "pclose"); |
1377
|
1563 else |
2095
|
1564 print_usage ("pclose"); |
1379
|
1565 |
|
1566 return retval; |
|
1567 } |
|
1568 |
2458
|
1569 DEFUN (tmpnam, args, , |
3372
|
1570 "-*- texinfo -*-\n\ |
4267
|
1571 @deftypefn {Built-in Function} {} tmpnam (@var{dir}, @var{prefix})\n\ |
3372
|
1572 Return a unique temporary file name as a string.\n\ |
|
1573 \n\ |
4267
|
1574 If @var{prefix} is omitted, a value of @code{\"oct-\"} is used.\n\ |
|
1575 If @var{dir} is also omitted, the default directory for temporary files\n\ |
|
1576 is used. If @var{dir} is provided, it must exist, otherwise the default\n\ |
|
1577 directory for temporary files is used. Since the named file is not\n\ |
|
1578 opened, by @code{tmpnam}, it is possible (though relatively unlikely)\n\ |
|
1579 that it will not be available by the time your program attempts to open it.\n\ |
4326
|
1580 @end deftypefn\n\ |
|
1581 @seealso{tmpfile, mkstemp, and P_tmpdir}") |
1802
|
1582 { |
2095
|
1583 octave_value retval; |
1802
|
1584 |
2936
|
1585 int len = args.length (); |
|
1586 |
|
1587 if (len < 3) |
|
1588 { |
3523
|
1589 std::string dir = len > 0 ? args(0).string_value () : std::string (); |
2936
|
1590 |
|
1591 if (! error_state) |
4267
|
1592 { |
|
1593 std::string pfx |
|
1594 = len > 1 ? args(1).string_value () : std::string ("oct-"); |
|
1595 |
|
1596 if (! error_state) |
|
1597 retval = file_ops::tempnam (dir, pfx); |
|
1598 else |
|
1599 ::error ("expecting second argument to be a string"); |
|
1600 } |
|
1601 else |
|
1602 ::error ("expecting first argument to be a string"); |
2936
|
1603 } |
1802
|
1604 else |
2458
|
1605 print_usage ("tmpnam"); |
1802
|
1606 |
|
1607 return retval; |
|
1608 } |
|
1609 |
2458
|
1610 DEFALIAS (octave_tmp_file_name, tmpnam); |
|
1611 |
4326
|
1612 DEFUN (tmpfile, args, , |
|
1613 "-*- texinfo -*-\n\ |
|
1614 @deftypefn {Built-in Function} {[@var{fid}, @var{msg}] =} tmpfile ()\n\ |
|
1615 Return the file ID corresponding to a new temporary file with a unique\n\ |
|
1616 name. The file is opened in binary read/write (@code{\"w+b\"}) mode.\n\ |
|
1617 The file will be deleted automatically when it is closed or when Octave\n\ |
|
1618 exits.\n\ |
|
1619 \n\ |
|
1620 If successful, @var{fid} is a valid file ID and @var{msg} is an empty\n\ |
|
1621 string. Otherwise, @var{fid} is -1 and @var{msg} contains a\n\ |
|
1622 system-dependent error message.\n\ |
|
1623 @end deftypefn\n\ |
|
1624 @seealso{tmpnam, mkstemp, and P_tmpdir}") |
|
1625 { |
|
1626 octave_value_list retval; |
|
1627 |
|
1628 retval(1) = std::string (); |
|
1629 retval(0) = -1; |
|
1630 |
|
1631 int nargin = args.length (); |
|
1632 |
|
1633 if (nargin == 0) |
|
1634 { |
|
1635 FILE *fid = tmpfile (); |
|
1636 |
|
1637 if (fid) |
|
1638 { |
|
1639 std::string nm; |
|
1640 |
|
1641 std::ios::openmode md = fopen_mode_to_ios_mode ("w+b"); |
|
1642 |
4327
|
1643 octave_stream s = octave_stdiostream::create (nm, fid, md); |
4326
|
1644 |
|
1645 if (s) |
|
1646 retval(0) = octave_stream_list::insert (s); |
|
1647 else |
4327
|
1648 error ("tmpfile: failed to create octave_stdiostream object"); |
4326
|
1649 |
|
1650 } |
|
1651 else |
|
1652 { |
|
1653 using namespace std; |
|
1654 retval(1) = ::strerror (errno); |
|
1655 retval(0) = -1; |
|
1656 } |
|
1657 } |
|
1658 else |
|
1659 print_usage ("tmpfile"); |
|
1660 |
|
1661 return retval; |
|
1662 } |
|
1663 |
|
1664 DEFUN (mkstemp, args, , |
|
1665 "-*- texinfo -*-\n\ |
|
1666 @deftypefn {Built-in Function} {[@var{fid}, @var{name}, @var{msg}] =} tmpfile (@var{template}, @var{delete})\n\ |
|
1667 Return the file ID corresponding to a new temporary file with a unique\n\ |
|
1668 name created from @var{template}. The last six characters of @var{template}\n\ |
|
1669 must be @code{XXXXXX} and tehse are replaced with a string that makes the\n\ |
|
1670 filename unique. The file is then created with mode read/write and\n\ |
|
1671 permissions that are system dependent (on GNU/Linux systems, the permissions\n\ |
|
1672 will be 0600 for versions of glibc 2.0.7 and later). The file is opened\n\ |
|
1673 with the @code{O_EXCL} flag.\n\ |
|
1674 \n\ |
|
1675 If the optional argument @var{delete} is supplied and is true,\n\ |
|
1676 the file will be deleted automatically when Octave exits, or when\n\ |
|
1677 the function @code{purge_tmp_files} is called.\n\ |
|
1678 \n\ |
|
1679 If successful, @var{fid} is a valid file ID, @var{name} is the name of\n\ |
|
1680 the file, and and @var{msg} is an empty string. Otherwise, @var{fid}\n\ |
|
1681 is -1, @var{name} is empty, and @var{msg} contains a system-dependent\n\ |
|
1682 error message.\n\ |
|
1683 @end deftypefn\n\ |
|
1684 @seealso{tmpfile, tmpnam, and P_tmpdir}") |
|
1685 { |
|
1686 octave_value_list retval; |
|
1687 |
|
1688 retval(2) = std::string (); |
|
1689 retval(1) = std::string (); |
|
1690 retval(0) = -1; |
|
1691 |
|
1692 #if defined (HAVE_MKSTEMP) |
|
1693 |
|
1694 int nargin = args.length (); |
|
1695 |
|
1696 if (nargin == 1 || nargin == 2) |
|
1697 { |
|
1698 std::string tmpl8 = args(0).string_value (); |
|
1699 |
|
1700 if (! error_state) |
|
1701 { |
4354
|
1702 OCTAVE_LOCAL_BUFFER (char, tmp, tmpl8.size () + 1); |
|
1703 strcpy (tmp, tmpl8.c_str ()); |
4326
|
1704 |
|
1705 int fd = mkstemp (tmp); |
|
1706 |
|
1707 if (fd < 0) |
|
1708 { |
|
1709 using namespace std; |
|
1710 retval(1) = ::strerror (errno); |
|
1711 retval(0) = fd; |
|
1712 } |
|
1713 else |
|
1714 { |
|
1715 const char *fopen_mode = "w+"; |
|
1716 |
|
1717 FILE *fid = fdopen (fd, fopen_mode); |
|
1718 |
|
1719 if (fid) |
|
1720 { |
|
1721 std::string nm = tmp; |
|
1722 |
|
1723 std::ios::openmode md = fopen_mode_to_ios_mode (fopen_mode); |
|
1724 |
4327
|
1725 octave_stream s = octave_stdiostream::create (nm, fid, md); |
4326
|
1726 |
|
1727 if (s) |
|
1728 { |
|
1729 retval(1) = nm; |
|
1730 retval(0) = octave_stream_list::insert (s); |
|
1731 |
|
1732 if (nargin == 2) |
|
1733 mark_for_deletion (nm); |
|
1734 } |
|
1735 else |
4327
|
1736 error ("mkstemp: failed to create octave_stdiostream object"); |
4326
|
1737 } |
|
1738 else |
|
1739 { |
|
1740 using namespace std; |
|
1741 retval(1) = ::strerror (errno); |
|
1742 retval(0) = -1; |
|
1743 } |
|
1744 } |
|
1745 } |
|
1746 else |
|
1747 error ("mkstemp: expecting string as first argument"); |
|
1748 } |
|
1749 else |
|
1750 print_usage ("mkstemp"); |
|
1751 |
|
1752 #else |
|
1753 retval(2) = "mkstemp: not supported on this sytem"; |
|
1754 #endif |
|
1755 |
|
1756 return retval; |
|
1757 } |
|
1758 |
1400
|
1759 static int |
|
1760 convert (int x, int ibase, int obase) |
|
1761 { |
|
1762 int retval = 0; |
|
1763 |
|
1764 int tmp = x % obase; |
|
1765 |
|
1766 if (tmp > ibase - 1) |
2095
|
1767 ::error ("umask: invalid digit"); |
1400
|
1768 else |
|
1769 { |
|
1770 retval = tmp; |
|
1771 int mult = ibase; |
|
1772 while ((x = (x - tmp) / obase)) |
|
1773 { |
|
1774 tmp = x % obase; |
|
1775 if (tmp > ibase - 1) |
|
1776 { |
2095
|
1777 ::error ("umask: invalid digit"); |
1400
|
1778 break; |
|
1779 } |
|
1780 retval += mult * tmp; |
|
1781 mult *= ibase; |
|
1782 } |
|
1783 } |
|
1784 |
|
1785 return retval; |
|
1786 } |
|
1787 |
1957
|
1788 DEFUN (umask, args, , |
3301
|
1789 "-*- texinfo -*-\n\ |
|
1790 @deftypefn {Built-in Function} {} umask (@var{mask})\n\ |
|
1791 Set the permission mask for file creation. The parameter @var{mask}\n\ |
|
1792 is an integer, interpreted as an octal number. If successful,\n\ |
|
1793 returns the previous value of the mask (as an integer to be\n\ |
|
1794 interpreted as an octal number); otherwise an error message is printed.\n\ |
|
1795 @end deftypefn") |
1400
|
1796 { |
2095
|
1797 octave_value_list retval; |
1400
|
1798 |
|
1799 int status = 0; |
|
1800 |
|
1801 if (args.length () == 1) |
|
1802 { |
3202
|
1803 int mask = args(0).int_value (true); |
1400
|
1804 |
3202
|
1805 if (! error_state) |
1400
|
1806 { |
3202
|
1807 if (mask < 0) |
1400
|
1808 { |
|
1809 status = -1; |
2095
|
1810 ::error ("umask: MASK must be a positive integer value"); |
1400
|
1811 } |
|
1812 else |
|
1813 { |
|
1814 int oct_mask = convert (mask, 8, 10); |
|
1815 |
|
1816 if (! error_state) |
2926
|
1817 status = convert (file_ops::umask (oct_mask), 10, 8); |
1400
|
1818 } |
|
1819 } |
3202
|
1820 else |
|
1821 { |
|
1822 status = -1; |
|
1823 ::error ("umask: expecting integer argument"); |
|
1824 } |
1400
|
1825 } |
|
1826 else |
|
1827 print_usage ("umask"); |
|
1828 |
|
1829 if (status >= 0) |
4233
|
1830 retval(0) = status; |
1400
|
1831 |
|
1832 return retval; |
|
1833 } |
|
1834 |
2189
|
1835 void |
|
1836 symbols_of_file_io (void) |
|
1837 { |
4267
|
1838 #if ! defined (P_tmpdir) |
|
1839 #define P_tmpdir "/tmp" |
|
1840 #endif |
|
1841 |
|
1842 DEFCONSTX ("P_tmpdir", SBV_P_tmpdir, P_tmpdir, |
|
1843 "-*- texinfo -*-\n\ |
|
1844 @defvr {Built-in Variable} P_tmpdir\n\ |
|
1845 The default name of the directory for temporary files on this system.\n\ |
|
1846 of this variable is system dependent.\n\ |
|
1847 @end defvr"); |
|
1848 |
2341
|
1849 // NOTE: the values of SEEK_SET, SEEK_CUR, and SEEK_END have to be |
|
1850 // this way for Matlab compatibility. |
|
1851 |
4233
|
1852 DEFCONSTX ("SEEK_SET", SBV_SEEK_SET, -1, |
3372
|
1853 "-*- texinfo -*-\n\ |
|
1854 @defvr {Built-in Variable} SEEK_SET\n\ |
|
1855 @defvrx {Built-in Variable} SEEK_CUR\n\ |
|
1856 @defvrx {Built-in Variable} SEEK_END\n\ |
|
1857 These variables may be used as the optional third argument for the\n\ |
|
1858 function @code{fseek}.\n\ |
|
1859 \n\ |
|
1860 @table @code\n\ |
|
1861 @item SEEK_SET\n\ |
|
1862 Position file relative to the beginning.\n\ |
|
1863 \n\ |
|
1864 @item SEEK_CUR\n\ |
|
1865 Position file relative to the current position.\n\ |
|
1866 \n\ |
|
1867 @item SEEK_END\n\ |
|
1868 used with fseek to position file relative to the end.\n\ |
|
1869 @end table\n\ |
|
1870 @end defvr"); |
2189
|
1871 |
4233
|
1872 DEFCONSTX ("SEEK_CUR", SBV_SEEK_CUR, 0, |
3458
|
1873 "-*- texinfo -*-\n\ |
|
1874 @defvr {Built-in Variable} SEEK_CUR\n\ |
|
1875 See SEEK_SET.\n\ |
|
1876 @end defvr"); |
2189
|
1877 |
4233
|
1878 DEFCONSTX ("SEEK_END", SBV_SEEK_END, 1, |
3458
|
1879 "-*- texinfo -*-\n\ |
|
1880 @defvr {Built-in Variable} SEEK_END\n\ |
|
1881 See SEEK_SET.\n\ |
|
1882 @end defvr"); |
2189
|
1883 |
3141
|
1884 DEFCONSTX ("stdin", SBV_stdin, stdin_file, |
3372
|
1885 "-*- texinfo -*-\n\ |
|
1886 @defvr {Built-in Variable} stdin\n\ |
|
1887 The standard input stream (file id 0). When Octave is used\n\ |
|
1888 interactively, this is filtered through the command line editing\n\ |
|
1889 functions.\n\ |
|
1890 @end defvr"); |
2189
|
1891 |
3141
|
1892 DEFCONSTX ("stdout", SBV_stdout, stdout_file, |
3372
|
1893 "-*- texinfo -*-\n\ |
|
1894 @defvr {Built-in Variable} stdout\n\ |
|
1895 The standard output stream (file id 1). Data written to the\n\ |
|
1896 standard output is normally filtered through the pager.\n\ |
|
1897 @end defvr"); |
2189
|
1898 |
3141
|
1899 DEFCONSTX ("stderr", SBV_stderr, stderr_file, |
3372
|
1900 "-*- texinfo -*-\n\ |
|
1901 @defvr {Built-in Variable} stderr\n\ |
|
1902 The standard error stream (file id 2). Even if paging is turned on,\n\ |
|
1903 the standard error is not sent to the pager. It is useful for error\n\ |
|
1904 messages and prompts.\n\ |
|
1905 @end defvr"); |
|
1906 |
2189
|
1907 } |
|
1908 |
444
|
1909 /* |
1
|
1910 ;;; Local Variables: *** |
|
1911 ;;; mode: C++ *** |
|
1912 ;;; End: *** |
|
1913 */ |