diff --git a/tools/Runtime Condenser/Main.cpp b/tools/Runtime Condenser/Main.cpp index c8127867645..6b3075450d2 100644 --- a/tools/Runtime Condenser/Main.cpp +++ b/tools/Runtime Condenser/Main.cpp @@ -34,7 +34,7 @@ #define PROGRESS_FPS 10 #define PROGRESS_BAR_INNER_WIDTH 50 -#define LINEBUFFER (512*1024) //512KiB +#define LINEBUFFER (32*1024) //32KiB using namespace std; @@ -132,28 +132,35 @@ inline void string_send(string * &from, string * &to) { inline void printprogressbar(unsigned short progress /*as percent*/) { double const modifer = 100.0L/(double)PROGRESS_BAR_INNER_WIDTH; size_t bars = (double)progress/modifer; - cout << "\r[" << string(bars, '=') << ((progress < 100) ? ">" : "") << string(PROGRESS_BAR_INNER_WIDTH-(bars+((progress < 100) ? 1 : 0)), ' ') << "] " << progress << "%"; - cout.flush(); + cerr << "\r[" << string(bars, '=') << ((progress < 100) ? ">" : "") << string(PROGRESS_BAR_INNER_WIDTH-(bars+((progress < 100) ? 1 : 0)), ' ') << "] " << progress << "%"; + cerr.flush(); } -bool readFromFile() { +bool readFromFile(bool isstdin) { //Open file to read - FILE * inputFile = fopen("Input.txt", "r"); + FILE * inputFile = stdin; + if (!isstdin) + inputFile = fopen("Input.txt", "r"); if (ferror(inputFile)) return false; - - fseek(inputFile, 0, SEEK_END); - long long fileLength = ftell(inputFile); - fseek(inputFile, 0, SEEK_SET); - clock_t nextupdate = clock(); + long long fileLength = 0; + clock_t nextupdate = 0; + if (!isstdin) { + fseek(inputFile, 0, SEEK_END); + fileLength = ftell(inputFile); + fseek(inputFile, 0, SEEK_SET); + nextupdate = clock(); + } + if (feof(inputFile)) return false; //empty file do { //Update our lines forward_progress(inputFile); //progress bar - if (clock() >= nextupdate) { + + if (!isstdin && clock() >= nextupdate) { int dProgress = (int)(((long double)ftell(inputFile) / (long double)fileLength) * 100.0L); printprogressbar(dProgress); nextupdate = clock() + (CLOCKS_PER_SEC/PROGRESS_FPS); @@ -251,10 +258,12 @@ bool readFromFile() { } } } while (!feof(inputFile) || !endofbuffer); //Until end of file - printprogressbar(100); - cout << endl; + if (!isstdin) + printprogressbar(100); + cerr << endl; return true; } + bool runtimeComp(const runtime &a, const runtime &b) { return a.count > b.count; } @@ -262,25 +271,30 @@ bool runtimeComp(const runtime &a, const runtime &b) { bool hardDelComp(const harddel &a, const harddel &b) { return a.count > b.count; } -bool writeToFile() { + +bool writeToFile(bool usestdio) { //Open and clear the file - ofstream outputFile("Output.txt", ios::trunc); + ostream * output = &cout; + ofstream * outputFile; + if (!usestdio) + output = outputFile = new ofstream("Output.txt", ios::trunc); - if(outputFile.is_open()) { - outputFile << "Note: The source file, src and usr are all from the FIRST of the identical runtimes. Everything else is cropped.\n\n"; + + if(usestdio || outputFile->is_open()) { + *output << "Note: The source file, src and usr are all from the FIRST of the identical runtimes. Everything else is cropped.\n\n"; if(storedInfiniteLoop.size() > 0) - outputFile << "Total unique infinite loops: " << storedInfiniteLoop.size() << endl; + *output << "Total unique infinite loops: " << storedInfiniteLoop.size() << endl; if(totalInfiniteLoops > 0) - outputFile << "Total infinite loops: " << totalInfiniteLoops << endl << endl; + *output << "Total infinite loops: " << totalInfiniteLoops << endl << endl; - outputFile << "Total unique runtimes: " << storedRuntime.size() << endl; - outputFile << "Total runtimes: " << totalRuntimes << endl << endl; + *output << "Total unique runtimes: " << storedRuntime.size() << endl; + *output << "Total runtimes: " << totalRuntimes << endl << endl; if(storedHardDel.size() > 0) - outputFile << "Total unique hard deletions: " << storedHardDel.size() << endl; + *output << "Total unique hard deletions: " << storedHardDel.size() << endl; if(totalHardDels > 0) - outputFile << "Total hard deletions: " << totalHardDels << endl << endl; + *output << "Total hard deletions: " << totalHardDels << endl << endl; //If we have infinite loops, display them first. @@ -291,28 +305,28 @@ bool writeToFile() { infiniteLoops.push_back(it->second); storedInfiniteLoop.clear(); sort(infiniteLoops.begin(), infiniteLoops.end(), runtimeComp); - outputFile << "** Infinite loops **"; + *output << "** Infinite loops **"; for (int i=0; i < infiniteLoops.size(); i++) { runtime* R = &infiniteLoops[i]; - outputFile << endl << endl << "The following infinite loop has occurred " << R->count << " time(s).\n"; - outputFile << R->text << endl; + *output << endl << endl << "The following infinite loop has occurred " << R->count << " time(s).\n"; + *output << R->text << endl; if(R->proc.length()) - outputFile << R->proc << endl; + *output << R->proc << endl; if(R->source.length()) - outputFile << R->source << endl; + *output << R->source << endl; if(R->usr.length()) - outputFile << R->usr << endl; + *output << R->usr << endl; if(R->src.length()) - outputFile << R->src << endl; + *output << R->src << endl; if(R->loc.length()) - outputFile << R->loc << endl; + *output << R->loc << endl; } - outputFile << endl << endl; //For spacing + *output << endl << endl; //For spacing } //Do runtimes next - outputFile << "** Runtimes **"; + *output << "** Runtimes **"; vector runtimes; runtimes.reserve(storedRuntime.size()); for (unordered_map::iterator it=storedRuntime.begin(); it != storedRuntime.end(); it++) @@ -321,24 +335,24 @@ bool writeToFile() { sort(runtimes.begin(), runtimes.end(), runtimeComp); for (int i=0; i < runtimes.size(); i++) { runtime* R = &runtimes[i]; - outputFile << endl << endl << "The following runtime has occurred " << R->count << " time(s).\n"; - outputFile << R->text << endl; + *output << endl << endl << "The following runtime has occurred " << R->count << " time(s).\n"; + *output << R->text << endl; if(R->proc.length()) - outputFile << R->proc << endl; + *output << R->proc << endl; if(R->source.length()) - outputFile << R->source << endl; + *output << R->source << endl; if(R->usr.length()) - outputFile << R->usr << endl; + *output << R->usr << endl; if(R->src.length()) - outputFile << R->src << endl; + *output << R->src << endl; if(R->loc.length()) - outputFile << R->loc << endl; + *output << R->loc << endl; } - outputFile << endl << endl; //For spacing + *output << endl << endl; //For spacing //and finally, hard deletes if(totalHardDels > 0) { - outputFile << endl << "** Hard deletions **"; + *output << endl << "** Hard deletions **"; vector hardDels; hardDels.reserve(storedHardDel.size()); for (unordered_map::iterator it=storedHardDel.begin(); it != storedHardDel.end(); it++) @@ -347,42 +361,55 @@ bool writeToFile() { sort(hardDels.begin(), hardDels.end(), hardDelComp); for(int i=0; i < hardDels.size(); i++) { harddel* D = &hardDels[i]; - outputFile << endl << D->type << " - " << D->count << " time(s).\n"; + *output << endl << D->type << " - " << D->count << " time(s).\n"; } } - outputFile.close(); + if (!usestdio) { + outputFile->close(); + delete outputFile; + } } else { return false; } return true; } -int main() { +int main(int argc, const char * argv[]) { ios_base::sync_with_stdio(false); ios::sync_with_stdio(false); + bool usestdio = false; + if (argc && !strcmp(argv[1], "-s")) + usestdio = true; + char exit; //Used to stop the program from immediately exiting - cout << "Reading input.\n"; - if(readFromFile()) { - cout << "Input read successfully!\n"; + cerr << "Reading input.\n"; + if(readFromFile(usestdio)) { + cerr << "Input read successfully!\n"; } else { - cout << "Input failed to open, shutting down.\n"; - cout << "\nEnter any letter to quit.\n"; - exit = cin.get(); + cerr << "Input failed to open, shutting down.\n"; + if (!usestdio) { + cerr << "\nEnter any letter to quit.\n"; + exit = cin.get(); + } return 1; } - cout << "Writing output.\n"; - if(writeToFile()) { - cout << "Output was successful!\n"; - cout << "\nEnter any letter to quit.\n"; - exit = cin.get(); + cerr << "Writing output.\n"; + if(writeToFile(usestdio)) { + cerr << "Output was successful!\n"; + if (!usestdio) { + cerr << "\nEnter any letter to quit.\n"; + exit = cin.get(); + } return 0; } else { - cout << "The output file could not be opened, shutting down.\n"; - cout << "\nEnter any letter to quit.\n"; - exit = cin.get(); - return 0; + cerr << "The output file could not be opened, shutting down.\n"; + if (!usestdio) { + cerr << "\nEnter any letter to quit.\n"; + exit = cin.get(); + } + return 1; } return 0; diff --git a/tools/Runtime Condenser/RuntimeCondenser.exe b/tools/Runtime Condenser/RuntimeCondenser.exe index e53102f219a..642efd09f03 100644 Binary files a/tools/Runtime Condenser/RuntimeCondenser.exe and b/tools/Runtime Condenser/RuntimeCondenser.exe differ