serverpath.cpp

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

CPP
1,077
字号
// FileZilla - a Windows ftp client

// Copyright (C) 2002 - Tim Kosse <tim.kosse@gmx.de>

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

// ServerPath.cpp: Implementierung der Klasse CServerPath.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ServerPath.h"
#include "structures.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Konstruktion/Destruktion
//////////////////////////////////////////////////////////////////////

CServerPath::CServerPath()
{
	m_nServerType=0;
	m_bEmpty=TRUE;
}

CServerPath::CServerPath(int nServerType)
{
	m_nServerType=nServerType;
	m_bEmpty=TRUE;
}

CServerPath::CServerPath(CString path)
{
	path.TrimLeft( _T(" ") );
	path.TrimRight( _T(" ") );
	if (path!="")
		m_bEmpty=FALSE;
	else
		m_bEmpty=TRUE;
	m_nServerType=FZ_SERVERTYPE_FTP;
	int pos1=path.Find( _T(":[") );
	if (pos1!=-1 && path.Right(1)=="]" && pos1!=(path.GetLength()-1))
		m_nServerType|=FZ_SERVERTYPE_SUB_FTP_VMS;
	switch (m_nServerType&FZ_SERVERTYPE_SUBMASK)
	{
	case FZ_SERVERTYPE_SUB_FTP_VMS:
		{
			if (pos1)
				m_Prefix=path.Left(pos1+1);
			path=path.Mid(pos1+2);
			int pos=path.Find( _T(".") );
			path.TrimRight( _T("]") );
			while(pos!=-1)
			{
				m_Segments.push_back(path.Left(pos));
				path=path.Mid(pos+1);
				pos=path.Find( _T(".") );
			}
			if (path!="")
				m_Segments.push_back(path);
		}
		break;
	default:
		path.Replace( _T("\\"), _T("/") );
		while(path.Replace( _T("//"), _T("/") ));
		path.TrimLeft( _T("/") );
		path.TrimRight( _T("/") );
		int pos=path.Find( _T("/") );
		while(pos!=-1)
		{
			m_Segments.push_back(path.Left(pos));
			path=path.Mid(pos+1);
			pos=path.Find( _T("/") );
		}
		if (path!="")
			m_Segments.push_back(path);
		break;
	}	
}

CServerPath::CServerPath(CString path, int nServerType)
{
	path.TrimLeft( _T(" ") );
	path.TrimRight( _T(" ") );
	m_bEmpty=FALSE;
	m_nServerType=nServerType;
	if (path=="")
	{
		m_bEmpty=TRUE;
		return;
	}

	switch (m_nServerType&FZ_SERVERTYPE_HIGHMASK)
	{
	case FZ_SERVERTYPE_FTP:
		switch(m_nServerType&FZ_SERVERTYPE_SUBMASK)
		{
		case FZ_SERVERTYPE_SUB_FTP_VMS:
			{
				int pos1=path.Find( _T("[") );
				if (pos1==-1)
					ASSERT(FALSE);
				if (path.Right(1)!="]")
					ASSERT(FALSE);
				path.TrimRight( _T("]") );
				if (pos1)
					m_Prefix=path.Left(pos1);
				path=path.Mid(pos1+1);
				int pos=path.Find( _T(".") );
				while(pos!=-1)
				{
					m_Segments.push_back(path.Left(pos));
					path=path.Mid(pos+1);
					pos=path.Find( _T(".") );
				}
				if (path!="")
					m_Segments.push_back(path);
			}
			break;
		default:
			path.Replace( _T("\\"), _T("/") );
			while(path.Replace( _T("//"), _T("/") ));
			path.TrimLeft( _T("/") );
			path.TrimRight( _T("/") );
			int pos=path.Find( _T("/") );
			while(pos!=-1)
			{
				m_Segments.push_back(path.Left(pos));
				path=path.Mid(pos+1);
				pos=path.Find( _T("/") );
			}
			if (path!="")
				m_Segments.push_back(path);
			break;
		}
		break;
	case FZ_SERVERTYPE_LOCAL:
		{
			path.TrimRight( _T("\\") );
			while (path.Replace( _T("\\\\"), _T("\\") ));
			int pos=path.Find( _T("\\") );
			if (pos==-1)
			{
				m_Prefix=path;
				return;
			}
			ASSERT(pos==2);
			m_Prefix=path.Left(pos);
			path=path.Mid(pos+1);
			pos=path.Find( _T("\\") );
			while (pos!=-1)
			{
				m_Segments.push_back(path.Left(pos));
				path=path.Mid(pos+1);
				pos=path.Find( _T("\\") );
			}
			if (path!="")
				m_Segments.push_back(path);			
		}
		break;
	default:
		ASSERT(FALSE);
	}	
}

CServerPath::CServerPath(const CServerPath &path)
{
	m_nServerType=path.m_nServerType;
	m_Prefix=path.m_Prefix;
	m_bEmpty=path.m_bEmpty;
	m_Segments=path.m_Segments;
}

CServerPath::~CServerPath()
{

}

void CServerPath::SetServer(const t_server &server)
{
	m_nServerType=server.nServerType;
}

BOOL CServerPath::SetPath(CString &newpath, BOOL bIsFile /*=FALSE*/)
{
	CString file;
	CString path=newpath;

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

const CString CServerPath::GetPath() const
{
	if (m_bEmpty)
		return "";
	CString path;
	tConstIter iter;
	switch (m_nServerType&FZ_SERVERTYPE_HIGHMASK)
	{
	case FZ_SERVERTYPE_FTP:
		switch (m_nServerType&FZ_SERVERTYPE_SUBMASK)
		{
		case FZ_SERVERTYPE_SUB_FTP_VMS:
			path=m_Prefix+"[";
			for (iter=m_Segments.begin(); iter!=m_Segments.end(); iter++)
				path+=*iter + _T(".");
			path.TrimRight( _T(".") );
			path+="]";
			break;
		default:
			path="/";
			for (iter=m_Segments.begin(); iter!=m_Segments.end(); iter++)
				path+=*iter + _T("/");
			break;
		}
		break;
	case FZ_SERVERTYPE_LOCAL:
		path=m_Prefix;
		if (!m_Segments.empty())
			path+="\\";
		for (iter=m_Segments.begin(); iter!=m_Segments.end(); iter++)
			path+=*iter + _T("\\");
				
		break;
	default:
		ASSERT(FALSE);
	}
	return path;
}

CServerPath& CServerPath::operator=(const CServerPath &op)
{
	if (this==&op)
		return *this;
	m_Segments.clear();
	
	m_nServerType=op.m_nServerType;
	
	m_Prefix=op.m_Prefix;
	m_bEmpty=op.m_bEmpty;

	m_Segments=op.m_Segments;

	return *this;
}

const BOOL CServerPath::operator==(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;
		if (*iter1 != *iter2)
			return FALSE;
		iter1++;
		iter2++;
	}
	if (iter2!=op.m_Segments.end())
		return FALSE;
	return TRUE;
}

const BOOL operator==(const CServerPath &a, const CString &b)
{
	CServerPath path(b);
	return a==path;
}

const BOOL CServerPath::operator!=(const CServerPath &op) const
{
	if (this==&op)
		return FALSE;

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

CString CServerPath::GetLastSegment() const
{
	if (m_Segments.empty())
		return "";
	else
		return m_Segments.back();
}

CServerPath CServerPath::GetParent() const
{
	ASSERT(HasParent());
	CServerPath path;
	path=*this;
	path.m_Segments.pop_back();	
	return path;
}

BOOL CServerPath::HasParent() const
{
	if (!m_Segments.empty())
		return TRUE;
	else
		return FALSE;
}

BOOL CServerPath::IsSubdirOf(const CServerPath &path, BOOL bCompareNoCase /*=FALSE*/) const
{
	if (this==&path)
		return FALSE;

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

BOOL CServerPath::IsParentOf(const CServerPath &path, BOOL bCompareNoCase /*=FALSE*/) const
{
	if (this==&path)
		return FALSE;

	if (m_bEmpty || path.m_bEmpty)
		return FALSE;
	if (m_Prefix!=path.m_Prefix)
		return FALSE;
	if (m_nServerType!=path.m_nServerType)
		return FALSE;
	
	tConstIter iter1=m_Segments.begin();
	tConstIter iter2=path.m_Segments.begin();
	while (iter1!=m_Segments.end())
	{
		if (iter2==path.m_Segments.end())
			return FALSE;
		if (bCompareNoCase)
		{

⌨️ 快捷键说明

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