annotate scripts/path/addpath.m @ 5789:7d77b6839ca7

[project @ 2006-05-04 13:38:49 by dbateman]
author dbateman
date Thu, 04 May 2006 13:38:50 +0000
parents e07baa8b93b9
children 296cefb48d7e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
1 ## Copyright (C) 2005 Bill Denney
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
2 ##
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
3 ## This program is free software; you can redistribute it and/or modify
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
4 ## it under the terms of the GNU General Public License as published by
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
5 ## the Free Software Foundation; either version 2 of the License, or
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
6 ## (at your option) any later version.
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
7 ##
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
8 ## This program is distributed in the hope that it will be useful,
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
11 ## GNU General Public License for more details.
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
12 ##
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
13 ## You should have received a copy of the GNU General Public License
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
14 ## along with this program; if not, write to the Free Software
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
16 ##
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
17 ## Based on code Copyright (C) 2000 Etienne Grossmann
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
18
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
19 ## -*- texinfo -*-
5736
e07baa8b93b9 [project @ 2006-04-04 19:41:45 by jwe]
jwe
parents: 5733
diff changeset
20 ## @deftypefn {Function File} {} addpath (@var{dir1}, @dots{})
5733
6b345b4961ca [project @ 2006-04-04 18:03:51 by jwe]
jwe
parents: 5732
diff changeset
21 ## Prepend @var{dir1}, @dots{} to the current @code{LOADPATH}.
6b345b4961ca [project @ 2006-04-04 18:03:51 by jwe]
jwe
parents: 5732
diff changeset
22 ## If the directory is already in the path, it is moved to the specified
6b345b4961ca [project @ 2006-04-04 18:03:51 by jwe]
jwe
parents: 5732
diff changeset
23 ## location, prepending by default.
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
24 ##
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
25 ## @example
5733
6b345b4961ca [project @ 2006-04-04 18:03:51 by jwe]
jwe
parents: 5732
diff changeset
26 ## addpath (dir1, "-end", dir2, "-begin", dir3, "-END", dir4, "-BEGIN", dir5)
6b345b4961ca [project @ 2006-04-04 18:03:51 by jwe]
jwe
parents: 5732
diff changeset
27 ## @result{} Prepend dir1, dir3 and dir5 and append dir2 and dir4.
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
28 ## @end example
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
29 ##
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
30 ## An error will be returned if the string is not a directory, the
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
31 ## directory doesn't exist or you don't have read access to it.
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
32 ##
5733
6b345b4961ca [project @ 2006-04-04 18:03:51 by jwe]
jwe
parents: 5732
diff changeset
33 ## BUG: This function can't add directories called @samp{-end} or
6b345b4961ca [project @ 2006-04-04 18:03:51 by jwe]
jwe
parents: 5732
diff changeset
34 ## @samp{-begin} (case insensitively).
6b345b4961ca [project @ 2006-04-04 18:03:51 by jwe]
jwe
parents: 5732
diff changeset
35 ##
5736
e07baa8b93b9 [project @ 2006-04-04 19:41:45 by jwe]
jwe
parents: 5733
diff changeset
36 ## @seealso{LOADPATH, rmpath, savepath, setpath}
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
37 ## @end deftypefn
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
38
5733
6b345b4961ca [project @ 2006-04-04 18:03:51 by jwe]
jwe
parents: 5732
diff changeset
39 ## Author: Etienne Grossmann <etienne@cs.uky.edu>
6b345b4961ca [project @ 2006-04-04 18:03:51 by jwe]
jwe
parents: 5732
diff changeset
40 ## Modified-By: Bill Denney <bill@givebillmoney.com>
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
41
5736
e07baa8b93b9 [project @ 2006-04-04 19:41:45 by jwe]
jwe
parents: 5733
diff changeset
42 ## PKGADD: mark_as_command addpath
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
43
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
44 function ret = addpath (varargin)
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
45
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
46 if (nargout > 0)
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
47 path = varargin{1};
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
48 varargin = varargin(2:end);
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
49 else
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
50 path = LOADPATH;
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
51 endif
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
52
5733
6b345b4961ca [project @ 2006-04-04 18:03:51 by jwe]
jwe
parents: 5732
diff changeset
53 dir = "";
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
54 if (length (varargin) > 0)
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
55 append = 0;
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
56 switch varargin{end}
5733
6b345b4961ca [project @ 2006-04-04 18:03:51 by jwe]
jwe
parents: 5732
diff changeset
57 case { 0, "0", "-begin", "-BEGIN" }
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
58 varargin = varargin(1:end-1);
5733
6b345b4961ca [project @ 2006-04-04 18:03:51 by jwe]
jwe
parents: 5732
diff changeset
59 case { 1, "1", "-end", "-END" }
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
60 varargin = varargin(1:end-1);
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
61 append = 1;
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
62 endswitch
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
63
5789
7d77b6839ca7 [project @ 2006-05-04 13:38:49 by dbateman]
dbateman
parents: 5736
diff changeset
64 psep = pathsep();
7d77b6839ca7 [project @ 2006-05-04 13:38:49 by dbateman]
dbateman
parents: 5736
diff changeset
65
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
66 ## Avoid duplicates by stripping pre-existing entries
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
67 path = rmpath (path, varargin{:});
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
68
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
69 ## Check if the directories are valid
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
70 for arg = 1:length (varargin)
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
71 p = varargin{arg};
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
72 if (nargout == 0 && ! isempty (p))
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
73 [s, err, m] = stat (p);
5733
6b345b4961ca [project @ 2006-04-04 18:03:51 by jwe]
jwe
parents: 5732
diff changeset
74 if (err != 0)
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
75 warning ("addpath %s : %s\n", p, m);
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
76 continue;
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
77 elseif (index (s.modestr, "d") != 1)
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
78 warning ("addpath %s : not a directory (mode=%s)\n", p, s.modestr);
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
79 continue;
5733
6b345b4961ca [project @ 2006-04-04 18:03:51 by jwe]
jwe
parents: 5732
diff changeset
80 elseif (! (s.modestr(8) == "r"
6b345b4961ca [project @ 2006-04-04 18:03:51 by jwe]
jwe
parents: 5732
diff changeset
81 || (getgid == s.gid && s.modestr(5) == "r")
6b345b4961ca [project @ 2006-04-04 18:03:51 by jwe]
jwe
parents: 5732
diff changeset
82 || (getuid == s.uid && s.modestr(2) == "r")))
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
83 warning ("addpath %s : not readable (mode=%s)\n", p, s.modestr);
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
84 continue;
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
85 endif
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
86 endif
5789
7d77b6839ca7 [project @ 2006-05-04 13:38:49 by dbateman]
dbateman
parents: 5736
diff changeset
87 dir = sprintf ("%s%s%s", dir, psep, p);
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
88 endfor
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
89
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
90 ## Add the directories to the current path
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
91 if (! isempty (dir))
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
92 dir = dir(2:end);
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
93 if (isempty (path) && ! isempty (dir))
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
94 path = dir;
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
95 else
5789
7d77b6839ca7 [project @ 2006-05-04 13:38:49 by dbateman]
dbateman
parents: 5736
diff changeset
96 if strcmp (path, psep), path = ""; end
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
97 if append
5789
7d77b6839ca7 [project @ 2006-05-04 13:38:49 by dbateman]
dbateman
parents: 5736
diff changeset
98 path = sprintf ("%s%s%s", path, psep, dir);
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
99 else
5789
7d77b6839ca7 [project @ 2006-05-04 13:38:49 by dbateman]
dbateman
parents: 5736
diff changeset
100 path = sprintf ("%s%s%s", dir, psep, path);
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
101 endif
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
102 endif
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
103 endif
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
104 endif
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
105
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
106 if nargout
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
107 ret = path;
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
108 else
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
109 LOADPATH = path;
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
110 endif
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
111
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
112 endfunction
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
113
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
114 %!assert(addpath('','hello'),'hello');
5789
7d77b6839ca7 [project @ 2006-05-04 13:38:49 by dbateman]
dbateman
parents: 5736
diff changeset
115 %!assert(addpath('','hello','world'),['hello',pathsep(),'world'])
7d77b6839ca7 [project @ 2006-05-04 13:38:49 by dbateman]
dbateman
parents: 5736
diff changeset
116 %!assert(addpath(pathsep(),'hello'),['hello',pathsep()]);
7d77b6839ca7 [project @ 2006-05-04 13:38:49 by dbateman]
dbateman
parents: 5736
diff changeset
117 %!assert(addpath(pathsep(),'hello','-end'),[pathsep(),'hello']);
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
118 %!assert(addpath('hello','hello'),'hello');
5789
7d77b6839ca7 [project @ 2006-05-04 13:38:49 by dbateman]
dbateman
parents: 5736
diff changeset
119 %!assert(addpath('hello','world'),['world',pathsep(),'hello'])
7d77b6839ca7 [project @ 2006-05-04 13:38:49 by dbateman]
dbateman
parents: 5736
diff changeset
120 %!assert(addpath('hello','world','-end'),['hello',pathsep(),'world'])
7d77b6839ca7 [project @ 2006-05-04 13:38:49 by dbateman]
dbateman
parents: 5736
diff changeset
121 %!assert(addpath(['hello',pathsep()],'world','-end'),['hello',pathsep(),pathsep(),'world'])
7d77b6839ca7 [project @ 2006-05-04 13:38:49 by dbateman]
dbateman
parents: 5736
diff changeset
122 %!assert(addpath(['hello',pathsep()],'hello','world','-end'),[pathsep(),'hello',pathsep(),'world'])
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
123
5789
7d77b6839ca7 [project @ 2006-05-04 13:38:49 by dbateman]
dbateman
parents: 5736
diff changeset
124 %!assert(addpath('',''),pathsep())
7d77b6839ca7 [project @ 2006-05-04 13:38:49 by dbateman]
dbateman
parents: 5736
diff changeset
125 %!assert(addpath(pathsep(),''),pathsep())
7d77b6839ca7 [project @ 2006-05-04 13:38:49 by dbateman]
dbateman
parents: 5736
diff changeset
126 %!assert(addpath('hello',''),[pathsep(),'hello'])
7d77b6839ca7 [project @ 2006-05-04 13:38:49 by dbateman]
dbateman
parents: 5736
diff changeset
127 %!assert(addpath(['hello',pathsep(),'world'],''),[pathsep(),'hello',pathsep(),'world'])
7d77b6839ca7 [project @ 2006-05-04 13:38:49 by dbateman]
dbateman
parents: 5736
diff changeset
128 %!assert(addpath(['hello',pathsep(),'world',pathsep()],''),[pathsep(),'hello',pathsep(),'world'])
7d77b6839ca7 [project @ 2006-05-04 13:38:49 by dbateman]
dbateman
parents: 5736
diff changeset
129 %!assert(addpath(['hello',pathsep(),pathsep(),'world'],''),[pathsep(),'hello',pathsep(),'world'])