Advertisement
Glenpl

Untitled

Aug 20th, 2015
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <regex>
  3. #include <vector>
  4. #include <map>
  5. #include <string>
  6. #include <fstream>
  7. #include <algorithm>
  8.  
  9. class TimeLabel
  10. {
  11.     int h, m, s;
  12. public:
  13.     TimeLabel (std::string l)
  14.     {
  15.         //hh:mm:ss
  16.         //01:34:67
  17.         h = std::stoi(l.substr(0, 2));
  18.         m = std::stoi(l.substr(3, 2));
  19.         s = std::stoi(l.substr(6, 2));
  20.     }
  21.     std::string getTime()
  22.     {
  23.         return (std::to_string(h).size() == 2 ? std::to_string(h) : "0" + std::to_string(h) ) + ":" +
  24.                 (std::to_string(m).size() == 2 ? std::to_string(m) : "0" + std::to_string(m) ) + ":" +
  25.                 (std::to_string(s).size() == 2 ? std::to_string(s) : "0" + std::to_string(s) );
  26.     }
  27.     std::string addSeconds(int sec)
  28.     {
  29.         s += sec;
  30.         m += s/60;
  31.         s %= 60;
  32.         h += m/60;
  33.         m %= 60;
  34.         return this->getTime();
  35.     }
  36. };
  37.  
  38. int main()
  39. {
  40.     std::string line;
  41.     std::string subtitles_all, subtitles_all_copy;
  42.     std::vector<std::string> matches;
  43.     std::fstream file;
  44.     file.open("23.txt", std::ios_base::in);
  45.  
  46.     while(std::getline(file, line))
  47.         subtitles_all += line + "\n";
  48.     subtitles_all_copy = subtitles_all;
  49.  
  50.     std::smatch m;
  51.     while (std::regex_search (subtitles_all_copy, m, std::regex("[0-9]{2}:[0-9]{2}:[0-9]{2}")))
  52.     {
  53.         for (auto x : m)
  54.             matches.push_back(x);
  55.         subtitles_all_copy = m.suffix().str();
  56.     }
  57.  
  58.     std::reverse(matches.begin(), matches.end());
  59.     std::cout << "Podaj o ile sekund przesunac (o ile sekund pozniej maja pokazac sie napisy):\n>";
  60.     int p;
  61.     std::cin >> p;
  62.  
  63.     for(auto mat : matches)
  64.     {
  65.         TimeLabel tl(mat);
  66.         subtitles_all = std::regex_replace(subtitles_all, std::regex(mat), tl.addSeconds(p));
  67.     }
  68.  
  69.     file.close();
  70.  
  71.     file.open("23.txt", std::ios_base::out | std::ios_base::trunc);
  72.     file << subtitles_all;
  73.     file.close();
  74.  
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement