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