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