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