uri.cpp
来自「A*算法 A*算法 A*算法 A*算法A*算法A*算法」· C++ 代码 · 共 1,303 行 · 第 1/3 页
CPP
1,303 行
/////////////////////////////////////////////////////////////////////////////
// Name: uri.cpp
// Purpose: Implementation of a uri parser
// Author: Ryan Norton
// Created: 10/26/04
// RCS-ID: $Id: uri.cpp,v 1.26.2.1 2006/02/09 03:12:27 VZ Exp $
// Copyright: (c) 2004 Ryan Norton
// Licence: wxWindows
/////////////////////////////////////////////////////////////////////////////
// ===========================================================================
// declarations
// ===========================================================================
// ---------------------------------------------------------------------------
// headers
// ---------------------------------------------------------------------------
#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
#pragma implementation "uri.h"
#endif
// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#include "wx/uri.h"
// ---------------------------------------------------------------------------
// definitions
// ---------------------------------------------------------------------------
IMPLEMENT_CLASS(wxURI, wxObject);
// ===========================================================================
// implementation
// ===========================================================================
// ---------------------------------------------------------------------------
// utilities
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
//
// wxURI
//
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// Constructors
// ---------------------------------------------------------------------------
wxURI::wxURI() : m_hostType(wxURI_REGNAME), m_fields(0)
{
}
wxURI::wxURI(const wxString& uri) : m_hostType(wxURI_REGNAME), m_fields(0)
{
Create(uri);
}
wxURI::wxURI(const wxURI& uri) : wxObject(), m_hostType(wxURI_REGNAME), m_fields(0)
{
Assign(uri);
}
// ---------------------------------------------------------------------------
// Destructor and cleanup
// ---------------------------------------------------------------------------
wxURI::~wxURI()
{
Clear();
}
void wxURI::Clear()
{
m_scheme = m_userinfo = m_server = m_port = m_path =
m_query = m_fragment = wxEmptyString;
m_hostType = wxURI_REGNAME;
m_fields = 0;
}
// ---------------------------------------------------------------------------
// Create
//
// This creates the URI - all we do here is call the main parsing method
// ---------------------------------------------------------------------------
const wxChar* wxURI::Create(const wxString& uri)
{
if (m_fields)
Clear();
return Parse(uri);
}
// ---------------------------------------------------------------------------
// Escape Methods
//
// TranslateEscape unencodes a 3 character URL escape sequence
//
// Escape encodes an invalid URI character into a 3 character sequence
//
// IsEscape determines if the input string contains an escape sequence,
// if it does, then it moves the input string past the escape sequence
//
// Unescape unencodes all 3 character URL escape sequences in a wxString
// ---------------------------------------------------------------------------
wxChar wxURI::TranslateEscape(const wxChar* s)
{
wxASSERT_MSG( IsHex(s[0]) && IsHex(s[1]), wxT("Invalid escape sequence!"));
return (wxChar)( CharToHex(s[0]) << 4 ) | CharToHex(s[1]);
}
wxString wxURI::Unescape(const wxString& uri)
{
wxString new_uri;
for(size_t i = 0; i < uri.length(); ++i)
{
if (uri[i] == wxT('%'))
{
new_uri += wxURI::TranslateEscape( &(uri.c_str()[i+1]) );
i += 2;
}
else
new_uri += uri[i];
}
return new_uri;
}
void wxURI::Escape(wxString& s, const wxChar& c)
{
const wxChar* hdig = wxT("0123456789abcdef");
s += wxT('%');
s += hdig[(c >> 4) & 15];
s += hdig[c & 15];
}
bool wxURI::IsEscape(const wxChar*& uri)
{
// pct-encoded = "%" HEXDIG HEXDIG
if(*uri == wxT('%') && IsHex(*(uri+1)) && IsHex(*(uri+2)))
return true;
else
return false;
}
// ---------------------------------------------------------------------------
// GetUser
// GetPassword
//
// Gets the username and password via the old URL method.
// ---------------------------------------------------------------------------
wxString wxURI::GetUser() const
{
size_t dwPasswordPos = m_userinfo.find(':');
if (dwPasswordPos == wxString::npos)
dwPasswordPos = 0;
return m_userinfo(0, dwPasswordPos);
}
wxString wxURI::GetPassword() const
{
size_t dwPasswordPos = m_userinfo.find(':');
if (dwPasswordPos == wxString::npos)
return wxT("");
else
return m_userinfo(dwPasswordPos+1, m_userinfo.length() + 1);
}
// ---------------------------------------------------------------------------
// BuildURI
//
// BuildURI() builds the entire URI into a useable
// representation, including proper identification characters such as slashes
//
// BuildUnescapedURI() does the same thing as BuildURI(), only it unescapes
// the components that accept escape sequences
// ---------------------------------------------------------------------------
wxString wxURI::BuildURI() const
{
wxString ret;
if (HasScheme())
ret = ret + m_scheme + wxT(":");
if (HasServer())
{
ret += wxT("//");
if (HasUserInfo())
ret = ret + m_userinfo + wxT("@");
ret += m_server;
if (HasPort())
ret = ret + wxT(":") + m_port;
}
ret += m_path;
if (HasQuery())
ret = ret + wxT("?") + m_query;
if (HasFragment())
ret = ret + wxT("#") + m_fragment;
return ret;
}
wxString wxURI::BuildUnescapedURI() const
{
wxString ret;
if (HasScheme())
ret = ret + m_scheme + wxT(":");
if (HasServer())
{
ret += wxT("//");
if (HasUserInfo())
ret = ret + wxURI::Unescape(m_userinfo) + wxT("@");
if (m_hostType == wxURI_REGNAME)
ret += wxURI::Unescape(m_server);
else
ret += m_server;
if (HasPort())
ret = ret + wxT(":") + m_port;
}
ret += wxURI::Unescape(m_path);
if (HasQuery())
ret = ret + wxT("?") + wxURI::Unescape(m_query);
if (HasFragment())
ret = ret + wxT("#") + wxURI::Unescape(m_fragment);
return ret;
}
// ---------------------------------------------------------------------------
// Assignment
// ---------------------------------------------------------------------------
wxURI& wxURI::Assign(const wxURI& uri)
{
//assign fields
m_fields = uri.m_fields;
//ref over components
m_scheme = uri.m_scheme;
m_userinfo = uri.m_userinfo;
m_server = uri.m_server;
m_hostType = uri.m_hostType;
m_port = uri.m_port;
m_path = uri.m_path;
m_query = uri.m_query;
m_fragment = uri.m_fragment;
return *this;
}
wxURI& wxURI::operator = (const wxURI& uri)
{
return Assign(uri);
}
wxURI& wxURI::operator = (const wxString& string)
{
Create(string);
return *this;
}
// ---------------------------------------------------------------------------
// Comparison
// ---------------------------------------------------------------------------
bool wxURI::operator == (const wxURI& uri) const
{
if (HasScheme())
{
if(m_scheme != uri.m_scheme)
return false;
}
else if (uri.HasScheme())
return false;
if (HasServer())
{
if (HasUserInfo())
{
if (m_userinfo != uri.m_userinfo)
return false;
}
else if (uri.HasUserInfo())
return false;
if (m_server != uri.m_server ||
m_hostType != uri.m_hostType)
return false;
if (HasPort())
{
if(m_port != uri.m_port)
return false;
}
else if (uri.HasPort())
return false;
}
else if (uri.HasServer())
return false;
if (HasPath())
{
if(m_path != uri.m_path)
return false;
}
else if (uri.HasPath())
return false;
if (HasQuery())
{
if (m_query != uri.m_query)
return false;
}
else if (uri.HasQuery())
return false;
if (HasFragment())
{
if (m_fragment != uri.m_fragment)
return false;
}
else if (uri.HasFragment())
return false;
return true;
}
// ---------------------------------------------------------------------------
// IsReference
//
// if there is no authority or scheme, it is a reference
// ---------------------------------------------------------------------------
bool wxURI::IsReference() const
{ return !HasScheme() || !HasServer(); }
// ---------------------------------------------------------------------------
// Parse
//
// Master URI parsing method. Just calls the individual parsing methods
//
// URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
// URI-reference = URI / relative
// ---------------------------------------------------------------------------
const wxChar* wxURI::Parse(const wxChar* uri)
{
uri = ParseScheme(uri);
uri = ParseAuthority(uri);
uri = ParsePath(uri);
uri = ParseQuery(uri);
return ParseFragment(uri);
}
// ---------------------------------------------------------------------------
// ParseXXX
//
// Individual parsers for each URI component
// ---------------------------------------------------------------------------
const wxChar* wxURI::ParseScheme(const wxChar* uri)
{
wxASSERT(uri != NULL);
//copy of the uri - used for figuring out
//length of each component
const wxChar* uricopy = uri;
//Does the uri have a scheme (first character alpha)?
if (IsAlpha(*uri))
{
m_scheme += *uri++;
//scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
while (IsAlpha(*uri) || IsDigit(*uri) ||
*uri == wxT('+') ||
*uri == wxT('-') ||
*uri == wxT('.'))
{
m_scheme += *uri++;
}
//valid scheme?
if (*uri == wxT(':'))
{
//mark the scheme as valid
m_fields |= wxURI_SCHEME;
//move reference point up to input buffer
uricopy = ++uri;
}
else
//relative uri with relative path reference
m_scheme = wxEmptyString;
}
// else
//relative uri with _possible_ relative path reference
return uricopy;
}
const wxChar* wxURI::ParseAuthority(const wxChar* uri)
{
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?