使用boost libcurl win32sdk

Thread Regex LibcURL

唉,挫得要死....最后发现VeryCD竟然有复制链接功能.

不过记录下来吧.也很有用

 

  1. #define WIN32_LEAN_AND_MEAN 
  2.  
  3. #include <windows.h> 
  4. #include <boost/thread.hpp> 
  5. #include <boost/regex.hpp> 
  6. #include <boost/lambda/lambda.hpp> 
  7. #include <algorithm> 
  8. #include <cstring> 
  9. #include <cstdlib> 
  10. #include <string> 
  11. #include <iostream> 
  12. #include <vector> 
  13. #include <curl/curl.h> 
  14.  
  15. #include "resource.h" 
  16.  
  17. #define SAMPLE_URL "http://www.verycd.com/topics/146220/" 
  18. #define WM_PARSE_COMPLETE WM_USER+1 
  19.  
  20. HINSTANCE hInst; 
  21. HWND main_hwnd; 
  22. HWND result_text; 
  23. CURL * main_curl; 
  24.  
  25. typedef struct downloadData 
  26.     int size; 
  27.     void * pData; 
  28. }DOWNLOADDATA,*LPDOWNLOADDATA; 
  29.  
  30. DOWNLOADDATA data; 
  31. char url[512]; 
  32.  
  33. std::vector<std::string> ed2klinks; 
  34. boost::thread * pThread; 
  35.  
  36. size_t write_callback( void *ptr, size_t size, size_t nmemb, void *userdata); 
  37. void start_getting_links(); 
  38.  
  39. BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) 
  40.     switch(uMsg) 
  41.     { 
  42.         case WM_INITDIALOG: 
  43.             /* 
  44.              * TODO: Add code to initialize the dialog. 
  45.              */ 
  46.             return TRUE; 
  47.         case WM_PARSE_COMPLETE: 
  48.             { 
  49.                 std::string all; 
  50.                 std::for_each( 
  51.                               ed2klinks.begin(), 
  52.                               ed2klinks.end(), 
  53.                               all+=boost::lambda::_1 
  54.                               ); 
  55.                   HWND hBT = ::GetDlgItem(main_hwnd,IDC_BTN_START); 
  56.                 ::EnableWindow(hBT,TRUE); 
  57.                 ::SetWindowText(result_text,all.c_str()); 
  58.             } 
  59.             ed2klinks.clear(); 
  60.             ::MessageBox(NULL,"获取完毕!","提示",MB_OK); 
  61.             break
  62.         case WM_CLOSE: 
  63.             PostQuitMessage(0); 
  64.             return TRUE; 
  65.  
  66.         case WM_COMMAND: 
  67.             switch(LOWORD(wParam)) 
  68.             { 
  69.                 /* 
  70.                  * TODO: Add more control ID's, when needed. 
  71.                  */ 
  72.                 case IDC_BTN_QUIT: 
  73.                     ::PostQuitMessage(0); 
  74.                     return TRUE; 
  75.  
  76.                 case IDC_BTN_START: 
  77.                     ::GetDlgItemText(main_hwnd,IDC_EDIT_URL,url,512); 
  78.                     boost::regex url_expression("http://.*"); 
  79.                     char * begin = url; 
  80.                     char * end = url+strlen(url); 
  81.                     boost::match_results<char *> result; 
  82.                     if( ! boost::regex_match(begin,end,result,url_expression,boost::match_default)) 
  83.                     { 
  84.                         ::MessageBox(NULL,"地址不合法","错误",MB_OK); 
  85.                         return TRUE; 
  86.                     } 
  87.  
  88.                     pThread = new boost::thread(&start_getting_links); 
  89.                     pThread->timed_join(boost::posix_time::milliseconds(1)); 
  90.                     printf("start"); 
  91.                     //printf((char *)data.pData); 
  92.                     //MessageBox(hwndDlg, "You clicked \"Test\" button!", "Information", MB_ICONINFORMATION); 
  93.                     return TRUE; 
  94.             } 
  95.     } 
  96.  
  97.     return FALSE; 
  98.  
  99.  
  100. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) 
  101.     hInst = hInstance; 
  102.     MSG messages; 
  103.     // The user interface is a modal dialog box 
  104.     main_hwnd = ::CreateDialog(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DialogProc); 
  105.     result_text = ::GetDlgItem(main_hwnd,IDC_EDIT_RESULT); 
  106.     ::SetWindowText(::GetDlgItem(main_hwnd,IDC_EDIT_URL),"http://www.verycd.com/topics/67240/"); 
  107.     ::ShowWindow(main_hwnd,SW_NORMAL); 
  108.     ::SendMessage(main_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)::LoadImage( 
  109.                                                                         hInst, 
  110.                                                                         MAKEINTRESOURCE(IDI_MAIN), 
  111.                                                                          IMAGE_ICON, 
  112.                                                                         GetSystemMetrics(SM_CXSMICON), 
  113.                                                                         GetSystemMetrics(SM_CYSMICON), 
  114.                                                                         LR_DEFAULTCOLOR)); 
  115.     ::SendMessage(main_hwnd, WM_SETICON, ICON_BIG, (LPARAM)::LoadImage( 
  116.                                                                         hInst, 
  117.                                                                         MAKEINTRESOURCE(IDI_MAIN), 
  118.                                                                        IMAGE_ICON, 
  119.                                                                         GetSystemMetrics(SM_CXSMICON), 
  120.                                                                         GetSystemMetrics(SM_CYSMICON), 
  121.                                                                         LR_DEFAULTCOLOR)); 
  122.  
  123.     while (GetMessage (&messages, NULL, 0, 0)) 
  124.     { 
  125.         /* Translate virtual-key messages into character messages */ 
  126.         TranslateMessage(&messages); 
  127.         /* Send message to WindowProcedure */ 
  128.         DispatchMessage(&messages); 
  129.     } 
  130.     return 0; 
  131.     //return DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DialogProc); 
  132.  
  133. size_t write_callback( void *ptr, size_t size, size_t nmemb, void *userdata) 
  134.     size_t data_size = size * nmemb; 
  135.     DOWNLOADDATA * pData = (LPDOWNLOADDATA)userdata; 
  136.     pData->pData = realloc(pData->pData,pData->size + data_size + 1); 
  137.     memcpy(pData->pData + pData->size,ptr,data_size); 
  138.     pData->size += data_size; 
  139.     ((char *)pData->pData)[pData->size] = 0; 
  140.     return data_size; 
  141.  
  142. void start_getting_links() 
  143.     HWND hBT = ::GetDlgItem(main_hwnd,IDC_BTN_START); 
  144.     ::EnableWindow(hBT,FALSE); 
  145.     main_curl = curl_easy_init(); 
  146.     if(!main_curl) 
  147.     { 
  148.         printf("curl init error!\n"); 
  149.     } 
  150.     curl_easy_setopt(main_curl,CURLOPT_URL,url); 
  151.     curl_easy_setopt(main_curl,CURLOPT_WRITEDATA,&data); 
  152.     memset(&data,0,sizeof(DOWNLOADDATA)); 
  153.     curl_easy_setopt(main_curl,CURLOPT_WRITEFUNCTION,&write_callback); 
  154.     curl_easy_perform(main_curl); 
  155.  
  156.     boost::regex ed2k_expression = boost::regex("(?<link>ed2k://\\\|file\\\|[^\\\">]*\\\|/)"); 
  157.  
  158.     char * begin = (char*)data.pData; 
  159.     char * end = (char*)data.pData + data.size; 
  160.     boost::match_results< char* > result; 
  161.  
  162.     while(boost::regex_search(begin,end,result,ed2k_expression,boost::match_default)) 
  163.     { 
  164.         std::string temp(result[0].first,result[0].second); 
  165.         temp+="\r\r\n"
  166.         if(!ed2klinks.empty()) 
  167.         { 
  168.             if(temp.compare(ed2klinks[ed2klinks.size()-1]) != 0) 
  169.             { 
  170.                 ed2klinks.push_back(temp); 
  171.             } 
  172.         } 
  173.         else 
  174.         { 
  175.             ed2klinks.push_back(temp); 
  176.         } 
  177.         begin = result[0].second; 
  178.     } 
  179.     free(data.pData); 
  180.     data.pData = NULL; 
  181.     data.size = 0; 
  182.     PostMessage(main_hwnd,WM_PARSE_COMPLETE,NULL,NULL); 
  183.     return

 

获取网页ed2k链接桌面程序实现_VeryCD