Refactor console handling and add support for processing all .datalog files
This commit is contained in:
+97
-11
@@ -10,12 +10,35 @@
|
||||
#include <iomanip>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
#include <filesystem>
|
||||
#include <future>
|
||||
#include <windows.h>
|
||||
#include <mutex>
|
||||
|
||||
|
||||
using namespace std;
|
||||
using namespace std::chrono;
|
||||
std::mutex cout_mutex;
|
||||
|
||||
const int BUFFER_SIZE = 2048;
|
||||
|
||||
// Helper function for Windows console cursor positioning
|
||||
inline void set_cursor_position(int row, int col) {
|
||||
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
COORD pos;
|
||||
pos.X = col - 1; // 0-based
|
||||
pos.Y = row - 1; // 0-based
|
||||
SetConsoleCursorPosition(hConsole, pos);
|
||||
}
|
||||
|
||||
int get_console_width() {
|
||||
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
||||
int columns = 80; // default
|
||||
if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
|
||||
columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
|
||||
}
|
||||
return columns;
|
||||
}
|
||||
|
||||
inline string join(vector<string> const &strings, string delim)
|
||||
{
|
||||
@@ -47,6 +70,8 @@ inline vector<string> tokenize(string s, string delimiter = " ")
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
system("cls");
|
||||
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(NULL);
|
||||
|
||||
@@ -58,11 +83,12 @@ int main(int argc, char** argv) {
|
||||
int append = 1;
|
||||
int crush_all = 0;
|
||||
int debug_mode = 0;
|
||||
int allfiles = 0;
|
||||
vector<string> testnames;
|
||||
|
||||
int c;
|
||||
bool no_opt = true;
|
||||
while ((c = getopt(argc, argv, "hf:rdt:x")) != -1) {
|
||||
while ((c = getopt(argc, argv, "hf:ardt:x")) != -1) {
|
||||
switch (c) {
|
||||
case 'h':
|
||||
cout << "Usage: " << argv[0] << " -f <filename> [-r <truncate>] [-t <testnames>] [-h]\n";
|
||||
@@ -76,6 +102,9 @@ int main(int argc, char** argv) {
|
||||
testnames.push_back(argv[optind] );
|
||||
}
|
||||
break;
|
||||
case 'a':
|
||||
allfiles = 1;
|
||||
break;
|
||||
case 'r':
|
||||
append = 0;
|
||||
break;
|
||||
@@ -101,11 +130,42 @@ int main(int argc, char** argv) {
|
||||
cout << "\t\t-f => Specify .datalog file name\n";
|
||||
cout << "\t\t-t => Specify tests to be extracted seprated by space\n";
|
||||
cout << "\t\t-r => Truncate or replace generated files (default is append)\n";
|
||||
cout << "\t\t-a => Process all .datalog files in current dir, overrides files specified in -f\n";
|
||||
cout << "\t\t-x => Exract all tests, overrides tests specified in -t\n\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
vector<string> datalog_files;
|
||||
|
||||
if (allfiles == 1 && filename == "") {
|
||||
namespace fs = std::filesystem;
|
||||
for (const auto& entry : fs::directory_iterator(fs::current_path())) {
|
||||
if (entry.is_regular_file() && entry.path().extension() == ".datalog") {
|
||||
datalog_files.push_back(entry.path().string());
|
||||
}
|
||||
}
|
||||
if (datalog_files.empty()) {
|
||||
cerr << "No .datalog files found in current directory!\n";
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
if (filename == "") {
|
||||
return 0;
|
||||
cerr << "No .datalog file specified!\n";
|
||||
return 1;
|
||||
}
|
||||
datalog_files.push_back(filename);
|
||||
}
|
||||
// You can now loop over datalog_files for processing
|
||||
vector<std::future<void>> futures;
|
||||
int file_num = 1;
|
||||
auto time_start = steady_clock::now();
|
||||
for (const auto& filename : datalog_files) {
|
||||
int my_line = ++file_num;
|
||||
|
||||
futures.push_back(std::async(std::launch::async, [&, filename, my_line]() {
|
||||
|
||||
if (filename == "") {
|
||||
return ;
|
||||
}
|
||||
|
||||
unordered_map<string, int> has_header;
|
||||
@@ -136,7 +196,7 @@ int main(int argc, char** argv) {
|
||||
|
||||
if (!file_input) {
|
||||
cerr << "Could not open input file!\n";
|
||||
return 1;
|
||||
return ;
|
||||
}
|
||||
|
||||
|
||||
@@ -182,7 +242,7 @@ int main(int argc, char** argv) {
|
||||
string line;
|
||||
|
||||
|
||||
auto time_start = steady_clock::now();
|
||||
|
||||
long line_pos = 1;
|
||||
long max_line_pos = m_vec_file.size();
|
||||
|
||||
@@ -205,15 +265,35 @@ int main(int argc, char** argv) {
|
||||
// std::cout << m_vec_file[part_size] << "\n";
|
||||
for (unsigned int ctr = 0; ctr < m_vec_file.size(); ++ctr) {
|
||||
line = m_vec_file[ctr];
|
||||
if ((int(line_pos) % 500 == 0) || (line_pos == max_line_pos)) {
|
||||
std::cout << "\r File: " << m_filename << " [" << fixed << setprecision(3) << (100* (float)line_pos / (float)max_line_pos) << "%] ";
|
||||
if ((int(line_pos) % 100 == 0) || (line_pos == max_line_pos)) {
|
||||
std::lock_guard<std::mutex> lock(cout_mutex); // Lock before updating console
|
||||
|
||||
int width = get_console_width();
|
||||
std::ostringstream oss;
|
||||
oss << " File: " << m_filename << " ";
|
||||
std::string left = oss.str();
|
||||
|
||||
std::ostringstream progress_oss;
|
||||
progress_oss << "[" << std::fixed << std::setprecision(3)
|
||||
<< (100 * (float)line_pos / (float)max_line_pos) << "%]";
|
||||
std::string right = progress_oss.str();
|
||||
|
||||
int padding = width - (int)left.length() - (int)right.length();
|
||||
if (padding < 0) padding = 0;
|
||||
|
||||
set_cursor_position(my_line, 1);
|
||||
std::cout << left << std::string(padding, ' ') << right << std::flush;
|
||||
}
|
||||
vector<string> split_line;
|
||||
string match1 = "99";
|
||||
string match2 = "99";
|
||||
|
||||
// istringstream iss(line);
|
||||
// string word;
|
||||
// while (iss >> word) split_line.push_back(word);
|
||||
split_line = tokenize(line);
|
||||
|
||||
|
||||
if (split_line.size()>0) {
|
||||
match1 = split_line.at(0);
|
||||
|
||||
@@ -369,10 +449,10 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
}
|
||||
|
||||
FILE_HANDLE[test].seekp(0, ios::end);
|
||||
if (FILE_HANDLE[test].tellp() == 0) {
|
||||
FILE_HANDLE[test] << "productname,programrev,operatorlotname,lotname,wafer,x,y,testsite,temperature,testname,vcc,vccio,vccaux,testdoublevalue,testintvalue,testpass,pattern,state,intval,condition1,condition2,condition3,text,bin,binname\n";
|
||||
has_header[test] = 1;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
for (auto &m_arr : hash_buff[test]) {
|
||||
@@ -390,9 +470,7 @@ int main(int argc, char** argv) {
|
||||
|
||||
++line_pos;
|
||||
}
|
||||
auto time_end = steady_clock::now();
|
||||
|
||||
cout << "\033[1;32mFinished: \033[0m" << duration_cast<milliseconds>(time_end - time_start).count() << " ms\n";
|
||||
|
||||
for ( const auto &my_pair : hash_buff ) {
|
||||
string test = my_pair.first;
|
||||
@@ -405,6 +483,14 @@ int main(int argc, char** argv) {
|
||||
|
||||
FILE_HANDLE[test].close();
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
|
||||
for (auto& fut : futures) fut.get();
|
||||
auto time_end = steady_clock::now();
|
||||
cout << "\n\n\033[1;32m Finished: \033[0m" << duration_cast<milliseconds>(time_end - time_start).count() << " ms\n";
|
||||
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user