Last active
January 2, 2017 12:18
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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