Mercurial > hg > octave-nkf
annotate src/dirfns.cc @ 9266:1d3b91166b9c
allow pathsep to be set
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 26 May 2009 17:20:07 -0400 |
parents | 7c02ec148a3c |
children | f9ac007bb926 |
rev | line source |
---|---|
523 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003, |
8920 | 4 2004, 2005, 2006, 2007, 2008, 2009 John W. Eaton |
523 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
523 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
523 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
1192 | 25 #include <config.h> |
523 | 26 #endif |
27 | |
1341 | 28 #include <cerrno> |
29 #include <cstdio> | |
30 #include <cstddef> | |
31 #include <cstdlib> | |
32 #include <cstring> | |
33 | |
5765 | 34 #include <sstream> |
1728 | 35 #include <string> |
36 | |
1832 | 37 #ifdef HAVE_UNISTD_H |
2442 | 38 #ifdef HAVE_SYS_TYPES_H |
1832 | 39 #include <sys/types.h> |
2442 | 40 #endif |
1832 | 41 #include <unistd.h> |
42 #endif | |
43 | |
2926 | 44 #include "file-ops.h" |
45 #include "file-stat.h" | |
46 #include "glob-match.h" | |
47 #include "oct-env.h" | |
5777 | 48 #include "pathsearch.h" |
1755 | 49 #include "str-vec.h" |
50 | |
5102 | 51 #include "Cell.h" |
1355 | 52 #include "defun.h" |
1781 | 53 #include "dir-ops.h" |
1355 | 54 #include "dirfns.h" |
55 #include "error.h" | |
1402 | 56 #include "gripes.h" |
5640 | 57 #include "input.h" |
5832 | 58 #include "load-path.h" |
1750 | 59 #include "oct-obj.h" |
1355 | 60 #include "pager.h" |
61 #include "procstream.h" | |
62 #include "sysdep.h" | |
1750 | 63 #include "toplev.h" |
1449 | 64 #include "unwind-prot.h" |
523 | 65 #include "utils.h" |
1742 | 66 #include "variables.h" |
523 | 67 |
5640 | 68 // TRUE means we ask for confirmation before recursively removing a |
69 // directory tree. | |
70 static bool Vconfirm_recursive_rmdir = true; | |
71 | |
6323 | 72 // The time we last time we changed directories. |
73 octave_time Vlast_chdir_time = 0.0; | |
74 | |
1328 | 75 static int |
3523 | 76 octave_change_to_directory (const std::string& newdir) |
1328 | 77 { |
5979 | 78 int cd_ok = octave_env::chdir (file_ops::tilde_expand (newdir)); |
1328 | 79 |
80 if (cd_ok) | |
5832 | 81 { |
6323 | 82 Vlast_chdir_time.stamp (); |
83 | |
5832 | 84 // FIXME -- should this be handled as a list of functions |
85 // to call so users can add their own chdir handlers? | |
86 | |
87 load_path::update (); | |
88 } | |
1328 | 89 else |
3531 | 90 { |
91 using namespace std; | |
92 | |
93 error ("%s: %s", newdir.c_str (), strerror (errno)); | |
94 } | |
1328 | 95 |
96 return cd_ok; | |
97 } | |
98 | |
8746
5dd06f19e9be
handle commands in the lexer
John W. Eaton <jwe@octave.org>
parents:
8503
diff
changeset
|
99 DEFUN (cd, args, , |
3301 | 100 "-*- texinfo -*-\n\ |
101 @deffn {Command} cd dir\n\ | |
102 @deffnx {Command} chdir dir\n\ | |
103 Change the current working directory to @var{dir}. If @var{dir} is\n\ | |
7001 | 104 omitted, the current directory is changed to the user's home\n\ |
3301 | 105 directory. For example,\n\ |
523 | 106 \n\ |
3301 | 107 @example\n\ |
108 cd ~/octave\n\ | |
109 @end example\n\ | |
110 \n\ | |
111 @noindent\n\ | |
112 Changes the current working directory to @file{~/octave}. If the\n\ | |
113 directory does not exist, an error message is printed and the working\n\ | |
114 directory is not changed.\n\ | |
5597 | 115 @seealso{mkdir, rmdir, dir}\n\ |
3301 | 116 @end deffn") |
523 | 117 { |
2086 | 118 octave_value_list retval; |
523 | 119 |
1755 | 120 int argc = args.length () + 1; |
121 | |
1965 | 122 string_vector argv = args.make_argv ("cd"); |
1755 | 123 |
124 if (error_state) | |
125 return retval; | |
523 | 126 |
127 if (argc > 1) | |
128 { | |
5872 | 129 std::string dirname = argv[1]; |
523 | 130 |
1750 | 131 if (dirname.length () > 0 |
1755 | 132 && ! octave_change_to_directory (dirname)) |
523 | 133 { |
134 return retval; | |
135 } | |
136 } | |
137 else | |
138 { | |
3523 | 139 std::string home_dir = octave_env::get_home_directory (); |
2926 | 140 |
141 if (home_dir.empty () || ! octave_change_to_directory (home_dir)) | |
142 return retval; | |
523 | 143 } |
144 | |
145 return retval; | |
146 } | |
147 | |
611 | 148 DEFALIAS (chdir, cd); |
149 | |
6482 | 150 DEFUN (pwd, , , |
3301 | 151 "-*- texinfo -*-\n\ |
152 @deftypefn {Built-in Function} {} pwd ()\n\ | |
153 Return the current working directory.\n\ | |
5597 | 154 @seealso{dir, ls}\n\ |
3301 | 155 @end deftypefn") |
523 | 156 { |
5979 | 157 return octave_value (octave_env::getcwd ()); |
523 | 158 } |
159 | |
1957 | 160 DEFUN (readdir, args, , |
3301 | 161 "-*- texinfo -*-\n\ |
162 @deftypefn {Built-in Function} {[@var{files}, @var{err}, @var{msg}] =} readdir (@var{dir})\n\ | |
4691 | 163 Return names of the files in the directory @var{dir} as a cell array of\n\ |
164 strings. If an error occurs, return an empty cell array in @var{files}.\n\ | |
1389 | 165 \n\ |
3301 | 166 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ |
167 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ | |
168 system-dependent error message.\n\ | |
5597 | 169 @seealso{dir, glob}\n\ |
3301 | 170 @end deftypefn") |
1389 | 171 { |
2086 | 172 octave_value_list retval; |
1389 | 173 |
3523 | 174 retval(2) = std::string (); |
2669 | 175 retval(1) = -1.0; |
4691 | 176 retval(0) = Cell (); |
2669 | 177 |
1401 | 178 if (args.length () == 1) |
1389 | 179 { |
3523 | 180 std::string dirname = args(0).string_value (); |
1389 | 181 |
1401 | 182 if (error_state) |
1781 | 183 gripe_wrong_type_arg ("readdir", args(0)); |
1401 | 184 else |
185 { | |
5872 | 186 dir_entry dir (dirname); |
1389 | 187 |
1401 | 188 if (dir) |
1389 | 189 { |
1781 | 190 string_vector dirlist = dir.read (); |
8503
8ba2ee57c594
remove qsort in favor of sort
Jaroslav Hajek <highegg@gmail.com>
parents:
8317
diff
changeset
|
191 retval(0) = Cell (dirlist.sort ()); |
2669 | 192 retval(1) = 0.0; |
1401 | 193 } |
194 else | |
195 { | |
2669 | 196 retval(2) = dir.error (); |
1401 | 197 } |
1389 | 198 } |
199 } | |
200 else | |
5823 | 201 print_usage (); |
1389 | 202 |
1401 | 203 return retval; |
204 } | |
205 | |
5775 | 206 // FIXME -- should maybe also allow second arg to specify |
5476 | 207 // mode? OTOH, that might cause trouble with compatibility later... |
1401 | 208 |
8746
5dd06f19e9be
handle commands in the lexer
John W. Eaton <jwe@octave.org>
parents:
8503
diff
changeset
|
209 DEFUN (mkdir, args, , |
3301 | 210 "-*- texinfo -*-\n\ |
5476 | 211 @deftypefn {Built-in Function} {[@var{status}, @var{msg}, @var{msgid}] =} mkdir (@var{dir})\n\ |
6192 | 212 @deftypefnx {Built-in Function} {[@var{status}, @var{msg}, @var{msgid}] =} mkdir (@var{parent}, @var{dir})\n\ |
8109 | 213 Create a directory named @var{dir} in the directory @var{parent}.\n\ |
1401 | 214 \n\ |
5476 | 215 If successful, @var{status} is 1, with @var{msg} and @var{msgid} empty\n\ |
216 character strings. Otherwise, @var{status} is 0, @var{msg} contains a\n\ | |
217 system-dependent error message, and @var{msgid} contains a unique\n\ | |
218 message identifier.\n\ | |
5597 | 219 @seealso{rmdir}\n\ |
3301 | 220 @end deftypefn") |
1401 | 221 { |
2086 | 222 octave_value_list retval; |
1401 | 223 |
5476 | 224 retval(2) = std::string (); |
3523 | 225 retval(1) = std::string (); |
5476 | 226 retval(0) = false; |
1401 | 227 |
6187 | 228 int nargin = args.length (); |
229 | |
230 std::string dirname; | |
231 | |
232 if (nargin == 2) | |
1401 | 233 { |
6187 | 234 std::string parent = args(0).string_value (); |
235 std::string dir = args(1).string_value (); | |
1401 | 236 |
237 if (error_state) | |
238 { | |
6187 | 239 gripe_wrong_type_arg ("mkdir", args(0)); |
240 return retval; | |
241 } | |
242 else | |
7272 | 243 dirname = file_ops::concat (parent, dir); |
6187 | 244 } |
6200 | 245 else if (nargin == 1) |
6187 | 246 { |
247 dirname = args(0).string_value (); | |
2669 | 248 |
6187 | 249 if (error_state) |
250 { | |
251 gripe_wrong_type_arg ("mkdir", args(0)); | |
252 return retval; | |
253 } | |
254 } | |
255 | |
256 if (nargin == 1 || nargin == 2) | |
257 { | |
258 std::string msg; | |
259 | |
7970
b6d4c644b4b6
Fmkdir: improve compatibility
John W. Eaton <jwe@octave.org>
parents:
7272
diff
changeset
|
260 dirname = file_ops::tilde_expand (dirname); |
b6d4c644b4b6
Fmkdir: improve compatibility
John W. Eaton <jwe@octave.org>
parents:
7272
diff
changeset
|
261 |
b6d4c644b4b6
Fmkdir: improve compatibility
John W. Eaton <jwe@octave.org>
parents:
7272
diff
changeset
|
262 file_stat fs (dirname); |
1489 | 263 |
7970
b6d4c644b4b6
Fmkdir: improve compatibility
John W. Eaton <jwe@octave.org>
parents:
7272
diff
changeset
|
264 if (fs && fs.is_dir ()) |
6187 | 265 { |
7970
b6d4c644b4b6
Fmkdir: improve compatibility
John W. Eaton <jwe@octave.org>
parents:
7272
diff
changeset
|
266 // For compatibility with Matlab, we return true when the |
b6d4c644b4b6
Fmkdir: improve compatibility
John W. Eaton <jwe@octave.org>
parents:
7272
diff
changeset
|
267 // directory already exists. |
b6d4c644b4b6
Fmkdir: improve compatibility
John W. Eaton <jwe@octave.org>
parents:
7272
diff
changeset
|
268 |
6187 | 269 retval(2) = "mkdir"; |
7970
b6d4c644b4b6
Fmkdir: improve compatibility
John W. Eaton <jwe@octave.org>
parents:
7272
diff
changeset
|
270 retval(1) = "directory exists"; |
b6d4c644b4b6
Fmkdir: improve compatibility
John W. Eaton <jwe@octave.org>
parents:
7272
diff
changeset
|
271 retval(0) = true; |
1401 | 272 } |
6187 | 273 else |
7970
b6d4c644b4b6
Fmkdir: improve compatibility
John W. Eaton <jwe@octave.org>
parents:
7272
diff
changeset
|
274 { |
b6d4c644b4b6
Fmkdir: improve compatibility
John W. Eaton <jwe@octave.org>
parents:
7272
diff
changeset
|
275 int status = file_ops::mkdir (dirname, 0777, msg); |
b6d4c644b4b6
Fmkdir: improve compatibility
John W. Eaton <jwe@octave.org>
parents:
7272
diff
changeset
|
276 |
b6d4c644b4b6
Fmkdir: improve compatibility
John W. Eaton <jwe@octave.org>
parents:
7272
diff
changeset
|
277 if (status < 0) |
b6d4c644b4b6
Fmkdir: improve compatibility
John W. Eaton <jwe@octave.org>
parents:
7272
diff
changeset
|
278 { |
b6d4c644b4b6
Fmkdir: improve compatibility
John W. Eaton <jwe@octave.org>
parents:
7272
diff
changeset
|
279 retval(2) = "mkdir"; |
b6d4c644b4b6
Fmkdir: improve compatibility
John W. Eaton <jwe@octave.org>
parents:
7272
diff
changeset
|
280 retval(1) = msg; |
b6d4c644b4b6
Fmkdir: improve compatibility
John W. Eaton <jwe@octave.org>
parents:
7272
diff
changeset
|
281 } |
b6d4c644b4b6
Fmkdir: improve compatibility
John W. Eaton <jwe@octave.org>
parents:
7272
diff
changeset
|
282 else |
b6d4c644b4b6
Fmkdir: improve compatibility
John W. Eaton <jwe@octave.org>
parents:
7272
diff
changeset
|
283 retval(0) = true; |
b6d4c644b4b6
Fmkdir: improve compatibility
John W. Eaton <jwe@octave.org>
parents:
7272
diff
changeset
|
284 } |
1401 | 285 } |
286 else | |
5823 | 287 print_usage (); |
1401 | 288 |
289 return retval; | |
290 } | |
291 | |
8746
5dd06f19e9be
handle commands in the lexer
John W. Eaton <jwe@octave.org>
parents:
8503
diff
changeset
|
292 DEFUN (rmdir, args, , |
3301 | 293 "-*- texinfo -*-\n\ |
5476 | 294 @deftypefn {Built-in Function} {[@var{status}, @var{msg}, @var{msgid}] =} rmdir (@var{dir})\n\ |
295 @deftypefnx {Built-in Function} {[@var{status}, @var{msg}, @var{msgid}] =} rmdir (@var{dir}, @code{\"s\"})\n\ | |
3301 | 296 Remove the directory named @var{dir}.\n\ |
1401 | 297 \n\ |
5476 | 298 If successful, @var{status} is 1, with @var{msg} and @var{msgid} empty\n\ |
299 character strings. Otherwise, @var{status} is 0, @var{msg} contains a\n\ | |
300 system-dependent error message, and @var{msgid} contains a unique\n\ | |
301 message identifier.\n\ | |
302 \n\ | |
7007 | 303 If the optional second parameter is supplied with value @code{\"s\"},\n\ |
6134 | 304 recursively remove all subdirectories as well.\n\ |
5640 | 305 @seealso{mkdir, confirm_recursive_rmdir}\n\ |
3301 | 306 @end deftypefn") |
1401 | 307 { |
2086 | 308 octave_value_list retval; |
1401 | 309 |
5476 | 310 retval(2) = std::string (); |
3523 | 311 retval(1) = std::string (); |
5476 | 312 retval(0) = false; |
1401 | 313 |
5476 | 314 int nargin = args.length (); |
315 | |
316 if (nargin == 1 || nargin == 2) | |
1401 | 317 { |
3523 | 318 std::string dirname = args(0).string_value (); |
1401 | 319 |
320 if (error_state) | |
1402 | 321 gripe_wrong_type_arg ("rmdir", args(0)); |
1489 | 322 else |
1401 | 323 { |
5640 | 324 std::string fulldir = file_ops::tilde_expand (dirname); |
325 int status = -1; | |
3523 | 326 std::string msg; |
2669 | 327 |
5639 | 328 if (nargin == 2) |
329 { | |
330 if (args(1).string_value () == "s") | |
5640 | 331 { |
332 bool doit = true; | |
333 | |
334 if (interactive && Vconfirm_recursive_rmdir) | |
335 { | |
336 std::string prompt | |
337 = "remove entire contents of " + fulldir + "? "; | |
338 | |
339 doit = octave_yes_or_no (prompt); | |
340 } | |
341 | |
342 if (doit) | |
343 status = file_ops::recursive_rmdir (fulldir, msg); | |
344 } | |
5639 | 345 else |
346 error ("rmdir: expecting second argument to be \"s\""); | |
347 } | |
348 else | |
5640 | 349 status = file_ops::rmdir (fulldir, msg); |
2669 | 350 |
351 if (status < 0) | |
5476 | 352 { |
353 retval(2) = "rmdir"; | |
354 retval(1) = msg; | |
355 } | |
356 else | |
357 retval(0) = true; | |
1401 | 358 } |
359 } | |
1389 | 360 else |
5823 | 361 print_usage (); |
1401 | 362 |
363 return retval; | |
364 } | |
365 | |
3710 | 366 DEFUN (link, args, , |
367 "-*- texinfo -*-\n\ | |
368 @deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} link (@var{old}, @var{new})\n\ | |
369 Create a new link (also known as a hard link) to an existing file.\n\ | |
370 \n\ | |
371 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ | |
372 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ | |
373 system-dependent error message.\n\ | |
5597 | 374 @seealso{symlink}\n\ |
3710 | 375 @end deftypefn") |
376 { | |
377 octave_value_list retval; | |
378 | |
379 retval(1) = std::string (); | |
380 retval(0) = -1.0; | |
381 | |
382 if (args.length () == 2) | |
383 { | |
384 std::string from = args(0).string_value (); | |
385 | |
386 if (error_state) | |
387 gripe_wrong_type_arg ("link", args(0)); | |
388 else | |
389 { | |
390 std::string to = args(1).string_value (); | |
391 | |
392 if (error_state) | |
393 gripe_wrong_type_arg ("link", args(1)); | |
394 else | |
395 { | |
396 std::string msg; | |
397 | |
398 int status = file_ops::link (from, to, msg); | |
399 | |
4233 | 400 retval(0) = status; |
3710 | 401 |
402 if (status < 0) | |
403 retval(1) = msg; | |
404 } | |
405 } | |
406 } | |
407 else | |
5823 | 408 print_usage (); |
3710 | 409 |
410 return retval; | |
411 } | |
412 | |
413 DEFUN (symlink, args, , | |
414 "-*- texinfo -*-\n\ | |
415 @deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} symlink (@var{old}, @var{new})\n\ | |
416 Create a symbolic link @var{new} which contains the string @var{old}.\n\ | |
417 \n\ | |
418 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ | |
419 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ | |
420 system-dependent error message.\n\ | |
5597 | 421 @seealso{link, readlink}\n\ |
3710 | 422 @end deftypefn") |
423 { | |
424 octave_value_list retval; | |
425 | |
426 retval(1) = std::string (); | |
427 retval(0) = -1.0; | |
428 | |
429 if (args.length () == 2) | |
430 { | |
431 std::string from = args(0).string_value (); | |
432 | |
433 if (error_state) | |
434 gripe_wrong_type_arg ("symlink", args(0)); | |
435 else | |
436 { | |
437 std::string to = args(1).string_value (); | |
438 | |
439 if (error_state) | |
440 gripe_wrong_type_arg ("symlink", args(1)); | |
441 else | |
442 { | |
443 std::string msg; | |
444 | |
445 int status = file_ops::symlink (from, to, msg); | |
446 | |
4233 | 447 retval(0) = status; |
3710 | 448 |
449 if (status < 0) | |
450 retval(1) = msg; | |
451 } | |
452 } | |
453 } | |
454 else | |
5823 | 455 print_usage (); |
3710 | 456 |
457 return retval; | |
458 } | |
459 | |
460 DEFUN (readlink, args, , | |
461 "-*- texinfo -*-\n\ | |
4169 | 462 @deftypefn {Built-in Function} {[@var{result}, @var{err}, @var{msg}] =} readlink (@var{symlink})\n\ |
3710 | 463 Read the value of the symbolic link @var{symlink}.\n\ |
464 \n\ | |
465 If successful, @var{result} contains the contents of the symbolic link\n\ | |
466 @var{symlink}, @var{err} is 0 and @var{msg} is an empty string.\n\ | |
467 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ | |
468 system-dependent error message.\n\ | |
5597 | 469 @seealso{link, symlink}\n\ |
3710 | 470 @end deftypefn") |
471 { | |
472 octave_value_list retval; | |
473 | |
474 retval(2) = std::string (); | |
475 retval(1) = -1.0; | |
476 retval(0) = std::string (); | |
477 | |
478 if (args.length () == 1) | |
479 { | |
480 std::string symlink = args(0).string_value (); | |
481 | |
482 if (error_state) | |
483 gripe_wrong_type_arg ("readlink", args(0)); | |
484 else | |
485 { | |
486 std::string result; | |
487 std::string msg; | |
488 | |
489 int status = file_ops::readlink (symlink, result, msg); | |
490 | |
491 retval(0) = result; | |
492 | |
4233 | 493 retval(1) = status; |
3710 | 494 |
495 if (status < 0) | |
496 retval(2) = msg; | |
497 } | |
498 } | |
499 else | |
5823 | 500 print_usage (); |
3710 | 501 |
502 return retval; | |
503 } | |
504 | |
1957 | 505 DEFUN (rename, args, , |
3301 | 506 "-*- texinfo -*-\n\ |
507 @deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} rename (@var{old}, @var{new})\n\ | |
508 Change the name of file @var{old} to @var{new}.\n\ | |
1401 | 509 \n\ |
3301 | 510 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ |
511 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ | |
512 system-dependent error message.\n\ | |
5597 | 513 @seealso{ls, dir}\n\ |
3301 | 514 @end deftypefn") |
1401 | 515 { |
2086 | 516 octave_value_list retval; |
1401 | 517 |
3523 | 518 retval(1) = std::string (); |
2669 | 519 retval(0) = -1.0; |
1401 | 520 |
521 if (args.length () == 2) | |
522 { | |
3523 | 523 std::string from = args(0).string_value (); |
1728 | 524 |
1401 | 525 if (error_state) |
1402 | 526 gripe_wrong_type_arg ("rename", args(0)); |
527 else | |
1401 | 528 { |
3523 | 529 std::string to = args(1).string_value (); |
1728 | 530 |
1402 | 531 if (error_state) |
532 gripe_wrong_type_arg ("rename", args(1)); | |
2669 | 533 else |
1402 | 534 { |
3523 | 535 std::string msg; |
2669 | 536 |
2926 | 537 int status = file_ops::rename (from, to, msg); |
2669 | 538 |
4233 | 539 retval(0) = status; |
2669 | 540 |
541 if (status < 0) | |
542 retval(1) = msg; | |
1402 | 543 } |
1401 | 544 } |
545 } | |
546 else | |
5823 | 547 print_usage (); |
1401 | 548 |
1389 | 549 return retval; |
550 } | |
551 | |
2495 | 552 DEFUN (glob, args, , |
3301 | 553 "-*- texinfo -*-\n\ |
554 @deftypefn {Built-in Function} {} glob (@var{pattern})\n\ | |
5444 | 555 Given an array of strings (as a char array or a cell array) in\n\ |
556 @var{pattern}, return a cell array of file names that match any of\n\ | |
557 them, or an empty cell array if no patterns match. Tilde expansion\n\ | |
558 is performed on each of the patterns before looking for matching file\n\ | |
559 names. For example,\n\ | |
2495 | 560 \n\ |
3301 | 561 @example\n\ |
562 @group\n\ | |
563 glob (\"/vm*\")\n\ | |
564 @result{} \"/vmlinuz\"\n\ | |
565 @end group\n\ | |
566 @end example\n\ | |
5597 | 567 @seealso{dir, ls, stat, readdir}\n\ |
568 @end deftypefn") | |
2495 | 569 { |
570 octave_value retval; | |
571 | |
572 if (args.length () == 1) | |
573 { | |
574 string_vector pat = args(0).all_strings (); | |
575 | |
576 if (error_state) | |
577 gripe_wrong_type_arg ("glob", args(0)); | |
578 else | |
579 { | |
2926 | 580 glob_match pattern (file_ops::tilde_expand (pat)); |
2495 | 581 |
4691 | 582 retval = Cell (pattern.glob ()); |
2495 | 583 } |
584 } | |
585 else | |
5823 | 586 print_usage (); |
2495 | 587 |
588 return retval; | |
589 } | |
590 | |
2496 | 591 DEFUN (fnmatch, args, , |
3301 | 592 "-*- texinfo -*-\n\ |
593 @deftypefn {Built-in Function} {} fnmatch (@var{pattern}, @var{string})\n\ | |
594 Return 1 or zero for each element of @var{string} that matches any of\n\ | |
595 the elements of the string array @var{pattern}, using the rules of\n\ | |
596 filename pattern matching. For example,\n\ | |
2496 | 597 \n\ |
3301 | 598 @example\n\ |
599 @group\n\ | |
6233 | 600 fnmatch (\"a*b\", @{\"ab\"; \"axyzb\"; \"xyzab\"@})\n\ |
3301 | 601 @result{} [ 1; 1; 0 ]\n\ |
602 @end group\n\ | |
603 @end example\n\ | |
604 @end deftypefn") | |
2496 | 605 { |
606 octave_value retval; | |
607 | |
608 if (args.length () == 2) | |
609 { | |
610 string_vector pat = args(0).all_strings (); | |
611 string_vector str = args(1).all_strings (); | |
612 | |
613 if (error_state) | |
614 gripe_wrong_type_arg ("fnmatch", args(0)); | |
615 else | |
616 { | |
2926 | 617 glob_match pattern (file_ops::tilde_expand (pat)); |
2496 | 618 |
619 Array<bool> tmp = pattern.match (str); | |
620 | |
5275 | 621 octave_idx_type n = tmp.length (); |
2496 | 622 |
623 ColumnVector result (n); | |
624 | |
5275 | 625 for (octave_idx_type i = 0; i < n; i++) |
2496 | 626 result(i) = tmp(i); |
627 | |
3418 | 628 retval = result; |
2496 | 629 } |
630 } | |
631 else | |
5823 | 632 print_usage (); |
2496 | 633 |
634 return retval; | |
635 } | |
636 | |
5777 | 637 DEFUN (filesep, args, , |
5832 | 638 "-*- texinfo -*-\n\ |
639 @deftypefn {Built-in Function} {} filesep ()\n\ | |
8317
135c0e7d7802
Extend filesep functionality by allowing to return all valid file separators
Michael Goffioul <michael.goffioul@gmail.com>
parents:
8109
diff
changeset
|
640 @deftypefnx {Built-in Function} {} filesep ('all')\n\ |
5777 | 641 Return the system-dependent character used to separate directory names.\n\ |
8317
135c0e7d7802
Extend filesep functionality by allowing to return all valid file separators
Michael Goffioul <michael.goffioul@gmail.com>
parents:
8109
diff
changeset
|
642 \n\ |
135c0e7d7802
Extend filesep functionality by allowing to return all valid file separators
Michael Goffioul <michael.goffioul@gmail.com>
parents:
8109
diff
changeset
|
643 If 'all' is given, the function return all valid file separators in\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
644 the form of a string. The list of file separators is system-dependent.\n\ |
8317
135c0e7d7802
Extend filesep functionality by allowing to return all valid file separators
Michael Goffioul <michael.goffioul@gmail.com>
parents:
8109
diff
changeset
|
645 It is / (forward slash) under UNIX or Mac OS X, / and \\ (forward and\n\ |
135c0e7d7802
Extend filesep functionality by allowing to return all valid file separators
Michael Goffioul <michael.goffioul@gmail.com>
parents:
8109
diff
changeset
|
646 backward slashes) under Windows.\n\ |
5777 | 647 @seealso{pathsep, dir, ls}\n\ |
648 @end deftypefn") | |
649 { | |
650 octave_value retval; | |
651 | |
652 if (args.length () == 0) | |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7970
diff
changeset
|
653 retval = file_ops::dir_sep_str (); |
8317
135c0e7d7802
Extend filesep functionality by allowing to return all valid file separators
Michael Goffioul <michael.goffioul@gmail.com>
parents:
8109
diff
changeset
|
654 else if (args.length () == 1) |
135c0e7d7802
Extend filesep functionality by allowing to return all valid file separators
Michael Goffioul <michael.goffioul@gmail.com>
parents:
8109
diff
changeset
|
655 { |
135c0e7d7802
Extend filesep functionality by allowing to return all valid file separators
Michael Goffioul <michael.goffioul@gmail.com>
parents:
8109
diff
changeset
|
656 std::string s = args(0).string_value (); |
135c0e7d7802
Extend filesep functionality by allowing to return all valid file separators
Michael Goffioul <michael.goffioul@gmail.com>
parents:
8109
diff
changeset
|
657 |
135c0e7d7802
Extend filesep functionality by allowing to return all valid file separators
Michael Goffioul <michael.goffioul@gmail.com>
parents:
8109
diff
changeset
|
658 if (! error_state) |
135c0e7d7802
Extend filesep functionality by allowing to return all valid file separators
Michael Goffioul <michael.goffioul@gmail.com>
parents:
8109
diff
changeset
|
659 { |
135c0e7d7802
Extend filesep functionality by allowing to return all valid file separators
Michael Goffioul <michael.goffioul@gmail.com>
parents:
8109
diff
changeset
|
660 if (s == "all") |
135c0e7d7802
Extend filesep functionality by allowing to return all valid file separators
Michael Goffioul <michael.goffioul@gmail.com>
parents:
8109
diff
changeset
|
661 retval = file_ops::dir_sep_chars (); |
135c0e7d7802
Extend filesep functionality by allowing to return all valid file separators
Michael Goffioul <michael.goffioul@gmail.com>
parents:
8109
diff
changeset
|
662 else |
135c0e7d7802
Extend filesep functionality by allowing to return all valid file separators
Michael Goffioul <michael.goffioul@gmail.com>
parents:
8109
diff
changeset
|
663 gripe_wrong_type_arg ("filesep", args(0)); |
135c0e7d7802
Extend filesep functionality by allowing to return all valid file separators
Michael Goffioul <michael.goffioul@gmail.com>
parents:
8109
diff
changeset
|
664 } |
135c0e7d7802
Extend filesep functionality by allowing to return all valid file separators
Michael Goffioul <michael.goffioul@gmail.com>
parents:
8109
diff
changeset
|
665 else |
135c0e7d7802
Extend filesep functionality by allowing to return all valid file separators
Michael Goffioul <michael.goffioul@gmail.com>
parents:
8109
diff
changeset
|
666 gripe_wrong_type_arg ("filesep", args(0)); |
135c0e7d7802
Extend filesep functionality by allowing to return all valid file separators
Michael Goffioul <michael.goffioul@gmail.com>
parents:
8109
diff
changeset
|
667 } |
5777 | 668 else |
5823 | 669 print_usage (); |
5777 | 670 |
671 return retval; | |
672 } | |
673 | |
9266 | 674 DEFUN (pathsep, args, nargout, |
5777 | 675 "-*- texinfo -*-\n\ |
9266 | 676 @deftypefn {Built-in Function} {@var{val} =} pathsep ()\n\ |
677 @deftypefn {Built-in Function} {@var{old_val} =} pathsep (@var{new_val})\n\ | |
678 Query or set the character used to separate directories in\n\ | |
5777 | 679 a path.\n\ |
680 @seealso{filesep, dir, ls}\n\ | |
681 @end deftypefn") | |
682 { | |
683 octave_value retval; | |
684 | |
9266 | 685 int nargin = args.length (); |
686 | |
687 if (nargout > 0 || nargin == 0) | |
8008
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
8007
diff
changeset
|
688 retval = dir_path::path_sep_str (); |
9266 | 689 |
690 if (nargin == 1) | |
691 { | |
692 std::string sval = args(0).string_value (); | |
693 | |
694 if (! error_state) | |
695 { | |
696 switch (sval.length ()) | |
697 { | |
698 case 1: | |
699 dir_path::path_sep_char (sval[0]); | |
700 break; | |
701 | |
702 case 0: | |
703 dir_path::path_sep_char ('\0'); | |
704 break; | |
705 | |
706 default: | |
707 error ("pathsep: argument must be a single character"); | |
708 break; | |
709 } | |
710 } | |
711 else | |
712 error ("pathsep: argument must be a single character"); | |
713 } | |
714 else if (nargin > 1) | |
5823 | 715 print_usage (); |
5777 | 716 |
717 return retval; | |
718 } | |
719 | |
5794 | 720 DEFUN (confirm_recursive_rmdir, args, nargout, |
721 "-*- texinfo -*-\n\ | |
722 @deftypefn {Built-in Function} {@var{val} =} confirm_recursive_rmdir ()\n\ | |
723 @deftypefnx {Built-in Function} {@var{old_val} =} confirm_recursive_rmdir (@var{new_val})\n\ | |
724 Query or set the internal variable that controls whether Octave\n\ | |
725 will ask for confirmation before recursively removing a directory tree.\n\ | |
726 @end deftypefn") | |
5640 | 727 { |
5794 | 728 return SET_INTERNAL_VARIABLE (confirm_recursive_rmdir); |
4264 | 729 } |
730 | |
523 | 731 /* |
732 ;;; Local Variables: *** | |
733 ;;; mode: C++ *** | |
734 ;;; End: *** | |
735 */ |