Skip to content

Instantly share code, notes, and snippets.

@Shayan-To
Last active January 2, 2017 12:18
Show Gist options
  • Save Shayan-To/672c77fbf9811d769d453c8a9b43747e to your computer and use it in GitHub Desktop.
Save Shayan-To/672c77fbf9811d769d453c8a9b43747e to your computer and use it in GitHub Desktop.
Simple C++ code to replay the output of script using the timing file
#include <iostream>
#include <fstream>
#include <thread>
#include <chrono>
#include <string>
#include <algorithm>
using namespace std;
int main(int argc, char **argv)
{
if (argc != 3 && argc != 4)
{
cerr << "Invalid arguments.\nUsage: scriptout file timing [speed_multiplier]\n";
exit(1);
}
ifstream input(argv[1]);
ifstream timing(argv[2]);
double multiplier = 1;
if (argc > 3)
{
multiplier = stod(argv[3]);
}
const int buffer_size = 2;
char buffer[buffer_size];
while (true)
{
double wait;
int count;
timing >> wait;
if (timing.eof())
{
break;
}
timing >> count;
this_thread::sleep_for(chrono::milliseconds(int(wait * 1000 / multiplier)));
while (count != 0)
{
if (input.eof())
{
cerr << "Invalid timing file.\n";
exit(1);
}
input.read(buffer, min(count, buffer_size));
int n = input.gcount();
count -= n;
cout.write(buffer, n);
cout.flush();
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment