使用boost libcurl win32sdk
Thread Regex LibcURL
唉,挫得要死....最后发现VeryCD竟然有复制链接功能.
不过记录下来吧.也很有用
- #define WIN32_LEAN_AND_MEAN
- #include <windows.h>
- #include <boost/thread.hpp>
- #include <boost/regex.hpp>
- #include <boost/lambda/lambda.hpp>
- #include <algorithm>
- #include <cstring>
- #include <cstdlib>
- #include <string>
- #include <iostream>
- #include <vector>
- #include <curl/curl.h>
- #include "resource.h"
- #define SAMPLE_URL "http://www.verycd.com/topics/146220/"
- #define WM_PARSE_COMPLETE WM_USER+1
- HINSTANCE hInst;
- HWND main_hwnd;
- HWND result_text;
- CURL * main_curl;
- typedef struct downloadData
- {
- int size;
- void * pData;
- }DOWNLOADDATA,*LPDOWNLOADDATA;
- DOWNLOADDATA data;
- char url[512];
- std::vector<std::string> ed2klinks;
- boost::thread * pThread;
- size_t write_callback( void *ptr, size_t size, size_t nmemb, void *userdata);
- void start_getting_links();
- BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- switch(uMsg)
- {
- case WM_INITDIALOG:
- /*
- * TODO: Add code to initialize the dialog.
- */
- return TRUE;
- case WM_PARSE_COMPLETE:
- {
- std::string all;
- std::for_each(
- ed2klinks.begin(),
- ed2klinks.end(),
- all+=boost::lambda::_1
- );
- HWND hBT = ::GetDlgItem(main_hwnd,IDC_BTN_START);
- ::EnableWindow(hBT,TRUE);
- ::SetWindowText(result_text,all.c_str());
- }
- ed2klinks.clear();
- ::MessageBox(NULL,"获取完毕!","提示",MB_OK);
- break;
- case WM_CLOSE:
- PostQuitMessage(0);
- return TRUE;
- case WM_COMMAND:
- switch(LOWORD(wParam))
- {
- /*
- * TODO: Add more control ID's, when needed.
- */
- case IDC_BTN_QUIT:
- ::PostQuitMessage(0);
- return TRUE;
- case IDC_BTN_START:
- ::GetDlgItemText(main_hwnd,IDC_EDIT_URL,url,512);
- boost::regex url_expression("http://.*");
- char * begin = url;
- char * end = url+strlen(url);
- boost::match_results<char *> result;
- if( ! boost::regex_match(begin,end,result,url_expression,boost::match_default))
- {
- ::MessageBox(NULL,"地址不合法","错误",MB_OK);
- return TRUE;
- }
- pThread = new boost::thread(&start_getting_links);
- pThread->timed_join(boost::posix_time::milliseconds(1));
- printf("start");
- //printf((char *)data.pData);
- //MessageBox(hwndDlg, "You clicked \"Test\" button!", "Information", MB_ICONINFORMATION);
- return TRUE;
- }
- }
- return FALSE;
- }
- int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
- {
- hInst = hInstance;
- MSG messages;
- // The user interface is a modal dialog box
- main_hwnd = ::CreateDialog(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DialogProc);
- result_text = ::GetDlgItem(main_hwnd,IDC_EDIT_RESULT);
- ::SetWindowText(::GetDlgItem(main_hwnd,IDC_EDIT_URL),"http://www.verycd.com/topics/67240/");
- ::ShowWindow(main_hwnd,SW_NORMAL);
- ::SendMessage(main_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)::LoadImage(
- hInst,
- MAKEINTRESOURCE(IDI_MAIN),
- IMAGE_ICON,
- GetSystemMetrics(SM_CXSMICON),
- GetSystemMetrics(SM_CYSMICON),
- LR_DEFAULTCOLOR));
- ::SendMessage(main_hwnd, WM_SETICON, ICON_BIG, (LPARAM)::LoadImage(
- hInst,
- MAKEINTRESOURCE(IDI_MAIN),
- IMAGE_ICON,
- GetSystemMetrics(SM_CXSMICON),
- GetSystemMetrics(SM_CYSMICON),
- LR_DEFAULTCOLOR));
- while (GetMessage (&messages, NULL, 0, 0))
- {
- /* Translate virtual-key messages into character messages */
- TranslateMessage(&messages);
- /* Send message to WindowProcedure */
- DispatchMessage(&messages);
- }
- return 0;
- //return DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DialogProc);
- }
- size_t write_callback( void *ptr, size_t size, size_t nmemb, void *userdata)
- {
- size_t data_size = size * nmemb;
- DOWNLOADDATA * pData = (LPDOWNLOADDATA)userdata;
- pData->pData = realloc(pData->pData,pData->size + data_size + 1);
- memcpy(pData->pData + pData->size,ptr,data_size);
- pData->size += data_size;
- ((char *)pData->pData)[pData->size] = 0;
- return data_size;
- }
- void start_getting_links()
- {
- HWND hBT = ::GetDlgItem(main_hwnd,IDC_BTN_START);
- ::EnableWindow(hBT,FALSE);
- main_curl = curl_easy_init();
- if(!main_curl)
- {
- printf("curl init error!\n");
- }
- curl_easy_setopt(main_curl,CURLOPT_URL,url);
- curl_easy_setopt(main_curl,CURLOPT_WRITEDATA,&data);
- memset(&data,0,sizeof(DOWNLOADDATA));
- curl_easy_setopt(main_curl,CURLOPT_WRITEFUNCTION,&write_callback);
- curl_easy_perform(main_curl);
- boost::regex ed2k_expression = boost::regex("(?<link>ed2k://\\\|file\\\|[^\\\">]*\\\|/)");
- char * begin = (char*)data.pData;
- char * end = (char*)data.pData + data.size;
- boost::match_results< char* > result;
- while(boost::regex_search(begin,end,result,ed2k_expression,boost::match_default))
- {
- std::string temp(result[0].first,result[0].second);
- temp+="\r\r\n";
- if(!ed2klinks.empty())
- {
- if(temp.compare(ed2klinks[ed2klinks.size()-1]) != 0)
- {
- ed2klinks.push_back(temp);
- }
- }
- else
- {
- ed2klinks.push_back(temp);
- }
- begin = result[0].second;
- }
- free(data.pData);
- data.pData = NULL;
- data.size = 0;
- PostMessage(main_hwnd,WM_PARSE_COMPLETE,NULL,NULL);
- return;
- }