荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: mmkiller (5月27日电台那个我是被kaman陷害的), 信区: Program
标  题: URL分解类
发信站: 荔园晨风BBS站 (2005年05月28日21:46:13 星期六), 站内信件

//by bigcat.

//URLParser.h
//--------------------------------------------------------------------------
#pragma once

class CURLParser
{
public:
    CURLParser(void);
    ~CURLParser(void);

protected:

    char m_szProtocol[20];
    char m_szHost[200];
    int m_nPort;
    char m_szPath[200];

private:
    // 重置环境
    void Reset(void);
public:
    // 分析URL
    bool Parse(char *szURL);

    void getHost( char *szHost )
    {
        strcpy( szHost, m_szHost);
    }

    void getPath( char *szPath )
    {
        strcpy( szPath, m_szPath);
    }

    void getProtocol( char *szProtocol )
    {
        strcpy( szProtocol, m_szProtocol );
    }

    int getPort()
    {
        return m_nPort;
    }

};
/////////////////////////////////////////////////////////////////////////


// URLParser.CPP
// By Bigcat.
#include "StdAfx.h"
#include ".\urlparser.h"

CURLParser::CURLParser(void)
{
    Reset();
}

CURLParser::~CURLParser(void)
{
}

// 重置环境
void CURLParser::Reset(void)
{
    memset( m_szProtocol, 0, sizeof( m_szProtocol ) );
    memset( m_szHost,   0, sizeof( m_szHost ) );
    memset( m_szPath, 0, sizeof( m_szPath ) );
    strcpy( m_szPath, "/" );
    m_nPort = 80;
}

// 分析URL
bool CURLParser::Parse(char *szURL)
{
    Reset();

    char szTemp[200] = "";
    char szPort[10] = "";
    char *pStartPos = NULL;
    char *pEndPos   = NULL;
    int startPos    = 0;
    int endPos      = 0;
    strcpy( szPort, "80" );

    pStartPos   = strstr( szURL, "//" );
    endPos      = (int)(pStartPos - szURL);
    strncpy( m_szProtocol, szURL, endPos - 1 );

    pEndPos     = strstr( szURL + endPos + 2, "/" );
    pStartPos   += 2;
    if ( pEndPos == NULL )
    {
        // 后面部分全为主机
        strncpy( szTemp, pStartPos, strlen(szURL) - (pStartPos - szURL) );

    }else{
        // 后面包含路径
        strncpy( szTemp, pStartPos, pEndPos - pStartPos );
        strncpy( m_szPath, pEndPos , strlen(szURL) - (pEndPos - szURL) );
    }

    pEndPos     = strstr( szTemp, ":" );
    if ( pEndPos != NULL )
    {
        // 指定端口
        strncpy( m_szHost, szTemp, (int)(pEndPos - szTemp) );
        strcpy( szPort, pEndPos + 1 );
    }else{
        // 默认端口 80
        strcpy( m_szHost, szTemp );
    }
    m_nPort = atoi( szPort );

    return true;
}
///////////////////////////////////////////////////////////////////



// demo.cpp
// 演示用法

#include "URLParser.h"

int _tmain(int argc, _TCHAR* argv[])
{

    CURLParser myURL;
    char szTemp[300] = "";

    myURL.Parse("http://www.163.com/default.asp");
    printf("URL: http://www.163.com/default.asp\n" );
    myURL.getProtocol( szTemp );
    printf("Protocol:       %s\n", szTemp);
    myURL.getHost( szTemp );
    printf("Host:           %s\n", szTemp);
    printf("Port:           %d\n", myURL.getPort() );
    myURL.getPath( szTemp );
    printf("Path:           %s\n", szTemp);

    printf("\n");

    myURL.Parse("ftp://www.qq.com:8081/service/request.htm");
    printf("URL: ftp://www.qq.com:8081/service/request.htm\n" );
    myURL.getProtocol( szTemp );
    printf("Protocol:       %s\n", szTemp);
    myURL.getHost( szTemp );
    printf("Host:           %s\n", szTemp);
    printf("Port:           %d\n", myURL.getPort() );
    myURL.getPath( szTemp );
    printf("Path:           %s\n", szTemp);


    return 0;

}


--
*┌*┬*┬╮┌*┬*┬╮╭*╮  ╮*╭*┬*┬*╮╭╮    *╭*╮    *╭*┬─*╮╭*┬─*?
*│*┤*│*│*│*┤*│*│*│┤  │  │*┤  │*┤    *│*┤    │┤    *│*┤  │
*│*┤*│*││*┤*│*│*├*┴*┬*╯  │*┤  *│*┤    │*┤    *│*┼*─*╯├*┴
*╰╯*╯*╯*╰*╯*╯*╯*╰*╯*╰*╯╰*┴*┴*╯*╰*┴*─*╯*╰*┴*─*╯╰┴*─╯*
*
※ 修改:·mmkiller 於 05月28日21:46:56 修改本文·[FROM: 210.21.224.235]
※ 来源:·荔园晨风BBS站 bbs.szu.edu.cn·[FROM: 210.21.224.235]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店