gj

来源:互联网 发布:js 字符串concat 编辑:程序博客网 时间:2024/05/17 03:14

// testDlg.h  URLDownloadToFile
// download by http://www.codefans.net/soft/5667.shtml

#if !defined    AFX_TESTDLG_H__
#define         AFX_TESTDLG_H__

#pragma once
#pragma warning (disable : 4786)

#include <string>
#include <map>
#include <list>
#include <queue>

using namespace std;

struct Bus_t
{
    bool            bSaved;         // 是否获取过了
    string          info;           // 简介
    string          url;            // 对应的 url 后缀
    list<string>    stationsGo;     // 站台 去程
    list<string>    stationsBack;   // 站台 回程
};

/*========================================================================*/
class CTestDlg : public CDialog
{
public:
 CFont m_font;
 CImageList m_imagelist;
 CTestDlg(CWnd* pParent = NULL); 

 //{{AFX_DATA(CTestDlg)
 enum { IDD = IDD_TEST_DIALOG };
 CProgressCtrl m_pp;
 CListCtrl m_list;
 //}}AFX_DATA


 //{{AFX_VIRTUAL(CTestDlg)
 protected:
 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
 //}}AFX_VIRTUAL

protected:
    void GetBusInfo();
    void SaveToFile();

    map<string, Bus_t>  m_mapBus;

 //{{AFX_MSG(CTestDlg)
 virtual BOOL OnInitDialog();
 afx_msg void OnButton1();
 afx_msg void OnKillfocusEdit1();
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()
};
/*========================================================================*/

CString getHTML(CString strURL);
void StripTags(LPTSTR pszBuffer);
void SplitStations(char * pstation, list<string>& lstStation);
void WriteFile(const char * pszContent, const char * pszFilename);

#endif


#include "stdafx.h"
#include "Test.h"
#include "TestDlg.h"
#include <afxinet.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/*========================================================================*/

#define STR_BUS         "1路"
#define STR_GO          "去程"
#define STR_BACK        "回程"
#define STR_END         "站)"
#define STR_LINE        "相关线路"
#define STR_HREF_END    "</a>"

CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
 : CDialog(CTestDlg::IDD, pParent)
{
 //{{AFX_DATA_INIT(CTestDlg)
 //}}AFX_DATA_INIT
}

void CTestDlg::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
 //{{AFX_DATA_MAP(CTestDlg)
 DDX_Control(pDX, IDC_PROGRESS1, m_pp);
 DDX_Control(pDX, IDC_LIST1, m_list);
 //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
 //{{AFX_MSG_MAP(CTestDlg)
 ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
 ON_EN_KILLFOCUS(IDC_EDIT1, OnKillfocusEdit1)
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/*========================================================================*/


#define MAX  1 //10

BOOL CTestDlg::OnInitDialog()
{
 CDialog::OnInitDialog();

 m_imagelist.Create(16,16,TRUE,2,2);
 m_imagelist.Add(AfxGetApp()->LoadIcon(IDI_ICON1));
 m_list.SetImageList(&m_imagelist,LVSIL_SMALL);

 m_font.CreateFont(16, 0,0,0,FW_NORMAL, 0,0,0,
  DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
  DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Arial");
 m_list.SetFont(&m_font);


/*-----------------------------------------------------------*/ 
 m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
 m_list.SetBkColor(RGB(247,247,255));
 m_list.SetTextColor(RGB(0,0,255));
 m_list.SetTextBkColor(RGB(247,247,255));
 m_list.InsertColumn(0, "序号", LVCFMT_LEFT, 50);
 m_list.InsertColumn(1, "公交路线", LVCFMT_LEFT, 350);
 m_list.InsertColumn(2, "下载状态", LVCFMT_LEFT, 150);

 m_pp.SetRange(1,MAX+1);
 m_pp.SetPos(0);
 m_pp.SetStep(1);

 CString strURL,strI;
/* 
 for(int i=1; i<=MAX; i++)
 {
  strI.Format("%d",i);
  strURL="http://www.xiaozhou.net/cooldog/magicface/flash/" +strI+ ".swf";

  int nIndex=m_list.InsertItem(0xffff,strI,0);
  m_list.SetItemText(nIndex,1,strURL);
 }
*/
 return TRUE; 
}

void CTestDlg::OnButton1()
{
 CString strURL,strI;
//
//  for(int i=1; i<=MAX; i++)
//  {
//   strI.Format("%d",i);
//   //strURL="http://www.xiaozhou.net/cooldog/magicface/flash/" +strI+ ".swf";
//         strURL="http://www.baidu.com/";
//         strURL="http://www.baidu.com/img/baidu_logo.gif";
//
//   if(::URLDownloadToFile(NULL,strURL,"d://"+strI+".swf",0,NULL) ==S_OK) 
//   {
//    m_pp.StepIt();
//    m_list.SetItemText(i-1,2,"文件下载完成!");
//   }
//   else
//   {
//    m_pp.StepIt();
//    m_list.SetItemText(i-1,2,"文件下载失败...");
//   }
//  }

    //CString sss = getHTML("http://shenzhen.8684.cn/x_24f5dad9");
    //int iij = 0;

    GetBusInfo();
    SaveToFile();
}

void CTestDlg::OnKillfocusEdit1()
{

}


CString getHTML(CString strURL)   
{   
    CInternetSession mySession(NULL,0);   
    CHttpFile* myHttpFile=NULL;   
    CString strHtml="";   
       
    CString myData;   
    myHttpFile=(CHttpFile*)mySession.OpenURL(strURL);
    while(myHttpFile->ReadString(myData))   
    {
        strHtml+=myData+"/n";   
    }   
    myHttpFile->Close();
    mySession.Close();

    WriteFile(strHtml, "3.txt");

    return strHtml;
}

// 写文件
void WriteFile(const char * pszContent, const char * pszFilename)
{
    FILE * fp;
    if( (fp = fopen(pszFilename, "w+t")) != NULL)
    {
        fwrite(pszContent, sizeof(char), strlen(pszContent), fp);
        fclose(fp);
    }
}


void CTestDlg::GetBusInfo()
{
    CString url = "http://shenzhen.8684.cn/";
    CString tmp;
    static char szTmp[1024*16]; // 使用静态变态,不占用函数栈中的内存
    queue<string> queBus;
    queBus.push(STR_BUS);

    Bus_t  t;
    t.bSaved = false;
    t.url = "x_24f5dad9";
    m_mapBus.insert(pair<string, Bus_t>(STR_BUS, t));


    int nL;
    int i = 1;
    int j = 0;
    CString strI;
    strI.Format("%d",i++);
    int nIndex=m_list.InsertItem(0xffff,strI,0);
    m_list.SetItemText(nIndex,1,STR_BUS);
    UpdateWindow();

    while(!queBus.empty())
    {
        string b = queBus.front();
        queBus.pop();

        map<string, Bus_t>::iterator ibus = m_mapBus.find(b);
        if (ibus != m_mapBus.end())
        {
            Bus_t & bsecond = ibus->second;
            CString html = getHTML(url + bsecond.url.c_str());
           
            // 解析html
            int pos = html.Find(bsecond.url.c_str());
            if (pos != -1)
            {
                html.Delete(0, pos);
                while(html.GetAt(0) != '>') html.Delete(0);
                if (html.GetAt(0) == '>')   html.Delete(0);
            }

            // 取 线路简介 信息
            pos = html.Find(STR_GO);
            if (pos != -1)
            {
                tmp = html.Left(pos);
                nL = tmp.GetLength();
                strcpy(szTmp, tmp);     // 需要注意不要拷贝越界
                StripTags(szTmp);
                bsecond.info = szTmp;   // 简介

                html.Delete(0, pos);
                while(html.GetAt(0) != '>') html.Delete(0);
                if (html.GetAt(0) == '>')   html.Delete(0);
            }

            // 取 去程 信息
            pos = html.Find(STR_BACK);
            if (pos != -1)
            {
                tmp = html.Left(pos);
                nL = tmp.GetLength();
                strcpy(szTmp, tmp);
                StripTags(szTmp);
                // 去程
                //SplitStations(szTmp, bsecond.stationsGo);

                html.Delete(0, pos);
                while(html.GetAt(0) != '>') html.Delete(0);
                if (html.GetAt(0) == '>')   html.Delete(0);
            }

            // 取 回程 信息
            pos = html.Find(STR_END);
            if (pos != -1)
            {
                tmp = html.Left(pos+strlen(STR_END));
                nL = tmp.GetLength();
                strcpy(szTmp, tmp);
                StripTags(szTmp);
                // 回程
                //SplitStations(szTmp, bsecond.stationsBack);

                html.Delete(0, pos);
                while(html.GetAt(0) != '>') html.Delete(0);
                if (html.GetAt(0) == '>')   html.Delete(0);
            }

            bsecond.bSaved = true;  // 该线路已经取到了

            m_list.SetItemText(j++,2,"下载完成!");
            UpdateWindow();         
           
            // 获取其他线路的链接
            pos = html.Find(STR_LINE);
            if (pos != -1)
            {
                // 相关线路
                html.Delete(0, pos);
                pos = html.Find("</div>");
                if (pos != -1)
                {
                    html.Delete(pos, html.GetLength()-pos);
                }

                map<string, Bus_t>::iterator ibusT;
                pos = html.Find(STR_HREF_END);
                while(pos != -1)
                {
                    tmp = html.Left(pos);
                    html.Delete(0, pos+strlen(STR_HREF_END));

                    int p1 = tmp.Find("/"");
                    tmp.Delete(0, p1+1);
                    p1 = tmp.Find("/"");

                    CString href = tmp.Left(p1);

                    strcpy(szTmp, tmp);
                    szTmp[0] = '<';
                    StripTags(szTmp);

                    ibusT = m_mapBus.find(szTmp);
                    // 之前没有该信息时,才记录,防重复
                    if (ibusT == m_mapBus.end())
                    {
                        t.url = href;
                        m_mapBus.insert(pair<string, Bus_t>(szTmp, t));
                        queBus.push(szTmp);

                        strI.Format("%d",i++);
                        int nIndex=m_list.InsertItem(0xffff,strI,0);
                        m_list.SetItemText(nIndex,1,szTmp);
                        UpdateWindow();
                    }

                    pos = html.Find(STR_HREF_END);
                }
            }
        }
    }
}

void CTestDlg::SaveToFile()
{
    char szCont[128];
    FILE * fp;
    if( (fp = fopen("sz.txt", "w+t")) != NULL)
    {
        map<string, Bus_t>::iterator ibus = m_mapBus.begin();
        for (; ibus != m_mapBus.end(); ++ibus)
        {
            Bus_t & t = ibus->second;
            fwrite(ibus->first.c_str(), sizeof(char), ibus->first.length(), fp);
            fwrite("/n", 1, 1, fp);

            // 简介
            fwrite(t.info.c_str(), sizeof(char), t.info.length(), fp);
            sprintf(szCont, "%s", "/n去程/n");
            fwrite(szCont, 1, strlen(szCont), fp);

            list<string>::iterator iSt;
            for (iSt=t.stationsGo.begin(); iSt!=t.stationsGo.end(); ++iSt)
            {
                fwrite(iSt->c_str(), 1, iSt->length(), fp);
                fwrite(" ", 1, 1, fp);
            }
            sprintf(szCont, "%d站%s", t.stationsGo.size(), "/n回程/n");
            fwrite(szCont, 1, strlen(szCont), fp);

            for (iSt=t.stationsBack.begin(); iSt!=t.stationsBack.end(); ++iSt)
            {
                fwrite(iSt->c_str(), 1, iSt->length(), fp);
                fwrite(" ", 1, 1, fp);
            }
            sprintf(szCont, "%d站%s", t.stationsBack.size(), "/n");
            fwrite(szCont, 1, strlen(szCont), fp);
        }

        fclose(fp);
    }
}


// StripTags() rips through a buffer and removes HTML tags from it.
// The function uses a static variable to remember its state in case
// a HTML tag spans a buffer boundary.
void StripTags(LPTSTR pszBuffer)
{
 static BOOL bInTag = FALSE;
 LPTSTR pszSource = pszBuffer;
 LPTSTR pszDest = pszBuffer;

 while (*pszSource != '/0')
 {
  if (bInTag)
  {
   if (*pszSource == '>')
    bInTag = FALSE;
   pszSource++;
  }
  else
  {
   if (*pszSource == '<')
    bInTag = TRUE;
   else
   {
    *pszDest = *pszSource;
    pszDest++;
   }
   pszSource++;
  }
 }
 *pszDest = '/0';
}


void SplitStations(char * pstation, list<string>& lstStation)
{
    char st[60];    // 站台名称不应该太长
    int i;
    while(*pstation != '/0')
    {
        i = 0;
        while(*pstation && *pstation == ' ') pstation++;    // 过滤前导空格
        while(*pstation && *pstation != '-')
            st[i++] = *pstation++;

        while (i>1 && st[i-1]==' ') i--;    // 过滤末尾空格

        st[i] = '/0';
        if (*pstation == '-') pstation++;
       
        lstStation.push_back(st);
    }

    // 删除 最后一个站台中的 "(xx站)"  信息
    CString strTotal;
    strTotal.Format("(%d站)", lstStation.size());
    list<string>::iterator iSt = --lstStation.end();
    size_t pos = iSt->find(strTotal);
    if(pos != string::npos)
    {
        iSt->resize(pos);
        while(iSt->at(iSt->length()-1)==' ')
            iSt->erase(iSt->length()-1);
    }
}

 

原创粉丝点击