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