ProtocolFunc.cpp
资源名称:1731.rar [点击查看]
上传用户:swkcbjrc
上传日期:2016-04-02
资源大小:45277k
文件大小:6k
源码类别:
游戏
开发平台:
Visual C++
- // ProtocolFunc.cpp: implementation of the ProtocolFunc class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "Protocol.h"
- #include "ProtocolFunc.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- ProtocolFunc::ProtocolFunc()
- {
- }
- ProtocolFunc::~ProtocolFunc()
- {
- }
- void ProtocolFunc::DeleteMateList(CPtrList* pMateList)
- {
- for(int i=0;i<pMateList->GetCount();i++)
- {
- LPNODEMATE pNodeMate=(LPNODEMATE)(pMateList->GetAt(pMateList->FindIndex(i)));
- if(pNodeMate!=NULL)
- {
- delete pNodeMate;
- pNodeMate=NULL;
- }
- }
- pMateList->RemoveAll();
- }
- //取得协议信息内容
- //input: 信息文本,开始关键字,结束关键字,内容地址
- //output: void
- void ProtocolFunc::GetProtoContent(CString strProtoText,CString strKeyStart,
- CString strKeyEnd,CString& strContent)
- {
- strContent="";
- if(strKeyStart.IsEmpty()||strKeyEnd.IsEmpty())
- return;
- int nStart=strProtoText.Find(strKeyStart,0);
- if(nStart<0)
- return;
- nStart+=strKeyStart.GetLength();
- int nEnd=strProtoText.Find(strKeyEnd,nStart);
- if(nEnd<0)
- return;
- strContent=strProtoText.Mid(nStart,nEnd-nStart);
- }
- //取得结束关键字
- //input: 开始关键字,结束关键字地址
- //output: void
- void ProtocolFunc::GetProtoLineEnd(CString strStart,CString& strEnd)
- {
- strEnd="";
- if(strStart.IsEmpty())
- return;
- int nStart=0;
- nStart=strStart.Find("<",nStart);
- if(nStart<0)
- return;
- nStart+=1;
- strEnd="</" + strStart.Right(strStart.GetLength()-1);
- }
- //通过协议文字取得列表
- //input: 协议内容,列表指针,特殊标记
- //output: void
- //##ModelId=3B394E3101B5
- void ProtocolFunc::GetListBySign(CString strText,
- CPtrList* pListTemp,
- CString strSign)
- {
- if(pListTemp==NULL||strSign.IsEmpty())
- return;
- int nStartPos,nEndPos;
- nStartPos=0;
- nEndPos=0;
- //strFileBuffer.Replace('n',NULL);
- strText.TrimLeft("rn");
- strText.TrimRight("rn");
- int nLengthAll=strText.GetLength();
- while(1)
- {
- if(nStartPos>=nLengthAll)
- return;
- nEndPos=strText.Find(strSign,nStartPos);
- if(nEndPos<=0)
- return;
- CString strContent;
- int nLength=nEndPos-nStartPos;
- if(nLength<=0)
- return;
- strContent=strText.Mid(nStartPos,nLength);
- strContent.TrimLeft("rn");
- strContent.TrimRight("rn");
- LPNODESTRING pNodeString=new NODESTRING;
- pNodeString->strContent=strContent;
- pListTemp->AddTail(pNodeString);
- nStartPos=nEndPos+1;
- }
- }
- //使用两个标记区分一个列表
- //input: 信息缓冲,列表指针,标志1,标志2
- //output: void
- //##ModelId=3B394E3101DD
- void ProtocolFunc::GetListByTwoSign(CString strText,
- CPtrList* pListTemp,CString strMidSign,
- CString strEndSign)
- {
- if(pListTemp==NULL||strText.IsEmpty()||strMidSign.IsEmpty()||
- strEndSign.IsEmpty())
- return;
- int nStartPos,nEndPos;
- nStartPos=0;
- nEndPos=0;
- //strFileBuffer.Replace('n',NULL);
- strText.TrimLeft("rn");
- strText.TrimRight("rn");
- int nLengthAll=strText.GetLength();
- while(1)
- {
- if(nStartPos>=nLengthAll)
- return;
- nEndPos=strText.Find(strEndSign,nStartPos);
- if(nEndPos<=0)
- nEndPos=strText.GetLength()+1;
- CString strContent;
- int nLength=nEndPos-nStartPos;
- if(nLength<=0)
- return;
- strContent=strText.Mid(nStartPos,nLength);
- strContent.TrimLeft("rn");
- strContent.TrimRight("rn");
- int nMidSignPos=0;
- nMidSignPos=strContent.Find(strMidSign,0);
- if(nMidSignPos>0)
- {
- CString strKey=strContent.Left(nMidSignPos);
- int nLineLength=strContent.GetLength();
- CString strValue=strContent.Right(nLineLength-nMidSignPos-1);
- LPNODEMATE pNode=new NODEMATE;
- pNode->strMateName=strKey;
- pNode->strMateValue =strValue;
- pListTemp->AddTail(pNode);
- }
- nStartPos=nEndPos+1;
- }
- }
- //从列表中取得指定信息
- //input: 关键字,输出信息内存,列表指针
- //output: void
- //##ModelId=3B394E310205
- void ProtocolFunc::GetValueFromList(CString strKey,
- CString& strValue,
- CPtrList* pListTemp)
- {
- if(strKey.IsEmpty())
- return;
- for(int i=0;i<pListTemp->GetCount();i++)
- {
- POSITION pos=pListTemp->FindIndex(i);
- if(pos==NULL)
- continue;
- LPNODEMATE pNode=(LPNODEMATE)pListTemp->GetAt(pos);
- if(pNode==NULL)
- continue;
- CString strKeyTemp=pNode->strMateName;
- CString strValueTemp=pNode->strMateValue;
- if(strKeyTemp==strKey)
- {
- strValue=strValueTemp;
- return;
- }
- }
- }
- //删除列表
- //input: 列表指针
- //output: void
- //##ModelId=3B394E31022D
- void ProtocolFunc::ReleaseList(CPtrList* pListTemp)
- {
- for(int i=0;i<pListTemp->GetCount();i++)
- {
- POSITION pos=pListTemp->FindIndex(i);
- if(pos==NULL)
- continue;
- LPNODESTRING pNodeString=(LPNODESTRING)pListTemp->GetAt(pos);
- if(pNodeString==NULL)
- continue;
- delete pNodeString;
- }
- pListTemp->RemoveAll();
- }
- //替换MATELIST
- //input: 列表指针,需要替换的内存区
- //output: void
- void ProtocolFunc::ReplaceMateList(CPtrList* pMateList,CString& strFileBuffer)
- {
- ASSERT(pMateList!=NULL);
- ASSERT(!strFileBuffer.IsEmpty());
- if(strFileBuffer.IsEmpty()||pMateList==NULL)
- return;
- for(int i=0;i<pMateList->GetCount();i++)
- {
- LPNODEMATE pMate=(LPNODEMATE)(pMateList->GetAt(pMateList->FindIndex(i)));
- if(pMate)
- {
- CString strMateName=pMate->strMateName;
- CString strMateValue=pMate->strMateValue;
- strMateName="<%" + strMateName + "%>";
- int nStart=0;
- int nResult;
- while((nResult=strFileBuffer.Find(strMateName,nStart))>=0)
- {
- int nRightCount=0;
- int nTemp=strFileBuffer.GetLength();
- int nTotal=strFileBuffer.GetLength();
- int nNameLength=strMateName.GetLength();
- nRightCount=nTotal-nResult-nNameLength;
- CString strTemp;
- strTemp=strFileBuffer.Left(nResult) + strMateValue + strFileBuffer.Right(nRightCount);
- nTemp=strTemp.GetLength();
- strFileBuffer="";
- strFileBuffer=strTemp;
- nStart=nResult+strMateName.GetLength();
- }
- }
- }
- }