eao197

C++/PCRE

May 25th, 2015
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3. #include <map>
  4. #include <algorithm>
  5.  
  6. #include <pcrecpp.h>
  7.  
  8. int main(int, char** )
  9. {
  10.     const pcrecpp::RE re("\"GET (?:https?://.+?/)?(.+?\\.mp3) "
  11.                      "HTTP/1\\.[01]\" \\d{3} (\\d+)");
  12.  
  13.     std::map<std::string, std::pair<long, long>> stat;
  14.     for( std::string line; std::getline(std::cin, line); )
  15.     {
  16.         std::string fname;
  17.         long size;
  18.  
  19.         if( re.PartialMatch( line, &fname, &size ) )
  20.         {
  21.             auto it = stat.find( fname );
  22.             if( it == stat.end() )
  23.                 stat.emplace( fname, std::make_pair(size, size) );
  24.             else
  25.             {
  26.                 const auto sum = it->second.first + size;
  27.                 const auto max = std::max( it->second.second, size );
  28.                 it->second = std::make_pair( sum, max );
  29.             }
  30.         }
  31.     }
  32.  
  33.     for( const auto& info: stat )
  34.     {
  35.         const double sum = info.second.first;
  36.         const auto max = std::max(info.second.second, 1L); // avoid division by zero
  37.         const auto downloads = sum / max;
  38.         std::cout << "Key: " << info.first << " downloads: " << downloads
  39.               << " (max size: " << max << ")" << std::endl;
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment