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