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