129 lines
3.0 KiB
C++
129 lines
3.0 KiB
C++
#include "snaverurlvalidator.h"
|
|
#include <QStringList>
|
|
#include <QDebug>
|
|
|
|
const QString SNaverUrlValidator::m_strSubDomain = "www";
|
|
const QString SNaverUrlValidator::m_strProtocol = "http";
|
|
const QString SNaverUrlValidator::m_strBaseHost = "blog.naver.com";
|
|
const QString SNaverUrlValidator::m_strSecondaryHost = "blog.me";
|
|
|
|
SNaverUrlValidator::SNaverUrlValidator(QString url) : m_isBaseHost(false), m_isSecondaryHost(false)
|
|
{
|
|
m_strUrl = url.trimmed();
|
|
}
|
|
|
|
QString SNaverUrlValidator::makeUrl()
|
|
{
|
|
if (!validateUrl())
|
|
{
|
|
return "";
|
|
}
|
|
|
|
QString strUrl = m_strProtocol + "://" + m_strBaseHost + "/" + m_strUserId + "/" + m_strArticleNum;
|
|
return strUrl;
|
|
}
|
|
|
|
bool SNaverUrlValidator::validateUrl()
|
|
{
|
|
int nStartIndex = getHostStartPosition();
|
|
if (nStartIndex < 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
nStartIndex = checkHost(nStartIndex);
|
|
if (nStartIndex < 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (checkPath(nStartIndex) < 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
int SNaverUrlValidator::getHostStartPosition()
|
|
{
|
|
int nHostStartPosition = m_strUrl.indexOf(':');
|
|
if (nHostStartPosition == -1)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
if (m_strUrl.at(nHostStartPosition+1) != '/' || m_strUrl.at(nHostStartPosition+2) != '/')
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
return nHostStartPosition + 3;
|
|
}
|
|
|
|
int SNaverUrlValidator::checkHost(int _nHostStartPosition)
|
|
{
|
|
int nHostEndPosition = m_strUrl.indexOf('/', _nHostStartPosition) - 1;
|
|
if (nHostEndPosition < 0)
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
m_isBaseHost = m_strUrl.contains(m_strBaseHost);
|
|
m_isSecondaryHost = m_strUrl.contains(m_strSecondaryHost);
|
|
if ((m_isBaseHost == false) && (m_isSecondaryHost == false))
|
|
{
|
|
return -2;
|
|
}
|
|
|
|
if (m_isSecondaryHost)
|
|
{
|
|
int nHostLength = nHostEndPosition - _nHostStartPosition + 1;
|
|
QString strHost = m_strUrl.mid(_nHostStartPosition, nHostLength);
|
|
QStringList listHostSplit = strHost.split('.');
|
|
for (int i=0; i<listHostSplit.count(); i++)
|
|
{
|
|
QString strHostSplit = listHostSplit.at(i);
|
|
|
|
if (listHostSplit.at(i) == m_strSubDomain)
|
|
continue;
|
|
|
|
m_strUserId = strHostSplit;
|
|
break;
|
|
}
|
|
}
|
|
|
|
return nHostEndPosition + 2;
|
|
}
|
|
|
|
int SNaverUrlValidator::checkPath(int _nHostStartPosition)
|
|
{
|
|
int nUrlLength = m_strUrl.length();
|
|
int nPathLength = nUrlLength - _nHostStartPosition + 1;
|
|
QString strPath = m_strUrl.mid(_nHostStartPosition, nPathLength);
|
|
QStringList pathList = strPath.split('/');
|
|
|
|
if (m_isBaseHost)
|
|
{
|
|
if (pathList.size() < 2)
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
m_strUserId = pathList[0];
|
|
m_strArticleNum = pathList[1];
|
|
}
|
|
|
|
if (m_isSecondaryHost)
|
|
{
|
|
if (pathList.size() < 1)
|
|
{
|
|
return -2;
|
|
}
|
|
|
|
m_strArticleNum = pathList[0];
|
|
}
|
|
|
|
return 0;
|
|
}
|