5732
|
1 ## Copyright (C) 2000 Etienne Grossmann |
|
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 ## -*- texinfo -*- |
|
18 ## @deftypefn {Function File} {} rmpath(dir1, ...) |
|
19 ## Removes dir1,... from the current LOADPATH. |
|
20 ## |
|
21 ## newpath = rmpath(path, dir1, ...) |
|
22 ## |
|
23 ## Removes dir1,... from path. |
|
24 ## @end deftypefn |
|
25 |
|
26 ## Author: Etienne Grossmann <etienne@cs.uky.edu> |
|
27 ## Last modified: June 2005 |
|
28 |
|
29 ##PKGADD: mark_as_command rmpath |
|
30 |
|
31 function ret = rmpath (varargin) |
|
32 |
|
33 if (nargout == 0) |
|
34 path = LOADPATH; |
|
35 else |
|
36 path = varargin{1}; |
|
37 endif |
|
38 |
|
39 strip_system_path = 0; |
|
40 for arg = nargout + 1:length (varargin) |
|
41 p = varargin{arg}; |
|
42 lp = length (p); |
|
43 |
|
44 ## '' is the system path |
|
45 if (lp==0) |
|
46 strip_system_path = 1; |
|
47 endif |
|
48 |
|
49 ## strip '...:p:...' -> '...:...' |
|
50 lo = 0 ; |
|
51 while (lo != length (path)) # Loop while I can substitute |
|
52 lo = length (path); |
|
53 path = strrep (path, sprintf(":%s:", p), ":"); |
|
54 endwhile |
|
55 |
|
56 ## strip 'p:...' and '...:p' -> '...' |
|
57 if (length (path) > lp+1 && strcmp (path(1:lp+1), sprintf ("%s:", p))) |
|
58 path = path(lp+2:end); |
|
59 endif |
|
60 if (length (path) > lp+1 && strcmp (path(end-lp:end), sprintf (":%s", p))) |
|
61 path = path(1:end-lp-1); |
|
62 endif |
|
63 |
|
64 ## strip 'p:' and ':p' -> ':' |
|
65 if (length (path) == lp+1 && (strcmp (path, sprintf("%s:", p)) || strcmp (path, sprintf (":%s", p)))) |
|
66 path = ':'; |
|
67 endif |
|
68 |
|
69 ## strip 'p' -> '' |
|
70 if (length (path) == lp && strcmp (path, p)) |
|
71 path = ''; |
|
72 endif |
|
73 |
|
74 endfor |
|
75 |
|
76 if (strip_system_path && strcmp (path, ':')) |
|
77 path = ''; |
|
78 endif |
|
79 |
|
80 if (nargout > 0) |
|
81 ret = path; |
|
82 elseif (! strcmp (LOADPATH, path)) |
|
83 LOADPATH = path; |
|
84 endif |
|
85 |
|
86 endfunction |
|
87 |
|
88 %!assert(rmpath(':',''),''); |
|
89 %!assert(rmpath('hello:',''),'hello'); |
|
90 %!assert(rmpath('hello:world',''),'hello:world'); |
|
91 %!assert(rmpath(':hello:world',''),'hello:world'); |
|
92 %!assert(rmpath(':hello:world:',''),'hello:world'); |
|
93 %!assert(rmpath(':hello::world:',''),'hello:world'); |
|
94 |
|
95 %!assert(rmpath('hello','hello'),''); |
|
96 %!assert(rmpath(':hello','hello'),':'); |
|
97 %!assert(rmpath('hello:','hello'),':'); |
|
98 %!assert(rmpath('hello:hello','hello'),''); |
|
99 %!assert(rmpath('hello:hello:hello','hello'),''); |
|
100 %!assert(rmpath('hello:hello:hello:hello','hello'),''); |
|
101 %!assert(rmpath(':hello:hello','hello'),':'); |
|
102 %!assert(rmpath('hello:hello:','hello'),':'); |
|
103 %!assert(rmpath('hello','world'),'hello'); |
|
104 %!assert(rmpath(':hello','','hello'),''); |
|
105 %!assert(rmpath(':hello','hello',''),''); |
|
106 |
|
107 %!assert(rmpath('hello:world','hello','world'),''); |
|
108 %!assert(rmpath('hello:world:','hello','world'),':'); |
|
109 %!assert(rmpath(':hello:world:','hello','world'),':'); |
|
110 |
|
111 %!assert(rmpath('hello:world','','hello','world'),''); |
|
112 %!assert(rmpath('hello:world:','','hello','world'),''); |
|
113 %!assert(rmpath(':hello:world:','','hello','world'),''); |
|
114 |
|
115 %!assert(rmpath('hello:world','hello'),'world'); |
|
116 %!assert(rmpath('hello:world','world'),'hello'); |
|
117 %!assert(rmpath('hello:world:','hello'),'world:'); |
|
118 %!assert(rmpath('hello:world:','world'),'hello:'); |
|
119 %!assert(rmpath(':hello:world:','hello'),':world:'); |
|
120 %!assert(rmpath(':hello:world:','world'),':hello:'); |