CheckLinks.cpp
上传用户:zexelpump
上传日期:2007-01-04
资源大小:22k
文件大小:3k
源码类别:

WEB源码(ASP,PHP,...)

开发平台:

Visual C++

  1. #include <windows.h>
  2. #include <iostream>
  3. #include "inet.h"
  4. #include "html.h"
  5. using namespace std;
  6. int main( int argc, char *argv[] )
  7. {
  8.     if ( argc < 2 )
  9.     {
  10.         cout << "syntax: CheckLinks <HTML filename or wildcard>" << endl;
  11. return 0;
  12.     }
  13.     HRESULT hRes;
  14.     hRes = OleInitialize( NULL );
  15.     if( !SUCCEEDED( hRes ) ) 
  16.         return 1;     
  17.     WinInet inet;
  18.     HTMLParser *php = HTMLParser::Create();
  19.     WIN32_FIND_DATA wfd;
  20. // make sure we got an internet connection
  21. if ( !inet.IsConnected() )
  22. {
  23. cerr << "Could not connect to the internet..." << endl;
  24. php->Release();
  25. OleUninitialize();
  26. return 2;
  27. }
  28. // make sure we connected to IE4 properly
  29. if ( !php->IsConnected() )
  30. {
  31. cerr << "Could not connect to Internet Explorer 4..." << endl;
  32. php->Release();
  33. OleUninitialize();
  34. return 3;
  35. }
  36. // cache the directory specified on the command line
  37. string strDir;
  38. char szDrive[_MAX_DRIVE];
  39. char szDir[_MAX_PATH];
  40. _splitpath(argv[1], szDrive, szDir, NULL, NULL );
  41. if ( szDrive[0] || szDir[0] )
  42. {
  43. strDir = szDrive;
  44. strDir += szDir;
  45. }
  46.     HANDLE hFind = FindFirstFile(argv[1], &wfd);
  47.     if ( hFind != INVALID_HANDLE_VALUE )
  48.     {
  49. string strFile;
  50.         do
  51.         {
  52.             if ( !(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
  53.             {
  54.                 strFile = strDir + wfd.cFileName;
  55.                 if ( php->LoadHTMLFile(strFile.c_str()) )
  56.                 {
  57.                     cout << php->GetLinkCount() << " links" << endl;
  58.                     for ( int i = 0; i < php->GetLinkCount(); i++ )
  59.                     {
  60.                         string strURL;
  61.                         if (php->GetLinkURL(i, strURL) )
  62.                         {
  63.                             if ( !inet.CheckLink(strURL.c_str()) )
  64.                                 cout << "DEAD LINK: " << strURL << endl;
  65.                             else
  66.                                 cout << "OK: " << strURL << endl;
  67.                         }
  68.                         else
  69.                             cout << "ERROR getting link" << endl;
  70.                     }
  71.                     cout << php->GetImageCount() << " images" << endl;
  72.                     for ( i = 0; i < php->GetImageCount(); i++ )
  73.                     {
  74.                         string strURL;
  75.                         if (php->GetImageURL(i, strURL) )
  76.                         {
  77.                             if ( !inet.CheckLink(strURL.c_str()) )
  78.                                 cout << "DEAD IMG: " << strURL << endl;
  79.                             else
  80.                                 cout << "OK: " << strURL << endl;
  81.                         }
  82.                         else
  83.                             cout << "ERROR getting image" << endl;
  84.                     }
  85.                 }
  86.                 else
  87.                     cout << "Error loading html file " << strFile << endl;
  88.             }
  89.         } while ( FindNextFile(hFind, &wfd));
  90.         FindClose(hFind);
  91.     }
  92. php->Release();
  93. OleUninitialize();
  94.     return( 0 );