articleelement.cpp
来自「深入浅出Visual C++入门进阶与应用实例 随书光盘 作者 何志丹」· C++ 代码 · 共 284 行
CPP
284 行
// ArticleElement.cpp: implementation of the CArticleElement class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ArticleElement.h"
#include "GlobeFun.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CArticleElement::CArticleElement()
{
}
CArticleElement::~CArticleElement()
{
}
CString CArticleTextElement::GetTextWithCurBBSFormat(int nMaxCharCount)
{
if(nMaxCharCount >= m_strText.GetLength())
return m_strText ;//最大长度比当前文字长,直接返回
if(nMaxCharCount < 20 )//最大字符数太小,返回空
return "";
//压缩算法: 取前面一部分和后面一部分,中间加几个省略号,不能拆分汉字,否则会有乱码
CString strShortText ;
int nLeftCount = (nMaxCharCount - 6 ) / 2 ;
int nRightCount = (nMaxCharCount - 6) - nLeftCount ;
for(int i = nLeftCount -1 ; i >= 0 ; i-- )
{
char ch = m_strText[i] ;
if(ch >= 0 && ch < 128 )
break ;
}
int nChineseCharCount = (nLeftCount -1) - i ;
if(1 == nChineseCharCount % 2 )
nLeftCount -- ; //前边有奇数个汉字字符,汉字被拆分,会有乱码
strShortText = m_strText.Left(nLeftCount);
strShortText += "\r\n\r\n..(为了访问速度,省略若干字)\r\n\r\n";
for( i = nRightCount + 1 ; i <= m_strText.GetLength() ; i++ )
{
char ch = m_strText.Right(i)[0];
if(ch >= 0 && ch < 128)
break;
}
nChineseCharCount = i - (nRightCount + 1);
if(1 == nChineseCharCount % 2 )
nRightCount ++ ;//前面有奇数个汉字字符,汉字被拆分,会有乱码
strShortText += m_strText.Right(nRightCount);
return strShortText ;
}
CString CArticleLinkElement::GetTextWithCurBBSFormat(int nMaxCharCount)
{
char szLinkFormat[1000] = {0};
GetPrivateProfileString("设置","链接格式","#Url#出错了#Text#",szLinkFormat,999,GetConfigurationfileName());
CString strLinkFormat = szLinkFormat ;
strLinkFormat.Replace("#Url#",m_strUrl);
strLinkFormat.Replace("#Text#",m_strText);
return strLinkFormat;
}
CString CArticleLinkElement::GetTextWithHtmlLink()
{
return ::GetTextWithHtmlLink(m_strText,m_strUrl);
}
int CArticleTextElement::GetCurBBSFormatTextLength()
{
return m_strText.GetLength() ;
}
int CArticleLinkElement::GetCurBBSFormatTextLength()
{
char szLinkFormat[1000] = {0} ;
GetPrivateProfileString("设置","链接格式","#Url#出错了#Text#",szLinkFormat,999,GetConfigurationfileName());
CString strLinkFormat = szLinkFormat ;
strLinkFormat.Replace("#Url#",m_strUrl);
strLinkFormat.Replace("#Text#",m_strText);
return strLinkFormat.GetLength();
}
CArticleElements::CArticleElements()
{
m_arArticleElements.RemoveAll();
}
CArticleElements::~CArticleElements()
{
Empty();
}
CArticleElements& CArticleElements::operator +=(CString strContent)
{
CArticleElement * pArtEle ;
do
{
int nPosBegin = strContent.Find("[Url=");
int nPosEnd = strContent.Find("[/Url]");
if(-1 == nPosBegin || -1 == nPosEnd )
break;
pArtEle = (CArticleElement*)new CArticleTextElement(strContent.Left(nPosBegin));
m_arArticleElements.Add(pArtEle);//文章元素
pArtEle = (CArticleElement*)new CArticleLinkElement(strContent.Mid(nPosBegin,nPosEnd - nPosBegin + strlen("[/Url]")));
m_arArticleElements.Add(pArtEle);//链接元素
strContent = strContent.Mid(nPosEnd + strlen("[/Url]"));
} while(true);
pArtEle = (CArticleElement*)new CArticleTextElement(strContent);
m_arArticleElements.Add(pArtEle);
return *this ;
}
CArticleElements& CArticleElements::operator =(CString strContent)
{
Empty();
*this += strContent ;
return *this ;
}
CArticleTextElement::CArticleTextElement()
{
}
CArticleTextElement::~CArticleTextElement()
{
}
CArticleTextElement::CArticleTextElement(CString strContent)
{
m_strText = strContent ;
}
CArticleLinkElement::CArticleLinkElement()
{
}
CArticleLinkElement::~CArticleLinkElement()
{
}
CArticleLinkElement::CArticleLinkElement(CString strContent)
{
strContent = strContent.Mid(strlen("[Url="),strContent.GetLength() - strlen("[Url=[/Url]"));
int nPos = strContent.Find("]");
m_strText = strContent.Mid(nPos+1);
m_strUrl = strContent.Left(nPos);
}
void CArticleElements::Empty()
{
for(int i = 0 ; i < m_arArticleElements.GetSize() ; i++ )
{
CArticleElement * pEle = m_arArticleElements[i] ;
if(NULL == pEle )
continue ;
delete pEle ;
}
m_arArticleElements.RemoveAll();
}
bool CArticleElements::MakeTextWithCurBBSFormat(int nEleMaxCharCount, int nCharAPage, CStringArray &arTexts)
{
arTexts.RemoveAll() ;
ASSERT( nEleMaxCharCount <= nCharAPage );
CString strText ;
for(int i = 0 ; i < m_arArticleElements.GetSize() ; i++ )
{
CArticleElement * pEle = m_arArticleElements[i] ;
if(NULL == pEle)
{
ASSERT(false);
continue ;
}
CString strTemp = pEle->GetTextWithCurBBSFormat(nEleMaxCharCount);
if(strTemp.IsEmpty())
continue ;//出错或本身为空无需处理
if(strTemp.GetLength() + strText.GetLength() > nCharAPage )
{
arTexts.Add(strText);
strText = strTemp ;
}
else
strText += strTemp ;
}
arTexts.Add(strText);
return true ;
}
CString CArticleElements::MakeTextWithHtmlLink()
{
CString strText ;
for(int i = 0 ; i < m_arArticleElements.GetSize() ; i++ )
{
CArticleElement * pEle = m_arArticleElements[i] ;
if(NULL == pEle)
{
ASSERT(false);
continue ;
}
CString strTemp = pEle->GetTextWithHtmlLink();
if(strTemp.IsEmpty())
continue ;//出错或本身为空无需处理
strText += strTemp ;
}
return strText ;
}
bool CArticleElements::AddLink(CString strText, CString strUrl)
{
CArticleElement * pArtEle = (CArticleElement*)new CArticleLinkElement(strText,strUrl);
m_arArticleElements.Add(pArtEle);
return true ;
}
CArticleLinkElement::CArticleLinkElement(CString strText, CString strUrl)
{
m_strText = strText ;
m_strUrl = strUrl ;
}
CString CArticleTextElement::GetTextWithHtmlLink()
{
return m_strText ;
}
CString CArticleElements::MakeTextWithCurBBSFormat()
{
const int nEleMaxCharCount = 10000 ;
CString strText ;
for(int i = 0 ; i < m_arArticleElements.GetSize() ; i++ )
{
CArticleElement * pEle = m_arArticleElements[i] ;
if(NULL == pEle)
{
ASSERT(false);
continue ;
}
CString strTemp = pEle->GetTextWithCurBBSFormat(nEleMaxCharCount);
if(strTemp.IsEmpty())
continue ;//出错或本身为空无需处理
strText += strTemp ;
}
return strText ;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?