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 |
4036
|
97 static std::ios::openmode |
3523
|
98 fopen_mode_to_ios_mode (const std::string& mode) |
1
|
99 { |
4036
|
100 std::ios::openmode retval = std::ios::in; |
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+") |
4070
|
117 retval = std::ios::in | std::ios::out | std::ios::ate; |
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") |
4036
|
127 retval = (std::ios::in | std::ios::out | std::ios::trunc |
|
128 | std::ios::binary); |
2095
|
129 else if (mode == "a+b") |
4070
|
130 retval = (std::ios::in | std::ios::out | std::ios::ate |
4036
|
131 | std::ios::binary); |
368
|
132 else |
2095
|
133 ::error ("invalid mode specified"); |
1
|
134 } |
|
135 |
|
136 return retval; |
|
137 } |
|
138 |
4028
|
139 DEFUN (isstream, args, , |
3448
|
140 "-*- texinfo -*-\n\ |
4028
|
141 @deftypefn {Built-in Function} {} isstream (@var{x})\n\ |
3448
|
142 Return true if @var{x} is a stream object. Otherwise, return false.\n\ |
|
143 @end deftypefn") |
3340
|
144 { |
|
145 octave_value retval; |
|
146 |
|
147 if (args.length () == 1) |
|
148 retval = args(0).is_stream (); |
|
149 else |
4028
|
150 print_usage ("isstream"); |
3340
|
151 |
|
152 return retval; |
|
153 } |
|
154 |
1957
|
155 DEFUN (fclose, args, , |
3372
|
156 "-*- texinfo -*-\n\ |
|
157 @deftypefn {Built-in Function} {} fclose (@var{fid})\n\ |
|
158 Closes the specified file. If an error is encountered while trying to\n\ |
|
159 close the file, an error message is printed and @code{fclose} returns\n\ |
|
160 0. Otherwise, it returns 1.\n\ |
|
161 @end deftypefn") |
529
|
162 { |
4233
|
163 octave_value retval = -1; |
529
|
164 |
|
165 int nargin = args.length (); |
|
166 |
2095
|
167 if (nargin == 1) |
4233
|
168 retval = octave_stream_list::remove (args(0), "fclose"); |
1
|
169 else |
2095
|
170 print_usage ("fclose"); |
1
|
171 |
|
172 return retval; |
|
173 } |
|
174 |
1957
|
175 DEFUN (fflush, args, , |
3372
|
176 "-*- texinfo -*-\n\ |
|
177 @deftypefn {Built-in Function} {} fflush (@var{fid})\n\ |
|
178 Flush output to @var{fid}. This is useful for ensuring that all\n\ |
|
179 pending output makes it to the screen before some other event occurs.\n\ |
|
180 For example, it is always a good idea to flush the standard output\n\ |
|
181 stream before calling @code{input}.\n\ |
|
182 @end deftypefn") |
1181
|
183 { |
4233
|
184 octave_value retval = -1; |
1181
|
185 |
|
186 int nargin = args.length (); |
|
187 |
2095
|
188 if (nargin == 1) |
|
189 { |
2609
|
190 // XXX FIXME XXX -- any way to avoid special case for stdout? |
|
191 |
|
192 int fid = octave_stream_list::get_file_number (args (0)); |
|
193 |
|
194 if (fid == 1) |
|
195 { |
|
196 flush_octave_stdout (); |
2095
|
197 |
4233
|
198 retval = 0; |
2609
|
199 } |
2095
|
200 else |
2609
|
201 { |
3341
|
202 octave_stream os = octave_stream_list::lookup (fid, "fflush"); |
2609
|
203 |
3341
|
204 if (! error_state) |
4233
|
205 retval = os.flush (); |
2609
|
206 } |
2095
|
207 } |
|
208 else |
1181
|
209 print_usage ("fflush"); |
|
210 |
|
211 return retval; |
|
212 } |
|
213 |
2095
|
214 DEFUN (fgetl, args, , |
3372
|
215 "-*- texinfo -*-\n\ |
|
216 @deftypefn {Built-in Function} {} fgetl (@var{fid}, @var{len})\n\ |
|
217 Read characters from a file, stopping after a newline, or EOF,\n\ |
|
218 or @var{len} characters have been read. The characters read, excluding\n\ |
|
219 the possible trailing newline, are returned as a string.\n\ |
1339
|
220 \n\ |
3372
|
221 If @var{len} is omitted, @code{fgetl} reads until the next newline\n\ |
|
222 character.\n\ |
|
223 \n\ |
|
224 If there are no more characters to read, @code{fgetl} returns @minus{}1.\n\ |
|
225 @end deftypefn") |
1339
|
226 { |
2599
|
227 octave_value_list retval; |
|
228 |
4233
|
229 retval(1) = 0; |
|
230 retval(0) = -1; |
1339
|
231 |
|
232 int nargin = args.length (); |
|
233 |
|
234 if (nargin == 1 || nargin == 2) |
2095
|
235 { |
3341
|
236 octave_stream os = octave_stream_list::lookup (args(0), "fgetl"); |
2095
|
237 |
3341
|
238 if (! error_state) |
2095
|
239 { |
|
240 octave_value len_arg = (nargin == 2) |
4233
|
241 ? args(1) : octave_value (INT_MAX); |
2095
|
242 |
|
243 bool err = false; |
|
244 |
3523
|
245 std::string tmp = os.getl (len_arg, err); |
2095
|
246 |
|
247 if (! err) |
2599
|
248 { |
2800
|
249 retval(1) = static_cast<double> (tmp.length ()); |
2599
|
250 retval(0) = tmp; |
|
251 } |
2095
|
252 } |
|
253 } |
1339
|
254 else |
|
255 print_usage ("fgetl"); |
|
256 |
|
257 return retval; |
|
258 } |
|
259 |
2095
|
260 DEFUN (fgets, args, , |
3372
|
261 "-*- texinfo -*-\n\ |
|
262 @deftypefn {Built-in Function} {} fgets (@var{fid}, @var{len})\n\ |
|
263 Read characters from a file, stopping after a newline, or EOF,\n\ |
|
264 or @var{len} characters have been read. The characters read, including\n\ |
|
265 the possible trailing newline, are returned as a string.\n\ |
529
|
266 \n\ |
3372
|
267 If @var{len} is omitted, @code{fgets} reads until the next newline\n\ |
|
268 character.\n\ |
|
269 \n\ |
|
270 If there are no more characters to read, @code{fgets} returns @minus{}1.\n\ |
|
271 @end deftypefn") |
529
|
272 { |
2599
|
273 octave_value_list retval; |
|
274 |
|
275 retval(1) = 0.0; |
|
276 retval(0) = -1.0; |
529
|
277 |
|
278 int nargin = args.length (); |
|
279 |
1338
|
280 if (nargin == 1 || nargin == 2) |
2095
|
281 { |
3341
|
282 octave_stream os = octave_stream_list::lookup (args(0), "fgets"); |
2095
|
283 |
3341
|
284 if (! error_state) |
2095
|
285 { |
|
286 octave_value len_arg = (nargin == 2) |
4233
|
287 ? args(1) : octave_value (INT_MAX); |
2095
|
288 |
|
289 bool err = false; |
|
290 |
3523
|
291 std::string tmp = os.gets (len_arg, err); |
2095
|
292 |
|
293 if (! err) |
2599
|
294 { |
2800
|
295 retval(1) = static_cast<double> (tmp.length ()); |
2599
|
296 retval(0) = tmp; |
|
297 } |
2095
|
298 } |
|
299 } |
1338
|
300 else |
1181
|
301 print_usage ("fgets"); |
529
|
302 |
|
303 return retval; |
|
304 } |
|
305 |
3340
|
306 static octave_stream |
3523
|
307 do_stream_open (const std::string& name, const std::string& mode, |
|
308 const std::string& arch, int& fid) |
1
|
309 { |
3340
|
310 octave_stream retval; |
1
|
311 |
2095
|
312 fid = -1; |
1
|
313 |
4036
|
314 std::ios::openmode md = fopen_mode_to_ios_mode (mode); |
1
|
315 |
2095
|
316 if (! error_state) |
|
317 { |
2318
|
318 oct_mach_info::float_format flt_fmt = |
|
319 oct_mach_info::string_to_float_format (arch); |
1
|
320 |
2095
|
321 if (! error_state) |
4036
|
322 retval = octave_fstream::create (name, md, flt_fmt); |
1
|
323 } |
|
324 |
2095
|
325 return retval; |
|
326 } |
1
|
327 |
3340
|
328 static octave_stream |
2095
|
329 do_stream_open (const octave_value& tc_name, const octave_value& tc_mode, |
|
330 const octave_value& tc_arch, const char *fcn, int& fid) |
|
331 { |
3340
|
332 octave_stream retval; |
2095
|
333 |
|
334 fid = -1; |
|
335 |
3523
|
336 std::string name = tc_name.string_value (); |
2095
|
337 |
|
338 if (! error_state) |
1
|
339 { |
3523
|
340 std::string mode = tc_mode.string_value (); |
2095
|
341 |
|
342 if (! error_state) |
|
343 { |
3523
|
344 std::string arch = tc_arch.string_value (); |
1
|
345 |
2095
|
346 if (! error_state) |
|
347 retval = do_stream_open (name, mode, arch, fid); |
|
348 else |
|
349 ::error ("%s: architecture type must be a string", fcn); |
|
350 } |
|
351 else |
|
352 ::error ("%s: file mode must be a string", fcn); |
1
|
353 } |
2095
|
354 else |
|
355 ::error ("%s: file name must be a string", fcn); |
1
|
356 |
|
357 return retval; |
|
358 } |
|
359 |
1957
|
360 DEFUN (fopen, args, , |
3372
|
361 "-*- texinfo -*-\n\ |
|
362 @deftypefn {Built-in Function} {[@var{fid}, @var{msg}] =} fopen (@var{name}, @var{mode}, @var{arch})\n\ |
|
363 @deftypefnx {Built-in Function} {@var{fid_list} =} fopen (\"all\")\n\ |
|
364 @deftypefnx {Built-in Function} {@var{file} =} fopen (@var{fid})\n\ |
|
365 The first form of the @code{fopen} function opens the named file with\n\ |
|
366 the specified mode (read-write, read-only, etc.) and architecture\n\ |
|
367 interpretation (IEEE big endian, IEEE little endian, etc.), and returns\n\ |
|
368 an integer value that may be used to refer to the file later. If an\n\ |
|
369 error occurs, @var{fid} is set to @minus{}1 and @var{msg} contains the\n\ |
|
370 corresponding system error message. The @var{mode} is a one or two\n\ |
|
371 character string that specifies whether the file is to be opened for\n\ |
|
372 reading, writing, or both.\n\ |
1181
|
373 \n\ |
3372
|
374 The second form of the @code{fopen} function returns a vector of file ids\n\ |
|
375 corresponding to all the currently open files, excluding the\n\ |
|
376 @code{stdin}, @code{stdout}, and @code{stderr} streams.\n\ |
2318
|
377 \n\ |
3372
|
378 The third form of the @code{fopen} function returns the name of a\n\ |
|
379 currently open file given its file id.\n\ |
1181
|
380 \n\ |
3372
|
381 For example,\n\ |
|
382 \n\ |
|
383 @example\n\ |
|
384 myfile = fopen (\"splat.dat\", \"r\", \"ieee-le\");\n\ |
|
385 @end example\n\ |
2095
|
386 \n\ |
3372
|
387 @noindent\n\ |
|
388 opens the file @file{splat.dat} for reading. If necessary, binary\n\ |
|
389 numeric values will be read assuming they are stored in IEEE format with\n\ |
|
390 the least significant bit first, and then converted to the native\n\ |
|
391 representation.\n\ |
2318
|
392 \n\ |
3372
|
393 Opening a file that is already open simply opens it again and returns a\n\ |
|
394 separate file id. It is not an error to open a file several times,\n\ |
|
395 though writing to the same file through several different file ids may\n\ |
|
396 produce unexpected results.\n\ |
|
397 \n\ |
|
398 The possible values @samp{mode} may have are\n\ |
|
399 \n\ |
|
400 @table @asis\n\ |
|
401 @item @samp{r}\n\ |
|
402 Open a file for reading.\n\ |
3263
|
403 \n\ |
3372
|
404 @item @samp{w}\n\ |
|
405 Open a file for writing. The previous contents are discared.\n\ |
|
406 \n\ |
|
407 @item @samp{a}\n\ |
|
408 Open or create a file for writing at the end of the file.\n\ |
|
409 \n\ |
|
410 @item @samp{r+}\n\ |
|
411 Open an existing file for reading and writing.\n\ |
|
412 \n\ |
|
413 @item @samp{w+}\n\ |
|
414 Open a file for reading or writing. The previous contents are\n\ |
|
415 discarded.\n\ |
|
416 \n\ |
|
417 @item @samp{a+}\n\ |
|
418 Open or create a file for reading or writing at the end of the\n\ |
|
419 file.\n\ |
|
420 @end table\n\ |
|
421 \n\ |
|
422 The parameter @var{arch} is a string specifying the default data format\n\ |
|
423 for the file. Valid values for @var{arch} are:\n\ |
2318
|
424 \n\ |
3372
|
425 @table @asis\n\ |
|
426 @samp{native}\n\ |
|
427 The format of the current machine (this is the default).\n\ |
|
428 \n\ |
4082
|
429 @samp{ieee-be}\n\ |
3372
|
430 IEEE big endian format.\n\ |
|
431 \n\ |
4082
|
432 @samp{ieee-le}\n\ |
3372
|
433 IEEE little endian format.\n\ |
2318
|
434 \n\ |
3372
|
435 @samp{vaxd}\n\ |
|
436 VAX D floating format.\n\ |
2318
|
437 \n\ |
3372
|
438 @samp{vaxg}\n\ |
|
439 VAX G floating format.\n\ |
2318
|
440 \n\ |
3372
|
441 @samp{cray}\n\ |
|
442 Cray floating format.\n\ |
|
443 @end table\n\ |
|
444 \n\ |
|
445 @noindent\n\ |
|
446 however, conversions are currently only supported for @samp{native}\n\ |
|
447 @samp{ieee-be}, and @samp{ieee-le} formats.\n\ |
|
448 @end deftypefn") |
529
|
449 { |
2599
|
450 octave_value_list retval; |
|
451 |
|
452 retval(0) = -1.0; |
529
|
453 |
|
454 int nargin = args.length (); |
|
455 |
2095
|
456 if (nargin == 1) |
|
457 { |
3263
|
458 if (args(0).is_string ()) |
|
459 { |
|
460 // If there is only one argument and it is a string but it |
|
461 // is not the string "all", we assume it is a file to open |
|
462 // with MODE = "r". To open a file called "all", you have |
|
463 // to supply more than one argument. |
|
464 |
|
465 if (args(0).string_value () == "all") |
|
466 return octave_stream_list::open_file_numbers (); |
|
467 } |
2095
|
468 else |
|
469 { |
|
470 string_vector tmp = octave_stream_list::get_info (args(0)); |
529
|
471 |
2095
|
472 if (! error_state) |
|
473 { |
|
474 retval(2) = tmp(2); |
|
475 retval(1) = tmp(1); |
|
476 retval(0) = tmp(0); |
|
477 } |
3263
|
478 |
|
479 return retval; |
2432
|
480 } |
1
|
481 } |
|
482 |
2095
|
483 if (nargin > 0 && nargin < 4) |
|
484 { |
|
485 octave_value mode = (nargin == 2 || nargin == 3) |
|
486 ? args(1) : octave_value ("r"); |
|
487 |
|
488 octave_value arch = (nargin == 3) |
|
489 ? args(2) : octave_value ("native"); |
|
490 |
|
491 int fid = -1; |
|
492 |
3340
|
493 octave_stream os = do_stream_open (args(0), mode, arch, "fopen", fid); |
2095
|
494 |
3340
|
495 if (os.is_valid ()) |
2095
|
496 { |
3340
|
497 if (os && ! error_state) |
2095
|
498 { |
|
499 retval(1) = ""; |
2902
|
500 retval(0) = octave_stream_list::insert (os); |
2095
|
501 } |
|
502 else |
|
503 { |
3162
|
504 int error_number = 0; |
|
505 |
3340
|
506 retval(1) = os.error (false, error_number); |
2095
|
507 retval(0) = -1.0; |
|
508 } |
|
509 } |
|
510 else |
3340
|
511 error ("fopen: internal error"); |
2095
|
512 } |
|
513 else |
|
514 print_usage ("fopen"); |
1
|
515 |
|
516 return retval; |
|
517 } |
|
518 |
1957
|
519 DEFUN (freport, args, , |
3372
|
520 "-*- texinfo -*-\n\ |
|
521 @deftypefn {Built-in Function} {} freport ()\n\ |
|
522 Print a list of which files have been opened, and whether they are open\n\ |
|
523 for reading, writing, or both. For example,\n\ |
|
524 \n\ |
|
525 @example\n\ |
|
526 @group\n\ |
|
527 freport ()\n\ |
|
528 \n\ |
|
529 @print{} number mode name\n\ |
|
530 @print{} \n\ |
|
531 @print{} 0 r stdin\n\ |
|
532 @print{} 1 w stdout\n\ |
|
533 @print{} 2 w stderr\n\ |
|
534 @print{} 3 r myfile\n\ |
|
535 @end group\n\ |
|
536 @end example\n\ |
|
537 @end deftypefn") |
1181
|
538 { |
2095
|
539 octave_value_list retval; |
1181
|
540 |
|
541 int nargin = args.length (); |
|
542 |
|
543 if (nargin > 0) |
|
544 warning ("freport: ignoring extra arguments"); |
|
545 |
2095
|
546 octave_stdout << octave_stream_list::list_open_files (); |
1181
|
547 |
|
548 return retval; |
|
549 } |
|
550 |
1957
|
551 DEFUN (frewind, args, , |
3372
|
552 "-*- texinfo -*-\n\ |
|
553 @deftypefn {Built-in Function} {} frewind (@var{fid})\n\ |
|
554 Move the file pointer to the beginning of the file @var{fid}, returning\n\ |
|
555 1 for success, and 0 if an error was encountered. It is equivalent to\n\ |
|
556 @code{fseek (@var{fid}, 0, SEEK_SET)}.\n\ |
|
557 @end deftypefn") |
529
|
558 { |
4233
|
559 octave_value retval = -1; |
1
|
560 |
506
|
561 int nargin = args.length (); |
|
562 |
2095
|
563 if (nargin == 1) |
1086
|
564 { |
3341
|
565 octave_stream os = octave_stream_list::lookup (args(0), "frewind"); |
636
|
566 |
3341
|
567 if (! error_state) |
4233
|
568 retval = os.rewind (); |
1
|
569 } |
|
570 else |
2095
|
571 print_usage ("frewind"); |
1
|
572 |
|
573 return retval; |
|
574 } |
|
575 |
1957
|
576 DEFUN (fseek, args, , |
3372
|
577 "-*- texinfo -*-\n\ |
|
578 @deftypefn {Built-in Function} {} fseek (@var{fid}, @var{offset}, @var{origin})\n\ |
|
579 Set the file pointer to any location within the file @var{fid}. The\n\ |
|
580 pointer is positioned @var{offset} characters from the @var{origin},\n\ |
|
581 which may be one of the predefined variables @code{SEEK_CUR} (current\n\ |
|
582 position), @code{SEEK_SET} (beginning), or @code{SEEK_END} (end of\n\ |
|
583 file). If @var{origin} is omitted, @code{SEEK_SET} is assumed. The\n\ |
|
584 offset must be zero, or a value returned by @code{ftell} (in which case\n\ |
|
585 @var{origin} must be @code{SEEK_SET}.\n\ |
|
586 @end deftypefn") |
529
|
587 { |
4233
|
588 octave_value retval = -1; |
529
|
589 |
|
590 int nargin = args.length (); |
|
591 |
2095
|
592 if (nargin == 2 || nargin == 3) |
|
593 { |
3341
|
594 octave_stream os = octave_stream_list::lookup (args(0), "fseek"); |
1181
|
595 |
3341
|
596 if (! error_state) |
2095
|
597 { |
|
598 octave_value origin_arg = (nargin == 3) |
2341
|
599 ? args(2) : octave_value (-1.0); |
1
|
600 |
4233
|
601 retval = os.seek (args(1), origin_arg); |
2095
|
602 } |
368
|
603 } |
2095
|
604 else |
|
605 print_usage ("fseek"); |
1
|
606 |
|
607 return retval; |
|
608 } |
|
609 |
1957
|
610 DEFUN (ftell, args, , |
3372
|
611 "-*- texinfo -*-\n\ |
|
612 @deftypefn {Built-in Function} {} ftell (@var{fid})\n\ |
|
613 Return the position of the file pointer as the number of characters\n\ |
|
614 from the beginning of the file @var{fid}.\n\ |
|
615 @end deftypefn") |
1181
|
616 { |
4233
|
617 octave_value retval = -1; |
1
|
618 |
506
|
619 int nargin = args.length (); |
|
620 |
2095
|
621 if (nargin == 1) |
1
|
622 { |
3341
|
623 octave_stream os = octave_stream_list::lookup (args(0), "ftell"); |
1
|
624 |
3341
|
625 if (! error_state) |
3340
|
626 retval = static_cast<double> (os.tell ()); |
1
|
627 } |
|
628 else |
2095
|
629 print_usage ("ftell"); |
1
|
630 |
|
631 return retval; |
|
632 } |
|
633 |
3737
|
634 DEFUN (fprintf, args, nargout, |
3372
|
635 "-*- texinfo -*-\n\ |
|
636 @deftypefn {Built-in Function} {} fprintf (@var{fid}, @var{template}, @dots{})\n\ |
|
637 This function is just like @code{printf}, except that the output is\n\ |
|
638 written to the stream @var{fid} instead of @code{stdout}.\n\ |
|
639 @end deftypefn") |
1181
|
640 { |
4233
|
641 octave_value retval = -1; |
3737
|
642 bool return_char_count = true; |
1181
|
643 |
|
644 int nargin = args.length (); |
|
645 |
2875
|
646 if (nargin > 1 || (nargin > 0 && args(0).is_string ())) |
2095
|
647 { |
3340
|
648 octave_stream os; |
2873
|
649 int fmt_n = 0; |
|
650 |
3737
|
651 if (args(0).is_string ()) |
|
652 { |
|
653 os = octave_stream_list::lookup (1, "fprintf"); |
|
654 |
|
655 // For compatibility with Matlab, which does not return the |
|
656 // character count when behaving like printf (no file id |
|
657 // parameter). |
|
658 |
|
659 return_char_count = (nargout != 0); |
|
660 } |
2873
|
661 else |
|
662 { |
|
663 fmt_n = 1; |
3341
|
664 os = octave_stream_list::lookup (args(0), "fprintf"); |
2873
|
665 } |
2095
|
666 |
3341
|
667 if (! error_state) |
2095
|
668 { |
2873
|
669 if (args(fmt_n).is_string ()) |
2095
|
670 { |
3523
|
671 std::string fmt = args(fmt_n).string_value (); |
2095
|
672 |
|
673 octave_value_list tmp_args; |
|
674 |
2873
|
675 if (nargin > 1 + fmt_n) |
2095
|
676 { |
2873
|
677 tmp_args.resize (nargin-fmt_n-1, octave_value ()); |
2095
|
678 |
2873
|
679 for (int i = fmt_n + 1; i < nargin; i++) |
|
680 tmp_args(i-fmt_n-1) = args(i); |
2095
|
681 } |
|
682 |
3340
|
683 retval = os.printf (fmt, tmp_args); |
2095
|
684 } |
|
685 else |
|
686 ::error ("fprintf: format must be a string"); |
|
687 } |
|
688 } |
|
689 else |
1181
|
690 print_usage ("fprintf"); |
|
691 |
3737
|
692 if (return_char_count) |
|
693 return retval; |
|
694 else |
|
695 return octave_value(); |
1181
|
696 } |
|
697 |
2095
|
698 DEFUN (fputs, args, , |
3372
|
699 "-*- texinfo -*-\n\ |
|
700 @deftypefn {Built-in Function} {} fputs (@var{fid}, @var{string})\n\ |
|
701 Write a string to a file with no formatting.\n\ |
|
702 @end deftypefn") |
1181
|
703 { |
4233
|
704 octave_value retval = -1; |
1181
|
705 |
|
706 int nargin = args.length (); |
|
707 |
2095
|
708 if (nargin == 2) |
|
709 { |
3341
|
710 octave_stream os = octave_stream_list::lookup (args(0), "fputs"); |
1181
|
711 |
3341
|
712 if (! error_state) |
3340
|
713 retval = os.puts (args(1)); |
2095
|
714 } |
1181
|
715 else |
2095
|
716 print_usage ("fputs"); |
1181
|
717 |
|
718 return retval; |
|
719 } |
|
720 |
2095
|
721 DEFUN (sprintf, args, , |
3372
|
722 "-*- texinfo -*-\n\ |
|
723 @deftypefn {Built-in Function} {} sprintf (@var{template}, @dots{})\n\ |
|
724 This is like @code{printf}, except that the output is returned as a\n\ |
|
725 string. Unlike the C library function, which requires you to provide a\n\ |
|
726 suitably sized string as an argument, Octave's @code{sprintf} function\n\ |
|
727 returns the string, automatically sized to hold all of the items\n\ |
|
728 converted.\n\ |
|
729 @end deftypefn") |
1
|
730 { |
2095
|
731 octave_value_list retval; |
1
|
732 |
2095
|
733 int nargin = args.length (); |
1
|
734 |
2095
|
735 if (nargin > 0) |
1
|
736 { |
2116
|
737 retval(2) = -1.0; |
|
738 retval(1) = "unknown error"; |
|
739 retval(0) = ""; |
|
740 |
3340
|
741 octave_ostrstream *ostr = new octave_ostrstream (); |
1
|
742 |
3340
|
743 octave_stream os (ostr); |
628
|
744 |
3340
|
745 if (os.is_valid ()) |
2095
|
746 { |
|
747 if (args(0).is_string ()) |
|
748 { |
3523
|
749 std::string fmt = args(0).string_value (); |
628
|
750 |
2095
|
751 octave_value_list tmp_args; |
1
|
752 |
2095
|
753 if (nargin > 1) |
|
754 { |
|
755 tmp_args.resize (nargin-1, octave_value ()); |
1
|
756 |
2095
|
757 for (int i = 1; i < nargin; i++) |
|
758 tmp_args(i-1) = args(i); |
|
759 } |
628
|
760 |
4233
|
761 retval(2) = os.printf (fmt, tmp_args); |
2095
|
762 retval(1) = os.error (); |
3340
|
763 retval(0) = ostr->str (); |
2095
|
764 } |
|
765 else |
|
766 ::error ("sprintf: format must be a string"); |
|
767 } |
|
768 else |
|
769 ::error ("sprintf: unable to create output buffer"); |
1
|
770 } |
|
771 else |
2095
|
772 print_usage ("sprintf"); |
1
|
773 |
|
774 return retval; |
|
775 } |
|
776 |
2095
|
777 DEFUN (fscanf, args, , |
3372
|
778 "-*- texinfo -*-\n\ |
|
779 @deftypefn {Built-in Function} {[@var{val}, @var{count}] =} fscanf (@var{fid}, @var{template}, @var{size})\n\ |
3491
|
780 @deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}, @var{count}] = } fscanf (@var{fid}, @var{template}, \"C\")\n\ |
3372
|
781 In the first form, read from @var{fid} according to @var{template},\n\ |
|
782 returning the result in the matrix @var{val}.\n\ |
2122
|
783 \n\ |
3372
|
784 The optional argument @var{size} specifies the amount of data to read\n\ |
|
785 and may be one of\n\ |
|
786 \n\ |
|
787 @table @code\n\ |
|
788 @item Inf\n\ |
|
789 Read as much as possible, returning a column vector.\n\ |
|
790 \n\ |
|
791 @item @var{nr}\n\ |
|
792 Read up to @var{nr} elements, returning a column vector.\n\ |
2122
|
793 \n\ |
3372
|
794 @item [@var{nr}, Inf]\n\ |
|
795 Read as much as possible, returning a matrix with @var{nr} rows. If the\n\ |
|
796 number of elements read is not an exact multiple of @var{nr}, the last\n\ |
|
797 column is padded with zeros.\n\ |
|
798 \n\ |
|
799 @item [@var{nr}, @var{nc}]\n\ |
|
800 Read up to @code{@var{nr} * @var{nc}} elements, returning a matrix with\n\ |
|
801 @var{nr} rows. If the number of elements read is not an exact multiple\n\ |
|
802 of @var{nr}, the last column is padded with zeros.\n\ |
|
803 @end table\n\ |
2122
|
804 \n\ |
3372
|
805 @noindent\n\ |
|
806 If @var{size} is omitted, a value of @code{Inf} is assumed.\n\ |
2122
|
807 \n\ |
3372
|
808 A string is returned if @var{template} specifies only character\n\ |
|
809 conversions.\n\ |
2215
|
810 \n\ |
3372
|
811 The number of items successfully read is returned in @var{count}.\n\ |
2215
|
812 \n\ |
3372
|
813 In the second form, read from @var{fid} according to @var{template},\n\ |
|
814 with each conversion specifier in @var{template} corresponding to a\n\ |
|
815 single scalar return value. This form is more `C-like', and also\n\ |
3491
|
816 compatible with previous versions of Octave. The number of successful\n\ |
|
817 conversions is returned in @var{count}\n\ |
3372
|
818 @end deftypefn") |
1181
|
819 { |
2095
|
820 octave_value_list retval; |
1181
|
821 |
|
822 int nargin = args.length (); |
|
823 |
2215
|
824 if (nargin == 3 && args(2).is_string ()) |
2095
|
825 { |
3341
|
826 octave_stream os = octave_stream_list::lookup (args(0), "fscanf"); |
1181
|
827 |
3341
|
828 if (! error_state) |
2095
|
829 { |
|
830 if (args(1).is_string ()) |
|
831 { |
3523
|
832 std::string fmt = args(1).string_value (); |
1181
|
833 |
3340
|
834 retval = os.oscanf (fmt); |
2095
|
835 } |
|
836 else |
|
837 ::error ("fscanf: format must be a string"); |
|
838 } |
|
839 } |
1181
|
840 else |
2215
|
841 { |
|
842 retval (1) = 0.0; |
|
843 retval (0) = Matrix (); |
|
844 |
|
845 if (nargin == 2 || nargin == 3) |
|
846 { |
3341
|
847 octave_stream os = octave_stream_list::lookup (args(0), "fscanf"); |
2215
|
848 |
3342
|
849 if (! error_state) |
2215
|
850 { |
|
851 if (args(1).is_string ()) |
|
852 { |
3523
|
853 std::string fmt = args(1).string_value (); |
2215
|
854 |
|
855 int count = 0; |
|
856 |
3810
|
857 Array<double> size = (nargin == 3) |
4102
|
858 ? args(2).vector_value () |
|
859 : Array<double> (1, lo_ieee_inf_value ()); |
2215
|
860 |
|
861 if (! error_state) |
|
862 { |
3340
|
863 octave_value tmp = os.scanf (fmt, size, count); |
2215
|
864 |
4233
|
865 retval(1) = 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 () |
4102
|
943 : Array<double> (1, lo_ieee_inf_value ()); |
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; |
4233
|
954 retval(1) = 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\ |
4101
|
1140 after each element is read. If it is not specified, a value of 0 is\n\ |
3372
|
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) |
4102
|
1188 ? args(1) : octave_value (lo_ieee_inf_value ()); |
2116
|
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 |
4233
|
1203 retval(1) = 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 { |
4233
|
1275 octave_value retval = -1; |
2116
|
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 { |
4233
|
1316 octave_value retval = -1; |
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 |
4233
|
1368 retval(1) = 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 { |
4233
|
1411 octave_value retval = -1; |
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 { |
4233
|
1459 octave_value retval = -1; |
1230
|
1460 |
|
1461 int nargin = args.length (); |
|
1462 |
2095
|
1463 if (nargin == 1) |
4233
|
1464 retval = octave_stream_list::remove (args(0), "pclose"); |
1377
|
1465 else |
2095
|
1466 print_usage ("pclose"); |
1379
|
1467 |
|
1468 return retval; |
|
1469 } |
|
1470 |
2458
|
1471 DEFUN (tmpnam, args, , |
3372
|
1472 "-*- texinfo -*-\n\ |
|
1473 @deftypefn {Built-in Function} {} tmpnam ()\n\ |
|
1474 Return a unique temporary file name as a string.\n\ |
|
1475 \n\ |
|
1476 Since the named file is not opened, by @code{tmpnam}, it\n\ |
|
1477 is possible (though relatively unlikely) that it will not be available\n\ |
|
1478 by the time your program attempts to open it.\n\ |
|
1479 @end deftypefn") |
1802
|
1480 { |
2095
|
1481 octave_value retval; |
1802
|
1482 |
2936
|
1483 int len = args.length (); |
|
1484 |
|
1485 if (len < 3) |
|
1486 { |
3523
|
1487 std::string dir = len > 0 ? args(0).string_value () : std::string (); |
|
1488 std::string pfx = len > 1 ? args(1).string_value () : std::string ("oct-"); |
2936
|
1489 |
|
1490 if (! error_state) |
|
1491 retval = file_ops::tempnam (dir, pfx); |
|
1492 } |
1802
|
1493 else |
2458
|
1494 print_usage ("tmpnam"); |
1802
|
1495 |
|
1496 return retval; |
|
1497 } |
|
1498 |
2458
|
1499 DEFALIAS (octave_tmp_file_name, tmpnam); |
|
1500 |
1400
|
1501 static int |
|
1502 convert (int x, int ibase, int obase) |
|
1503 { |
|
1504 int retval = 0; |
|
1505 |
|
1506 int tmp = x % obase; |
|
1507 |
|
1508 if (tmp > ibase - 1) |
2095
|
1509 ::error ("umask: invalid digit"); |
1400
|
1510 else |
|
1511 { |
|
1512 retval = tmp; |
|
1513 int mult = ibase; |
|
1514 while ((x = (x - tmp) / obase)) |
|
1515 { |
|
1516 tmp = x % obase; |
|
1517 if (tmp > ibase - 1) |
|
1518 { |
2095
|
1519 ::error ("umask: invalid digit"); |
1400
|
1520 break; |
|
1521 } |
|
1522 retval += mult * tmp; |
|
1523 mult *= ibase; |
|
1524 } |
|
1525 } |
|
1526 |
|
1527 return retval; |
|
1528 } |
|
1529 |
1957
|
1530 DEFUN (umask, args, , |
3301
|
1531 "-*- texinfo -*-\n\ |
|
1532 @deftypefn {Built-in Function} {} umask (@var{mask})\n\ |
|
1533 Set the permission mask for file creation. The parameter @var{mask}\n\ |
|
1534 is an integer, interpreted as an octal number. If successful,\n\ |
|
1535 returns the previous value of the mask (as an integer to be\n\ |
|
1536 interpreted as an octal number); otherwise an error message is printed.\n\ |
|
1537 @end deftypefn") |
1400
|
1538 { |
2095
|
1539 octave_value_list retval; |
1400
|
1540 |
|
1541 int status = 0; |
|
1542 |
|
1543 if (args.length () == 1) |
|
1544 { |
3202
|
1545 int mask = args(0).int_value (true); |
1400
|
1546 |
3202
|
1547 if (! error_state) |
1400
|
1548 { |
3202
|
1549 if (mask < 0) |
1400
|
1550 { |
|
1551 status = -1; |
2095
|
1552 ::error ("umask: MASK must be a positive integer value"); |
1400
|
1553 } |
|
1554 else |
|
1555 { |
|
1556 int oct_mask = convert (mask, 8, 10); |
|
1557 |
|
1558 if (! error_state) |
2926
|
1559 status = convert (file_ops::umask (oct_mask), 10, 8); |
1400
|
1560 } |
|
1561 } |
3202
|
1562 else |
|
1563 { |
|
1564 status = -1; |
|
1565 ::error ("umask: expecting integer argument"); |
|
1566 } |
1400
|
1567 } |
|
1568 else |
|
1569 print_usage ("umask"); |
|
1570 |
|
1571 if (status >= 0) |
4233
|
1572 retval(0) = status; |
1400
|
1573 |
|
1574 return retval; |
|
1575 } |
|
1576 |
2189
|
1577 void |
|
1578 symbols_of_file_io (void) |
|
1579 { |
2341
|
1580 // NOTE: the values of SEEK_SET, SEEK_CUR, and SEEK_END have to be |
|
1581 // this way for Matlab compatibility. |
|
1582 |
4233
|
1583 DEFCONSTX ("SEEK_SET", SBV_SEEK_SET, -1, |
3372
|
1584 "-*- texinfo -*-\n\ |
|
1585 @defvr {Built-in Variable} SEEK_SET\n\ |
|
1586 @defvrx {Built-in Variable} SEEK_CUR\n\ |
|
1587 @defvrx {Built-in Variable} SEEK_END\n\ |
|
1588 These variables may be used as the optional third argument for the\n\ |
|
1589 function @code{fseek}.\n\ |
|
1590 \n\ |
|
1591 @table @code\n\ |
|
1592 @item SEEK_SET\n\ |
|
1593 Position file relative to the beginning.\n\ |
|
1594 \n\ |
|
1595 @item SEEK_CUR\n\ |
|
1596 Position file relative to the current position.\n\ |
|
1597 \n\ |
|
1598 @item SEEK_END\n\ |
|
1599 used with fseek to position file relative to the end.\n\ |
|
1600 @end table\n\ |
|
1601 @end defvr"); |
2189
|
1602 |
4233
|
1603 DEFCONSTX ("SEEK_CUR", SBV_SEEK_CUR, 0, |
3458
|
1604 "-*- texinfo -*-\n\ |
|
1605 @defvr {Built-in Variable} SEEK_CUR\n\ |
|
1606 See SEEK_SET.\n\ |
|
1607 @end defvr"); |
2189
|
1608 |
4233
|
1609 DEFCONSTX ("SEEK_END", SBV_SEEK_END, 1, |
3458
|
1610 "-*- texinfo -*-\n\ |
|
1611 @defvr {Built-in Variable} SEEK_END\n\ |
|
1612 See SEEK_SET.\n\ |
|
1613 @end defvr"); |
2189
|
1614 |
3141
|
1615 DEFCONSTX ("stdin", SBV_stdin, stdin_file, |
3372
|
1616 "-*- texinfo -*-\n\ |
|
1617 @defvr {Built-in Variable} stdin\n\ |
|
1618 The standard input stream (file id 0). When Octave is used\n\ |
|
1619 interactively, this is filtered through the command line editing\n\ |
|
1620 functions.\n\ |
|
1621 @end defvr"); |
2189
|
1622 |
3141
|
1623 DEFCONSTX ("stdout", SBV_stdout, stdout_file, |
3372
|
1624 "-*- texinfo -*-\n\ |
|
1625 @defvr {Built-in Variable} stdout\n\ |
|
1626 The standard output stream (file id 1). Data written to the\n\ |
|
1627 standard output is normally filtered through the pager.\n\ |
|
1628 @end defvr"); |
2189
|
1629 |
3141
|
1630 DEFCONSTX ("stderr", SBV_stderr, stderr_file, |
3372
|
1631 "-*- texinfo -*-\n\ |
|
1632 @defvr {Built-in Variable} stderr\n\ |
|
1633 The standard error stream (file id 2). Even if paging is turned on,\n\ |
|
1634 the standard error is not sent to the pager. It is useful for error\n\ |
|
1635 messages and prompts.\n\ |
|
1636 @end defvr"); |
|
1637 |
2189
|
1638 } |
|
1639 |
444
|
1640 /* |
1
|
1641 ;;; Local Variables: *** |
|
1642 ;;; mode: C++ *** |
|
1643 ;;; End: *** |
|
1644 */ |