Project reconstructed && Fix speed issue caused by excessive IO frequency && New information and progress bar output
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
#include "Global_Variables.hpp"
|
||||
|
||||
// 文件列表
|
||||
file_node *file_list_head = nullptr;
|
||||
// 值列表
|
||||
value_node *value_list_head = nullptr;
|
||||
// 起始时间戳
|
||||
double time_begin = 0.0;
|
||||
+266
@@ -0,0 +1,266 @@
|
||||
#include "Tool_Functions.hpp"
|
||||
#include "Global_Variables.hpp"
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include "stdio.h"
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
#include "stdint.h"
|
||||
#include "ctype.h"
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
// 处理指令输入
|
||||
for (int count = 1; count < argc; count++)
|
||||
{
|
||||
// 处理时间戳
|
||||
if (!strcmp(argv[count], "-t"))
|
||||
{
|
||||
// 参数截至
|
||||
if ((count + 1) > argc)
|
||||
continue;
|
||||
|
||||
// 读取时间戳起始
|
||||
count++;
|
||||
sscanf(argv[count], "%lf", &time_begin);
|
||||
continue;
|
||||
}
|
||||
|
||||
// 处理格式串指令
|
||||
{
|
||||
}
|
||||
|
||||
// 其余输入作为文件输入
|
||||
static file_node *target_node = nullptr;
|
||||
if (file_list_head == nullptr)
|
||||
{
|
||||
file_list_head = (file_node *)malloc(sizeof(file_node));
|
||||
file_list_head->file_name_str = nullptr;
|
||||
file_list_head->p_next = nullptr;
|
||||
|
||||
target_node = file_list_head;
|
||||
}
|
||||
else
|
||||
{
|
||||
target_node->p_next = (file_node *)malloc(sizeof(file_node));
|
||||
target_node = target_node->p_next;
|
||||
target_node->file_name_str = nullptr;
|
||||
target_node->p_next = nullptr;
|
||||
}
|
||||
target_node->file_name_str = argv[count];
|
||||
// printf("%s\n", argv[count]);
|
||||
}
|
||||
|
||||
printf("\n\n");
|
||||
|
||||
// 遍历文件
|
||||
file_node *target_file_node = file_list_head;
|
||||
while (target_file_node != nullptr)
|
||||
{
|
||||
double timestamp = time_begin;
|
||||
// 打开相关文件
|
||||
FILE *input_file = nullptr;
|
||||
FILE *output_file = nullptr;
|
||||
input_file = fopen(target_file_node->file_name_str, "rb");
|
||||
{
|
||||
char buffer[100] = {'\0'};
|
||||
sprintf(buffer, "%s%s", target_file_node->file_name_str, ".csv");
|
||||
output_file = fopen(buffer, "wb");
|
||||
}
|
||||
|
||||
if (input_file == nullptr || output_file == nullptr)
|
||||
{
|
||||
target_file_node = target_file_node->p_next;
|
||||
continue;
|
||||
}
|
||||
|
||||
printf("Solving file: \"%s\"\n", target_file_node->file_name_str);
|
||||
printf("----------------------------------------------------------------\n");
|
||||
printf("%-40s %-16s %s\n", "Value Name", "Out Type", "ID");
|
||||
printf("----------------------------------------------------------------\n");
|
||||
|
||||
char segment_buffer[1000] = {'\0'};
|
||||
|
||||
size_t total_line;
|
||||
|
||||
// 跳过非数据行
|
||||
while (true)
|
||||
{
|
||||
f_getline(input_file, segment_buffer, sizeof(segment_buffer));
|
||||
if (segment_buffer[0] == '-')
|
||||
{
|
||||
sscanf(segment_buffer + 1, "%zu", &total_line);
|
||||
f_seek_pre_line_begin(input_file);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 处理首行数据
|
||||
{
|
||||
// 分配数值链表节点
|
||||
value_list_head = (value_node *)malloc(sizeof(value_node));
|
||||
value_list_head->p_next = nullptr;
|
||||
value_list_head->value_type_str = DEFAULT_VALUE_TYPE;
|
||||
value_list_head->raw_value = 0x0000000000000000;
|
||||
memset(value_list_head->value_name_str, '\0', sizeof(value_list_head->value_name_str));
|
||||
|
||||
char value_name_str[100] = {'\0'};
|
||||
char value_str[100] = {'\0'};
|
||||
char time_str[100] = {'\0'};
|
||||
|
||||
f_getline(input_file, segment_buffer, sizeof(segment_buffer));
|
||||
|
||||
// -0000475879| 0| D:FE49E582 snoop 0000 .l\Dip_SignalGet_MotCurrent+0x2 66.903us
|
||||
sscanf(segment_buffer, "%*s%*s%*s%*s%s%s%s", value_str, value_name_str, time_str);
|
||||
// 记录首个节点信息
|
||||
strcpy(value_list_head->value_name_str, value_name_str);
|
||||
value_list_head->raw_value = hex_to_decimal(value_str);
|
||||
}
|
||||
|
||||
// 处理第一个数值循环记录
|
||||
{
|
||||
value_node *target_value_node = value_list_head;
|
||||
while (true)
|
||||
{
|
||||
char value_name_str[100] = {'\0'};
|
||||
char value_str[100] = {'\0'};
|
||||
char time_str[100] = {'\0'};
|
||||
|
||||
// 获取行
|
||||
memset(segment_buffer, '\0', sizeof(segment_buffer));
|
||||
f_getline(input_file, segment_buffer, sizeof(segment_buffer));
|
||||
sscanf(segment_buffer, "%*s%*s%*s%*s%s%s%s", value_str, value_name_str, time_str);
|
||||
|
||||
// 完成回环
|
||||
if (!strcmp(value_name_str, value_list_head->value_name_str))
|
||||
{
|
||||
// 回转到文件的数据行起始位置
|
||||
memset(segment_buffer, '\0', sizeof(segment_buffer));
|
||||
fseek(input_file, 0, SEEK_SET);
|
||||
// 跳过非数据行
|
||||
while (true)
|
||||
{
|
||||
f_getline(input_file, segment_buffer, sizeof(segment_buffer));
|
||||
if (segment_buffer[0] == '-')
|
||||
{
|
||||
sscanf(segment_buffer + 1, "%zu", &total_line);
|
||||
f_seek_pre_line_begin(input_file);
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// 未完成回环,分配数值链表节点
|
||||
target_value_node->p_next = (value_node *)malloc(sizeof(value_node));
|
||||
target_value_node = target_value_node->p_next;
|
||||
target_value_node->p_next = nullptr;
|
||||
target_value_node->value_type_str = DEFAULT_VALUE_TYPE;
|
||||
target_value_node->raw_value = 0x0000000000000000;
|
||||
memset(target_value_node->value_name_str, '\0', sizeof(target_value_node->value_name_str));
|
||||
|
||||
// 记录节点信息
|
||||
strcpy(target_value_node->value_name_str, value_name_str);
|
||||
target_value_node->raw_value = hex_to_decimal(value_str);
|
||||
}
|
||||
}
|
||||
|
||||
// 展示数据类型输出
|
||||
{
|
||||
value_node *target_value_node = value_list_head;
|
||||
size_t value_count = 0;
|
||||
|
||||
while (target_value_node != nullptr)
|
||||
{
|
||||
printf("%-40s %-16s [%d]\n", target_value_node->value_name_str, target_value_node->value_type_str, value_count++);
|
||||
target_value_node = target_value_node->p_next;
|
||||
}
|
||||
}
|
||||
|
||||
// 输出csv抬头部分
|
||||
{
|
||||
value_node *target_value_node = value_list_head;
|
||||
fprintf(output_file, "time");
|
||||
while (target_value_node != nullptr)
|
||||
{
|
||||
fprintf(output_file, ",%s", target_value_node->value_name_str);
|
||||
target_value_node = target_value_node->p_next;
|
||||
}
|
||||
fprintf(output_file, "\n");
|
||||
}
|
||||
|
||||
// 循环处理数据
|
||||
{
|
||||
printf("----------------------------------------------------------------\n");
|
||||
|
||||
memset(segment_buffer, '\0', sizeof(segment_buffer));
|
||||
size_t line_count = -1;
|
||||
while (f_getline(input_file, segment_buffer, sizeof(segment_buffer)) != 0)
|
||||
{
|
||||
char value_name_str[100] = {'\0'};
|
||||
char value_str[100] = {'\0'};
|
||||
char time_str[100] = {'\0'};
|
||||
sscanf(segment_buffer, "%*s%*s%*s%*s%s%s%s", value_str, value_name_str, time_str);
|
||||
|
||||
// 处理时间戳
|
||||
{
|
||||
double time = 0.0;
|
||||
sscanf(time_str, "%lf", &time);
|
||||
|
||||
// 处理时间戳
|
||||
if (strstr(time_str, "us"))
|
||||
timestamp += time / (1000 * 1000);
|
||||
else if (strstr(time_str, "ms"))
|
||||
timestamp += time / (1000);
|
||||
else if (strstr(time_str, "s"))
|
||||
timestamp += time;
|
||||
}
|
||||
|
||||
line_count++;
|
||||
if ((line_count % 10000) == 0)
|
||||
progress_print(line_count, total_line, true);
|
||||
// printf("timestamp: %lfs %s\n", timestamp, segment_buffer);
|
||||
|
||||
// 处理数值链表
|
||||
value_node *target_value_node = value_list_head;
|
||||
fprintf(output_file, "%lf", timestamp);
|
||||
while (target_value_node != nullptr)
|
||||
{
|
||||
// 更新对应的值
|
||||
if (!strcmp(value_name_str, target_value_node->value_name_str))
|
||||
target_value_node->raw_value = hex_to_decimal(value_str);
|
||||
|
||||
// 特殊处理
|
||||
if (strstr(target_value_node->value_name_str, "wmrmeasured_speed"))
|
||||
{
|
||||
float *value = (float *)&(target_value_node->raw_value);
|
||||
fprintf(output_file, ",%f", *value);
|
||||
}
|
||||
else
|
||||
fprintf(output_file, ",%llu", target_value_node->raw_value);
|
||||
|
||||
target_value_node = target_value_node->p_next;
|
||||
}
|
||||
fprintf(output_file, "\n");
|
||||
|
||||
// 清理缓冲区
|
||||
memset(segment_buffer, '\0', sizeof(segment_buffer));
|
||||
}
|
||||
|
||||
progress_print(line_count, total_line, true);
|
||||
printf("\n\n\n");
|
||||
}
|
||||
|
||||
// 清理数据列表
|
||||
{
|
||||
}
|
||||
|
||||
target_file_node = target_file_node->p_next;
|
||||
fclose(input_file);
|
||||
fclose(output_file);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
#include "Tool_Functions.hpp"
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include "string.h"
|
||||
#include "ctype.h"
|
||||
}
|
||||
|
||||
// 行进到上一行起始位置
|
||||
size_t f_seek_pre_line_begin(FILE *file)
|
||||
{
|
||||
int count = 0;
|
||||
|
||||
// 本身就在文件起始位置
|
||||
if (ftell(file) == 0)
|
||||
return 0;
|
||||
// 本身在文件结尾
|
||||
if (fgetc(file) == EOF)
|
||||
{
|
||||
// 移动到之前
|
||||
do
|
||||
{
|
||||
fseek(file, -2, SEEK_CUR);
|
||||
count--;
|
||||
} while (ftell(file) != 0 && fgetc(file) != '\n');
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
// 其它情况
|
||||
|
||||
// 移动到上一行的结尾处(回到本行起始位置)
|
||||
while (ftell(file) != 0 && fgetc(file) != '\n')
|
||||
{
|
||||
fseek(file, -2, SEEK_CUR);
|
||||
count--;
|
||||
}
|
||||
|
||||
// 移动到上上一行的结尾处
|
||||
do
|
||||
{
|
||||
fseek(file, -2, SEEK_CUR);
|
||||
count--;
|
||||
} while (ftell(file) != 0 && fgetc(file) != '\n');
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
// 读取文件一行
|
||||
size_t f_getline(FILE *file, char *buffer, const size_t buffer_len)
|
||||
{
|
||||
if (buffer == nullptr || file == nullptr)
|
||||
return 0;
|
||||
|
||||
memset(buffer, '\0', buffer_len);
|
||||
|
||||
size_t count = 0;
|
||||
|
||||
// 循环读取
|
||||
while (true)
|
||||
{
|
||||
// 超长判定
|
||||
if (count + 1 == buffer_len)
|
||||
return buffer_len;
|
||||
|
||||
// 正常读取
|
||||
char ch = fgetc(file);
|
||||
|
||||
// 仅文件结尾时
|
||||
if (ch == EOF && count == 0)
|
||||
return 0;
|
||||
|
||||
// 成功换行
|
||||
if (ch == '\n')
|
||||
{
|
||||
buffer[count] = '\n';
|
||||
return count + 1;
|
||||
}
|
||||
|
||||
// 没有换行但是遇到了文件结尾
|
||||
if (ch == EOF)
|
||||
{
|
||||
buffer[count] = '\n';
|
||||
return count;
|
||||
}
|
||||
|
||||
// 其它情况下正常复制
|
||||
buffer[count] = ch;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
// 进度条打印
|
||||
void progress_print(size_t completed, size_t total, bool reflush)
|
||||
{
|
||||
|
||||
// 消除单行内容
|
||||
if (reflush)
|
||||
putchar('\r');
|
||||
else
|
||||
putchar('\n');
|
||||
|
||||
double p_percent = 1.0 * completed / total;
|
||||
|
||||
putchar('[');
|
||||
|
||||
// 输出完成部分填充
|
||||
size_t completed_count = (PROGRESS_LENGTH * p_percent) - 1;
|
||||
if (p_percent - (int)(p_percent) > 0.5)
|
||||
completed_count++;
|
||||
|
||||
for (int count = completed_count; count > 0; count--)
|
||||
putchar('=');
|
||||
putchar('>');
|
||||
|
||||
// 填充未完成部分
|
||||
for (int count = PROGRESS_LENGTH - (completed_count + 1); count > 0; count--)
|
||||
putchar(' ');
|
||||
putchar(']');
|
||||
|
||||
// 进度数显
|
||||
printf(" %d/%d (%%%.2lf)", completed, total, p_percent * 100);
|
||||
}
|
||||
|
||||
// 16进制字符串转十进制数值(8字节)
|
||||
uint64_t hex_to_decimal(const char *str)
|
||||
{
|
||||
uint64_t value = 0;
|
||||
|
||||
int len = strlen(str);
|
||||
// 循环读取
|
||||
for (int count = 0; count < len; count++)
|
||||
{
|
||||
value *= 16;
|
||||
if (str[count] >= '0' && str[count] <= '9')
|
||||
value += str[count] - '0';
|
||||
else
|
||||
value += toupper(str[count]) - 'A' + 10;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
Reference in New Issue
Block a user