DownloadThread.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include "Main.h"
  2. #pragma warning(disable:4786)
  3. #include "DownloadThread.h"
  4. #include "api__ml_downloads.h"
  5. #include <api/service/waServiceFactory.h>
  6. #include "errors.h"
  7. #include <strsafe.h>
  8. DownloadThread::DownloadThread() : parser( 0 ), parserFactory( 0 )
  9. {
  10. parserFactory = plugin.service->service_getServiceByGuid( obj_xmlGUID );
  11. if ( parserFactory )
  12. parser = (obj_xml *)parserFactory->getInterface();
  13. if ( parser )
  14. {
  15. parser->xmlreader_setCaseSensitive();
  16. parser->xmlreader_registerCallback( L"*", &xmlDOM );
  17. parser->xmlreader_open();
  18. }
  19. }
  20. DownloadThread::~DownloadThread()
  21. {
  22. if ( parser )
  23. {
  24. parser->xmlreader_unregisterCallback( &xmlDOM );
  25. parser->xmlreader_close();
  26. }
  27. if ( parserFactory && parser )
  28. parserFactory->releaseInterface( parser );
  29. parserFactory = 0;
  30. parser = 0;
  31. }
  32. #define FILE_BUFFER_SIZE 32768
  33. void DownloadThread::DownloadFile( const wchar_t *fileName )
  34. {
  35. if ( !parser )
  36. return; // no sense in continuing if there's no parser available
  37. HANDLE file = CreateFile( fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL );
  38. if ( file == INVALID_HANDLE_VALUE )
  39. return;
  40. while ( true )
  41. {
  42. char data[ FILE_BUFFER_SIZE ] = { 0 };
  43. DWORD bytesRead = 0;
  44. if ( ReadFile( file, data, FILE_BUFFER_SIZE, &bytesRead, NULL ) && bytesRead )
  45. {
  46. parser->xmlreader_feed( (void *)data, bytesRead );
  47. }
  48. else
  49. break;
  50. }
  51. CloseHandle( file );
  52. parser->xmlreader_feed( 0, 0 );
  53. ReadNodes( fileName );
  54. }