Mercurial > hg > octave-nkf
annotate src/file-io.cc @ 10824:1e6664326d32
rsf2csf: Change first documentation line to active voice.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Tue, 27 Jul 2010 10:46:11 -0700 |
parents | 3140cb7a05a1 |
children | 89f4d7e294cc |
rev | line source |
---|---|
1 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, |
8920 | 4 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 John W. Eaton |
1 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
1 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
1 | 21 |
22 */ | |
23 | |
2095 | 24 // Originally written by John C. Campbell <jcc@bevo.che.wisc.edu> |
1230 | 25 // |
2095 | 26 // Thomas Baier <baier@ci.tuwien.ac.at> added the original versions of |
27 // the following functions: | |
1230 | 28 // |
2095 | 29 // popen |
30 // pclose | |
31 // execute (now popen2.m) | |
32 // sync_system (now merged with system) | |
33 // async_system (now merged with system) | |
1230 | 34 |
9322 | 35 // Extensively revised by John W. Eaton <jwe@octave.org>, |
2095 | 36 // April 1996. |
1 | 37 |
240 | 38 #ifdef HAVE_CONFIG_H |
1230 | 39 #include <config.h> |
1 | 40 #endif |
41 | |
4797 | 42 #include <cerrno> |
2095 | 43 #include <climits> |
4797 | 44 #include <cstdio> |
1343 | 45 |
3503 | 46 #include <iostream> |
7336 | 47 #include <stack> |
4726 | 48 #include <vector> |
1350 | 49 |
1230 | 50 #include <sys/types.h> |
1 | 51 #include <unistd.h> |
1350 | 52 |
5325 | 53 #ifdef HAVE_ZLIB_H |
54 #include <zlib.h> | |
55 #endif | |
56 | |
5102 | 57 #include "error.h" |
2926 | 58 #include "file-ops.h" |
6159 | 59 #include "file-stat.h" |
5102 | 60 #include "lo-ieee.h" |
6159 | 61 #include "oct-env.h" |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
62 #include "oct-locbuf.h" |
2926 | 63 |
1352 | 64 #include "defun.h" |
6108 | 65 #include "file-io.h" |
6159 | 66 #include "load-path.h" |
2095 | 67 #include "oct-fstrm.h" |
68 #include "oct-iostrm.h" | |
1377 | 69 #include "oct-map.h" |
1750 | 70 #include "oct-obj.h" |
2095 | 71 #include "oct-prcstrm.h" |
72 #include "oct-stream.h" | |
73 #include "oct-strstrm.h" | |
1 | 74 #include "pager.h" |
444 | 75 #include "sysdep.h" |
1352 | 76 #include "utils.h" |
2370 | 77 #include "variables.h" |
1583 | 78 |
2902 | 79 static octave_value stdin_file; |
80 static octave_value stdout_file; | |
81 static octave_value stderr_file; | |
82 | |
4468 | 83 static octave_stream stdin_stream; |
84 static octave_stream stdout_stream; | |
85 static octave_stream stderr_stream; | |
86 | |
1 | 87 void |
164 | 88 initialize_file_io (void) |
1 | 89 { |
4468 | 90 stdin_stream = octave_istream::create (&std::cin, "stdin"); |
2116 | 91 |
3531 | 92 // This uses octave_stdout (see pager.h), not std::cout so that Octave's |
2116 | 93 // standard output stream will pass through the pager. |
94 | |
4468 | 95 stdout_stream = octave_ostream::create (&octave_stdout, "stdout"); |
2116 | 96 |
4468 | 97 stderr_stream = octave_ostream::create (&std::cerr, "stderr"); |
1 | 98 |
2902 | 99 stdin_file = octave_stream_list::insert (stdin_stream); |
100 stdout_file = octave_stream_list::insert (stdout_stream); | |
101 stderr_file = octave_stream_list::insert (stderr_stream); | |
1 | 102 } |
103 | |
2095 | 104 void |
105 close_files (void) | |
1 | 106 { |
2095 | 107 octave_stream_list::clear (); |
108 } | |
636 | 109 |
5102 | 110 // List of files to delete when we exit or crash. |
111 // | |
5775 | 112 // FIXME -- this should really be static, but that causes |
5102 | 113 // problems on some systems. |
114 std::stack <std::string> tmp_files; | |
115 | |
116 void | |
117 mark_for_deletion (const std::string& file) | |
118 { | |
119 tmp_files.push (file); | |
120 } | |
121 | |
122 void | |
123 cleanup_tmp_files (void) | |
124 { | |
125 while (! tmp_files.empty ()) | |
126 { | |
127 std::string filename = tmp_files.top (); | |
128 tmp_files.pop (); | |
10411 | 129 gnulib::unlink (filename.c_str ()); |
5102 | 130 } |
131 } | |
132 | |
4036 | 133 static std::ios::openmode |
5325 | 134 fopen_mode_to_ios_mode (const std::string& mode_arg) |
1 | 135 { |
4036 | 136 std::ios::openmode retval = std::ios::in; |
896 | 137 |
5325 | 138 if (! mode_arg.empty ()) |
368 | 139 { |
2095 | 140 // Could probably be faster, but does it really matter? |
1766 | 141 |
5325 | 142 std::string mode = mode_arg; |
143 | |
7078 | 144 // 'W' and 'R' are accepted as 'w' and 'r', but we warn about |
145 // them because Matlab says they perform "automatic flushing" | |
146 // but we don't know precisely what action that implies. | |
147 | |
148 size_t pos = mode.find ('W'); | |
149 | |
8021 | 150 if (pos != std::string::npos) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
151 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
152 warning ("fopen: treating mode \"W\" as equivalent to \"w\""); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
153 mode[pos] = 'w'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
154 } |
7078 | 155 |
156 pos = mode.find ('R'); | |
157 | |
8021 | 158 if (pos != std::string::npos) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
159 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
160 warning ("fopen: treating mode \"R\" as equivalent to \"r\""); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
161 mode[pos] = 'r'; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
162 } |
7078 | 163 |
164 pos = mode.find ('z'); | |
5325 | 165 |
8021 | 166 if (pos != std::string::npos) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
167 { |
5325 | 168 #if defined (HAVE_ZLIB) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
169 mode.erase (pos, 1); |
5325 | 170 #else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
171 error ("this version of Octave does not support gzipped files"); |
5325 | 172 #endif |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
173 } |
5325 | 174 |
175 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
176 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
177 if (mode == "rt") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
178 retval = std::ios::in; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
179 else if (mode == "wt") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
180 retval = std::ios::out | std::ios::trunc; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
181 else if (mode == "at") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
182 retval = std::ios::out | std::ios::app; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
183 else if (mode == "r+t" || mode == "rt+") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
184 retval = std::ios::in | std::ios::out; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
185 else if (mode == "w+t" || mode == "wt+") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
186 retval = std::ios::in | std::ios::out | std::ios::trunc; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
187 else if (mode == "a+t" || mode == "at+") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
188 retval = std::ios::in | std::ios::out | std::ios::app; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
189 else if (mode == "rb" || mode == "r") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
190 retval = std::ios::in | std::ios::binary; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
191 else if (mode == "wb" || mode == "w") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
192 retval = std::ios::out | std::ios::trunc | std::ios::binary; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
193 else if (mode == "ab" || mode == "a") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
194 retval = std::ios::out | std::ios::app | std::ios::binary; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
195 else if (mode == "r+b" || mode == "rb+" || mode == "r+") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
196 retval = std::ios::in | std::ios::out | std::ios::binary; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
197 else if (mode == "w+b" || mode == "wb+" || mode == "w+") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
198 retval = (std::ios::in | std::ios::out | std::ios::trunc |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
199 | std::ios::binary); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
200 else if (mode == "a+b" || mode == "ab+" || mode == "a+") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
201 retval = (std::ios::in | std::ios::out | std::ios::app |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
202 | std::ios::binary); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
203 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
204 ::error ("invalid mode specified"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
205 } |
1 | 206 } |
207 | |
208 return retval; | |
209 } | |
210 | |
1957 | 211 DEFUN (fclose, args, , |
3372 | 212 "-*- texinfo -*-\n\ |
213 @deftypefn {Built-in Function} {} fclose (@var{fid})\n\ | |
4594 | 214 Closes the specified file. If successful, @code{fclose} returns 0,\n\ |
215 otherwise, it returns -1.\n\ | |
5642 | 216 @seealso{fopen, fseek, ftell}\n\ |
217 @end deftypefn") | |
529 | 218 { |
4233 | 219 octave_value retval = -1; |
529 | 220 |
221 int nargin = args.length (); | |
222 | |
2095 | 223 if (nargin == 1) |
4233 | 224 retval = octave_stream_list::remove (args(0), "fclose"); |
1 | 225 else |
5823 | 226 print_usage (); |
1 | 227 |
228 return retval; | |
229 } | |
230 | |
5144 | 231 DEFUN (fclear, args, , |
232 "-*- texinfo -*-\n\ | |
233 @deftypefn {Built-in Function} {} fclear (@var{fid})\n\ | |
234 Clear the stream state for the specified file.\n\ | |
235 @end deftypefn") | |
236 { | |
237 octave_value retval; | |
238 | |
239 int nargin = args.length (); | |
240 | |
241 if (nargin == 1) | |
242 { | |
243 int fid = octave_stream_list::get_file_number (args (0)); | |
244 | |
245 octave_stream os = octave_stream_list::lookup (fid, "fclear"); | |
246 | |
247 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
248 os.clearerr (); |
5144 | 249 } |
250 else | |
5823 | 251 print_usage (); |
5144 | 252 |
253 return retval; | |
254 } | |
255 | |
1957 | 256 DEFUN (fflush, args, , |
3372 | 257 "-*- texinfo -*-\n\ |
258 @deftypefn {Built-in Function} {} fflush (@var{fid})\n\ | |
259 Flush output to @var{fid}. This is useful for ensuring that all\n\ | |
260 pending output makes it to the screen before some other event occurs.\n\ | |
261 For example, it is always a good idea to flush the standard output\n\ | |
262 stream before calling @code{input}.\n\ | |
5095 | 263 \n\ |
264 @code{fflush} returns 0 on success and an OS dependent error value\n\ | |
10791
3140cb7a05a1
Add spellchecker scripts for Octave and run spellcheck of documentation
Rik <octave@nomad.inbox5.com>
parents:
10411
diff
changeset
|
265 (@minus{}1 on Unix) on error.\n\ |
5642 | 266 @seealso{fopen, fclose}\n\ |
267 @end deftypefn") | |
1181 | 268 { |
4233 | 269 octave_value retval = -1; |
1181 | 270 |
271 int nargin = args.length (); | |
272 | |
2095 | 273 if (nargin == 1) |
274 { | |
5775 | 275 // FIXME -- any way to avoid special case for stdout? |
2609 | 276 |
277 int fid = octave_stream_list::get_file_number (args (0)); | |
278 | |
279 if (fid == 1) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
280 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
281 flush_octave_stdout (); |
2095 | 282 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
283 retval = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
284 } |
2095 | 285 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
286 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
287 octave_stream os = octave_stream_list::lookup (fid, "fflush"); |
2609 | 288 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
289 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
290 retval = os.flush (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
291 } |
2095 | 292 } |
293 else | |
5823 | 294 print_usage (); |
1181 | 295 |
296 return retval; | |
297 } | |
298 | |
2095 | 299 DEFUN (fgetl, args, , |
3372 | 300 "-*- texinfo -*-\n\ |
301 @deftypefn {Built-in Function} {} fgetl (@var{fid}, @var{len})\n\ | |
302 Read characters from a file, stopping after a newline, or EOF,\n\ | |
303 or @var{len} characters have been read. The characters read, excluding\n\ | |
304 the possible trailing newline, are returned as a string.\n\ | |
1339 | 305 \n\ |
3372 | 306 If @var{len} is omitted, @code{fgetl} reads until the next newline\n\ |
307 character.\n\ | |
308 \n\ | |
309 If there are no more characters to read, @code{fgetl} returns @minus{}1.\n\ | |
5642 | 310 @seealso{fread, fscanf}\n\ |
311 @end deftypefn") | |
1339 | 312 { |
4468 | 313 static std::string who = "fgetl"; |
314 | |
2599 | 315 octave_value_list retval; |
316 | |
4233 | 317 retval(1) = 0; |
318 retval(0) = -1; | |
1339 | 319 |
320 int nargin = args.length (); | |
321 | |
322 if (nargin == 1 || nargin == 2) | |
2095 | 323 { |
4468 | 324 octave_stream os = octave_stream_list::lookup (args(0), who); |
2095 | 325 |
3341 | 326 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
327 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
328 octave_value len_arg = (nargin == 2) ? args(1) : octave_value (); |
2095 | 329 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
330 bool err = false; |
2095 | 331 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
332 std::string tmp = os.getl (len_arg, err, who); |
2095 | 333 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
334 if (! (error_state || err)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
335 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
336 retval(1) = tmp.length (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
337 retval(0) = tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
338 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
339 } |
2095 | 340 } |
1339 | 341 else |
5823 | 342 print_usage (); |
1339 | 343 |
344 return retval; | |
345 } | |
346 | |
2095 | 347 DEFUN (fgets, args, , |
3372 | 348 "-*- texinfo -*-\n\ |
349 @deftypefn {Built-in Function} {} fgets (@var{fid}, @var{len})\n\ | |
350 Read characters from a file, stopping after a newline, or EOF,\n\ | |
351 or @var{len} characters have been read. The characters read, including\n\ | |
352 the possible trailing newline, are returned as a string.\n\ | |
529 | 353 \n\ |
3372 | 354 If @var{len} is omitted, @code{fgets} reads until the next newline\n\ |
355 character.\n\ | |
356 \n\ | |
357 If there are no more characters to read, @code{fgets} returns @minus{}1.\n\ | |
7781 | 358 @seealso{fputs, fopen, fread, fscanf}\n\ |
5642 | 359 @end deftypefn") |
529 | 360 { |
4468 | 361 static std::string who = "fgets"; |
362 | |
2599 | 363 octave_value_list retval; |
364 | |
365 retval(1) = 0.0; | |
366 retval(0) = -1.0; | |
529 | 367 |
368 int nargin = args.length (); | |
369 | |
1338 | 370 if (nargin == 1 || nargin == 2) |
2095 | 371 { |
4468 | 372 octave_stream os = octave_stream_list::lookup (args(0), who); |
2095 | 373 |
3341 | 374 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
375 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
376 octave_value len_arg = (nargin == 2) ? args(1) : octave_value (); |
2095 | 377 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
378 bool err = false; |
2095 | 379 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
380 std::string tmp = os.gets (len_arg, err, who); |
2095 | 381 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
382 if (! (error_state || err)) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
383 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
384 retval(1) = tmp.length (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
385 retval(0) = tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
386 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
387 } |
2095 | 388 } |
1338 | 389 else |
5823 | 390 print_usage (); |
529 | 391 |
392 return retval; | |
393 } | |
394 | |
9701 | 395 DEFUN (fskipl, args, , |
396 "-*- texinfo -*-\n\ | |
397 @deftypefn {Built-in Function} {} fskipl (@var{fid}, @var{count})\n\ | |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9701
diff
changeset
|
398 Skips a given number of lines, i.e., discards characters until an end-of-line\n\ |
9701 | 399 is met exactly @var{count}-times, or end-of-file occurs.\n\ |
400 Returns the number of lines skipped (end-of-line sequences encountered).\n\ | |
401 If @var{count} is omitted, it defaults to 1. @var{count} may also be\n\ | |
402 @code{Inf}, in which case lines are skipped to the end of file.\n\ | |
403 This form is suitable for counting lines in a file.\n\ | |
404 @seealso{fgetl, fgets}\n\ | |
405 @end deftypefn") | |
406 { | |
407 static std::string who = "fskipl"; | |
408 | |
409 octave_value retval; | |
410 | |
411 int nargin = args.length (); | |
412 | |
413 if (nargin == 1 || nargin == 2) | |
414 { | |
415 octave_stream os = octave_stream_list::lookup (args(0), who); | |
416 | |
417 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
418 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
419 octave_value count_arg = (nargin == 2) ? args(1) : octave_value (); |
9701 | 420 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
421 bool err = false; |
9701 | 422 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
423 long tmp = os.skipl (count_arg, err, who); |
9701 | 424 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
425 if (! (error_state || err)) |
9701 | 426 retval = tmp; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
427 } |
9701 | 428 } |
429 else | |
430 print_usage (); | |
431 | |
432 return retval; | |
433 } | |
434 | |
435 | |
3340 | 436 static octave_stream |
3523 | 437 do_stream_open (const std::string& name, const std::string& mode, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
438 const std::string& arch, int& fid) |
1 | 439 { |
3340 | 440 octave_stream retval; |
1 | 441 |
2095 | 442 fid = -1; |
1 | 443 |
4036 | 444 std::ios::openmode md = fopen_mode_to_ios_mode (mode); |
1 | 445 |
2095 | 446 if (! error_state) |
447 { | |
2318 | 448 oct_mach_info::float_format flt_fmt = |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
449 oct_mach_info::string_to_float_format (arch); |
1 | 450 |
4798 | 451 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
452 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
453 std::string fname = file_ops::tilde_expand (name); |
6159 | 454 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
455 file_stat fs (fname); |
7730
b68e44c90afe
file-io.cc (do_stream_open): return -1 for directories
John W. Eaton <jwe@octave.org>
parents:
7708
diff
changeset
|
456 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
457 if (! (md & std::ios::out |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
458 || octave_env::absolute_pathname (fname) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
459 || octave_env::rooted_relative_pathname (fname))) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
460 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
461 if (! fs.exists ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
462 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
463 std::string tmp |
10250 | 464 = octave_env::make_absolute (load_path::find_file (fname)); |
6159 | 465 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
466 if (! tmp.empty ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
467 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
468 warning_with_id ("Octave:fopen-file-in-path", |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
469 "fopen: file found in load path"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
470 fname = tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
471 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
472 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
473 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
474 |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
475 if (! fs.is_dir ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
476 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
477 std::string tmode = mode; |
6405 | 478 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
479 // Use binary mode if 't' is not specified, but don't add |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
480 // 'b' if it is already present. |
6405 | 481 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
482 size_t bpos = tmode.find ('b'); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
483 size_t tpos = tmode.find ('t'); |
7730
b68e44c90afe
file-io.cc (do_stream_open): return -1 for directories
John W. Eaton <jwe@octave.org>
parents:
7708
diff
changeset
|
484 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
485 if (bpos == std::string::npos && tpos == std::string::npos) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
486 tmode += 'b'; |
6405 | 487 |
6905 | 488 #if defined (HAVE_ZLIB) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
489 size_t pos = tmode.find ('z'); |
4798 | 490 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
491 if (pos != std::string::npos) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
492 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
493 tmode.erase (pos, 1); |
7730
b68e44c90afe
file-io.cc (do_stream_open): return -1 for directories
John W. Eaton <jwe@octave.org>
parents:
7708
diff
changeset
|
494 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
495 gzFile fptr = ::gzopen (fname.c_str (), tmode.c_str ()); |
5325 | 496 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
497 if (fptr) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
498 retval = octave_zstdiostream::create (fname, fptr, md, flt_fmt); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
499 else |
10411 | 500 retval.error (gnulib::strerror (errno)); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
501 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
502 else |
5325 | 503 #endif |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
504 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
505 FILE *fptr = ::fopen (fname.c_str (), tmode.c_str ()); |
5325 | 506 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
507 retval = octave_stdiostream::create (fname, fptr, md, flt_fmt); |
5370 | 508 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
509 if (! fptr) |
10411 | 510 retval.error (gnulib::strerror (errno)); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
511 } |
7730
b68e44c90afe
file-io.cc (do_stream_open): return -1 for directories
John W. Eaton <jwe@octave.org>
parents:
7708
diff
changeset
|
512 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
513 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
514 } |
1 | 515 } |
516 | |
2095 | 517 return retval; |
518 } | |
1 | 519 |
3340 | 520 static octave_stream |
2095 | 521 do_stream_open (const octave_value& tc_name, const octave_value& tc_mode, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
522 const octave_value& tc_arch, const char *fcn, int& fid) |
2095 | 523 { |
3340 | 524 octave_stream retval; |
2095 | 525 |
526 fid = -1; | |
527 | |
3523 | 528 std::string name = tc_name.string_value (); |
2095 | 529 |
530 if (! error_state) | |
1 | 531 { |
3523 | 532 std::string mode = tc_mode.string_value (); |
2095 | 533 |
534 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
535 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
536 std::string arch = tc_arch.string_value (); |
1 | 537 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
538 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
539 retval = do_stream_open (name, mode, arch, fid); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
540 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
541 ::error ("%s: architecture type must be a string", fcn); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
542 } |
2095 | 543 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
544 ::error ("%s: file mode must be a string", fcn); |
1 | 545 } |
2095 | 546 else |
547 ::error ("%s: file name must be a string", fcn); | |
1 | 548 |
549 return retval; | |
550 } | |
551 | |
1957 | 552 DEFUN (fopen, args, , |
3372 | 553 "-*- texinfo -*-\n\ |
554 @deftypefn {Built-in Function} {[@var{fid}, @var{msg}] =} fopen (@var{name}, @var{mode}, @var{arch})\n\ | |
555 @deftypefnx {Built-in Function} {@var{fid_list} =} fopen (\"all\")\n\ | |
5353 | 556 @deftypefnx {Built-in Function} {[@var{file}, @var{mode}, @var{arch}] =} fopen (@var{fid})\n\ |
3372 | 557 The first form of the @code{fopen} function opens the named file with\n\ |
558 the specified mode (read-write, read-only, etc.) and architecture\n\ | |
559 interpretation (IEEE big endian, IEEE little endian, etc.), and returns\n\ | |
560 an integer value that may be used to refer to the file later. If an\n\ | |
561 error occurs, @var{fid} is set to @minus{}1 and @var{msg} contains the\n\ | |
562 corresponding system error message. The @var{mode} is a one or two\n\ | |
563 character string that specifies whether the file is to be opened for\n\ | |
564 reading, writing, or both.\n\ | |
1181 | 565 \n\ |
3372 | 566 The second form of the @code{fopen} function returns a vector of file ids\n\ |
567 corresponding to all the currently open files, excluding the\n\ | |
568 @code{stdin}, @code{stdout}, and @code{stderr} streams.\n\ | |
2318 | 569 \n\ |
5353 | 570 The third form of the @code{fopen} function returns information about the\n\ |
571 open file given its file id.\n\ | |
1181 | 572 \n\ |
3372 | 573 For example,\n\ |
574 \n\ | |
575 @example\n\ | |
576 myfile = fopen (\"splat.dat\", \"r\", \"ieee-le\");\n\ | |
577 @end example\n\ | |
2095 | 578 \n\ |
3372 | 579 @noindent\n\ |
580 opens the file @file{splat.dat} for reading. If necessary, binary\n\ | |
581 numeric values will be read assuming they are stored in IEEE format with\n\ | |
582 the least significant bit first, and then converted to the native\n\ | |
583 representation.\n\ | |
2318 | 584 \n\ |
3372 | 585 Opening a file that is already open simply opens it again and returns a\n\ |
586 separate file id. It is not an error to open a file several times,\n\ | |
587 though writing to the same file through several different file ids may\n\ | |
588 produce unexpected results.\n\ | |
589 \n\ | |
590 The possible values @samp{mode} may have are\n\ | |
591 \n\ | |
592 @table @asis\n\ | |
593 @item @samp{r}\n\ | |
594 Open a file for reading.\n\ | |
3263 | 595 \n\ |
3372 | 596 @item @samp{w}\n\ |
7001 | 597 Open a file for writing. The previous contents are discarded.\n\ |
3372 | 598 \n\ |
599 @item @samp{a}\n\ | |
600 Open or create a file for writing at the end of the file.\n\ | |
601 \n\ | |
602 @item @samp{r+}\n\ | |
603 Open an existing file for reading and writing.\n\ | |
604 \n\ | |
605 @item @samp{w+}\n\ | |
606 Open a file for reading or writing. The previous contents are\n\ | |
607 discarded.\n\ | |
608 \n\ | |
609 @item @samp{a+}\n\ | |
610 Open or create a file for reading or writing at the end of the\n\ | |
611 file.\n\ | |
612 @end table\n\ | |
613 \n\ | |
4865 | 614 Append a \"t\" to the mode string to open the file in text mode or a\n\ |
615 \"b\" to open in binary mode. On Windows and Macintosh systems, text\n\ | |
616 mode reading and writing automatically converts linefeeds to the\n\ | |
617 appropriate line end character for the system (carriage-return linefeed\n\ | |
6665 | 618 on Windows, carriage-return on Macintosh). The default if no mode is\n\ |
4865 | 619 specified is binary mode.\n\ |
620 \n\ | |
5325 | 621 Additionally, you may append a \"z\" to the mode string to open a\n\ |
622 gzipped file for reading or writing. For this to be successful, you\n\ | |
623 must also open the file in binary mode.\n\ | |
624 \n\ | |
3372 | 625 The parameter @var{arch} is a string specifying the default data format\n\ |
626 for the file. Valid values for @var{arch} are:\n\ | |
2318 | 627 \n\ |
3372 | 628 @table @asis\n\ |
629 @samp{native}\n\ | |
630 The format of the current machine (this is the default).\n\ | |
631 \n\ | |
4082 | 632 @samp{ieee-be}\n\ |
3372 | 633 IEEE big endian format.\n\ |
634 \n\ | |
4082 | 635 @samp{ieee-le}\n\ |
3372 | 636 IEEE little endian format.\n\ |
2318 | 637 \n\ |
3372 | 638 @samp{vaxd}\n\ |
639 VAX D floating format.\n\ | |
2318 | 640 \n\ |
3372 | 641 @samp{vaxg}\n\ |
642 VAX G floating format.\n\ | |
2318 | 643 \n\ |
3372 | 644 @samp{cray}\n\ |
645 Cray floating format.\n\ | |
646 @end table\n\ | |
647 \n\ | |
648 @noindent\n\ | |
649 however, conversions are currently only supported for @samp{native}\n\ | |
650 @samp{ieee-be}, and @samp{ieee-le} formats.\n\ | |
7781 | 651 @seealso{fclose, fgets, fputs, fread, fseek, ferror, fprintf, fscanf, ftell, fwrite}\n\ |
5642 | 652 @end deftypefn") |
529 | 653 { |
2599 | 654 octave_value_list retval; |
655 | |
656 retval(0) = -1.0; | |
529 | 657 |
658 int nargin = args.length (); | |
659 | |
2095 | 660 if (nargin == 1) |
661 { | |
3263 | 662 if (args(0).is_string ()) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
663 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
664 // If there is only one argument and it is a string but it |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
665 // is not the string "all", we assume it is a file to open |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
666 // with MODE = "r". To open a file called "all", you have |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
667 // to supply more than one argument. |
3263 | 668 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
669 if (args(0).string_value () == "all") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
670 return octave_stream_list::open_file_numbers (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
671 } |
2095 | 672 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
673 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
674 string_vector tmp = octave_stream_list::get_info (args(0)); |
529 | 675 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
676 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
677 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
678 retval(2) = tmp(2); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
679 retval(1) = tmp(1); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
680 retval(0) = tmp(0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
681 } |
3263 | 682 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
683 return retval; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
684 } |
1 | 685 } |
686 | |
2095 | 687 if (nargin > 0 && nargin < 4) |
688 { | |
689 octave_value mode = (nargin == 2 || nargin == 3) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
690 ? args(1) : octave_value ("r"); |
2095 | 691 |
692 octave_value arch = (nargin == 3) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
693 ? args(2) : octave_value ("native"); |
2095 | 694 |
695 int fid = -1; | |
696 | |
3340 | 697 octave_stream os = do_stream_open (args(0), mode, arch, "fopen", fid); |
2095 | 698 |
5370 | 699 if (os && ! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
700 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
701 retval(1) = ""; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
702 retval(0) = octave_stream_list::insert (os); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
703 } |
2095 | 704 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
705 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
706 int error_number = 0; |
5370 | 707 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
708 retval(1) = os.error (false, error_number); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
709 retval(0) = -1.0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
710 } |
2095 | 711 } |
712 else | |
5823 | 713 print_usage (); |
1 | 714 |
715 return retval; | |
716 } | |
717 | |
1957 | 718 DEFUN (freport, args, , |
3372 | 719 "-*- texinfo -*-\n\ |
720 @deftypefn {Built-in Function} {} freport ()\n\ | |
721 Print a list of which files have been opened, and whether they are open\n\ | |
722 for reading, writing, or both. For example,\n\ | |
723 \n\ | |
724 @example\n\ | |
725 @group\n\ | |
726 freport ()\n\ | |
727 \n\ | |
728 @print{} number mode name\n\ | |
729 @print{} \n\ | |
730 @print{} 0 r stdin\n\ | |
731 @print{} 1 w stdout\n\ | |
732 @print{} 2 w stderr\n\ | |
733 @print{} 3 r myfile\n\ | |
734 @end group\n\ | |
735 @end example\n\ | |
736 @end deftypefn") | |
1181 | 737 { |
2095 | 738 octave_value_list retval; |
1181 | 739 |
740 int nargin = args.length (); | |
741 | |
742 if (nargin > 0) | |
743 warning ("freport: ignoring extra arguments"); | |
744 | |
2095 | 745 octave_stdout << octave_stream_list::list_open_files (); |
1181 | 746 |
747 return retval; | |
748 } | |
749 | |
4715 | 750 DEFUN (frewind, args, nargout, |
3372 | 751 "-*- texinfo -*-\n\ |
752 @deftypefn {Built-in Function} {} frewind (@var{fid})\n\ | |
753 Move the file pointer to the beginning of the file @var{fid}, returning\n\ | |
4715 | 754 0 for success, and -1 if an error was encountered. It is equivalent to\n\ |
3372 | 755 @code{fseek (@var{fid}, 0, SEEK_SET)}.\n\ |
756 @end deftypefn") | |
529 | 757 { |
4715 | 758 octave_value retval; |
759 | |
760 int result = -1; | |
1 | 761 |
506 | 762 int nargin = args.length (); |
763 | |
2095 | 764 if (nargin == 1) |
1086 | 765 { |
3341 | 766 octave_stream os = octave_stream_list::lookup (args(0), "frewind"); |
636 | 767 |
3341 | 768 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
769 result = os.rewind (); |
1 | 770 } |
771 else | |
5823 | 772 print_usage (); |
1 | 773 |
4715 | 774 if (nargout > 0) |
775 retval = result; | |
776 | |
1 | 777 return retval; |
778 } | |
779 | |
1957 | 780 DEFUN (fseek, args, , |
3372 | 781 "-*- texinfo -*-\n\ |
782 @deftypefn {Built-in Function} {} fseek (@var{fid}, @var{offset}, @var{origin})\n\ | |
5095 | 783 Set the file pointer to any location within the file @var{fid}.\n\ |
784 \n\ | |
785 The pointer is positioned @var{offset} characters from the @var{origin},\n\ | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
786 which may be one of the predefined variables @w{@code{SEEK_CUR}} (current\n\ |
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
787 position), @w{@code{SEEK_SET}} (beginning), or @w{@code{SEEK_END}} (end of\n\ |
9039
51dc9691f23f
Cleanup documentation files errors.texi, debug.texi, io.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
788 file) or strings \"cof\", \"bof\" or \"eof\". If @var{origin} is omitted,\n\ |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
789 @w{@code{SEEK_SET}} is assumed. The offset must be zero, or a value returned\n\ |
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
790 by @code{ftell} (in which case @var{origin} must be @w{@code{SEEK_SET}}).\n\ |
5095 | 791 \n\ |
792 Return 0 on success and -1 on error.\n\ | |
5642 | 793 @seealso{ftell, fopen, fclose}\n\ |
794 @end deftypefn") | |
529 | 795 { |
4233 | 796 octave_value retval = -1; |
529 | 797 |
798 int nargin = args.length (); | |
799 | |
2095 | 800 if (nargin == 2 || nargin == 3) |
801 { | |
3341 | 802 octave_stream os = octave_stream_list::lookup (args(0), "fseek"); |
1181 | 803 |
3341 | 804 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
805 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
806 octave_value origin_arg = (nargin == 3) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
807 ? args(2) : octave_value (-1.0); |
1 | 808 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
809 retval = os.seek (args(1), origin_arg); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
810 } |
368 | 811 } |
2095 | 812 else |
5823 | 813 print_usage (); |
1 | 814 |
815 return retval; | |
816 } | |
817 | |
1957 | 818 DEFUN (ftell, args, , |
3372 | 819 "-*- texinfo -*-\n\ |
820 @deftypefn {Built-in Function} {} ftell (@var{fid})\n\ | |
821 Return the position of the file pointer as the number of characters\n\ | |
822 from the beginning of the file @var{fid}.\n\ | |
5642 | 823 @seealso{fseek, fopen, fclose}\n\ |
824 @end deftypefn") | |
1181 | 825 { |
4797 | 826 octave_value retval = -1; |
1 | 827 |
506 | 828 int nargin = args.length (); |
829 | |
2095 | 830 if (nargin == 1) |
1 | 831 { |
3341 | 832 octave_stream os = octave_stream_list::lookup (args(0), "ftell"); |
1 | 833 |
3341 | 834 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
835 retval = os.tell (); |
1 | 836 } |
837 else | |
5823 | 838 print_usage (); |
1 | 839 |
840 return retval; | |
841 } | |
842 | |
3737 | 843 DEFUN (fprintf, args, nargout, |
3372 | 844 "-*- texinfo -*-\n\ |
845 @deftypefn {Built-in Function} {} fprintf (@var{fid}, @var{template}, @dots{})\n\ | |
846 This function is just like @code{printf}, except that the output is\n\ | |
847 written to the stream @var{fid} instead of @code{stdout}.\n\ | |
8606
0611b48a2b61
file-io.cc (Ffprintf): doc fix
John W. Eaton <jwe@octave.org>
parents:
8377
diff
changeset
|
848 If @var{fid} is omitted, the output is written to @code{stdout}.\n\ |
5642 | 849 @seealso{printf, sprintf, fread, fscanf, fopen, fclose}\n\ |
850 @end deftypefn") | |
1181 | 851 { |
4468 | 852 static std::string who = "fprintf"; |
853 | |
4715 | 854 octave_value retval; |
855 | |
856 int result = -1; | |
4468 | 857 |
1181 | 858 int nargin = args.length (); |
859 | |
2875 | 860 if (nargin > 1 || (nargin > 0 && args(0).is_string ())) |
2095 | 861 { |
3340 | 862 octave_stream os; |
2873 | 863 int fmt_n = 0; |
864 | |
3737 | 865 if (args(0).is_string ()) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
866 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
867 os = octave_stream_list::lookup (1, who); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
868 } |
2873 | 869 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
870 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
871 fmt_n = 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
872 os = octave_stream_list::lookup (args(0), who); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
873 } |
2095 | 874 |
3341 | 875 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
876 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
877 if (args(fmt_n).is_string ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
878 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
879 octave_value_list tmp_args; |
2095 | 880 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
881 if (nargin > 1 + fmt_n) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
882 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
883 tmp_args.resize (nargin-fmt_n-1, octave_value ()); |
2095 | 884 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
885 for (int i = fmt_n + 1; i < nargin; i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
886 tmp_args(i-fmt_n-1) = args(i); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
887 } |
2095 | 888 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
889 result = os.printf (args(fmt_n), tmp_args, who); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
890 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
891 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
892 ::error ("%s: format must be a string", who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
893 } |
2095 | 894 } |
895 else | |
5823 | 896 print_usage (); |
1181 | 897 |
4715 | 898 if (nargout > 0) |
899 retval = result; | |
900 | |
901 return retval; | |
1181 | 902 } |
903 | |
4715 | 904 DEFUN (printf, args, nargout, |
4468 | 905 "-*- texinfo -*-\n\ |
5095 | 906 @deftypefn {Built-in Function} {} printf (@var{template}, @dots{})\n\ |
4468 | 907 Print optional arguments under the control of the template string\n\ |
5653 | 908 @var{template} to the stream @code{stdout} and return the number of\n\ |
909 characters printed.\n\ | |
910 @ifclear OCTAVE_MANUAL\n\ | |
5095 | 911 \n\ |
5653 | 912 See the Formatted Output section of the GNU Octave manual for a\n\ |
913 complete description of the syntax of the template string.\n\ | |
914 @end ifclear\n\ | |
5642 | 915 @seealso{fprintf, sprintf, scanf}\n\ |
916 @end deftypefn") | |
4468 | 917 { |
918 static std::string who = "printf"; | |
919 | |
4715 | 920 octave_value retval; |
921 | |
922 int result = -1; | |
4468 | 923 |
924 int nargin = args.length (); | |
925 | |
926 if (nargin > 0) | |
927 { | |
928 if (args(0).is_string ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
929 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
930 octave_value_list tmp_args; |
4468 | 931 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
932 if (nargin > 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
933 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
934 tmp_args.resize (nargin-1, octave_value ()); |
4468 | 935 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
936 for (int i = 1; i < nargin; i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
937 tmp_args(i-1) = args(i); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
938 } |
4468 | 939 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
940 result = stdout_stream.printf (args(0), tmp_args, who); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
941 } |
4468 | 942 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
943 ::error ("%s: format must be a string", who.c_str ()); |
4468 | 944 } |
945 else | |
5823 | 946 print_usage (); |
4468 | 947 |
4715 | 948 if (nargout > 0) |
949 retval = result; | |
950 | |
4468 | 951 return retval; |
952 } | |
953 | |
2095 | 954 DEFUN (fputs, args, , |
3372 | 955 "-*- texinfo -*-\n\ |
956 @deftypefn {Built-in Function} {} fputs (@var{fid}, @var{string})\n\ | |
957 Write a string to a file with no formatting.\n\ | |
5095 | 958 \n\ |
959 Return a non-negative number on success and EOF on error.\n\ | |
7781 | 960 @seealso{scanf, sscanf, fread, fprintf, fgets, fscanf}\n\ |
3372 | 961 @end deftypefn") |
1181 | 962 { |
4468 | 963 static std::string who = "fputs"; |
964 | |
4233 | 965 octave_value retval = -1; |
1181 | 966 |
967 int nargin = args.length (); | |
968 | |
2095 | 969 if (nargin == 2) |
970 { | |
4468 | 971 octave_stream os = octave_stream_list::lookup (args(0), who); |
1181 | 972 |
3341 | 973 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
974 retval = os.puts (args(1), who); |
2095 | 975 } |
1181 | 976 else |
5823 | 977 print_usage (); |
4468 | 978 |
979 return retval; | |
980 } | |
981 | |
982 DEFUN (puts, args, , | |
983 "-*- texinfo -*-\n\ | |
984 @deftypefn {Built-in Function} {} puts (@var{string})\n\ | |
985 Write a string to the standard output with no formatting.\n\ | |
5095 | 986 \n\ |
987 Return a non-negative number on success and EOF on error.\n\ | |
4468 | 988 @end deftypefn") |
989 { | |
990 static std::string who = "puts"; | |
991 | |
992 octave_value retval = -1; | |
993 | |
994 if (args.length () == 1) | |
995 retval = stdout_stream.puts (args(0), who); | |
996 else | |
5823 | 997 print_usage (); |
1181 | 998 |
999 return retval; | |
1000 } | |
1001 | |
2095 | 1002 DEFUN (sprintf, args, , |
3372 | 1003 "-*- texinfo -*-\n\ |
1004 @deftypefn {Built-in Function} {} sprintf (@var{template}, @dots{})\n\ | |
1005 This is like @code{printf}, except that the output is returned as a\n\ | |
1006 string. Unlike the C library function, which requires you to provide a\n\ | |
1007 suitably sized string as an argument, Octave's @code{sprintf} function\n\ | |
1008 returns the string, automatically sized to hold all of the items\n\ | |
1009 converted.\n\ | |
5642 | 1010 @seealso{printf, fprintf, sscanf}\n\ |
1011 @end deftypefn") | |
1 | 1012 { |
4468 | 1013 static std::string who = "sprintf"; |
1014 | |
2095 | 1015 octave_value_list retval; |
1 | 1016 |
2095 | 1017 int nargin = args.length (); |
1 | 1018 |
2095 | 1019 if (nargin > 0) |
1 | 1020 { |
2116 | 1021 retval(2) = -1.0; |
1022 retval(1) = "unknown error"; | |
1023 retval(0) = ""; | |
1024 | |
3340 | 1025 octave_ostrstream *ostr = new octave_ostrstream (); |
1 | 1026 |
3340 | 1027 octave_stream os (ostr); |
628 | 1028 |
3340 | 1029 if (os.is_valid ()) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1030 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1031 octave_value fmt_arg = args(0); |
6007 | 1032 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1033 if (fmt_arg.is_string ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1034 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1035 octave_value_list tmp_args; |
1 | 1036 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1037 if (nargin > 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1038 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1039 tmp_args.resize (nargin-1, octave_value ()); |
1 | 1040 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1041 for (int i = 1; i < nargin; i++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1042 tmp_args(i-1) = args(i); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1043 } |
628 | 1044 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1045 retval(2) = os.printf (fmt_arg, tmp_args, who); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1046 retval(1) = os.error (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1047 retval(0) = octave_value (ostr->str (), |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1048 fmt_arg.is_sq_string () ? '\'' : '"'); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1049 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1050 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1051 ::error ("%s: format must be a string", who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1052 } |
2095 | 1053 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1054 ::error ("%s: unable to create output buffer", who.c_str ()); |
1 | 1055 } |
1056 else | |
5823 | 1057 print_usage (); |
1 | 1058 |
1059 return retval; | |
1060 } | |
1061 | |
2095 | 1062 DEFUN (fscanf, args, , |
3372 | 1063 "-*- texinfo -*-\n\ |
1064 @deftypefn {Built-in Function} {[@var{val}, @var{count}] =} fscanf (@var{fid}, @var{template}, @var{size})\n\ | |
7650 | 1065 @deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}, @var{count}] =} fscanf (@var{fid}, @var{template}, \"C\")\n\ |
3372 | 1066 In the first form, read from @var{fid} according to @var{template},\n\ |
1067 returning the result in the matrix @var{val}.\n\ | |
2122 | 1068 \n\ |
3372 | 1069 The optional argument @var{size} specifies the amount of data to read\n\ |
1070 and may be one of\n\ | |
1071 \n\ | |
1072 @table @code\n\ | |
1073 @item Inf\n\ | |
1074 Read as much as possible, returning a column vector.\n\ | |
1075 \n\ | |
1076 @item @var{nr}\n\ | |
1077 Read up to @var{nr} elements, returning a column vector.\n\ | |
2122 | 1078 \n\ |
3372 | 1079 @item [@var{nr}, Inf]\n\ |
1080 Read as much as possible, returning a matrix with @var{nr} rows. If the\n\ | |
1081 number of elements read is not an exact multiple of @var{nr}, the last\n\ | |
1082 column is padded with zeros.\n\ | |
1083 \n\ | |
1084 @item [@var{nr}, @var{nc}]\n\ | |
1085 Read up to @code{@var{nr} * @var{nc}} elements, returning a matrix with\n\ | |
1086 @var{nr} rows. If the number of elements read is not an exact multiple\n\ | |
1087 of @var{nr}, the last column is padded with zeros.\n\ | |
1088 @end table\n\ | |
2122 | 1089 \n\ |
3372 | 1090 @noindent\n\ |
1091 If @var{size} is omitted, a value of @code{Inf} is assumed.\n\ | |
2122 | 1092 \n\ |
3372 | 1093 A string is returned if @var{template} specifies only character\n\ |
1094 conversions.\n\ | |
2215 | 1095 \n\ |
3372 | 1096 The number of items successfully read is returned in @var{count}.\n\ |
2215 | 1097 \n\ |
3372 | 1098 In the second form, read from @var{fid} according to @var{template},\n\ |
1099 with each conversion specifier in @var{template} corresponding to a\n\ | |
1100 single scalar return value. This form is more `C-like', and also\n\ | |
3491 | 1101 compatible with previous versions of Octave. The number of successful\n\ |
1102 conversions is returned in @var{count}\n\ | |
5653 | 1103 @ifclear OCTAVE_MANUAL\n\ |
1104 \n\ | |
1105 See the Formatted Input section of the GNU Octave manual for a\n\ | |
1106 complete description of the syntax of the template string.\n\ | |
1107 @end ifclear\n\ | |
7781 | 1108 @seealso{scanf, sscanf, fread, fprintf, fgets, fputs}\n\ |
5642 | 1109 @end deftypefn") |
1181 | 1110 { |
4468 | 1111 static std::string who = "fscanf"; |
1112 | |
2095 | 1113 octave_value_list retval; |
1181 | 1114 |
1115 int nargin = args.length (); | |
1116 | |
2215 | 1117 if (nargin == 3 && args(2).is_string ()) |
2095 | 1118 { |
4468 | 1119 octave_stream os = octave_stream_list::lookup (args(0), who); |
1181 | 1120 |
3341 | 1121 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1122 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1123 if (args(1).is_string ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1124 retval = os.oscanf (args(1), who); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1125 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1126 ::error ("%s: format must be a string", who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1127 } |
2095 | 1128 } |
1181 | 1129 else |
2215 | 1130 { |
1131 retval (1) = 0.0; | |
1132 retval (0) = Matrix (); | |
1133 | |
1134 if (nargin == 2 || nargin == 3) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1135 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1136 octave_stream os = octave_stream_list::lookup (args(0), who); |
2215 | 1137 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1138 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1139 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1140 if (args(1).is_string ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1141 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1142 octave_idx_type count = 0; |
2215 | 1143 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1144 Array<double> size = (nargin == 3) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1145 ? args(2).vector_value () |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
1146 : Array<double> (1, 1, lo_ieee_inf_value ()); |
2215 | 1147 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1148 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1149 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1150 octave_value tmp = os.scanf (args(1), size, count, who); |
2215 | 1151 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1152 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1153 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1154 retval(1) = count; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1155 retval(0) = tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1156 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1157 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1158 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1159 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1160 ::error ("%s: format must be a string", who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1161 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1162 } |
2215 | 1163 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1164 print_usage (); |
2215 | 1165 } |
1181 | 1166 |
1167 return retval; | |
1168 } | |
1169 | |
2095 | 1170 DEFUN (sscanf, args, , |
3372 | 1171 "-*- texinfo -*-\n\ |
1172 @deftypefn {Built-in Function} {[@var{val}, @var{count}] =} sscanf (@var{string}, @var{template}, @var{size})\n\ | |
7650 | 1173 @deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}, @var{count}] =} sscanf (@var{string}, @var{template}, \"C\")\n\ |
3372 | 1174 This is like @code{fscanf}, except that the characters are taken from the\n\ |
1175 string @var{string} instead of from a stream. Reaching the end of the\n\ | |
1176 string is treated as an end-of-file condition.\n\ | |
5642 | 1177 @seealso{fscanf, scanf, sprintf}\n\ |
1178 @end deftypefn") | |
444 | 1179 { |
4468 | 1180 static std::string who = "sscanf"; |
1181 | |
2095 | 1182 octave_value_list retval; |
444 | 1183 |
506 | 1184 int nargin = args.length (); |
1185 | |
2215 | 1186 if (nargin == 3 && args(2).is_string ()) |
2095 | 1187 { |
1188 if (args(0).is_string ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1189 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1190 std::string data = args(0).string_value (); |
444 | 1191 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1192 octave_stream os = octave_istrstream::create (data); |
1358 | 1193 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1194 if (os.is_valid ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1195 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1196 if (args(1).is_string ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1197 retval = os.oscanf (args(1), who); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1198 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1199 ::error ("%s: format must be a string", who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1200 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1201 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1202 ::error ("%s: unable to create temporary input buffer", |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1203 who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1204 } |
636 | 1205 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1206 ::error ("%s: first argument must be a string", who.c_str ()); |
444 | 1207 } |
1208 else | |
2215 | 1209 { |
1210 if (nargin == 2 || nargin == 3) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1211 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1212 retval(3) = -1.0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1213 retval(2) = "unknown error"; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1214 retval(1) = 0.0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1215 retval(0) = Matrix (); |
2215 | 1216 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1217 if (args(0).is_string ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1218 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1219 std::string data = args(0).string_value (); |
2215 | 1220 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1221 octave_stream os = octave_istrstream::create (data); |
2215 | 1222 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1223 if (os.is_valid ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1224 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1225 if (args(1).is_string ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1226 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1227 octave_idx_type count = 0; |
2215 | 1228 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1229 Array<double> size = (nargin == 3) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1230 ? args(2).vector_value () |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
1231 : Array<double> (1, 1, lo_ieee_inf_value ()); |
2215 | 1232 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1233 octave_value tmp = os.scanf (args(1), size, count, who); |
2215 | 1234 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1235 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1236 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1237 // FIXME -- is this the right thing to do? |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1238 // Extract error message first, because getting |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1239 // position will clear it. |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1240 std::string errmsg = os.error (); |
2215 | 1241 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1242 retval(3) = os.tell () + 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1243 retval(2) = errmsg; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1244 retval(1) = count; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1245 retval(0) = tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1246 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1247 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1248 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1249 ::error ("%s: format must be a string", who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1250 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1251 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1252 ::error ("%s: unable to create temporary input buffer", |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1253 who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1254 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1255 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1256 ::error ("%s: first argument must be a string", who.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1257 } |
2215 | 1258 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1259 print_usage (); |
2215 | 1260 } |
444 | 1261 |
1262 return retval; | |
1263 } | |
1264 | |
2215 | 1265 DEFUN (scanf, args, nargout, |
3372 | 1266 "-*- texinfo -*-\n\ |
1267 @deftypefn {Built-in Function} {[@var{val}, @var{count}] =} scanf (@var{template}, @var{size})\n\ | |
7650 | 1268 @deftypefnx {Built-in Function} {[@var{v1}, @var{v2}, @dots{}, @var{count}]] =} scanf (@var{template}, \"C\")\n\ |
3372 | 1269 This is equivalent to calling @code{fscanf} with @var{fid} = @code{stdin}.\n\ |
1270 \n\ | |
1271 It is currently not useful to call @code{scanf} in interactive\n\ | |
1272 programs.\n\ | |
5642 | 1273 @seealso{fscanf, sscanf, printf}\n\ |
1274 @end deftypefn") | |
2215 | 1275 { |
1276 int nargin = args.length (); | |
1277 | |
1278 octave_value_list tmp_args (nargin+1, octave_value ()); | |
1279 | |
1280 tmp_args (0) = 0.0; | |
1281 for (int i = 0; i < nargin; i++) | |
1282 tmp_args (i+1) = args (i); | |
1283 | |
1284 return Ffscanf (tmp_args, nargout); | |
1285 } | |
1286 | |
2116 | 1287 static octave_value |
1288 do_fread (octave_stream& os, const octave_value& size_arg, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1289 const octave_value& prec_arg, const octave_value& skip_arg, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1290 const octave_value& arch_arg, octave_idx_type& count) |
2116 | 1291 { |
1292 octave_value retval; | |
1293 | |
1294 count = -1; | |
1295 | |
3810 | 1296 Array<double> size = size_arg.vector_value (); |
2116 | 1297 |
1298 if (! error_state) | |
1299 { | |
3523 | 1300 std::string prec = prec_arg.string_value (); |
2116 | 1301 |
1302 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1303 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1304 int block_size = 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1305 oct_data_conv::data_type input_type; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1306 oct_data_conv::data_type output_type; |
4944 | 1307 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1308 oct_data_conv::string_to_data_type (prec, block_size, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1309 input_type, output_type); |
2116 | 1310 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1311 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1312 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1313 int skip = skip_arg.int_value (true); |
2116 | 1314 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1315 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1316 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1317 std::string arch = arch_arg.string_value (); |
3202 | 1318 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1319 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1320 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1321 oct_mach_info::float_format flt_fmt |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1322 = oct_mach_info::string_to_float_format (arch); |
2116 | 1323 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1324 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1325 retval = os.read (size, block_size, input_type, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1326 output_type, skip, flt_fmt, count); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1327 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1328 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1329 ::error ("fread: architecture type must be a string"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1330 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1331 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1332 ::error ("fread: skip must be an integer"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1333 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1334 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1335 ::error ("fread: invalid data type specified"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1336 } |
2116 | 1337 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1338 ::error ("fread: precision must be a string"); |
2116 | 1339 } |
1340 else | |
1341 ::error ("fread: invalid size specified"); | |
1342 | |
1343 return retval; | |
1344 } | |
1345 | |
1346 DEFUN (fread, args, , | |
3372 | 1347 "-*- texinfo -*-\n\ |
1348 @deftypefn {Built-in Function} {[@var{val}, @var{count}] =} fread (@var{fid}, @var{size}, @var{precision}, @var{skip}, @var{arch})\n\ | |
1349 Read binary data of type @var{precision} from the specified file ID\n\ | |
1350 @var{fid}.\n\ | |
1351 \n\ | |
1352 The optional argument @var{size} specifies the amount of data to read\n\ | |
1353 and may be one of\n\ | |
1354 \n\ | |
1355 @table @code\n\ | |
1356 @item Inf\n\ | |
1357 Read as much as possible, returning a column vector.\n\ | |
529 | 1358 \n\ |
3372 | 1359 @item @var{nr}\n\ |
1360 Read up to @var{nr} elements, returning a column vector.\n\ | |
1361 \n\ | |
1362 @item [@var{nr}, Inf]\n\ | |
1363 Read as much as possible, returning a matrix with @var{nr} rows. If the\n\ | |
1364 number of elements read is not an exact multiple of @var{nr}, the last\n\ | |
1365 column is padded with zeros.\n\ | |
1366 \n\ | |
1367 @item [@var{nr}, @var{nc}]\n\ | |
1368 Read up to @code{@var{nr} * @var{nc}} elements, returning a matrix with\n\ | |
1369 @var{nr} rows. If the number of elements read is not an exact multiple\n\ | |
1370 of @var{nr}, the last column is padded with zeros.\n\ | |
1371 @end table\n\ | |
1372 \n\ | |
1373 @noindent\n\ | |
1374 If @var{size} is omitted, a value of @code{Inf} is assumed.\n\ | |
2318 | 1375 \n\ |
3372 | 1376 The optional argument @var{precision} is a string specifying the type of\n\ |
1377 data to read and may be one of\n\ | |
1378 \n\ | |
1379 @table @code\n\ | |
4944 | 1380 @item \"schar\"\n\ |
1381 @itemx \"signed char\"\n\ | |
3372 | 1382 Signed character.\n\ |
529 | 1383 \n\ |
4944 | 1384 @item \"uchar\"\n\ |
1385 @itemx \"unsigned char\"\n\ | |
3372 | 1386 Unsigned character.\n\ |
1387 \n\ | |
4944 | 1388 @item \"int8\"\n\ |
1389 @itemx \"integer*1\"\n\ | |
1390 \n\ | |
1391 8-bit signed integer.\n\ | |
2318 | 1392 \n\ |
4944 | 1393 @item \"int16\"\n\ |
1394 @itemx \"integer*2\"\n\ | |
1395 16-bit signed integer.\n\ | |
3372 | 1396 \n\ |
4944 | 1397 @item \"int32\"\n\ |
1398 @itemx \"integer*4\"\n\ | |
1399 32-bit signed integer.\n\ | |
3372 | 1400 \n\ |
4944 | 1401 @item \"int64\"\n\ |
1402 @itemx \"integer*8\"\n\ | |
1403 64-bit signed integer.\n\ | |
1404 \n\ | |
1405 @item \"uint8\"\n\ | |
1406 8-bit unsigned integer.\n\ | |
529 | 1407 \n\ |
4944 | 1408 @item \"uint16\"\n\ |
1409 16-bit unsigned integer.\n\ | |
3372 | 1410 \n\ |
4944 | 1411 @item \"uint32\"\n\ |
1412 32-bit unsigned integer.\n\ | |
3372 | 1413 \n\ |
4944 | 1414 @item \"uint64\"\n\ |
1415 64-bit unsigned integer.\n\ | |
1416 \n\ | |
1417 @item \"single\"\n\ | |
3372 | 1418 @itemx \"float32\"\n\ |
1419 @itemx \"real*4\"\n\ | |
4944 | 1420 32-bit floating point number.\n\ |
3372 | 1421 \n\ |
1422 @item \"double\"\n\ | |
1423 @itemx \"float64\"\n\ | |
1424 @itemx \"real*8\"\n\ | |
4944 | 1425 64-bit floating point number.\n\ |
1426 \n\ | |
1427 @item \"char\"\n\ | |
1428 @itemx \"char*1\"\n\ | |
1429 Single character.\n\ | |
3372 | 1430 \n\ |
4944 | 1431 @item \"short\"\n\ |
1432 Short integer (size is platform dependent).\n\ | |
1433 \n\ | |
1434 @item \"int\"\n\ | |
1435 Integer (size is platform dependent).\n\ | |
1436 \n\ | |
1437 @item \"long\"\n\ | |
1438 Long integer (size is platform dependent).\n\ | |
3372 | 1439 \n\ |
4944 | 1440 @item \"ushort\"\n\ |
1441 @itemx \"unsigned short\"\n\ | |
1442 Unsigned short integer (size is platform dependent).\n\ | |
4610 | 1443 \n\ |
4944 | 1444 @item \"uint\"\n\ |
1445 @itemx \"unsigned int\"\n\ | |
1446 Unsigned integer (size is platform dependent).\n\ | |
4610 | 1447 \n\ |
4944 | 1448 @item \"ulong\"\n\ |
1449 @itemx \"unsigned long\"\n\ | |
1450 Unsigned long integer (size is platform dependent).\n\ | |
1451 \n\ | |
1452 @item \"float\"\n\ | |
1453 Single precision floating point number (size is platform dependent).\n\ | |
3372 | 1454 @end table\n\ |
1455 \n\ | |
1456 @noindent\n\ | |
1457 The default precision is @code{\"uchar\"}.\n\ | |
2318 | 1458 \n\ |
4944 | 1459 The @var{precision} argument may also specify an optional repeat\n\ |
1460 count. For example, @samp{32*single} causes @code{fread} to read\n\ | |
1461 a block of 32 single precision floating point numbers. Reading in\n\ | |
1462 blocks is useful in combination with the @var{skip} argument.\n\ | |
1463 \n\ | |
1464 The @var{precision} argument may also specify a type conversion.\n\ | |
1465 For example, @samp{int16=>int32} causes @code{fread} to read 16-bit\n\ | |
1466 integer values and return an array of 32-bit integer values. By\n\ | |
1467 default, @code{fread} returns a double precision array. The special\n\ | |
1468 form @samp{*TYPE} is shorthand for @samp{TYPE=>TYPE}.\n\ | |
1469 \n\ | |
7096 | 1470 The conversion and repeat counts may be combined. For example, the\n\ |
1471 specification @samp{32*single=>single} causes @code{fread} to read\n\ | |
1472 blocks of single precision floating point values and return an array\n\ | |
1473 of single precision values instead of the default array of double\n\ | |
1474 precision values.\n\ | |
4944 | 1475 \n\ |
3372 | 1476 The optional argument @var{skip} specifies the number of bytes to skip\n\ |
4944 | 1477 after each element (or block of elements) is read. If it is not\n\ |
1478 specified, a value of 0 is assumed. If the final block read is not\n\ | |
1479 complete, the final skip is omitted. For example,\n\ | |
1480 \n\ | |
1481 @example\n\ | |
1482 fread (f, 10, \"3*single=>single\", 8)\n\ | |
1483 @end example\n\ | |
1484 \n\ | |
1485 @noindent\n\ | |
1486 will omit the final 8-byte skip because the last read will not be\n\ | |
1487 a complete block of 3 values.\n\ | |
3372 | 1488 \n\ |
1489 The optional argument @var{arch} is a string specifying the data format\n\ | |
1490 for the file. Valid values are\n\ | |
529 | 1491 \n\ |
3372 | 1492 @table @code\n\ |
1493 @item \"native\"\n\ | |
1494 The format of the current machine.\n\ | |
1495 \n\ | |
4546 | 1496 @item \"ieee-be\"\n\ |
3372 | 1497 IEEE big endian.\n\ |
1498 \n\ | |
4546 | 1499 @item \"ieee-le\"\n\ |
3372 | 1500 IEEE little endian.\n\ |
2318 | 1501 \n\ |
3372 | 1502 @item \"vaxd\"\n\ |
1503 VAX D floating format.\n\ | |
1504 \n\ | |
1505 @item \"vaxg\"\n\ | |
1506 VAX G floating format.\n\ | |
2318 | 1507 \n\ |
3372 | 1508 @item \"cray\"\n\ |
1509 Cray floating format.\n\ | |
1510 @end table\n\ | |
2318 | 1511 \n\ |
3372 | 1512 @noindent\n\ |
1513 Conversions are currently only supported for @code{\"ieee-be\"} and\n\ | |
1514 @code{\"ieee-le\"} formats.\n\ | |
2318 | 1515 \n\ |
3372 | 1516 The data read from the file is returned in @var{val}, and the number of\n\ |
1517 values read is returned in @code{count}\n\ | |
5642 | 1518 @seealso{fwrite, fopen, fclose}\n\ |
1519 @end deftypefn") | |
529 | 1520 { |
2095 | 1521 octave_value_list retval; |
2116 | 1522 |
1523 int nargin = args.length (); | |
1524 | |
2318 | 1525 if (nargin > 0 && nargin < 6) |
2116 | 1526 { |
1527 retval(1) = -1.0; | |
1528 retval(0) = Matrix (); | |
1529 | |
3341 | 1530 octave_stream os = octave_stream_list::lookup (args(0), "fread"); |
2116 | 1531 |
3341 | 1532 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1533 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1534 octave_value size = lo_ieee_inf_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1535 octave_value prec = "uchar"; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1536 octave_value skip = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1537 octave_value arch = "unknown"; |
2116 | 1538 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1539 int idx = 1; |
2116 | 1540 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1541 if (nargin > idx && ! args(idx).is_string ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1542 size = args(idx++); |
4257 | 1543 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1544 if (nargin > idx) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1545 prec = args(idx++); |
2116 | 1546 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1547 if (nargin > idx) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1548 skip = args(idx++); |
4257 | 1549 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1550 if (nargin > idx) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1551 arch = args(idx++); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1552 else if (skip.is_string ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1553 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1554 arch = skip; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1555 skip = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1556 } |
2116 | 1557 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1558 octave_idx_type count = -1; |
2116 | 1559 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1560 octave_value tmp = do_fread (os, size, prec, skip, arch, count); |
2116 | 1561 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1562 retval(1) = count; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1563 retval(0) = tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1564 } |
2116 | 1565 } |
1566 else | |
5823 | 1567 print_usage (); |
2116 | 1568 |
529 | 1569 return retval; |
1570 } | |
1571 | |
2116 | 1572 static int |
1573 do_fwrite (octave_stream& os, const octave_value& data, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1574 const octave_value& prec_arg, const octave_value& skip_arg, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1575 const octave_value& arch_arg) |
2116 | 1576 { |
1577 int retval = -1; | |
1578 | |
3523 | 1579 std::string prec = prec_arg.string_value (); |
2116 | 1580 |
1581 if (! error_state) | |
1582 { | |
4944 | 1583 int block_size = 1; |
1584 oct_data_conv::data_type output_type; | |
1585 | |
1586 oct_data_conv::string_to_data_type (prec, block_size, output_type); | |
2116 | 1587 |
1588 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1589 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1590 int skip = skip_arg.int_value (true); |
2116 | 1591 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1592 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1593 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1594 std::string arch = arch_arg.string_value (); |
3202 | 1595 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1596 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1597 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1598 oct_mach_info::float_format flt_fmt |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1599 = oct_mach_info::string_to_float_format (arch); |
2116 | 1600 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1601 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1602 retval = os.write (data, block_size, output_type, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1603 skip, flt_fmt); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1604 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1605 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1606 ::error ("fwrite: architecture type must be a string"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1607 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1608 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1609 ::error ("fwrite: skip must be an integer"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1610 } |
3202 | 1611 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1612 ::error ("fwrite: invalid precision specified"); |
2116 | 1613 } |
1614 else | |
1615 ::error ("fwrite: precision must be a string"); | |
1616 | |
1617 return retval; | |
1618 } | |
1619 | |
1620 DEFUN (fwrite, args, , | |
3372 | 1621 "-*- texinfo -*-\n\ |
1622 @deftypefn {Built-in Function} {@var{count} =} fwrite (@var{fid}, @var{data}, @var{precision}, @var{skip}, @var{arch})\n\ | |
1623 Write data in binary form of type @var{precision} to the specified file\n\ | |
1624 ID @var{fid}, returning the number of values successfully written to the\n\ | |
1625 file.\n\ | |
1181 | 1626 \n\ |
3372 | 1627 The argument @var{data} is a matrix of values that are to be written to\n\ |
1628 the file. The values are extracted in column-major order.\n\ | |
1181 | 1629 \n\ |
3372 | 1630 The remaining arguments @var{precision}, @var{skip}, and @var{arch} are\n\ |
1631 optional, and are interpreted as described for @code{fread}.\n\ | |
1181 | 1632 \n\ |
3372 | 1633 The behavior of @code{fwrite} is undefined if the values in @var{data}\n\ |
1634 are too large to fit in the specified precision.\n\ | |
5642 | 1635 @seealso{fread, fopen, fclose}\n\ |
1636 @end deftypefn") | |
1181 | 1637 { |
4233 | 1638 octave_value retval = -1; |
2116 | 1639 |
1640 int nargin = args.length (); | |
1641 | |
1642 if (nargin > 1 && nargin < 6) | |
1643 { | |
3341 | 1644 octave_stream os = octave_stream_list::lookup (args(0), "fwrite"); |
2116 | 1645 |
3341 | 1646 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1647 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1648 octave_value prec = "uchar"; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1649 octave_value skip = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1650 octave_value arch = "unknown"; |
2318 | 1651 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1652 int idx = 1; |
7708
b42abee70a98
fread, fwrite: allow SKIP arg to be omitted
John W. Eaton <jwe@octave.org>
parents:
7650
diff
changeset
|
1653 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1654 octave_value data = args(idx++); |
7708
b42abee70a98
fread, fwrite: allow SKIP arg to be omitted
John W. Eaton <jwe@octave.org>
parents:
7650
diff
changeset
|
1655 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1656 if (nargin > idx) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1657 prec = args(idx++); |
2318 | 1658 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1659 if (nargin > idx) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1660 skip = args(idx++); |
2318 | 1661 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1662 if (nargin > idx) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1663 arch = args(idx++); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1664 else if (skip.is_string ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1665 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1666 arch = skip; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1667 skip = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1668 } |
2116 | 1669 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1670 double status = do_fwrite (os, data, prec, skip, arch); |
2825 | 1671 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1672 retval = status; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1673 } |
2116 | 1674 } |
1675 else | |
5823 | 1676 print_usage (); |
2116 | 1677 |
1181 | 1678 return retval; |
1679 } | |
1680 | |
5906 | 1681 DEFUNX ("feof", Ffeof, args, , |
3372 | 1682 "-*- texinfo -*-\n\ |
1683 @deftypefn {Built-in Function} {} feof (@var{fid})\n\ | |
1684 Return 1 if an end-of-file condition has been encountered for a given\n\ | |
1685 file and 0 otherwise. Note that it will only return 1 if the end of the\n\ | |
1686 file has already been encountered, not if the next read operation will\n\ | |
1687 result in an end-of-file condition.\n\ | |
5642 | 1688 @seealso{fread, fopen, fclose}\n\ |
1689 @end deftypefn") | |
529 | 1690 { |
4233 | 1691 octave_value retval = -1; |
529 | 1692 |
1693 int nargin = args.length (); | |
1694 | |
2095 | 1695 if (nargin == 1) |
1696 { | |
3341 | 1697 octave_stream os = octave_stream_list::lookup (args(0), "feof"); |
444 | 1698 |
3341 | 1699 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1700 retval = os.eof () ? 1.0 : 0.0; |
2095 | 1701 } |
529 | 1702 else |
5823 | 1703 print_usage (); |
444 | 1704 |
1705 return retval; | |
1706 } | |
1707 | |
5906 | 1708 DEFUNX ("ferror", Fferror, args, , |
3372 | 1709 "-*- texinfo -*-\n\ |
9797
f569f46a1c34
update ferror doc string, take 2
John W. Eaton <jwe@octave.org>
parents:
9796
diff
changeset
|
1710 @deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} ferror (@var{fid}, \"clear\")\n\ |
f569f46a1c34
update ferror doc string, take 2
John W. Eaton <jwe@octave.org>
parents:
9796
diff
changeset
|
1711 Return 1 if an error condition has been encountered for the file ID\n\ |
f569f46a1c34
update ferror doc string, take 2
John W. Eaton <jwe@octave.org>
parents:
9796
diff
changeset
|
1712 @var{fid} and 0 otherwise. Note that it will only return 1 if an error\n\ |
f569f46a1c34
update ferror doc string, take 2
John W. Eaton <jwe@octave.org>
parents:
9796
diff
changeset
|
1713 has already been encountered, not if the next operation will result in\n\ |
f569f46a1c34
update ferror doc string, take 2
John W. Eaton <jwe@octave.org>
parents:
9796
diff
changeset
|
1714 an error condition.\n\ |
f569f46a1c34
update ferror doc string, take 2
John W. Eaton <jwe@octave.org>
parents:
9796
diff
changeset
|
1715 \n\ |
f569f46a1c34
update ferror doc string, take 2
John W. Eaton <jwe@octave.org>
parents:
9796
diff
changeset
|
1716 The second argument is optional. If it is supplied, also clear the\n\ |
3372 | 1717 error condition.\n\ |
1718 @end deftypefn") | |
1230 | 1719 { |
2095 | 1720 octave_value_list retval; |
1230 | 1721 |
2095 | 1722 int nargin = args.length (); |
1230 | 1723 |
2095 | 1724 if (nargin == 1 || nargin == 2) |
1725 { | |
3341 | 1726 octave_stream os = octave_stream_list::lookup (args(0), "ferror"); |
1230 | 1727 |
3341 | 1728 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1729 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1730 bool clear = false; |
1230 | 1731 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1732 if (nargin == 2) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1733 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1734 std::string opt = args(1).string_value (); |
2095 | 1735 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1736 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1737 clear = (opt == "clear"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1738 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1739 return retval; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1740 } |
1755 | 1741 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1742 int error_number = 0; |
2095 | 1743 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1744 std::string error_message = os.error (clear, error_number); |
1230 | 1745 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1746 retval(1) = error_number; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1747 retval(0) = error_message; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1748 } |
1230 | 1749 } |
2095 | 1750 else |
5823 | 1751 print_usage (); |
1230 | 1752 |
1753 return retval; | |
1754 } | |
1755 | |
1957 | 1756 DEFUN (popen, args, , |
3301 | 1757 "-*- texinfo -*-\n\ |
4326 | 1758 @deftypefn {Built-in Function} {@var{fid} =} popen (@var{command}, @var{mode})\n\ |
3301 | 1759 Start a process and create a pipe. The name of the command to run is\n\ |
1760 given by @var{command}. The file identifier corresponding to the input\n\ | |
1761 or output stream of the process is returned in @var{fid}. The argument\n\ | |
1762 @var{mode} may be\n\ | |
1763 \n\ | |
1764 @table @code\n\ | |
1765 @item \"r\"\n\ | |
1766 The pipe will be connected to the standard output of the process, and\n\ | |
1767 open for reading.\n\ | |
1230 | 1768 \n\ |
3301 | 1769 @item \"w\"\n\ |
1770 The pipe will be connected to the standard input of the process, and\n\ | |
1771 open for writing.\n\ | |
1772 @end table\n\ | |
1773 \n\ | |
1774 For example,\n\ | |
1230 | 1775 \n\ |
3301 | 1776 @example\n\ |
1777 @group\n\ | |
1778 fid = popen (\"ls -ltr / | tail -3\", \"r\");\n\ | |
7768
a2d9f325b65a
Use isschar instead of deprecated isstr
Rafael Laboissiere <rafael@debian.org>
parents:
7730
diff
changeset
|
1779 while (ischar (s = fgets (fid)))\n\ |
3301 | 1780 fputs (stdout, s);\n\ |
1781 endwhile\n\ | |
1782 @print{} drwxr-xr-x 33 root root 3072 Feb 15 13:28 etc\n\ | |
1783 @print{} drwxr-xr-x 3 root root 1024 Feb 15 13:28 lib\n\ | |
1784 @print{} drwxrwxrwt 15 root root 2048 Feb 17 14:53 tmp\n\ | |
1785 @end group\n\ | |
1786 @end example\n\ | |
1787 @end deftypefn") | |
1230 | 1788 { |
4233 | 1789 octave_value retval = -1; |
1230 | 1790 |
1791 int nargin = args.length (); | |
1792 | |
2095 | 1793 if (nargin == 2) |
1794 { | |
3523 | 1795 std::string name = args(0).string_value (); |
1230 | 1796 |
2095 | 1797 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1798 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1799 std::string mode = args(1).string_value (); |
1230 | 1800 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1801 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1802 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1803 if (mode == "r") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1804 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1805 octave_stream ips = octave_iprocstream::create (name); |
1230 | 1806 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1807 retval = octave_stream_list::insert (ips); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1808 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1809 else if (mode == "w") |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1810 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1811 octave_stream ops = octave_oprocstream::create (name); |
1230 | 1812 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1813 retval = octave_stream_list::insert (ops); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1814 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1815 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1816 ::error ("popen: invalid mode specified"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1817 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1818 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1819 ::error ("popen: mode must be a string"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1820 } |
2095 | 1821 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1822 ::error ("popen: name must be a string"); |
2095 | 1823 } |
1230 | 1824 else |
5823 | 1825 print_usage (); |
1230 | 1826 |
1827 return retval; | |
1828 } | |
1829 | |
1957 | 1830 DEFUN (pclose, args, , |
3381 | 1831 "-*- texinfo -*-\n\ |
3301 | 1832 @deftypefn {Built-in Function} {} pclose (@var{fid})\n\ |
1833 Close a file identifier that was opened by @code{popen}. You may also\n\ | |
1834 use @code{fclose} for the same purpose.\n\ | |
1835 @end deftypefn") | |
1230 | 1836 { |
4233 | 1837 octave_value retval = -1; |
1230 | 1838 |
1839 int nargin = args.length (); | |
1840 | |
2095 | 1841 if (nargin == 1) |
4233 | 1842 retval = octave_stream_list::remove (args(0), "pclose"); |
1377 | 1843 else |
5823 | 1844 print_usage (); |
1379 | 1845 |
1846 return retval; | |
1847 } | |
1848 | |
10197
4d433bd2d4dc
attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
1849 DEFUNX ("tmpnam", Ftmpnam, args, , |
3372 | 1850 "-*- texinfo -*-\n\ |
4267 | 1851 @deftypefn {Built-in Function} {} tmpnam (@var{dir}, @var{prefix})\n\ |
3372 | 1852 Return a unique temporary file name as a string.\n\ |
1853 \n\ | |
4267 | 1854 If @var{prefix} is omitted, a value of @code{\"oct-\"} is used.\n\ |
1855 If @var{dir} is also omitted, the default directory for temporary files\n\ | |
1856 is used. If @var{dir} is provided, it must exist, otherwise the default\n\ | |
1857 directory for temporary files is used. Since the named file is not\n\ | |
1858 opened, by @code{tmpnam}, it is possible (though relatively unlikely)\n\ | |
1859 that it will not be available by the time your program attempts to open it.\n\ | |
5642 | 1860 @seealso{tmpfile, mkstemp, P_tmpdir}\n\ |
1861 @end deftypefn") | |
1802 | 1862 { |
2095 | 1863 octave_value retval; |
1802 | 1864 |
2936 | 1865 int len = args.length (); |
1866 | |
1867 if (len < 3) | |
1868 { | |
3523 | 1869 std::string dir = len > 0 ? args(0).string_value () : std::string (); |
2936 | 1870 |
1871 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1872 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1873 std::string pfx |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1874 = len > 1 ? args(1).string_value () : std::string ("oct-"); |
4267 | 1875 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1876 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1877 retval = octave_tempnam (dir, pfx); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1878 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1879 ::error ("expecting second argument to be a string"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1880 } |
4267 | 1881 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1882 ::error ("expecting first argument to be a string"); |
2936 | 1883 } |
1802 | 1884 else |
5823 | 1885 print_usage (); |
1802 | 1886 |
1887 return retval; | |
1888 } | |
1889 | |
2458 | 1890 DEFALIAS (octave_tmp_file_name, tmpnam); |
1891 | |
4326 | 1892 DEFUN (tmpfile, args, , |
1893 "-*- texinfo -*-\n\ | |
1894 @deftypefn {Built-in Function} {[@var{fid}, @var{msg}] =} tmpfile ()\n\ | |
1895 Return the file ID corresponding to a new temporary file with a unique\n\ | |
1896 name. The file is opened in binary read/write (@code{\"w+b\"}) mode.\n\ | |
1897 The file will be deleted automatically when it is closed or when Octave\n\ | |
1898 exits.\n\ | |
1899 \n\ | |
1900 If successful, @var{fid} is a valid file ID and @var{msg} is an empty\n\ | |
1901 string. Otherwise, @var{fid} is -1 and @var{msg} contains a\n\ | |
1902 system-dependent error message.\n\ | |
5642 | 1903 @seealso{tmpnam, mkstemp, P_tmpdir}\n\ |
1904 @end deftypefn") | |
4326 | 1905 { |
1906 octave_value_list retval; | |
1907 | |
1908 retval(1) = std::string (); | |
1909 retval(0) = -1; | |
1910 | |
1911 int nargin = args.length (); | |
1912 | |
1913 if (nargin == 0) | |
1914 { | |
1915 FILE *fid = tmpfile (); | |
1916 | |
1917 if (fid) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1918 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1919 std::string nm; |
4326 | 1920 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1921 std::ios::openmode md = fopen_mode_to_ios_mode ("w+b"); |
4326 | 1922 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1923 octave_stream s = octave_stdiostream::create (nm, fid, md); |
4326 | 1924 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1925 if (s) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1926 retval(0) = octave_stream_list::insert (s); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1927 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1928 error ("tmpfile: failed to create octave_stdiostream object"); |
4326 | 1929 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1930 } |
4326 | 1931 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1932 { |
10411 | 1933 retval(1) = gnulib::strerror (errno); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1934 retval(0) = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1935 } |
4326 | 1936 } |
1937 else | |
5823 | 1938 print_usage (); |
4326 | 1939 |
1940 return retval; | |
1941 } | |
1942 | |
8792
bbb3fa6778f3
use mkstemps as replacement for mkstemp on mingw32
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8773
diff
changeset
|
1943 #if defined (HAVE_MKSTEMPS) |
bbb3fa6778f3
use mkstemps as replacement for mkstemp on mingw32
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8773
diff
changeset
|
1944 // Prototype for mkstemps in libiberty |
bbb3fa6778f3
use mkstemps as replacement for mkstemp on mingw32
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8773
diff
changeset
|
1945 extern "C" int mkstemps (char *pattern, int suffix_len); |
bbb3fa6778f3
use mkstemps as replacement for mkstemp on mingw32
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8773
diff
changeset
|
1946 #endif |
bbb3fa6778f3
use mkstemps as replacement for mkstemp on mingw32
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8773
diff
changeset
|
1947 |
9236
c02224afead6
Add mkstemp implementation based on mktemp, for platforms missing it
Michael Goffioul <michael.goffioul@gmail.com>
parents:
9209
diff
changeset
|
1948 #if ! defined (HAVE_MKSTEMP) && ! defined (HAVE_MKSTEMPS) && defined (_MSC_VER) |
10259 | 1949 #include <fcntl.h> |
1950 #include <sys/stat.h> | |
9236
c02224afead6
Add mkstemp implementation based on mktemp, for platforms missing it
Michael Goffioul <michael.goffioul@gmail.com>
parents:
9209
diff
changeset
|
1951 int mkstemp (char *tmpl) |
c02224afead6
Add mkstemp implementation based on mktemp, for platforms missing it
Michael Goffioul <michael.goffioul@gmail.com>
parents:
9209
diff
changeset
|
1952 { |
c02224afead6
Add mkstemp implementation based on mktemp, for platforms missing it
Michael Goffioul <michael.goffioul@gmail.com>
parents:
9209
diff
changeset
|
1953 int ret=-1; |
c02224afead6
Add mkstemp implementation based on mktemp, for platforms missing it
Michael Goffioul <michael.goffioul@gmail.com>
parents:
9209
diff
changeset
|
1954 mktemp (tmpl); |
c02224afead6
Add mkstemp implementation based on mktemp, for platforms missing it
Michael Goffioul <michael.goffioul@gmail.com>
parents:
9209
diff
changeset
|
1955 ret = open (tmpl, O_RDWR | O_BINARY | O_CREAT | O_EXCL | _O_SHORT_LIVED, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1956 _S_IREAD | _S_IWRITE); |
9236
c02224afead6
Add mkstemp implementation based on mktemp, for platforms missing it
Michael Goffioul <michael.goffioul@gmail.com>
parents:
9209
diff
changeset
|
1957 return ret; |
c02224afead6
Add mkstemp implementation based on mktemp, for platforms missing it
Michael Goffioul <michael.goffioul@gmail.com>
parents:
9209
diff
changeset
|
1958 } |
c02224afead6
Add mkstemp implementation based on mktemp, for platforms missing it
Michael Goffioul <michael.goffioul@gmail.com>
parents:
9209
diff
changeset
|
1959 #define HAVE_MKSTEMP 1 |
c02224afead6
Add mkstemp implementation based on mktemp, for platforms missing it
Michael Goffioul <michael.goffioul@gmail.com>
parents:
9209
diff
changeset
|
1960 #endif |
c02224afead6
Add mkstemp implementation based on mktemp, for platforms missing it
Michael Goffioul <michael.goffioul@gmail.com>
parents:
9209
diff
changeset
|
1961 |
4326 | 1962 DEFUN (mkstemp, args, , |
1963 "-*- texinfo -*-\n\ | |
5109 | 1964 @deftypefn {Built-in Function} {[@var{fid}, @var{name}, @var{msg}] =} mkstemp (@var{template}, @var{delete})\n\ |
4326 | 1965 Return the file ID corresponding to a new temporary file with a unique\n\ |
1966 name created from @var{template}. The last six characters of @var{template}\n\ | |
5016 | 1967 must be @code{XXXXXX} and these are replaced with a string that makes the\n\ |
4326 | 1968 filename unique. The file is then created with mode read/write and\n\ |
1969 permissions that are system dependent (on GNU/Linux systems, the permissions\n\ | |
1970 will be 0600 for versions of glibc 2.0.7 and later). The file is opened\n\ | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
1971 with the @w{@code{O_EXCL}} flag.\n\ |
4326 | 1972 \n\ |
1973 If the optional argument @var{delete} is supplied and is true,\n\ | |
1974 the file will be deleted automatically when Octave exits, or when\n\ | |
1975 the function @code{purge_tmp_files} is called.\n\ | |
1976 \n\ | |
1977 If successful, @var{fid} is a valid file ID, @var{name} is the name of\n\ | |
7001 | 1978 the file, and @var{msg} is an empty string. Otherwise, @var{fid}\n\ |
4326 | 1979 is -1, @var{name} is empty, and @var{msg} contains a system-dependent\n\ |
1980 error message.\n\ | |
5642 | 1981 @seealso{tmpfile, tmpnam, P_tmpdir}\n\ |
1982 @end deftypefn") | |
4326 | 1983 { |
1984 octave_value_list retval; | |
1985 | |
1986 retval(2) = std::string (); | |
1987 retval(1) = std::string (); | |
1988 retval(0) = -1; | |
1989 | |
8792
bbb3fa6778f3
use mkstemps as replacement for mkstemp on mingw32
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8773
diff
changeset
|
1990 #if defined (HAVE_MKSTEMP) || defined (HAVE_MKSTEMPS) |
4326 | 1991 |
1992 int nargin = args.length (); | |
1993 | |
1994 if (nargin == 1 || nargin == 2) | |
1995 { | |
1996 std::string tmpl8 = args(0).string_value (); | |
1997 | |
1998 if (! error_state) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
1999 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2000 OCTAVE_LOCAL_BUFFER (char, tmp, tmpl8.size () + 1); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2001 strcpy (tmp, tmpl8.c_str ()); |
4326 | 2002 |
8792
bbb3fa6778f3
use mkstemps as replacement for mkstemp on mingw32
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8773
diff
changeset
|
2003 #if defined (HAVE_MKSTEMP) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2004 int fd = mkstemp (tmp); |
8792
bbb3fa6778f3
use mkstemps as replacement for mkstemp on mingw32
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8773
diff
changeset
|
2005 #else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2006 int fd = mkstemps (tmp, 0); |
8792
bbb3fa6778f3
use mkstemps as replacement for mkstemp on mingw32
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8773
diff
changeset
|
2007 #endif |
4326 | 2008 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2009 if (fd < 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2010 { |
10411 | 2011 retval(2) = gnulib::strerror (errno); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2012 retval(0) = fd; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2013 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2014 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2015 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2016 const char *fopen_mode = "w+"; |
4326 | 2017 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2018 FILE *fid = fdopen (fd, fopen_mode); |
4326 | 2019 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2020 if (fid) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2021 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2022 std::string nm = tmp; |
4326 | 2023 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2024 std::ios::openmode md = fopen_mode_to_ios_mode (fopen_mode); |
4326 | 2025 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2026 octave_stream s = octave_stdiostream::create (nm, fid, md); |
4326 | 2027 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2028 if (s) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2029 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2030 retval(1) = nm; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2031 retval(0) = octave_stream_list::insert (s); |
4326 | 2032 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2033 if (nargin == 2 && args(1).is_true ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2034 mark_for_deletion (nm); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2035 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2036 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2037 error ("mkstemp: failed to create octave_stdiostream object"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2038 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2039 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2040 { |
10411 | 2041 retval(2) = gnulib::strerror (errno); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2042 retval(0) = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2043 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2044 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2045 } |
4326 | 2046 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2047 error ("mkstemp: expecting string as first argument"); |
4326 | 2048 } |
2049 else | |
5823 | 2050 print_usage (); |
4326 | 2051 |
2052 #else | |
2053 retval(2) = "mkstemp: not supported on this sytem"; | |
2054 #endif | |
2055 | |
2056 return retval; | |
2057 } | |
2058 | |
1400 | 2059 static int |
2060 convert (int x, int ibase, int obase) | |
2061 { | |
2062 int retval = 0; | |
2063 | |
2064 int tmp = x % obase; | |
2065 | |
2066 if (tmp > ibase - 1) | |
2095 | 2067 ::error ("umask: invalid digit"); |
1400 | 2068 else |
2069 { | |
2070 retval = tmp; | |
2071 int mult = ibase; | |
2072 while ((x = (x - tmp) / obase)) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2073 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2074 tmp = x % obase; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2075 if (tmp > ibase - 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2076 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2077 ::error ("umask: invalid digit"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2078 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2079 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2080 retval += mult * tmp; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2081 mult *= ibase; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2082 } |
1400 | 2083 } |
2084 | |
2085 return retval; | |
2086 } | |
2087 | |
10197
4d433bd2d4dc
attempt to avoid trouble with gnulib #defines in a consistent way
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
2088 DEFUNX ("umask", Fumask, args, , |
3301 | 2089 "-*- texinfo -*-\n\ |
2090 @deftypefn {Built-in Function} {} umask (@var{mask})\n\ | |
2091 Set the permission mask for file creation. The parameter @var{mask}\n\ | |
4715 | 2092 is an integer, interpreted as an octal number. If successful,\n\ |
2093 returns the previous value of the mask (as an integer to be\n\ | |
2094 interpreted as an octal number); otherwise an error message is printed.\n\ | |
3301 | 2095 @end deftypefn") |
1400 | 2096 { |
2095 | 2097 octave_value_list retval; |
1400 | 2098 |
2099 int status = 0; | |
2100 | |
2101 if (args.length () == 1) | |
2102 { | |
3202 | 2103 int mask = args(0).int_value (true); |
1400 | 2104 |
3202 | 2105 if (! error_state) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2106 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2107 if (mask < 0) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2108 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2109 status = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2110 ::error ("umask: MASK must be a positive integer value"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2111 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2112 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2113 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2114 int oct_mask = convert (mask, 8, 10); |
1400 | 2115 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2116 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2117 status = convert (octave_umask (oct_mask), 10, 8); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2118 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2119 } |
3202 | 2120 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2121 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2122 status = -1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2123 ::error ("umask: expecting integer argument"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2124 } |
1400 | 2125 } |
2126 else | |
5823 | 2127 print_usage (); |
1400 | 2128 |
2129 if (status >= 0) | |
4233 | 2130 retval(0) = status; |
1400 | 2131 |
2132 return retval; | |
2133 } | |
2134 | |
5749 | 2135 static octave_value |
6483 | 2136 const_value (const char *, const octave_value_list& args, int val) |
2189 | 2137 { |
5749 | 2138 octave_value retval; |
2139 | |
2140 int nargin = args.length (); | |
2141 | |
2142 if (nargin == 0) | |
2143 retval = val; | |
2144 else | |
5823 | 2145 print_usage (); |
5749 | 2146 |
2147 return retval; | |
2148 } | |
2149 | |
4267 | 2150 #if ! defined (P_tmpdir) |
2151 #define P_tmpdir "/tmp" | |
2152 #endif | |
2153 | |
5749 | 2154 DEFUNX ("P_tmpdir", FP_tmpdir, args, , |
2155 "-*- texinfo -*-\n\ | |
2156 @deftypefn {Built-in Function} {} P_tmpdir ()\n\ | |
2157 Return the default name of the directory for temporary files on\n\ | |
2158 this system. The name of this directory is system dependent.\n\ | |
2159 @end deftypefn") | |
2160 { | |
2161 octave_value retval; | |
2162 | |
2163 int nargin = args.length (); | |
4267 | 2164 |
5749 | 2165 if (nargin == 0) |
2166 retval = P_tmpdir; | |
2167 else | |
5823 | 2168 print_usage (); |
5749 | 2169 |
2170 return retval; | |
2171 } | |
2341 | 2172 |
5749 | 2173 // NOTE: the values of SEEK_SET, SEEK_CUR, and SEEK_END have to be |
2174 // this way for Matlab compatibility. | |
2175 | |
2176 DEFUNX ("SEEK_SET", FSEEK_SET, args, , | |
2177 "-*- texinfo -*-\n\ | |
2178 @deftypefn {Built-in Function} {} SEEK_SET ()\n\ | |
2179 @deftypefnx {Built-in Function} {} SEEK_CUR ()\n\ | |
2180 @deftypefnx {Built-in Function} {} SEEK_END ()\n\ | |
2181 Return the value required to request that @code{fseek} perform\n\ | |
2182 one of the following actions:\n\ | |
3372 | 2183 @table @code\n\ |
2184 @item SEEK_SET\n\ | |
2185 Position file relative to the beginning.\n\ | |
2186 \n\ | |
2187 @item SEEK_CUR\n\ | |
2188 Position file relative to the current position.\n\ | |
2189 \n\ | |
2190 @item SEEK_END\n\ | |
5749 | 2191 Position file relative to the end.\n\ |
3372 | 2192 @end table\n\ |
5749 | 2193 @end deftypefn") |
2194 { | |
2195 return const_value ("SEEK_SET", args, -1); | |
2196 } | |
2189 | 2197 |
5749 | 2198 DEFUNX ("SEEK_CUR", FSEEK_CUR, args, , |
2199 "-*- texinfo -*-\n\ | |
2200 @deftypefn {Built-in Function} {} SEEK_CUR ()\n\ | |
3458 | 2201 See SEEK_SET.\n\ |
5749 | 2202 @end deftypefn") |
2203 { | |
2204 return const_value ("SEEK_CUR", args, 0); | |
2205 } | |
2189 | 2206 |
5749 | 2207 DEFUNX ("SEEK_END", FSEEK_END, args, , |
2208 "-*- texinfo -*-\n\ | |
2209 @deftypefn {Built-in Function} {} SEEK_END ()\n\ | |
3458 | 2210 See SEEK_SET.\n\ |
5749 | 2211 @end deftypefn") |
2212 { | |
2213 return const_value ("SEEK_END", args, 1); | |
2214 } | |
2215 | |
2216 static octave_value | |
6483 | 2217 const_value (const char *, const octave_value_list& args, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
2218 const octave_value& val) |
5749 | 2219 { |
2220 octave_value retval; | |
2221 | |
2222 int nargin = args.length (); | |
2189 | 2223 |
5749 | 2224 if (nargin == 0) |
2225 retval = val; | |
2226 else | |
5823 | 2227 print_usage (); |
5749 | 2228 |
2229 return retval; | |
2230 } | |
2231 | |
2232 DEFUNX ("stdin", Fstdin, args, , | |
2233 "-*- texinfo -*-\n\ | |
2234 @deftypefn {Built-in Function} {} stdin ()\n\ | |
2235 Return the numeric value corresponding to the standard input stream.\n\ | |
2236 When Octave is used interactively, this is filtered through the command\n\ | |
2237 line editing functions.\n\ | |
5333 | 2238 @seealso{stdout, stderr}\n\ |
5749 | 2239 @end deftypefn") |
2240 { | |
2241 return const_value ("stdin", args, stdin_file); | |
2242 } | |
2189 | 2243 |
5749 | 2244 DEFUNX ("stdout", Fstdout, args, , |
2245 "-*- texinfo -*-\n\ | |
2246 @deftypefn {Built-in Function} {} stdout ()\n\ | |
2247 Return the numeric value corresponding to the standard output stream.\n\ | |
2248 Data written to the standard output is normally filtered through the pager.\n\ | |
5333 | 2249 @seealso{stdin, stderr}\n\ |
5749 | 2250 @end deftypefn") |
2251 { | |
2252 return const_value ("stdout", args, stdout_file); | |
2253 } | |
2189 | 2254 |
5749 | 2255 DEFUNX ("stderr", Fstderr, args, , |
2256 "-*- texinfo -*-\n\ | |
2257 @deftypefn {Built-in Function} {} stderr ()\n\ | |
2258 Return the numeric value corresponding to the standard error stream.\n\ | |
2259 Even if paging is turned on, the standard error is not sent to the\n\ | |
2260 pager. It is useful for error messages and prompts.\n\ | |
5333 | 2261 @seealso{stdin, stdout}\n\ |
5749 | 2262 @end deftypefn") |
2263 { | |
2264 return const_value ("stderr", args, stderr_file); | |
2189 | 2265 } |