Mercurial > hg > octave-lyh
comparison scripts/path/addpath.m @ 5732:17d87fbd7010
[project @ 2006-04-04 17:50:46 by jwe]
author | jwe |
---|---|
date | Tue, 04 Apr 2006 17:50:46 +0000 |
parents | |
children | 6b345b4961ca |
comparison
equal
deleted
inserted
replaced
5731:c7d5a534afa5 | 5732:17d87fbd7010 |
---|---|
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 -*- | |
20 ## @deftypefn {Function File} {} addpath(dir1, ...) | |
21 ## Prepends @code{dir1}, @code{...} to the current @code{LOADPATH}. | |
22 ## If the directory is already in the path, it will place it where you | |
23 ## specify in the path (defaulting to prepending it). | |
24 ## | |
25 ## @example | |
26 ## addpath(dir1,'-end',dir2,'-begin',dir3,'-END',dir4,'-BEGIN',dir5) | |
27 ## @result{} Prepends dir1, dir3 and dir5 and appends dir2 and dir4. | |
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 ## | |
33 ## BUG: This function can't add directories called @code{-end} or | |
34 ## @code{-begin} (case insensitively). | |
35 ## @end deftypefn | |
36 | |
37 ## Author: Etienne Grossmann <etienne@cs.uky.edu> | |
38 ## Modified-By: Bill Denney <bill@givebillmoney.com> | |
39 ## Last modified: June 2005 | |
40 | |
41 ##PKGADD: mark_as_command addpath | |
42 | |
43 function ret = addpath (varargin) | |
44 | |
45 if (nargout > 0) | |
46 path = varargin{1}; | |
47 varargin = varargin(2:end); | |
48 else | |
49 path = LOADPATH; | |
50 endif | |
51 | |
52 dir = ''; | |
53 if (length (varargin) > 0) | |
54 append = 0; | |
55 switch varargin{end} | |
56 case { 0, '0', '-begin', '-BEGIN' } | |
57 varargin = varargin(1:end-1); | |
58 case { 1, '1', '-end', '-END' } | |
59 varargin = varargin(1:end-1); | |
60 append = 1; | |
61 endswitch | |
62 | |
63 ## Avoid duplicates by stripping pre-existing entries | |
64 path = rmpath (path, varargin{:}); | |
65 | |
66 ## Check if the directories are valid | |
67 for arg = 1:length (varargin) | |
68 p = varargin{arg}; | |
69 if (nargout == 0 && ! isempty (p)) | |
70 [s, err, m] = stat (p); | |
71 if (err ~= 0) | |
72 warning ("addpath %s : %s\n", p, m); | |
73 continue; | |
74 elseif (index (s.modestr, "d") != 1) | |
75 warning ("addpath %s : not a directory (mode=%s)\n", p, s.modestr); | |
76 continue; | |
77 elseif ! ((s.modestr(8) == 'r') || ... | |
78 ((getgid == s.gid) && (s.modestr(5) == 'r')) || ... | |
79 ((getuid == s.uid) && (s.modestr(2) == 'r'))) | |
80 warning ("addpath %s : not readable (mode=%s)\n", p, s.modestr); | |
81 continue; | |
82 endif | |
83 endif | |
84 dir = sprintf ("%s:%s", dir, p); | |
85 endfor | |
86 | |
87 ## Add the directories to the current path | |
88 if (! isempty (dir)) | |
89 dir = dir(2:end); | |
90 if (isempty (path) && ! isempty (dir)) | |
91 path = dir; | |
92 else | |
93 if strcmp (path, ':'), path = ''; end | |
94 if append | |
95 path = sprintf ("%s:%s", path, dir); | |
96 else | |
97 path = sprintf ("%s:%s", dir, path); | |
98 endif | |
99 endif | |
100 endif | |
101 endif | |
102 | |
103 if nargout | |
104 ret = path; | |
105 else | |
106 LOADPATH = path; | |
107 endif | |
108 | |
109 endfunction | |
110 | |
111 %!assert(addpath('','hello'),'hello'); | |
112 %!assert(addpath('','hello','world'),'hello:world') | |
113 %!assert(addpath(':','hello'),'hello:'); | |
114 %!assert(addpath(':','hello','-end'),':hello'); | |
115 %!assert(addpath('hello','hello'),'hello'); | |
116 %!assert(addpath('hello','world'),'world:hello') | |
117 %!assert(addpath('hello','world','-end'),'hello:world') | |
118 %!assert(addpath('hello:','world','-end'),'hello::world') | |
119 %!assert(addpath('hello:','hello','world','-end'),':hello:world') | |
120 | |
121 %!assert(addpath('',''),':') | |
122 %!assert(addpath(':',''),':') | |
123 %!assert(addpath('hello',''),':hello') | |
124 %!assert(addpath('hello:world',''),':hello:world') | |
125 %!assert(addpath('hello:world:',''),':hello:world') | |
126 %!assert(addpath('hello::world',''),':hello:world') |