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