A very simple MFC class to Encode and Decode an url string

来源:互联网 发布:知解的成语 编辑:程序博客网 时间:2024/06/05 03:24
A very simple MFC class to Encode and Decode an url string

  Tis is my contribution to encode and decode an url string; My objective was simplifying source code, using CString existing functions...
/*****************************************************************************Module :     UrlString.hNotices:     Written 2006 by Stephane ErhardtDescription: H URL Encoder/Decoder*****************************************************************************/#ifndef __CURLSTRING_H_#define __CURLSTRING_H_class CUrlString{private:CString m_csUnsafe;public:CUrlString();virtual ~CUrlString() { };CString Encode(CString csDecoded);CString Decode(CString csEncoded);};#endif //__CURLSTRING_H_/*****************************************************************************Module :     UrlString.cppNotices:     Written 2006 by Stephane ErhardtDescription: CPP URL Encoder/Decoder*****************************************************************************/#include "stdafx.h"#include "UrlString.h"/*****************************************************************************/CUrlString::CUrlString(){m_csUnsafe = _T("%=/"<>//^[]`+$,@:;/!#?&'");for(int iChar = 1; iChar < 33; iChar++)m_csUnsafe += (char)iChar;for(int iChar = 124; iChar < 256; iChar++)m_csUnsafe += (char)iChar;}/*****************************************************************************/CString CUrlString::Encode(CString csDecoded){CString csCharEncoded, csCharDecoded;CString csEncoded = csDecoded;for(int iPos = 0; iPos < m_csUnsafe.GetLength(); iPos++){csCharEncoded.Format(_T("%%%02X"), m_csUnsafe[iPos]);csCharDecoded = m_csUnsafe[iPos];csEncoded.Replace(csCharDecoded, csCharEncoded);}return csEncoded;}/*****************************************************************************/CString CUrlString::Decode(CString csEncoded){CString csUnsafeEncoded = Encode(m_csUnsafe);CString csDecoded = csEncoded;CString csCharEncoded, csCharDecoded;for(int iPos = 0; iPos < csUnsafeEncoded.GetLength(); iPos += 3){csCharEncoded = csUnsafeEncoded.Mid(iPos, 3);csCharDecoded = (char)strtol(csUnsafeEncoded.Mid(iPos + 1, 2), NULL, 16);csDecoded.Replace(csCharEncoded, csCharDecoded);}return csDecoded;}