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