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