Example #1
0
string ParsePath(string curDir, string path)
{
	list<string>	pathDirs, curDirs;
	string		ret("/");
	
	if(path.BeginsWith("/"))	// absolute path
		curDir = "/";
	
	path.Tokenize('/', pathDirs);	// split up the path
	curDir.Tokenize('/', curDirs);	// split up the current directory
	
	// go through and remove all the .. & . stuff
	for(list<string>::iterator cur = pathDirs.begin(); cur != pathDirs.end(); ++cur)
	{
		if(*cur == "..")
		{
			if(curDirs.size() != 0)
				curDirs.erase(--curDirs.end());	// remove the last item
		}
		
		else if(*cur == ".")		// move on
			continue;
		
		else	// just copy this dir on
			curDirs.push_back(*cur);
	}
	
	// put the string back together
	for(list<string>::iterator it = curDirs.begin(); it != curDirs.end(); ++it)
	{
		ret += *it;
		ret += "/";
	}
	
	ret.erase(--ret.end());	// no trailing /
	
	if(ret.size() == 0)
		ret = "/";
	
	return ret;
}