Merge branch 'fixes' into main/rendor-staging
[ryzomcore.git] / ryzom / common / src / game_share / utils.cpp
blob9a30152f97559e30da9ccb1de98eca1eff21cbfa
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
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.
8 //
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 //-------------------------------------------------------------------------------------------------
18 // includes
19 //-------------------------------------------------------------------------------------------------
21 #include "stdpch.h"
22 #include "nel/misc/sstring.h"
25 //-------------------------------------------------------------------------------------------------
26 // namespaces
27 //-------------------------------------------------------------------------------------------------
29 using namespace NLMISC;
32 //-----------------------------------------------------------------------------
33 // cleanPath - convert a path to standardised format
34 //-----------------------------------------------------------------------------
36 CSString cleanPath(const CSString& path,bool addTrailingSlash)
38 CSString result;
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)
47 // skip "." entries
48 if (pathComponents[i]==".")
50 pathComponents[i].clear();
51 continue;
54 // deal with ".."
55 if (pathComponents[i]=="..")
57 // run back through our component list looking for an element to remove
58 uint32 j;
59 for (j=i;j--;)
61 if (!pathComponents[j].empty())
62 break;
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();
70 continue;
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];
86 result+= '/';
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);
96 return result;