1+ function fpath = abspath(fpath )
2+ % ABSPATH Converts path to an absolute path
3+ %
4+ % Converts a relative path from the current working directory to an
5+ % absolute path by glueing the pwd and the relative path together and
6+ % eliminating relative references like '.' and '..'. If the provided path
7+ % is already absolute, only the references like '.' and '..' are removed.
8+ %
9+ % Syntax:
10+ % fpath = abspath(fpath)
11+ %
12+ % Input:
13+ % fpath = path to be converted
14+ %
15+ % Output:
16+ % fpath = absolute path
17+ %
18+ % Example
19+ % fpath = abspath(fpath)
20+ %
21+ % See also
22+
23+ %% Copyright notice
24+ % --------------------------------------------------------------------
25+ % Copyright (C) 2010 Deltares
26+ % Bas Hoonhout
27+ %
28+ 29+ %
30+ % Rotterdamseweg 185
31+ % 2629HD Delft
32+ %
33+ % This library is free software: you can redistribute it and/or
34+ % modify it under the terms of the GNU Lesser General Public
35+ % License as published by the Free Software Foundation, either
36+ % version 2.1 of the License, or (at your option) any later version.
37+ %
38+ % This library is distributed in the hope that it will be useful,
39+ % but WITHOUT ANY WARRANTY; without even the implied warranty of
40+ % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41+ % Lesser General Public License for more details.
42+ %
43+ % You should have received a copy of the GNU Lesser General Public
44+ % License along with this library. If not, see <http://www.gnu.org/licenses/>.
45+ % --------------------------------------------------------------------
46+
47+ % This tool is part of <a href="http://OpenEarth.nl">OpenEarthTools</a>.
48+ % OpenEarthTools is an online collaboration to share and manage data and
49+ % programming tools in an open source, version controlled environment.
50+ % Sign up to recieve regular updates of this function, and to contribute
51+ % your own tools.
52+
53+ %% Version <http://svnbook.red-bean.com/en/1.5/svn.advanced.props.special.keywords.html>
54+ % Created: 24 Nov 2010
55+ % Created with Matlab version: 7.9.0.529 (R2009b)
56+
57+ % $Id$
58+ % $Date$
59+ % $Author$
60+ % $Revision$
61+ % $HeadURL$
62+ % $Keywords: $
63+
64+ %% convert to absolute path
65+ % make sure fileseperators are right
66+ fpath = fullfile(fpath , ' ' );
67+
68+ % check if path is relative
69+ isRelative = false ;
70+ if ispc()
71+ if length(fpath ) < 2 || ...
72+ (fpath(2 ) ~= ' :' && ~strcmpi(repmat(filesep ,1 ,2 ), fpath(1 : 2 )))
73+ isRelative = true ;
74+ end
75+ elseif isunix()
76+ if ~any(strcmp(fpath(1 ), {filesep ' ~' }))
77+ isRelative = true ;
78+ end
79+ else
80+ error(' Unsupported operating system' );
81+ end
82+
83+ if isRelative
84+ fpath = fullfile(pwd , fpath );
85+ end
86+
87+ if ispc()
88+ root = fpath(1 : 2 );
89+ fpath = fpath(3 : end );
90+ elseif isunix()
91+ root = fpath(1 );
92+ fpath = fpath(2 : end );
93+ end
94+
95+ p = regexp(fpath , filesep , ' split' );
96+
97+ % remove '.' elements
98+ p = p(~strcmp(p , ' .' ));
99+
100+ % replace relative references
101+ i = 1 ;
102+ while i <= length(p )
103+ if strcmp(p{i }, ' ..' )
104+ p(i - 1 : i ) = [];
105+ i = i - 2 ;
106+ end
107+ i = i + 1 ;
108+ end
109+
110+ % glue path together
111+ fpath = fullfile(root , p{: }, ' ' );
112+
113+ % help unix users
114+ if isunix && ...
115+ (isempty(fpath ) || ~strcmp(fpath(1 ), ' ~' ))
116+ fpath = [filesep fpath ];
117+ end
0 commit comments