Click here to Skip to main content
15,892,059 members
Articles / Desktop Programming / WTL

CSplitURL - split a URL into component parts

Rate me:
Please Sign up or sign in to vote.
4.17/5 (9 votes)
10 Nov 2002 116.5K   1.8K   38  
Wrapper class for the WinInet InternetCrackUrl function
// Implementation of the CURLComponents and CSplitURL classes.

#pragma once
#pragma comment(lib, "wininet.lib")

#include "wininet.h"

// Class to wrap the Win32 URL_COMPONENTS structure
class CURLComponents : public URL_COMPONENTS
{
public:	
	CURLComponents(void)
	{
		memset(this, 0, sizeof(URL_COMPONENTS));
		dwStructSize = sizeof(URL_COMPONENTS);
	}
};

// Class used to split a URL into component parts.
// Note: Uses WININET InternetCrackUrl function.
class CSplitURL
{
private:
	CString m_strScheme;
	INTERNET_SCHEME m_nScheme;
	CString m_strHostName;
	INTERNET_PORT m_nPort;
	CString m_strUserName;
	CString m_strPassword;
	CString m_strURLPath;
	CString m_strExtraInfo;
public:	
	CSplitURL(void)
		: m_nScheme(INTERNET_SCHEME_DEFAULT)
		, m_nPort(0)
	{
	}
	
	CSplitURL(LPCTSTR lpsz)
		: m_nScheme(INTERNET_SCHEME_DEFAULT)
		, m_nPort(0)
	{
		Split(lpsz);
	}

	~CSplitURL(void)
	{
	}

	// Split a URL into component parts
	bool Split(LPCTSTR lpsz)
	{
		// Be defensive
		ATLASSERT(lpsz != NULL && *lpsz != '\0');
		// Get the URL length
		DWORD dwLength = _tcslen(lpsz);

		CURLComponents url;		
		// Fill structure
		url.lpszScheme = m_strScheme.GetBuffer(dwLength);
		url.dwSchemeLength = dwLength;
		url.lpszHostName = m_strHostName.GetBuffer(dwLength);
		url.dwHostNameLength = dwLength;
		url.lpszUserName = m_strUserName.GetBuffer(dwLength);
		url.dwUserNameLength = dwLength;
		url.lpszPassword = m_strPassword.GetBuffer(dwLength);
		url.dwPasswordLength = dwLength;
		url.lpszUrlPath = m_strURLPath.GetBuffer(dwLength);
		url.dwUrlPathLength = dwLength;
		url.lpszExtraInfo = m_strExtraInfo.GetBuffer(dwLength);
		url.dwExtraInfoLength = dwLength;
		// Split
		bool bRet = InternetCrackUrl(lpsz, 0, 0, &url) != FALSE;
		// Release buffers
		m_strScheme.ReleaseBuffer();
		m_strHostName.ReleaseBuffer();
		m_strUserName.ReleaseBuffer();
		m_strPassword.ReleaseBuffer();
		m_strURLPath.ReleaseBuffer();
		m_strExtraInfo.ReleaseBuffer();
		// Get the scheme/port
		m_nScheme = url.nScheme;
		m_nPort = url.nPort;
		// Done
		return bRet;
	}

	// Get the scheme number
	inline INTERNET_SCHEME GetScheme(void) const throw() { return m_nScheme; }
	// Get the port number
	inline INTERNET_PORT GetPort(void) const throw() { return m_nPort; }
	// Get the scheme name
	inline LPCTSTR GetSchemeName(void) const throw() { return m_strScheme; }
	// Get the host name
	inline LPCTSTR GetHostName(void) const throw() { return m_strHostName; }
	// Get the user name
	inline LPCTSTR GetUserName(void) const throw() { return m_strUserName; }
	// Get the password
	inline LPCTSTR GetPassword(void) const throw() { return m_strPassword; }
	// Get the URL path
	inline LPCTSTR GetURLPath(void) const throw() { return m_strURLPath; }
	// Get the extra info
	inline LPCTSTR GetExtraInfo(void) const throw() { return m_strExtraInfo; }
};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here



Comments and Discussions