📄 urlparser.cpp
字号:
#include "UrlParser.h"
#include "StrOperation.h"
#include "Globle.h"
CUrlParser& CUrlParser::operator = (const CUrlParser &other)
{
if(this == &other)
return *this;
m_strUrl = other.m_strUrl;
m_strProtocolName = other.m_strProtocolName;
m_strHostName = other.m_strHostName;
m_nPort = other.m_nPort;
m_strPath = other.m_strPath;
m_strFullPath = other.m_strFullPath;
m_strFilename = other.m_strFilename;
m_strExtInfo = other.m_strExtInfo;
m_mapDictionary = other.m_mapDictionary;
return *this;
}
Bool CUrlParser::ParserUrl(const string &strUrl)
{
if(strUrl.empty())
return FALSE;
m_strUrl = strUrl;
if(!DecodeUrl())
return FALSE;
string strTemp = m_strUrl;
//协议头
Int32 iPos = strTemp.find("://");
if(iPos < 0)
{
m_strProtocolName = "file";
CStrOperate::TransPathToStdFormat(strTemp);
}
else
{
m_strProtocolName = strTemp.substr(0, iPos);
Tolower(m_strProtocolName);
strTemp.erase(0, iPos+3);//同时删掉"://"
//主机名和端口
iPos = strTemp.find("/");
if(iPos < 0)
return FALSE;
string strTmp1 = strTemp.substr(0, iPos);
strTemp.erase(0, iPos);//不删"/"
iPos = strTmp1.find(":");
if(iPos < 0)
{
m_strHostName = strTmp1;
m_nPort = GetDefaultPort(m_strProtocolName);
}
else
{
m_strHostName = strTmp1.substr(0, iPos);
strTmp1.erase(0, iPos+1);
m_nPort = atol(strTmp1.c_str());
}
}
//路径和附加信息
iPos = strTemp.find("?");
if(iPos < 0)
{
m_strPath = strTemp;
}
else
{
m_strPath = strTemp.substr(0, iPos);
strTemp.erase(0, iPos+1);
m_strExtInfo = strTemp;
ParseExtraInfo();
}
if(m_strProtocolName != "file")
{
if(m_strPath.find("/") == 0)
m_strPath.erase(0, 1);
}
//文件名
Int32 nPos = m_strPath.rfind("/");
nPos += 1;//去除"/"
m_strFilename = m_strPath.substr(nPos, m_strPath.length() - nPos);
//获得在本地的全路径
if(m_strProtocolName == "file")
{
m_strFullPath = m_strPath;
}
else
{
m_strFullPath = SConfigPara::m_MoviesDir;
if(m_strFullPath.at(m_strFullPath.length()-1) != kPathDelimiterChar)
m_strFullPath += kPathDelimiterChar;
m_strFullPath += m_strPath;
}
CStrOperate::TransPathFormat(m_strFullPath);
if(m_strProtocolName == "file")
m_strPath = m_strFullPath;
return TRUE;
}
string CUrlParser::GetExtName()
{
Int32 nPos = m_strFilename.rfind(".");
nPos += 1;//去除"."
string strTmp = m_strFilename.substr(nPos, m_strFilename.length() - nPos);
Tolower(strTmp);
return strTmp;
}
Bool CUrlParser::EncodeUrl()
{
//todo
return TRUE;
}
Bool CUrlParser::DecodeUrl()
{
//todo
return TRUE;
}
UInt16 CUrlParser::GetDefaultPort(const string &strProtocol)
{
if(strProtocol == "rtsp" ||
strProtocol == "rrtsp")
return 554;
else if(strProtocol == "rstp")
return 22808;
return 0;
}
void CUrlParser::Tolower(string &ioStr)
{
int i;
for(i=0; i<ioStr.length(); i++)
{
ioStr.at(i) = tolower(ioStr.at(i)); //为了使对大小写不敏感
}
}
//提取扩展域(?xxx=aaa&&yyy=bbb)
string CUrlParser::operator[](const string &strExtendKey)
{
map<string, string>::iterator it = m_mapDictionary.find(strExtendKey);
if(it != m_mapDictionary.end())
return it->second;
else
return string("");
}
void CUrlParser::ParseExtraInfo()
{
//保存
string tmp = m_strExtInfo;
string key,content;
Int32 iPos;
while(!tmp.empty())
{
//find key
iPos = tmp.find("=");
if(iPos == string::npos)
break;
key.assign(tmp.c_str(),iPos - 0);
//move to content '='
tmp.erase(0,iPos + 1);
//find content
iPos = tmp.find("&&");
if(iPos == string::npos)
{//可能到了字符串结束
content = tmp;
tmp = "";
}
else
{
content.assign(tmp.c_str(),iPos -0 );
//move to next key
tmp.erase(0,iPos +2);
}
m_mapDictionary[key] = content;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -