• 로그인 함 해보끄나?

  • Sarangnamu.net June 17, 2003
    Home Login Profile Study Open Project Q&A Google Code
    percent encode
    Last Modify : 13 January, 2011(06:52)
    /**
     * @file CPercentEncode.h
     * @author cheol-dong choi <aucd29@gmail.com>
     * @version 1.0
     * @date June 17, 2010 18:3:17
     * @section LICENSE
     *
     * @section DESCRIPTION
     * <b>sample code</b>
     * @code
     *		CPercentEncode percent;
     *		percent.Encode("가나다라abcd");
     * @encode
     *
     * @section CHANGE_LOG
     * - doxygen style 로 주석 변경 2010-08-20 (16:36:19)
     */
    
    
    #ifndef __CD_CPERCENTENCODE_H__
    #define __CD_CPERCENTENCODE_H__
    
    
    #include <string>
    
    
    /**
     * @class CPercentEncode
     * @brief 문자열을 percent encoding 시킨다.
     */
    class CPercentEncode
    {
    public:
    
    	/*
    	 * Construction
    	 */
    	CPercentEncode();
    
    	/*
    	 * Destructor
    	 */
    	virtual ~CPercentEncode();
    
    public:
    
    	/*
    	 * Getter methods
    	 */
    	std::string Encode(std::string szData);
    
    	std::string EncodeUTF8(std::string szData);
    
    	void SetContentType(bool bContentType);
    
    protected:
    
    	/*
    	 * Internal methods
    	 */
    
    protected:
    
    	/*
    	 * Attributes
    	 */
    	bool _bContentType;
    };
    
    #endif
    
    /**
     * @file CPercentEncode.cpp
     * @author cheol-dong choi <aucd29@gmail.com>
     * @version 1.0
     * @date June 17, 2010 18:3:20
     * @section LICENSE
     *
     *
     * @section DESCRIPTION
     * @see http://en.wikipedia.org/wiki/Percent-encoding
     *
     *    - Characters in the unreserved character set as defined by
     *      [RFC3986], Section 2.3 (ALPHA, DIGIT, '-', '.', '_', '~')
     *      MUST NOT be encoded.
     *
     *    - All other characters MUST be encoded
     *
     *    - The two hexadecimal characters used to represent encoded
     *      characters MUST be uppercase
     *
     *
     * @see http://ko.wikipedia.org/wiki/ISO/IEC_8859
     *
     * @section CHANGE_LOG
     * - doxygen style 로 주석 변경 2010-08-20 (16:37:01)
     *
     */
    
    
    #include "CPercentEncode.h"
    #include "RSLOGGER.H"
    #include "CConvertor.h"
    
    
    /*
     * CPercentEncode
     */
    CPercentEncode::CPercentEncode()
    	: _bContentType(false)
    {
    
    }
    
    
    /*
     * ~CPercentEncode
     */
    CPercentEncode::~CPercentEncode()
    {
    
    }
    
    
    /**
     * 문자열을 percent encoding(url encoding) 한다. 
     *
     * @param szData 변경할 문자열
     * @return 변경된 문자열
     */
    std::string CPercentEncode::Encode(std::string szData)
    {
    	LOG1("CPercentEncode::Encode\n");
    
    	if (!szData.size())
    	{
    		return "";
    	}
    
    	int nToNumber;
    	char szEncodedChar[4] = {0};
    	std::string szEncodedData;
    
    	for (size_t i=0; i<szData.size(); ++i)
    	{
    		switch(szData[i])
    		{
    		case '0': case '1': case '2': case '3': case '4':
    		case '5': case '6': case '7': case '8': case '9':				//	[DIGIT]
    
    		case 'a': case 'b': case 'c': case 'd': case 'e':
    		case 'f': case 'g': case 'h': case 'i': case 'j':
    		case 'k': case 'l': case 'm': case 'n': case 'o':
    		case 'p': case 'q': case 'r': case 's': case 't':
    		case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
    
    		case 'A': case 'B': case 'C': case 'D': case 'E':
    		case 'F': case 'G': case 'H': case 'I': case 'J':
    		case 'K': case 'L': case 'M': case 'N': case 'O':
    		case 'P': case 'Q': case 'R': case 'S': case 'T':
    		case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':		// [ALPHA]
    
    		case '_': case '~': case '.': case '-':	
    			szEncodedData += szData[i];
    			break;
    
    		case ' ':
    			if (_bContentType) // The application/x-www-form-urlencoded type
    			{
    				szEncodedData += "+";
    				break;
    			}
    		default:
    			{
    				nToNumber = (int)szData[i];
    				sprintf(szEncodedChar, "%%%02X", nToNumber < 0 ? (nToNumber + 256) : nToNumber);
    				szEncodedData += szEncodedChar;
    			}
    			break;
    		}
    	}
    	
    	return szEncodedData;
    }
    
    
    /**
     * 문자열을 UTF-8 로 인코딩 후 percent encoding 한다.
     *
     * @param szData 변경할 문자열
     * @return 변경된 문자열
     */
    std::string CPercentEncode::EncodeUTF8(std::string szData)
    {
    	LOG1("CPercentEncode::EncodeUTF8\n");
    
    	if (szData.size() == 0)
    	{
    		return "";
    	}
    
    	std::string szUTF8;
    	ConvertType("UTF-8", "EUC-KR", szUTF8, szData);
    
    	return Encode(szUTF8);
    }
    
    
    /**
     * content 형태를 변경한다. 이 변경에 따라서 [space] 가 
     * + 로 변경되거나 %20 으로 변경이 된다.
     *
     * @param bURLEncoded content type 옵션 값
     */
    void CPercentEncode::SetContentType(bool bContentType)
    {
    	LOG1("CPercentEncode::SetContentType\n");
    
    	_bContentType = bContentType;
    }
    

    Comment


    입력하3 1513100827



    Locations of visitors to this page