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