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