serverpath.cpp

来自「一个FTP下载的源代码。代码质量非常高」· C++ 代码 · 共 1,077 行 · 第 1/2 页

CPP
1,077
字号
			CString Segment1=*iter1;
			CString Segment2=*iter2;
			Segment1.MakeLower();
			Segment2.MakeLower();
			if (Segment1!=Segment2)
				return FALSE;
		}
		else
			if (*iter1 != *iter2)
				return FALSE;
		iter1++;
		iter2++;
	}
	if (iter2!=path.m_Segments.end())
		return TRUE;
	return FALSE;
}

/*CServerPath::operator CString() const
{
	return GetPath();
}*/

const BOOL CServerPath::IsEmpty() const
{
	return m_bEmpty;
}

/*CServerPath& CServerPath::operator +=(const CString &op)
{
	ASSERT(!IsEmpty());
	CString subdir=op;
	subdir.TrimLeft( _T(" ") );
	subdir.TrimRight( _T(" ") );
	switch(m_nSubServerType)
	{
	case FTP_SERVERTYPE_VMS:
		{
			int pos1=subdir.Find( _T("[") );
			int pos2=subdir.ReverseFind(']');
			if (pos1!=-1)
			{
				if (pos2==-1)
					ASSERT(FALSE);
				if (pos1>=pos2)
					ASSERT(FALSE);
				subdir=subdir.Mid(pos1+1,pos2-pos1-1);
			}
			else if (pos2!=-1)
				ASSERT(FALSE);
			int pos=subdir.Find( _T(".") );
			while(pos!=-1)
			{
				m_Segments.push_back(subdir.Left(pos));
				subdir=subdir.Mid(pos+1);
				pos=subdir.Find( _T(".") );
			}
			if (subdir!="")
				m_Segments.push_back(subdir);		
		}
		break;		
	default:
		subdir.Replace( _T("\\"), _T("/") );
		while(subdir.Replace( _T("//"), _T("/") ));
		subdir.TrimLeft( _T("/") );
		subdir.TrimRight( _T("/") );
		int pos=subdir.Find( _T("/") );
		while(pos!=-1)
		{
			m_Segments.push_back(subdir.Left(pos));
			subdir=subdir.Mid(pos+1);
			pos=subdir.Find( _T("/") );
		}
		if (subdir!="")
			m_Segments.push_back(subdir);		
		break;
	}
	return *this;
}*/

BOOL CServerPath::SetSafePath(CString path)
{
	m_bEmpty=TRUE;
	m_Prefix="";
	m_Segments.clear();

	int pos1=path.Find( _T(" ") );
	int pos2=0;
	if (pos1<=pos2)
		return FALSE;
	m_nServerType=_ttoi(path.Mid(pos2,pos1));
	pos2=pos1+1;
	
	pos1=path.Find(_T(" "), pos2);
	if (pos1<=pos2)
		return FALSE;
	int len=_ttoi(path.Mid(pos2,pos1));
	pos2=pos1+1;

	if (len)
	{
		m_Prefix=path.Mid(pos2,len);
		pos2+=len+1;
	}
	
	while (pos2<path.GetLength())
	{
		pos1=path.Find(_T(" "), pos2);
		if (pos1<=pos2)
			return FALSE;
		int len=_ttoi(path.Mid(pos2,pos1));
			pos2=pos1+1;
		if (len)
		{
			m_Segments.push_back(path.Mid(pos2,len));
			pos2+=len+1;
		}
	}
	m_bEmpty=FALSE;
	return TRUE;
}

CString CServerPath::GetSafePath() const
{
	if (m_bEmpty)
		return _T("");

	CString safepath;
	safepath.Format(_T("%d %d "), m_nServerType, m_Prefix.GetLength());
	if (m_Prefix!="")
		safepath+=m_Prefix+" ";
	tConstIter iter = m_Segments.begin();
	while(iter!=m_Segments.end())
	{
		CString len;
		len.Format(_T("%d "), iter->GetLength());
		safepath+=len;
		safepath+=*iter;
		iter++;
		if (iter!=m_Segments.end())
			safepath+=" ";
	}
	return safepath;
}

CString CServerPath::GetSubdirsOf(const CServerPath &path) const
{
	ASSERT(IsParentOf(path));
	CServerPath subdirs=path;
	CString ret;
	while(IsParentOf(subdirs))
	{
		CString tmp;
		tmp.Format(_T(" %d %s"), subdirs.GetLastSegment().GetLength(),subdirs.GetLastSegment());
		ret+=tmp;
		subdirs=subdirs.GetParent();
	}
	ret.TrimLeft( _T(" ") );
	return ret;
}

BOOL CServerPath::AddSubdirs(CString subdirs)
{
	int pos1;
	int pos2=0;
	while (pos2<subdirs.GetLength())
	{
		pos1=subdirs.Find(_T(" "), pos2);
		if (pos1<=pos2)
			return FALSE;
		int len=_ttoi(subdirs.Mid(pos2,pos1));
			pos2=pos1+1;
		if (len)
		{
			m_Segments.push_back(subdirs.Mid(pos2,len));
			pos2+=len+1;
		}
	}
	return TRUE;
}

BOOL CServerPath::AddSubdir(CString subdir)
{
	subdir.TrimLeft( _T(" ") );
	subdir.TrimRight( _T(" ") );
	if (subdir=="")
		return FALSE;
	m_Segments.push_back(subdir);
	m_bEmpty=FALSE;
	return TRUE;
}

CServerPath::CServerPath(CString subdir, const CServerPath &parent)
{
	*this=parent;
	subdir.TrimLeft( _T(" ") );
	subdir.TrimRight( _T(" ") );

	if ( subdir==_T("") )
	{
		if (IsEmpty())
			ASSERT(FALSE);
		else
			return;
	}

	if (!(m_nServerType&FZ_SERVERTYPE_HIGHMASK))
		m_nServerType=FZ_SERVERTYPE_FTP;
	
	m_bEmpty=FALSE;

	switch (m_nServerType&FZ_SERVERTYPE_HIGHMASK)
	{
	case FZ_SERVERTYPE_FTP:
		switch(m_nServerType&FZ_SERVERTYPE_SUBMASK)
		{
		case FZ_SERVERTYPE_SUB_FTP_VMS:
			{
				int pos1=subdir.Find( _T("[") );
				if (pos1==-1)
				{
					ASSERT( subdir.Right(1)!=_T("]") );
					while ( subdir.Replace( _T(".."), _T(".") ) );
				}
				else
				{
					if (subdir.Right(1)!="]")
						ASSERT(FALSE);
					subdir=subdir.Left(subdir.GetLength()-1);
					if (pos1)
						m_Prefix=subdir.Left(pos1);
					else
						m_Prefix="";
					m_Segments.clear();
					subdir=subdir.Mid(pos1+1);

					pos1=subdir.Find( _T("[") );
					int pos2=subdir.Find( _T("]") );
					if (pos1!=-1 || pos2!=-1)
						ASSERT(FALSE);
				}
				int pos=subdir.Find( _T(".") );
				while(pos!=-1)
				{
					m_Segments.push_back(subdir.Left(pos));
					subdir=subdir.Mid(pos+1);
					pos=subdir.Find( _T(".") );
				}
				if (subdir!="")
					m_Segments.push_back(subdir);
			}
			break;
		default:
			subdir.Replace( _T("\\"), _T("/") );
			while(subdir.Replace( _T("//"), _T("/") ));
			if (subdir[0]=='/')
			{
				m_Segments.clear();
				subdir.TrimLeft( _T("/") );
			}
			subdir.TrimRight( _T("/") );
			int pos=subdir.Find( _T("/") );
			while(pos!=-1)
			{
				m_Segments.push_back(subdir.Left(pos));
				subdir=subdir.Mid(pos+1);
				pos=subdir.Find( _T("/") );
			}
			if (subdir!="")
				m_Segments.push_back(subdir);
			break;
		}
		break;
	case FZ_SERVERTYPE_LOCAL:
		{
			subdir.TrimRight( _T("\\") );
			while (subdir.Replace( _T("\\\\"), _T("\\") ));
			subdir.TrimLeft( _T("\\") );
			int pos=subdir.Find( _T(":") );
		
			if (pos==1) //subdir is absolute path
			{
				m_Segments.clear();
				m_Prefix=subdir.Left(pos+1);
				subdir=subdir.Mid(pos+1);
				subdir.TrimLeft( _T("\\") );
				if (subdir.Find( _T(":") )!=-1)
					ASSERT(FALSE);
			}
			if (pos==-1 || pos==1)
			{
				pos=subdir.Find( _T("\\") );
				while (pos!=-1)
				{
					m_Segments.push_back(subdir.Left(pos));
					subdir=subdir.Mid(pos+1);
					pos=subdir.Find( _T("\\") );
				}
				if ( subdir!=_T("") )
					m_Segments.push_back(subdir);			
			}
			else
				ASSERT(FALSE);
		}
		break;
	default:
		ASSERT(FALSE);
	}	
}


BOOL CServerPath::ChangePath(CString &subdir, BOOL bIsFile /*=FALSE*/)
{
	CServerPath newpath=*this;
	CString dir=subdir;
	if (!(newpath.m_nServerType&FZ_SERVERTYPE_HIGHMASK))
		newpath.m_nServerType=FZ_SERVERTYPE_FTP;
	
	dir.TrimLeft( _T(" ") );
	dir.TrimRight( _T(" ") );

	if ( dir==_T("") )
	{
		if (newpath.IsEmpty() || bIsFile)
			return FALSE;
		else
		{
			*this=newpath;
			return TRUE;
		}
	}

	switch (newpath.m_nServerType&FZ_SERVERTYPE_HIGHMASK)
	{
	case FZ_SERVERTYPE_FTP:
		switch(newpath.m_nServerType&FZ_SERVERTYPE_SUBMASK)
		{
		case FZ_SERVERTYPE_SUB_FTP_VMS:
			{
				int pos1=dir.Find( _T("[") );
				if (pos1==-1)
				{
					int pos2=dir.ReverseFind(']');
					if (pos2!=-1)
						return FALSE;
					if (bIsFile)
					{
						if (newpath.IsEmpty())
							return FALSE;
						subdir=dir;
						*this=newpath;
						return TRUE;
					}
					while ( dir.Replace( _T(".."), _T(".") ) );
				}
				else
				{
					int pos2=dir.ReverseFind(']');
					if (pos2==-1)
						return FALSE;
					if (bIsFile && pos2==(dir.GetLength()-1))
						return FALSE;
					if (!bIsFile && pos2!=(dir.GetLength()-1))
						return FALSE;
					if (pos2<=pos1)
						return FALSE;
					if (bIsFile)
						subdir=dir.Mid(pos2+2);
					dir=dir.Left(pos2);
					if (pos1)
						newpath.m_Prefix=dir.Left(pos1);
					else
						newpath.m_Prefix="";
					newpath.m_Segments.clear();
					dir=dir.Mid(pos1+1);
					pos1=dir.Find( _T("[") );
					pos2=dir.Find( _T("]") );
					if (pos1!=-1 || pos2!=-1)
						return FALSE;
				}
				int pos=dir.Find( _T(".") );
				while(pos!=-1)
				{
					newpath.m_Segments.push_back(dir.Left(pos));
					dir=dir.Mid(pos+1);
					pos=dir.Find( _T(".") );
				}
				if (dir!="")
					newpath.m_Segments.push_back(dir);
			}
			break;
		default:
			dir.Replace( _T("\\"), _T("/") );
			while(dir.Replace( _T("//"), _T("/") ));
			if (dir[0]=='/')
			{
				newpath.m_Segments.clear();
				if (dir!="/")
					dir.TrimLeft( _T("/") );
			}
			else
				if (newpath.IsEmpty())
					return FALSE;
			if (dir.Right(1)==_T("/") && bIsFile)
				return FALSE;
			dir.TrimRight( _T("/") );

			int pos=dir.ReverseFind('/');
			if (bIsFile)
				if (pos==-1)
				{
					subdir=dir;
					newpath.m_bEmpty=FALSE;
					*this=newpath;
					return TRUE;
				}
				else
				{
					subdir=dir.Mid(pos+1);
					dir=dir.Left(pos);
					dir.TrimRight( _T("/") );
				}
			
			pos=dir.Find( _T("/") );
			while(pos!=-1)
			{
				newpath.m_Segments.push_back(dir.Left(pos));
				dir=dir.Mid(pos+1);
				pos=dir.Find( _T("/") );
			}
			if (dir!="")
				newpath.m_Segments.push_back(dir);
			break;
		}
		break;
	case FZ_SERVERTYPE_LOCAL:
		{
			if (dir.Right(1)==_T("\\") && bIsFile)
				return FALSE;
			dir.TrimRight( _T("\\") );
			while (dir.Replace( _T("\\\\"), _T("\\") ));
			if ( dir.Left(1) == _T("\\") )
				newpath.m_Segments.clear();
			else
				if (newpath.IsEmpty())
					return FALSE;
			dir.TrimLeft( _T("\\") );
			if (bIsFile)
			{
				int pos=dir.ReverseFind('\\');
				if (pos==-1)
				{
					if (dir.Find( _T(":") )!=-1)
						return FALSE;
					subdir=dir;
					newpath.m_bEmpty=FALSE;
					*this=newpath;
					return TRUE;
				}
				else
				{
					if (dir.Find( _T(":"), pos+1)!=-1)
						return FALSE;
					subdir=dir.Mid(pos+1);
					dir=dir.Left(pos);
				}
			}
			int pos=dir.Find( _T(":") );
		
			if (pos==1) //dir is absolute path
			{
				newpath.m_Segments.clear();
				newpath.m_Prefix=dir.Left(pos+1);
				dir=dir.Mid(pos+1);
				dir.TrimLeft( _T("\\") );
				if (dir.Find( _T(":") )!=-1)
					return FALSE;
			}
			if (pos==-1 || pos==1)
			{
				pos=dir.Find( _T("\\") );
				while (pos!=-1)
				{
					newpath.m_Segments.push_back(dir.Left(pos));
					dir=dir.Mid(pos+1);
					pos=dir.Find( _T("\\") );
				}
				if (dir!="")
					newpath.m_Segments.push_back(dir);			
			}
			else
				return FALSE;
		}
		break;
	default:
		return FALSE;
	}
	newpath.m_bEmpty=FALSE;
	*this=newpath;
	return TRUE;
}

BOOL CServerPath::SetPath(CString newpath)
{
	return SetPath(newpath, FALSE);
}

const bool CServerPath::MatchNoCase(const CServerPath &op) const
{
	if (this==&op)
		return TRUE;

	if (m_bEmpty!=op.m_bEmpty)
		return FALSE;
	if (m_Prefix!=op.m_Prefix)
		return FALSE;
	if (m_nServerType!=op.m_nServerType)
		return FALSE;
	tConstIter iter1=m_Segments.begin();
	tConstIter iter2=op.m_Segments.begin();
	while (iter1!=m_Segments.end())
	{
		if (iter2==op.m_Segments.end())
			return FALSE;
		CString Segment1=*iter1;
		CString Segment2=*iter2;
		Segment1.MakeLower();
		Segment2.MakeLower();
		if (Segment1!=Segment2)
			return FALSE;
		iter1++;
		iter2++;
	}
	if (iter2!=op.m_Segments.end())
		return FALSE;
	return TRUE;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?