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 |
2095
|
43 #include <iostream.h> |
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 { |
3340
|
76 octave_stream stdin_stream = octave_istream::create (&cin, "stdin"); |
2116
|
77 |
|
78 // This uses octave_stdout (see pager.h), not cout so that Octave's |
|
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 |
3340
|
84 octave_stream stderr_stream = octave_ostream::create (&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 |
|
98 fopen_mode_to_ios_mode (const 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") |
|
107 retval = ios::in; |
|
108 else if (mode == "w") |
|
109 retval = ios::out | ios::trunc; |
|
110 else if (mode == "a") |
|
111 retval = ios::out | ios::app; |
|
112 else if (mode == "r+") |
|
113 retval = ios::in | ios::out; |
|
114 else if (mode == "w+") |
|
115 retval = ios::in | ios::out | ios::trunc; |
|
116 else if (mode == "a+") |
|
117 retval = ios::in | ios::out | ios::app; |
|
118 else if (mode == "rb") |
|
119 retval = ios::in | ios::bin; |
|
120 else if (mode == "wb") |
|
121 retval = ios::out | ios::trunc | ios::bin; |
|
122 else if (mode == "ab") |
|
123 retval = ios::out | ios::app | ios::bin; |
|
124 else if (mode == "r+b") |
|
125 retval = ios::in | ios::out | ios::bin; |
|
126 else if (mode == "w+b") |
|
127 retval = ios::in | ios::out | ios::trunc | ios::bin; |
|
128 else if (mode == "a+b") |
|
129 retval = ios::in | ios::out | ios::app | ios::bin; |
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 |
3340
|
244 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 |
3340
|
290 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 |
2095
|
306 do_stream_open (const string& name, const string& mode, |
|
307 const 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 |
|
335 string name = tc_name.string_value (); |
|
336 |
|
337 if (! error_state) |
1
|
338 { |
2095
|
339 string mode = tc_mode.string_value (); |
|
340 |
|
341 if (! error_state) |
|
342 { |
|
343 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 { |
2873
|
661 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 { |
|
736 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\ |
|
767 @deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}] = } fscanf (@var{fid}, @var{template}, \"C\")\n\ |
|
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\ |
|
803 compatible with previous versions of Octave.\n\ |
|
804 @end deftypefn") |
1181
|
805 { |
2095
|
806 octave_value_list retval; |
1181
|
807 |
|
808 int nargin = args.length (); |
|
809 |
2215
|
810 if (nargin == 3 && args(2).is_string ()) |
2095
|
811 { |
3341
|
812 octave_stream os = octave_stream_list::lookup (args(0), "fscanf"); |
1181
|
813 |
3341
|
814 if (! error_state) |
2095
|
815 { |
|
816 if (args(1).is_string ()) |
|
817 { |
|
818 string fmt = args(1).string_value (); |
1181
|
819 |
3340
|
820 retval = os.oscanf (fmt); |
2095
|
821 } |
|
822 else |
|
823 ::error ("fscanf: format must be a string"); |
|
824 } |
|
825 } |
1181
|
826 else |
2215
|
827 { |
|
828 retval (1) = 0.0; |
|
829 retval (0) = Matrix (); |
|
830 |
|
831 if (nargin == 2 || nargin == 3) |
|
832 { |
3341
|
833 octave_stream os = octave_stream_list::lookup (args(0), "fscanf"); |
2215
|
834 |
3342
|
835 if (! error_state) |
2215
|
836 { |
|
837 if (args(1).is_string ()) |
|
838 { |
|
839 string fmt = args(1).string_value (); |
|
840 |
|
841 int count = 0; |
|
842 |
|
843 Matrix size = (nargin == 3) |
|
844 ? args(2).matrix_value () : Matrix (1, 1, octave_Inf); |
|
845 |
|
846 if (! error_state) |
|
847 { |
3340
|
848 octave_value tmp = os.scanf (fmt, size, count); |
2215
|
849 |
2800
|
850 retval(1) = static_cast<double> (count); |
2215
|
851 retval(0) = tmp; |
|
852 } |
|
853 } |
|
854 else |
|
855 ::error ("fscanf: format must be a string"); |
|
856 } |
|
857 } |
|
858 else |
|
859 print_usage ("fscanf"); |
|
860 } |
1181
|
861 |
|
862 return retval; |
|
863 } |
|
864 |
2095
|
865 DEFUN (sscanf, args, , |
3372
|
866 "-*- texinfo -*-\n\ |
|
867 @deftypefn {Built-in Function} {[@var{val}, @var{count}] =} sscanf (@var{string}, @var{template}, @var{size})\n\ |
|
868 @deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}] = } sscanf (@var{string}, @var{template}, \"C\")\n\ |
|
869 This is like @code{fscanf}, except that the characters are taken from the\n\ |
|
870 string @var{string} instead of from a stream. Reaching the end of the\n\ |
|
871 string is treated as an end-of-file condition.\n\ |
|
872 @end deftypefn") |
444
|
873 { |
2095
|
874 octave_value_list retval; |
444
|
875 |
506
|
876 int nargin = args.length (); |
|
877 |
2215
|
878 if (nargin == 3 && args(2).is_string ()) |
2095
|
879 { |
|
880 if (args(0).is_string ()) |
|
881 { |
|
882 string data = args(0).string_value (); |
444
|
883 |
3340
|
884 octave_stream os = octave_istrstream::create (data); |
1358
|
885 |
3340
|
886 if (os.is_valid ()) |
2095
|
887 { |
|
888 if (args(1).is_string ()) |
|
889 { |
|
890 string fmt = args(1).string_value (); |
444
|
891 |
2215
|
892 retval = os.oscanf (fmt); |
2095
|
893 } |
|
894 else |
|
895 ::error ("sscanf: format must be a string"); |
|
896 } |
|
897 else |
|
898 ::error ("sscanf: unable to create temporary input buffer"); |
444
|
899 } |
636
|
900 else |
2095
|
901 ::error ("sscanf: first argument must be a string"); |
444
|
902 } |
|
903 else |
2215
|
904 { |
|
905 if (nargin == 2 || nargin == 3) |
|
906 { |
|
907 retval(3) = -1.0; |
|
908 retval(2) = "unknown error"; |
|
909 retval(1) = 0.0; |
|
910 retval(0) = Matrix (); |
|
911 |
|
912 if (args(0).is_string ()) |
|
913 { |
|
914 string data = args(0).string_value (); |
|
915 |
3340
|
916 octave_stream os = octave_istrstream::create (data); |
2215
|
917 |
3340
|
918 if (os.is_valid ()) |
2215
|
919 { |
|
920 if (args(1).is_string ()) |
|
921 { |
|
922 string fmt = args(1).string_value (); |
|
923 |
|
924 int count = 0; |
|
925 |
|
926 Matrix size = (nargin == 3) |
|
927 ? args(2).matrix_value () : Matrix (1, 1, octave_Inf); |
|
928 |
|
929 octave_value tmp = os.scanf (fmt, size, count); |
|
930 |
|
931 // XXX FIXME XXX -- is this the right thing to do? |
|
932 // Extract error message first, because getting |
|
933 // position will clear it. |
|
934 string errmsg = os.error (); |
|
935 |
2800
|
936 retval(3) = static_cast<double> (os.tell () + 1); |
2215
|
937 retval(2) = errmsg; |
2800
|
938 retval(1) = static_cast<double> (count); |
2215
|
939 retval(0) = tmp; |
|
940 } |
|
941 else |
|
942 ::error ("sscanf: format must be a string"); |
|
943 } |
|
944 else |
|
945 ::error ("sscanf: unable to create temporary input buffer"); |
|
946 } |
|
947 else |
|
948 ::error ("sscanf: first argument must be a string"); |
|
949 } |
|
950 else |
|
951 print_usage ("sscanf"); |
|
952 } |
444
|
953 |
|
954 return retval; |
|
955 } |
|
956 |
2215
|
957 DEFUN (scanf, args, nargout, |
3372
|
958 "-*- texinfo -*-\n\ |
|
959 @deftypefn {Built-in Function} {[@var{val}, @var{count}] =} scanf (@var{template}, @var{size})\n\ |
|
960 @deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}] = } scanf (@var{template}, \"C\")\n\ |
|
961 This is equivalent to calling @code{fscanf} with @var{fid} = @code{stdin}.\n\ |
|
962 \n\ |
|
963 It is currently not useful to call @code{scanf} in interactive\n\ |
|
964 programs.\n\ |
|
965 @end deftypefn") |
2215
|
966 { |
|
967 int nargin = args.length (); |
|
968 |
|
969 octave_value_list tmp_args (nargin+1, octave_value ()); |
|
970 |
|
971 tmp_args (0) = 0.0; |
|
972 for (int i = 0; i < nargin; i++) |
|
973 tmp_args (i+1) = args (i); |
|
974 |
|
975 return Ffscanf (tmp_args, nargout); |
|
976 } |
|
977 |
2116
|
978 static octave_value |
|
979 do_fread (octave_stream& os, const octave_value& size_arg, |
|
980 const octave_value& prec_arg, const octave_value& skip_arg, |
|
981 const octave_value& arch_arg, int& count) |
|
982 { |
|
983 octave_value retval; |
|
984 |
|
985 count = -1; |
|
986 |
|
987 Matrix size = size_arg.matrix_value (); |
|
988 |
|
989 if (! error_state) |
|
990 { |
|
991 string prec = prec_arg.string_value (); |
|
992 |
|
993 if (! error_state) |
|
994 { |
2318
|
995 oct_data_conv::data_type dt |
|
996 = oct_data_conv::string_to_data_type (prec); |
2116
|
997 |
|
998 if (! error_state) |
|
999 { |
3202
|
1000 int skip = skip_arg.int_value (true); |
2116
|
1001 |
|
1002 if (! error_state) |
|
1003 { |
3202
|
1004 string arch = arch_arg.string_value (); |
|
1005 |
|
1006 if (! error_state) |
2116
|
1007 { |
3202
|
1008 oct_mach_info::float_format flt_fmt |
|
1009 = oct_mach_info::string_to_float_format (arch); |
2116
|
1010 |
|
1011 if (! error_state) |
3202
|
1012 retval = os.read (size, dt, skip, flt_fmt, count); |
2116
|
1013 } |
|
1014 else |
3202
|
1015 ::error ("fread: architecture type must be a string"); |
2116
|
1016 } |
|
1017 else |
3202
|
1018 ::error ("fread: skip must be an integer"); |
2116
|
1019 } |
|
1020 else |
|
1021 ::error ("fread: invalid data type specified"); |
|
1022 } |
|
1023 else |
|
1024 ::error ("fread: precision must be a string"); |
|
1025 } |
|
1026 else |
|
1027 ::error ("fread: invalid size specified"); |
|
1028 |
|
1029 return retval; |
|
1030 } |
|
1031 |
|
1032 DEFUN (fread, args, , |
3372
|
1033 "-*- texinfo -*-\n\ |
|
1034 @deftypefn {Built-in Function} {[@var{val}, @var{count}] =} fread (@var{fid}, @var{size}, @var{precision}, @var{skip}, @var{arch})\n\ |
|
1035 Read binary data of type @var{precision} from the specified file ID\n\ |
|
1036 @var{fid}.\n\ |
|
1037 \n\ |
|
1038 The optional argument @var{size} specifies the amount of data to read\n\ |
|
1039 and may be one of\n\ |
|
1040 \n\ |
|
1041 @table @code\n\ |
|
1042 @item Inf\n\ |
|
1043 Read as much as possible, returning a column vector.\n\ |
529
|
1044 \n\ |
3372
|
1045 @item @var{nr}\n\ |
|
1046 Read up to @var{nr} elements, returning a column vector.\n\ |
|
1047 \n\ |
|
1048 @item [@var{nr}, Inf]\n\ |
|
1049 Read as much as possible, returning a matrix with @var{nr} rows. If the\n\ |
|
1050 number of elements read is not an exact multiple of @var{nr}, the last\n\ |
|
1051 column is padded with zeros.\n\ |
|
1052 \n\ |
|
1053 @item [@var{nr}, @var{nc}]\n\ |
|
1054 Read up to @code{@var{nr} * @var{nc}} elements, returning a matrix with\n\ |
|
1055 @var{nr} rows. If the number of elements read is not an exact multiple\n\ |
|
1056 of @var{nr}, the last column is padded with zeros.\n\ |
|
1057 @end table\n\ |
|
1058 \n\ |
|
1059 @noindent\n\ |
|
1060 If @var{size} is omitted, a value of @code{Inf} is assumed.\n\ |
2318
|
1061 \n\ |
3372
|
1062 The optional argument @var{precision} is a string specifying the type of\n\ |
|
1063 data to read and may be one of\n\ |
|
1064 \n\ |
|
1065 @table @code\n\ |
|
1066 @item \"char\"\n\ |
|
1067 @itemx \"char*1\"\n\ |
|
1068 @itemx \"integer*1\"\n\ |
|
1069 @itemx \"int8\"\n\ |
|
1070 Single character.\n\ |
|
1071 \n\ |
|
1072 @item \"signed char\"\n\ |
|
1073 @itemx \"schar\"\n\ |
|
1074 Signed character.\n\ |
529
|
1075 \n\ |
3372
|
1076 @item \"unsigned char\"\n\ |
|
1077 @itemx \"uchar\"\n\ |
|
1078 Unsigned character.\n\ |
|
1079 \n\ |
|
1080 @item \"short\"\n\ |
|
1081 Short integer.\n\ |
2318
|
1082 \n\ |
3372
|
1083 @item \"unsigned short\"\n\ |
|
1084 @itemx \"ushort\"\n\ |
|
1085 Unsigned short integer.\n\ |
|
1086 \n\ |
|
1087 @item \"int\"\n\ |
|
1088 Integer.\n\ |
|
1089 \n\ |
|
1090 @item \"unsigned int\"\n\ |
|
1091 @itemx \"uint\"\n\ |
|
1092 Unsigned integer.\n\ |
529
|
1093 \n\ |
3372
|
1094 @item \"long\"\n\ |
|
1095 Long integer.\n\ |
|
1096 \n\ |
|
1097 @item \"unsigned long\"\n\ |
|
1098 @itemx \"ulong\"\n\ |
|
1099 Unsigned long integer.\n\ |
|
1100 \n\ |
|
1101 @item \"float\"\n\ |
|
1102 @itemx \"float32\"\n\ |
|
1103 @itemx \"real*4\"\n\ |
|
1104 Single precision float.\n\ |
|
1105 \n\ |
|
1106 @item \"double\"\n\ |
|
1107 @itemx \"float64\"\n\ |
|
1108 @itemx \"real*8\"\n\ |
|
1109 Double precision float.\n\ |
|
1110 \n\ |
|
1111 @item \"integer*2\"\n\ |
|
1112 @itemx \"int16\"\n\ |
|
1113 Two byte integer.\n\ |
|
1114 \n\ |
|
1115 @item \"integer*4\"\n\ |
|
1116 @itemx \"int32\"\n\ |
|
1117 Four byte integer.\n\ |
|
1118 @end table\n\ |
|
1119 \n\ |
|
1120 @noindent\n\ |
|
1121 The default precision is @code{\"uchar\"}.\n\ |
2318
|
1122 \n\ |
3372
|
1123 The optional argument @var{skip} specifies the number of bytes to skip\n\ |
|
1124 before each element is read. If it is not specified, a value of 0 is\n\ |
|
1125 assumed.\n\ |
|
1126 \n\ |
|
1127 The optional argument @var{arch} is a string specifying the data format\n\ |
|
1128 for the file. Valid values are\n\ |
529
|
1129 \n\ |
3372
|
1130 @table @code\n\ |
|
1131 @item \"native\"\n\ |
|
1132 The format of the current machine.\n\ |
|
1133 \n\ |
|
1134 @item \"ieee-le\"\n\ |
|
1135 IEEE big endian.\n\ |
|
1136 \n\ |
|
1137 @item \"ieee-be\"\n\ |
|
1138 IEEE little endian.\n\ |
2318
|
1139 \n\ |
3372
|
1140 @item \"vaxd\"\n\ |
|
1141 VAX D floating format.\n\ |
|
1142 \n\ |
|
1143 @item \"vaxg\"\n\ |
|
1144 VAX G floating format.\n\ |
2318
|
1145 \n\ |
3372
|
1146 @item \"cray\"\n\ |
|
1147 Cray floating format.\n\ |
|
1148 @end table\n\ |
2318
|
1149 \n\ |
3372
|
1150 @noindent\n\ |
|
1151 Conversions are currently only supported for @code{\"ieee-be\"} and\n\ |
|
1152 @code{\"ieee-le\"} formats.\n\ |
2318
|
1153 \n\ |
3372
|
1154 The data read from the file is returned in @var{val}, and the number of\n\ |
|
1155 values read is returned in @code{count}\n\ |
|
1156 @end deftypefn") |
529
|
1157 { |
2095
|
1158 octave_value_list retval; |
2116
|
1159 |
|
1160 int nargin = args.length (); |
|
1161 |
2318
|
1162 if (nargin > 0 && nargin < 6) |
2116
|
1163 { |
|
1164 retval(1) = -1.0; |
|
1165 retval(0) = Matrix (); |
|
1166 |
3341
|
1167 octave_stream os = octave_stream_list::lookup (args(0), "fread"); |
2116
|
1168 |
3341
|
1169 if (! error_state) |
2116
|
1170 { |
|
1171 octave_value size = (nargin > 1) |
|
1172 ? args(1) : octave_value (octave_Inf); |
|
1173 |
|
1174 octave_value prec = (nargin > 2) |
|
1175 ? args(2) : octave_value ("uchar"); |
|
1176 |
|
1177 octave_value skip = (nargin > 3) |
|
1178 ? args(3) : octave_value (0.0); |
|
1179 |
|
1180 octave_value arch = (nargin > 4) |
2318
|
1181 ? args(4) : octave_value ("unknown"); |
2116
|
1182 |
|
1183 int count = -1; |
|
1184 |
3340
|
1185 octave_value tmp = do_fread (os, size, prec, skip, arch, count); |
2116
|
1186 |
2800
|
1187 retval(1) = static_cast<double> (count); |
2116
|
1188 retval(0) = tmp; |
|
1189 } |
|
1190 } |
|
1191 else |
|
1192 print_usage ("fread"); |
|
1193 |
529
|
1194 return retval; |
|
1195 } |
|
1196 |
2116
|
1197 static int |
|
1198 do_fwrite (octave_stream& os, const octave_value& data, |
|
1199 const octave_value& prec_arg, const octave_value& skip_arg, |
|
1200 const octave_value& arch_arg) |
|
1201 { |
|
1202 int retval = -1; |
|
1203 |
|
1204 string prec = prec_arg.string_value (); |
|
1205 |
|
1206 if (! error_state) |
|
1207 { |
2318
|
1208 oct_data_conv::data_type dt |
|
1209 = oct_data_conv::string_to_data_type (prec); |
2116
|
1210 |
|
1211 if (! error_state) |
|
1212 { |
3202
|
1213 int skip = skip_arg.int_value (true); |
2116
|
1214 |
|
1215 if (! error_state) |
|
1216 { |
3202
|
1217 string arch = arch_arg.string_value (); |
|
1218 |
|
1219 if (! error_state) |
2116
|
1220 { |
3202
|
1221 oct_mach_info::float_format flt_fmt |
|
1222 = oct_mach_info::string_to_float_format (arch); |
2116
|
1223 |
|
1224 if (! error_state) |
3202
|
1225 retval = os.write (data, dt, skip, flt_fmt); |
2116
|
1226 } |
|
1227 else |
3202
|
1228 ::error ("fwrite: architecture type must be a string"); |
2116
|
1229 } |
|
1230 else |
3202
|
1231 ::error ("fwrite: skip must be an integer"); |
2116
|
1232 } |
3202
|
1233 else |
|
1234 ::error ("fwrite: invalid precision specified"); |
2116
|
1235 } |
|
1236 else |
|
1237 ::error ("fwrite: precision must be a string"); |
|
1238 |
|
1239 return retval; |
|
1240 } |
|
1241 |
|
1242 DEFUN (fwrite, args, , |
3372
|
1243 "-*- texinfo -*-\n\ |
|
1244 @deftypefn {Built-in Function} {@var{count} =} fwrite (@var{fid}, @var{data}, @var{precision}, @var{skip}, @var{arch})\n\ |
|
1245 Write data in binary form of type @var{precision} to the specified file\n\ |
|
1246 ID @var{fid}, returning the number of values successfully written to the\n\ |
|
1247 file.\n\ |
1181
|
1248 \n\ |
3372
|
1249 The argument @var{data} is a matrix of values that are to be written to\n\ |
|
1250 the file. The values are extracted in column-major order.\n\ |
1181
|
1251 \n\ |
3372
|
1252 The remaining arguments @var{precision}, @var{skip}, and @var{arch} are\n\ |
|
1253 optional, and are interpreted as described for @code{fread}.\n\ |
1181
|
1254 \n\ |
3372
|
1255 The behavior of @code{fwrite} is undefined if the values in @var{data}\n\ |
|
1256 are too large to fit in the specified precision.\n\ |
|
1257 @end deftypefn") |
1181
|
1258 { |
2116
|
1259 octave_value retval = -1.0; |
|
1260 |
|
1261 int nargin = args.length (); |
|
1262 |
|
1263 if (nargin > 1 && nargin < 6) |
|
1264 { |
3341
|
1265 octave_stream os = octave_stream_list::lookup (args(0), "fwrite"); |
2116
|
1266 |
3341
|
1267 if (! error_state) |
2116
|
1268 { |
|
1269 octave_value data = args(1); |
2318
|
1270 |
|
1271 octave_value prec = (nargin > 2) |
|
1272 ? args(2) : octave_value ("uchar"); |
|
1273 |
|
1274 octave_value skip = (nargin > 3) |
|
1275 ? args(3) : octave_value (0.0); |
|
1276 |
|
1277 octave_value arch = (nargin > 4) |
|
1278 ? args(4) : octave_value ("unknown"); |
2116
|
1279 |
3340
|
1280 double status = do_fwrite (os, data, prec, skip, arch); |
2825
|
1281 |
|
1282 retval = status; |
2116
|
1283 } |
|
1284 } |
|
1285 else |
|
1286 print_usage ("fwrite"); |
|
1287 |
1181
|
1288 return retval; |
|
1289 } |
|
1290 |
1957
|
1291 DEFUN (feof, args, , |
3372
|
1292 "-*- texinfo -*-\n\ |
|
1293 @deftypefn {Built-in Function} {} feof (@var{fid})\n\ |
|
1294 Return 1 if an end-of-file condition has been encountered for a given\n\ |
|
1295 file and 0 otherwise. Note that it will only return 1 if the end of the\n\ |
|
1296 file has already been encountered, not if the next read operation will\n\ |
|
1297 result in an end-of-file condition.\n\ |
|
1298 @end deftypefn") |
529
|
1299 { |
2095
|
1300 double retval = -1.0; |
529
|
1301 |
|
1302 int nargin = args.length (); |
|
1303 |
2095
|
1304 if (nargin == 1) |
|
1305 { |
3341
|
1306 octave_stream os = octave_stream_list::lookup (args(0), "feof"); |
444
|
1307 |
3341
|
1308 if (! error_state) |
3340
|
1309 retval = os.eof () ? 1.0 : 0.0; |
2095
|
1310 } |
529
|
1311 else |
2095
|
1312 print_usage ("feof"); |
444
|
1313 |
|
1314 return retval; |
|
1315 } |
|
1316 |
2095
|
1317 DEFUN (ferror, args, , |
3372
|
1318 "-*- texinfo -*-\n\ |
|
1319 @deftypefn {Built-in Function} {} ferror (@var{fid})\n\ |
|
1320 Return 1 if an error condition has been encountered for a given file\n\ |
|
1321 and 0 otherwise. Note that it will only return 1 if an error has\n\ |
|
1322 already been encountered, not if the next operation will result in an\n\ |
|
1323 error condition.\n\ |
|
1324 @end deftypefn") |
1230
|
1325 { |
2095
|
1326 octave_value_list retval; |
1230
|
1327 |
2095
|
1328 int nargin = args.length (); |
1230
|
1329 |
2095
|
1330 if (nargin == 1 || nargin == 2) |
|
1331 { |
3341
|
1332 octave_stream os = octave_stream_list::lookup (args(0), "ferror"); |
1230
|
1333 |
3341
|
1334 if (! error_state) |
2095
|
1335 { |
|
1336 bool clear = false; |
1230
|
1337 |
2095
|
1338 if (nargin == 2) |
|
1339 { |
|
1340 string opt = args(1).string_value (); |
|
1341 |
|
1342 if (! error_state) |
|
1343 clear = (opt == "clear"); |
|
1344 else |
|
1345 return retval; |
|
1346 } |
1755
|
1347 |
2095
|
1348 int error_number = 0; |
|
1349 |
3340
|
1350 string error_message = os.error (clear, error_number); |
1230
|
1351 |
2800
|
1352 retval(1) = static_cast<double> (error_number); |
2095
|
1353 retval(0) = error_message; |
|
1354 } |
1230
|
1355 } |
2095
|
1356 else |
|
1357 print_usage ("ferror"); |
1230
|
1358 |
|
1359 return retval; |
|
1360 } |
|
1361 |
1957
|
1362 DEFUN (popen, args, , |
3301
|
1363 "-*- texinfo -*-\n\ |
|
1364 @deftypefn {Built-in Function} {fid =} popen (@var{command}, @var{mode})\n\ |
|
1365 Start a process and create a pipe. The name of the command to run is\n\ |
|
1366 given by @var{command}. The file identifier corresponding to the input\n\ |
|
1367 or output stream of the process is returned in @var{fid}. The argument\n\ |
|
1368 @var{mode} may be\n\ |
|
1369 \n\ |
|
1370 @table @code\n\ |
|
1371 @item \"r\"\n\ |
|
1372 The pipe will be connected to the standard output of the process, and\n\ |
|
1373 open for reading.\n\ |
1230
|
1374 \n\ |
3301
|
1375 @item \"w\"\n\ |
|
1376 The pipe will be connected to the standard input of the process, and\n\ |
|
1377 open for writing.\n\ |
|
1378 @end table\n\ |
|
1379 \n\ |
|
1380 For example,\n\ |
1230
|
1381 \n\ |
3301
|
1382 @example\n\ |
|
1383 @group\n\ |
|
1384 fid = popen (\"ls -ltr / | tail -3\", \"r\");\n\ |
|
1385 while (isstr (s = fgets (fid)))\n\ |
|
1386 fputs (stdout, s);\n\ |
|
1387 endwhile\n\ |
|
1388 @print{} drwxr-xr-x 33 root root 3072 Feb 15 13:28 etc\n\ |
|
1389 @print{} drwxr-xr-x 3 root root 1024 Feb 15 13:28 lib\n\ |
|
1390 @print{} drwxrwxrwt 15 root root 2048 Feb 17 14:53 tmp\n\ |
|
1391 @end group\n\ |
|
1392 @end example\n\ |
|
1393 @end deftypefn") |
1230
|
1394 { |
2902
|
1395 octave_value retval = -1.0; |
1230
|
1396 |
|
1397 int nargin = args.length (); |
|
1398 |
2095
|
1399 if (nargin == 2) |
|
1400 { |
|
1401 string name = args(0).string_value (); |
1230
|
1402 |
2095
|
1403 if (! error_state) |
|
1404 { |
|
1405 string mode = args(1).string_value (); |
1230
|
1406 |
2095
|
1407 if (! error_state) |
|
1408 { |
|
1409 if (mode == "r") |
|
1410 { |
3340
|
1411 octave_stream ips = octave_iprocstream::create (name); |
1230
|
1412 |
2095
|
1413 retval = octave_stream_list::insert (ips); |
|
1414 } |
|
1415 else if (mode == "w") |
|
1416 { |
3340
|
1417 octave_stream ops = octave_oprocstream::create (name); |
1230
|
1418 |
2095
|
1419 retval = octave_stream_list::insert (ops); |
|
1420 } |
|
1421 else |
|
1422 ::error ("popen: invalid mode specified"); |
|
1423 } |
|
1424 else |
|
1425 ::error ("popen: mode must be a string"); |
|
1426 } |
|
1427 else |
|
1428 ::error ("popen: name must be a string"); |
|
1429 } |
1230
|
1430 else |
2095
|
1431 print_usage ("popen"); |
1230
|
1432 |
|
1433 return retval; |
|
1434 } |
|
1435 |
1957
|
1436 DEFUN (pclose, args, , |
3381
|
1437 "-*- texinfo -*-\n\ |
3301
|
1438 @deftypefn {Built-in Function} {} pclose (@var{fid})\n\ |
|
1439 Close a file identifier that was opened by @code{popen}. You may also\n\ |
|
1440 use @code{fclose} for the same purpose.\n\ |
|
1441 @end deftypefn") |
1230
|
1442 { |
2095
|
1443 double retval = -1.0; |
1230
|
1444 |
|
1445 int nargin = args.length (); |
|
1446 |
2095
|
1447 if (nargin == 1) |
3341
|
1448 retval = static_cast<double> (octave_stream_list::remove (args(0), |
|
1449 "pclose")); |
1377
|
1450 else |
2095
|
1451 print_usage ("pclose"); |
1379
|
1452 |
|
1453 return retval; |
|
1454 } |
|
1455 |
2458
|
1456 DEFUN (tmpnam, args, , |
3372
|
1457 "-*- texinfo -*-\n\ |
|
1458 @deftypefn {Built-in Function} {} tmpnam ()\n\ |
|
1459 Return a unique temporary file name as a string.\n\ |
|
1460 \n\ |
|
1461 Since the named file is not opened, by @code{tmpnam}, it\n\ |
|
1462 is possible (though relatively unlikely) that it will not be available\n\ |
|
1463 by the time your program attempts to open it.\n\ |
|
1464 @end deftypefn") |
1802
|
1465 { |
2095
|
1466 octave_value retval; |
1802
|
1467 |
2936
|
1468 int len = args.length (); |
|
1469 |
|
1470 if (len < 3) |
|
1471 { |
|
1472 string dir = len > 0 ? args(0).string_value () : string (); |
|
1473 string pfx = len > 1 ? args(1).string_value () : string ("oct-"); |
|
1474 |
|
1475 if (! error_state) |
|
1476 retval = file_ops::tempnam (dir, pfx); |
|
1477 } |
1802
|
1478 else |
2458
|
1479 print_usage ("tmpnam"); |
1802
|
1480 |
|
1481 return retval; |
|
1482 } |
|
1483 |
2458
|
1484 DEFALIAS (octave_tmp_file_name, tmpnam); |
|
1485 |
1400
|
1486 static int |
|
1487 convert (int x, int ibase, int obase) |
|
1488 { |
|
1489 int retval = 0; |
|
1490 |
|
1491 int tmp = x % obase; |
|
1492 |
|
1493 if (tmp > ibase - 1) |
2095
|
1494 ::error ("umask: invalid digit"); |
1400
|
1495 else |
|
1496 { |
|
1497 retval = tmp; |
|
1498 int mult = ibase; |
|
1499 while ((x = (x - tmp) / obase)) |
|
1500 { |
|
1501 tmp = x % obase; |
|
1502 if (tmp > ibase - 1) |
|
1503 { |
2095
|
1504 ::error ("umask: invalid digit"); |
1400
|
1505 break; |
|
1506 } |
|
1507 retval += mult * tmp; |
|
1508 mult *= ibase; |
|
1509 } |
|
1510 } |
|
1511 |
|
1512 return retval; |
|
1513 } |
|
1514 |
1957
|
1515 DEFUN (umask, args, , |
3301
|
1516 "-*- texinfo -*-\n\ |
|
1517 @deftypefn {Built-in Function} {} umask (@var{mask})\n\ |
|
1518 Set the permission mask for file creation. The parameter @var{mask}\n\ |
|
1519 is an integer, interpreted as an octal number. If successful,\n\ |
|
1520 returns the previous value of the mask (as an integer to be\n\ |
|
1521 interpreted as an octal number); otherwise an error message is printed.\n\ |
|
1522 @end deftypefn") |
1400
|
1523 { |
2095
|
1524 octave_value_list retval; |
1400
|
1525 |
|
1526 int status = 0; |
|
1527 |
|
1528 if (args.length () == 1) |
|
1529 { |
3202
|
1530 int mask = args(0).int_value (true); |
1400
|
1531 |
3202
|
1532 if (! error_state) |
1400
|
1533 { |
3202
|
1534 if (mask < 0) |
1400
|
1535 { |
|
1536 status = -1; |
2095
|
1537 ::error ("umask: MASK must be a positive integer value"); |
1400
|
1538 } |
|
1539 else |
|
1540 { |
|
1541 int oct_mask = convert (mask, 8, 10); |
|
1542 |
|
1543 if (! error_state) |
2926
|
1544 status = convert (file_ops::umask (oct_mask), 10, 8); |
1400
|
1545 } |
|
1546 } |
3202
|
1547 else |
|
1548 { |
|
1549 status = -1; |
|
1550 ::error ("umask: expecting integer argument"); |
|
1551 } |
1400
|
1552 } |
|
1553 else |
|
1554 print_usage ("umask"); |
|
1555 |
|
1556 if (status >= 0) |
2800
|
1557 retval(0) = static_cast<double> (status); |
1400
|
1558 |
|
1559 return retval; |
|
1560 } |
|
1561 |
2189
|
1562 void |
|
1563 symbols_of_file_io (void) |
|
1564 { |
2341
|
1565 // NOTE: the values of SEEK_SET, SEEK_CUR, and SEEK_END have to be |
|
1566 // this way for Matlab compatibility. |
|
1567 |
3321
|
1568 DEFCONSTX ("SEEK_SET", SBV_SEEK_SET, -1.0, |
3372
|
1569 "-*- texinfo -*-\n\ |
|
1570 @defvr {Built-in Variable} SEEK_SET\n\ |
|
1571 @defvrx {Built-in Variable} SEEK_CUR\n\ |
|
1572 @defvrx {Built-in Variable} SEEK_END\n\ |
|
1573 These variables may be used as the optional third argument for the\n\ |
|
1574 function @code{fseek}.\n\ |
|
1575 \n\ |
|
1576 @table @code\n\ |
|
1577 @item SEEK_SET\n\ |
|
1578 Position file relative to the beginning.\n\ |
|
1579 \n\ |
|
1580 @item SEEK_CUR\n\ |
|
1581 Position file relative to the current position.\n\ |
|
1582 \n\ |
|
1583 @item SEEK_END\n\ |
|
1584 used with fseek to position file relative to the end.\n\ |
|
1585 @end table\n\ |
|
1586 @end defvr"); |
2189
|
1587 |
3321
|
1588 DEFCONSTX ("SEEK_CUR", SBV_SEEK_CUR, 0.0, |
3458
|
1589 "-*- texinfo -*-\n\ |
|
1590 @defvr {Built-in Variable} SEEK_CUR\n\ |
|
1591 See SEEK_SET.\n\ |
|
1592 @end defvr"); |
2189
|
1593 |
3321
|
1594 DEFCONSTX ("SEEK_END", SBV_SEEK_END, 1.0, |
3458
|
1595 "-*- texinfo -*-\n\ |
|
1596 @defvr {Built-in Variable} SEEK_END\n\ |
|
1597 See SEEK_SET.\n\ |
|
1598 @end defvr"); |
2189
|
1599 |
3141
|
1600 DEFCONSTX ("stdin", SBV_stdin, stdin_file, |
3372
|
1601 "-*- texinfo -*-\n\ |
|
1602 @defvr {Built-in Variable} stdin\n\ |
|
1603 The standard input stream (file id 0). When Octave is used\n\ |
|
1604 interactively, this is filtered through the command line editing\n\ |
|
1605 functions.\n\ |
|
1606 @end defvr"); |
2189
|
1607 |
3141
|
1608 DEFCONSTX ("stdout", SBV_stdout, stdout_file, |
3372
|
1609 "-*- texinfo -*-\n\ |
|
1610 @defvr {Built-in Variable} stdout\n\ |
|
1611 The standard output stream (file id 1). Data written to the\n\ |
|
1612 standard output is normally filtered through the pager.\n\ |
|
1613 @end defvr"); |
2189
|
1614 |
3141
|
1615 DEFCONSTX ("stderr", SBV_stderr, stderr_file, |
3372
|
1616 "-*- texinfo -*-\n\ |
|
1617 @defvr {Built-in Variable} stderr\n\ |
|
1618 The standard error stream (file id 2). Even if paging is turned on,\n\ |
|
1619 the standard error is not sent to the pager. It is useful for error\n\ |
|
1620 messages and prompts.\n\ |
|
1621 @end defvr"); |
|
1622 |
2189
|
1623 } |
|
1624 |
444
|
1625 /* |
1
|
1626 ;;; Local Variables: *** |
|
1627 ;;; mode: C++ *** |
|
1628 ;;; End: *** |
|
1629 */ |