Index: addpath.m =================================================================== RCS file: /cvsroot/octave/octave-forge/main/path/addpath.m,v retrieving revision 1.6 diff -u -r1.6 addpath.m --- addpath.m 25 May 2005 03:43:41 -0000 1.6 +++ addpath.m 8 Jun 2005 03:11:12 -0000 @@ -1,4 +1,4 @@ -## Copyright (C) 2000 Etienne Grossmann +## Copyright (C) 2000 Etienne Grossmann, 2005 Bill Denney ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by @@ -16,8 +16,9 @@ ## -*- texinfo -*- ## @deftypefn {Function File} {} addpath(dir1, ...) -## ## Prepends @code{dir1}, @code{...} to the current @code{LOADPATH}. +## If the directory is already in the path, it will place it where you +## specify in the path (defaulting to prepending it). ## ## @example ## addpath(dir1,'-end',dir2,'-begin',dir3,'-END',dir4,'-BEGIN',dir5) @@ -27,43 +28,87 @@ ## An error will be returned if the string is not a directory, the ## directory doesn't exist or you don't have read access to it. ## -## BUG : Can't add directories called @code{-end} or @code{-begin} (case -## insensitively) -## +## BUG: This function can't add directories called @code{-end} or +## @code{-begin} (case insensitively). ## @end deftypefn ## Author: Etienne Grossmann ## Modified-By: Bill Denney -## Last modified: March 2005 +## Last modified: June 2005 function addpath(varargin) app = 0 ; # Append? Default is 'no'. for arg=1:length(varargin) - p = nth (varargin, arg) ; - if strcmpi(p,"-end"), - app = 1 ; - elseif strcmpi(p,"-begin"), - app = 0 ; + p = nth (varargin, arg); + if strcmpi(p,"-end") + app = 1; + elseif strcmpi(p,"-begin") + app = 0; else - pp = p ; - ## Not needed - ## while rindex(pp,"/") == size(pp,2), pp = pp(1:size(pp,2)-1) ; end - [s,err,m] = stat(pp) ; # Check for existence - if err, + pp = p; + [s,err,m] = stat(pp); # Check for existence + if (err ~= 0) error("addpath %s : %s\n",pp,m); - elseif index(s.modestr,"d")!=1, + elseif (index(s.modestr,"d") != 1) error("addpath %s : not a directory (mode=%s)\n",pp, s.modestr); elseif !((s.modestr(8) == 'r') || ... ((getgid == s.gid) && (s.modestr(5) == 'r')) || ... ((getuid == s.uid) && (s.modestr(2) == 'r'))) error("addpath %s : not readable (mode=%s)\n", pp,s.modestr); - elseif ! app, + elseif (~ app) + LOADPATH = uniquepath(LOADPATH, p); LOADPATH = [p,':',LOADPATH] ; else + LOADPATH = uniquepath(LOADPATH, p); LOADPATH = [LOADPATH,':',p] ; end end end - + + + function [pathstr] = uniquepath(pathstr, varargin) + + ## go through the pathstr and remove any duplicates + + paths = split(pathstr, ":"); + for i = 1:size(paths,1) + pathscell{i} = deblank(paths(i,:)); + end + + if (~ isempty(varargin)) + i = 0; + else + i = 1; + end + + while (i < length(pathscell) - 1) + j = i + 1; + while (j <= length(pathscell)) + if (i == 0) + thisstr = varargin{1}; + else + thisstr = pathscell{i}; + end + + if (strcmpi(thisstr, pathscell{j})) + if (j == 1) + pathscell = pathscell(2:length(pathscell)); + elseif (j == length(pathscell)) + pathscell = pathscell(1:j-1); + else + pathscell = [pathscell(1:j-1) pathscell(j+1:length(pathscell))]; + end + else + j = j + 1; + end + end + i = i + 1; + end + + tmploadpath = pathscell{1}; + for i = 2:length(pathscell) + tmploadpath = [tmploadpath ":" pathscell{i}]; + end + pathstr = tmploadpath; \ No newline at end of file