1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 //-------------------------------------------------------------------------------------------------
19 //-------------------------------------------------------------------------------------------------
22 #include "nel/misc/sstring.h"
25 //-------------------------------------------------------------------------------------------------
27 //-------------------------------------------------------------------------------------------------
29 using namespace NLMISC
;
32 //-----------------------------------------------------------------------------
33 // cleanPath - convert a path to standardised format
34 //-----------------------------------------------------------------------------
36 CSString
cleanPath(const CSString
& path
,bool addTrailingSlash
)
40 // split the path up into its component elements
41 CVectorSString pathComponents
;
42 path
.unquoteIfQuoted().splitByOneOfSeparators("/\\",pathComponents
,false,false,true,false,true);
44 // iterate over path components collapsing '.' and '..' entries
45 for (uint32 i
=0;i
<pathComponents
.size();++i
)
48 if (pathComponents
[i
]==".")
50 pathComponents
[i
].clear();
55 if (pathComponents
[i
]=="..")
57 // run back through our component list looking for an element to remove
61 if (!pathComponents
[j
].empty())
64 // if we found an element then remove it and the '..' as well
65 if (j
!=std::numeric_limits
<uint32
>::max())
67 pathComponents
[j
].clear();
68 pathComponents
[i
].clear();
74 // treat the special case where original path started with a '/' or '//'
75 if (path
.left(1)=="/" || path
.left(1)=="\\")
77 result
= (path
.left(2).right(1)=="/" || path
.left(2).right(1)=="\\")? "//": "/";
80 // concatenate the path bits
81 for (uint32 i
=0;i
<pathComponents
.size();++i
)
83 if (!pathComponents
[i
].empty())
85 result
+= pathComponents
[i
];
90 // if we're not supposed to have a trailing slash then get rid of the one that's added by default
91 if (addTrailingSlash
==false && path
.right(1)!='/' && path
.right(1)!='\\')
93 result
.resize(result
.size()-1);