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