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