Achieved output of different format types && Modified some functions

This commit is contained in:
LuChiChick 2025-10-11 18:43:37 +08:00
parent 0367071115
commit 700f969892
7 changed files with 192 additions and 30 deletions

View File

@ -5,6 +5,10 @@
#define PROGRESS_LENGTH 29 #define PROGRESS_LENGTH 29
// 默认数值类型 // 默认数值类型
#define DEFAULT_VALUE_TYPE "uint64_t" #define DEFAULT_VALUE_TYPE "uint64_t (DEF)"
// 名称字符串最大长度
#define VARIABLE_NAME_STR_LENGTH_MAX 100
// 段缓冲区长度
#define SEGMENT_BUFF_LENGTH 5000
#endif #endif

View File

@ -7,6 +7,8 @@
extern file_node *file_list_head; extern file_node *file_list_head;
// 值列表 // 值列表
extern value_node *value_list_head; extern value_node *value_list_head;
// 类型列表
extern format_list_node *format_list_head;
// 起始时间戳 // 起始时间戳
extern double time_begin; extern double time_begin;

View File

@ -19,7 +19,13 @@ size_t f_getline(FILE *file, char *buffer, const size_t buffer_len);
// 进度条打印 // 进度条打印
void progress_print(size_t completed, size_t total, bool reflush); void progress_print(size_t completed, size_t total, bool reflush);
// 16进制字符串转十进制数值8字节 // 16进制字符串转有效无符号整数8字节
uint64_t hex_to_decimal(const char *str); uint64_t hex_to_value(const char *str);
// 判定字符串是否全部为有效数字
bool is_full_num(const char *str);
// 十进制字符串转有效无符号整数8字节
uint64_t str_to_value(const char *str);
#endif #endif

View File

@ -18,11 +18,20 @@ typedef struct file_node_struct
// 数据列表 // 数据列表
typedef struct value_node_struct typedef struct value_node_struct
{ {
char value_name_str[100] = {'\0'}; char value_name_str[VARIABLE_NAME_STR_LENGTH_MAX] = {'\0'};
const char *value_type_str = "uint64_t"; const char *value_type_str = DEFAULT_VALUE_TYPE;
uint64_t raw_value = 0x0000000000000000; uint64_t raw_value = 0x0000000000000000;
value_node_struct *p_next; value_node_struct *p_next;
} value_node; } value_node;
// 格式定义列表
typedef struct format_list_node_struct
{
char target_value_name_str[VARIABLE_NAME_STR_LENGTH_MAX] = {'\0'}; // 目标变量名
int target_numID = -1; // 目标数字ID
const char *value_type_str = nullptr; // 目标类型
format_list_node_struct *p_next;
} format_list_node;
// 数据类型 // 数据类型
#endif #endif

View File

@ -4,5 +4,7 @@
file_node *file_list_head = nullptr; file_node *file_list_head = nullptr;
// 值列表 // 值列表
value_node *value_list_head = nullptr; value_node *value_list_head = nullptr;
// 类型列表
format_list_node *format_list_head = nullptr;
// 起始时间戳 // 起始时间戳
double time_begin = 0.0; double time_begin = 0.0;

View File

@ -36,7 +36,7 @@ int main(int argc, char *argv[])
// 读取时间戳起始 // 读取时间戳起始
count++; count++;
sscanf(argv[count], "%lf", &time_begin); sscanf(argv[count], "%lf", &time_begin);
printf("Timestamp begin: %lf(s).", time_begin); printf("Timestamp begin: %lf(s).\n", time_begin);
continue; continue;
} }
@ -86,6 +86,55 @@ int main(int argc, char *argv[])
continue; continue;
} }
// 添加到类型列表中
{
char *p_param = argv[count + 1];
int param_len = strlen(argv[count + 1]);
format_list_node *target_format_node = nullptr;
while (p_param - argv[count + 1] < param_len)
{
// 分配空间
if (format_list_head == nullptr)
{
format_list_head = (format_list_node *)malloc(sizeof(format_list_node));
format_list_head->p_next = nullptr;
format_list_head->target_numID = -1;
memset(format_list_head->target_value_name_str, '\0', sizeof(format_list_head->target_value_name_str));
format_list_head->value_type_str = DEFAULT_VALUE_TYPE;
target_format_node = format_list_head;
}
else
{
target_format_node->p_next = (format_list_node *)malloc(sizeof(format_list_node));
target_format_node = target_format_node->p_next;
target_format_node->p_next = nullptr;
target_format_node->target_numID = -1;
memset(target_format_node->target_value_name_str, '\0', sizeof(target_format_node->target_value_name_str));
target_format_node->value_type_str = DEFAULT_VALUE_TYPE;
}
char value_identification[VARIABLE_NAME_STR_LENGTH_MAX] = {'\0'};
size_t id_count = 0;
while (p_param - argv[count + 1] < param_len)
{
value_identification[id_count++] = *p_param;
if (p_param[1] == ',')
{
p_param += 2;
break;
}
p_param++;
}
// 判定名称orID顺序
if (is_full_num(value_identification))
target_format_node->target_numID = str_to_value(value_identification);
else
sprintf(target_format_node->target_value_name_str, value_identification);
target_format_node->value_type_str = argv[count + 2];
}
}
count += 2; count += 2;
continue; continue;
} }
@ -172,7 +221,7 @@ int main(int argc, char *argv[])
printf("%-40s %-16s %s\n", "Value Name", "Out Type", "ID"); printf("%-40s %-16s %s\n", "Value Name", "Out Type", "ID");
printf("----------------------------------------------------------------\n"); printf("----------------------------------------------------------------\n");
char segment_buffer[1000] = {'\0'}; char segment_buffer[SEGMENT_BUFF_LENGTH] = {'\0'};
size_t total_line; size_t total_line;
@ -197,7 +246,7 @@ int main(int argc, char *argv[])
value_list_head->raw_value = 0x0000000000000000; value_list_head->raw_value = 0x0000000000000000;
memset(value_list_head->value_name_str, '\0', sizeof(value_list_head->value_name_str)); memset(value_list_head->value_name_str, '\0', sizeof(value_list_head->value_name_str));
char value_name_str[100] = {'\0'}; char value_name_str[VARIABLE_NAME_STR_LENGTH_MAX] = {'\0'};
char value_str[100] = {'\0'}; char value_str[100] = {'\0'};
char time_str[100] = {'\0'}; char time_str[100] = {'\0'};
@ -207,7 +256,7 @@ int main(int argc, char *argv[])
sscanf(segment_buffer, "%*s%*s%*s%*s%s%s%s", value_str, value_name_str, time_str); 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); strcpy(value_list_head->value_name_str, value_name_str);
value_list_head->raw_value = hex_to_decimal(value_str); value_list_head->raw_value = hex_to_value(value_str);
} }
// 处理第一个数值循环记录 // 处理第一个数值循环记录
@ -215,7 +264,7 @@ int main(int argc, char *argv[])
value_node *target_value_node = value_list_head; value_node *target_value_node = value_list_head;
while (true) while (true)
{ {
char value_name_str[100] = {'\0'}; char value_name_str[VARIABLE_NAME_STR_LENGTH_MAX] = {'\0'};
char value_str[100] = {'\0'}; char value_str[100] = {'\0'};
char time_str[100] = {'\0'}; char time_str[100] = {'\0'};
@ -254,18 +303,52 @@ int main(int argc, char *argv[])
// 记录节点信息 // 记录节点信息
strcpy(target_value_node->value_name_str, value_name_str); strcpy(target_value_node->value_name_str, value_name_str);
target_value_node->raw_value = hex_to_decimal(value_str); target_value_node->raw_value = hex_to_value(value_str);
} }
} }
// 展示数据类型输出 // 展示数据类型输出及类型匹配
{ {
value_node *target_value_node = value_list_head; value_node *target_value_node = value_list_head;
size_t value_count = 0; int value_count = 0;
while (target_value_node != nullptr) while (target_value_node != nullptr)
{ {
printf("%-40s %-16s [%d]\n", target_value_node->value_name_str, target_value_node->value_type_str, value_count++); // 查找是否由对应的类型列表
{
format_list_node *target_format_node = format_list_head;
while (target_format_node != nullptr)
{
// 检查对应的名称(宽松匹配)
if ((target_format_node->target_value_name_str[0] != '\0') && (strstr(target_value_node->value_name_str, target_format_node->target_value_name_str) != nullptr))
{
target_value_node->value_type_str = target_format_node->value_type_str;
break;
}
else if (value_count == target_format_node->target_numID)
{
// 数字ID仅生效一次随后记录名称以适配批量处理文件时指定格式
sprintf(target_format_node->target_value_name_str, target_value_node->value_name_str);
target_format_node->target_numID = -1;
target_value_node->value_type_str = target_format_node->value_type_str;
break;
}
target_format_node = target_format_node->p_next;
}
// 一些预先标识的变量
if (strstr(target_value_node->value_name_str, "wmrmeasured_speed") ||
strstr(target_value_node->value_name_str, "wmrcontrol_signal") ||
strstr(target_value_node->value_name_str, "tagetSpeed") ||
strstr(target_value_node->value_name_str, "WinUp_PID") ||
strstr(target_value_node->value_name_str, "WinDw_PID"))
{
target_value_node->value_type_str = "float";
}
}
printf("%-40s %-16s [%02d]\n", target_value_node->value_name_str, target_value_node->value_type_str, value_count++);
target_value_node = target_value_node->p_next; target_value_node = target_value_node->p_next;
} }
} }
@ -290,7 +373,7 @@ int main(int argc, char *argv[])
size_t line_count = -1; size_t line_count = -1;
while (f_getline(input_file, segment_buffer, sizeof(segment_buffer)) != 0) while (f_getline(input_file, segment_buffer, sizeof(segment_buffer)) != 0)
{ {
char value_name_str[100] = {'\0'}; char value_name_str[VARIABLE_NAME_STR_LENGTH_MAX] = {'\0'};
char value_str[100] = {'\0'}; char value_str[100] = {'\0'};
char time_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); sscanf(segment_buffer, "%*s%*s%*s%*s%s%s%s", value_str, value_name_str, time_str);
@ -321,21 +404,51 @@ int main(int argc, char *argv[])
{ {
// 更新对应的值 // 更新对应的值
if (!strcmp(value_name_str, target_value_node->value_name_str)) if (!strcmp(value_name_str, target_value_node->value_name_str))
target_value_node->raw_value = hex_to_decimal(value_str); target_value_node->raw_value = hex_to_value(value_str);
// 特殊处理 // 按类型输出值
if (strstr(target_value_node->value_name_str, "wmrmeasured_speed") || {
strstr(target_value_node->value_name_str, "wmrcontrol_signal") || // int8_t sbyte
strstr(target_value_node->value_name_str, "tagetSpeed") || if (!strcmp(target_value_node->value_type_str, "sbyte") ||
strstr(target_value_node->value_name_str, "WinUp_PID") || !strcmp(target_value_node->value_type_str, "int8_t"))
strstr(target_value_node->value_name_str, "WinDw_PID")) {
int8_t *value = (int8_t *)&(target_value_node->raw_value);
fprintf(output_file, ",%d", *value);
}
// int16_t
else if (!strcmp(target_value_node->value_type_str, "int16_t"))
{
int16_t *value = (int16_t *)&(target_value_node->raw_value);
fprintf(output_file, ",%d", *value);
}
// int32_t int
else if (!strcmp(target_value_node->value_type_str, "int32_t"))
{
int32_t *value = (int32_t *)&(target_value_node->raw_value);
fprintf(output_file, ",%d", *value);
}
// int64_t
else if (!strcmp(target_value_node->value_type_str, "int64_t"))
{
int64_t *value = (int64_t *)&(target_value_node->raw_value);
fprintf(output_file, ",%lld", *value);
}
// float 类型
else if (!strcmp(target_value_node->value_type_str, "float"))
{ {
float *value = (float *)&(target_value_node->raw_value); float *value = (float *)&(target_value_node->raw_value);
fprintf(output_file, ",%f", *value); fprintf(output_file, ",%f", *value);
} }
// double 类型
else if (!strcmp(target_value_node->value_type_str, "float"))
{
double *value = (double *)&(target_value_node->raw_value);
fprintf(output_file, ",%lf", *value);
}
// 其它类型全部按照uint输出
else else
fprintf(output_file, ",%llu", target_value_node->raw_value); fprintf(output_file, ",%llu", target_value_node->raw_value);
}
target_value_node = target_value_node->p_next; target_value_node = target_value_node->p_next;
} }
fprintf(output_file, "\n"); fprintf(output_file, "\n");

View File

@ -122,8 +122,8 @@ void progress_print(size_t completed, size_t total, bool reflush)
printf(" %d/%d (%%%.2lf)", completed, total, p_percent * 100); printf(" %d/%d (%%%.2lf)", completed, total, p_percent * 100);
} }
// 16进制字符串转十进制数值8字节 // 16进制字符串转有效无符号整数8字节
uint64_t hex_to_decimal(const char *str) uint64_t hex_to_value(const char *str)
{ {
uint64_t value = 0; uint64_t value = 0;
@ -139,3 +139,29 @@ uint64_t hex_to_decimal(const char *str)
} }
return value; return value;
} }
// 判定字符串是否全部为有效数字
bool is_full_num(const char *str)
{
int len = strlen(str);
for (int count = 0; count < len; count++)
if (!isdigit(str[count]))
return false;
return true;
}
// 十进制字符串转有效无符号整数8字节
uint64_t str_to_value(const char *str)
{
uint64_t value = 0;
int len = strlen(str);
// 循环读取
for (int count = 0; count < len; count++)
{
value *= 10;
if (str[count] >= '0' && str[count] <= '9')
value += str[count] - '0';
}
return value;
}