From ed6a7717ab37b92c0727ce93abc2a0c799f8326b Mon Sep 17 00:00:00 2001 From: LuChiChick <1084116302@qq.com> Date: Tue, 5 Aug 2025 18:45:45 +0800 Subject: [PATCH] initial commit & basic workable ver --- DLT_Splitter.cpp | 213 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 213 insertions(+) create mode 100644 DLT_Splitter.cpp diff --git a/DLT_Splitter.cpp b/DLT_Splitter.cpp new file mode 100644 index 0000000..513991a --- /dev/null +++ b/DLT_Splitter.cpp @@ -0,0 +1,213 @@ +#include "iostream" +extern "C" +{ +#include "stdio.h" +#include "string.h" +#include "stdlib.h" +} + +#define SEGMENT_BUFF_LENTH 1024 * 20 // 20KB段缓冲区 +#define BYTE_BEGIN 4 // Payload起始位置字节计数 + +// 文件链表节点 +typedef struct file_node_struct +{ + const char *file_name_str = nullptr; + file_node_struct *p_next = nullptr; +} file_node; + +// 读取文件一行 +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++; + } +} + +// 文件列表 +file_node *file_list_head = nullptr; + +int main(int argc, char *argv[]) +{ + // 处理指令输入 + for (int count = 1; count < argc; count++) + { + + 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]; + } + + // 遍历文件 + file_node *target_file_node = file_list_head; + while (target_file_node != nullptr) + { + // 打开相关文件 + FILE *input_file = nullptr; + FILE *output_file = nullptr; + input_file = fopen(target_file_node->file_name_str, "rb"); + if (input_file == nullptr) + printf("[-ERROR-] File \"%s\" open failed.\n", target_file_node->file_name_str); + else + { + // 拼接输出文件名及打开对应文件 + char *buffer = (char *)malloc(strlen(target_file_node->file_name_str) + 100); + + const char *file_name_do_dir = target_file_node->file_name_str + strlen(target_file_node->file_name_str); + while (file_name_do_dir != target_file_node->file_name_str) + { + if (*file_name_do_dir == '\\' || *file_name_do_dir == '/') + { + file_name_do_dir++; + break; + } + file_name_do_dir--; + } + for (int count = 0; count < file_name_do_dir - target_file_node->file_name_str; count++) + buffer[count] = target_file_node->file_name_str[count]; + + sprintf(buffer + (file_name_do_dir - target_file_node->file_name_str), "[Split]%s", file_name_do_dir); + output_file = fopen(buffer, "wb"); + + if (output_file == nullptr) + printf("[-ERROR-] File \"%s\" create failed.\n", buffer); + + free(buffer); + } + + // 处理文件 + if (input_file != nullptr && output_file != nullptr) + { + char segment_buff[SEGMENT_BUFF_LENTH]; + size_t byte_count_max = 0; + // 求解Payload最长有多少个字节 + { + // 忽略标题 + f_getline(input_file, segment_buff, sizeof(segment_buff)); + // 循环处理每行 + while (f_getline(input_file, segment_buff, sizeof(segment_buff)) != 0) + { + // 计数直到数据起始 + const char *p_data_begin = segment_buff + strlen(segment_buff); + size_t byte_count = 0; + while (*(p_data_begin - 1) != '|') + { + if (*p_data_begin == ' ') + byte_count++; + p_data_begin--; + } + byte_count++; + + if (byte_count > byte_count_max) + byte_count_max = byte_count; // 记录最大值 + } + + // 回到起始位置 + fseek(input_file, 0, SEEK_SET); + } + // 处理标题部分 + { + f_getline(input_file, segment_buff, sizeof(segment_buff)); + const char *p_char = segment_buff; + const char *p_end = segment_buff + strlen(segment_buff); + while (*(p_end - 1) != '\"') + p_end--; + + for (int count = p_end - segment_buff; count > 0; count--) + fputc(*p_char++, output_file); + + // 打印Byte编号 + for (int count = 0; count < byte_count_max; count++) + fprintf(output_file, ",\"B%u\"", count + BYTE_BEGIN); + fprintf(output_file, "\r\n"); + } + int line_count; + // 循环处理每行 + while (f_getline(input_file, segment_buff, sizeof(segment_buff)) != 0) + { + // 找到数据起始 + const char *p_data_begin = segment_buff + strlen(segment_buff); + while (*(p_data_begin - 1) != '|') + p_data_begin--; + + // 输出分段前内容 + for (const char *p_target = segment_buff; p_target != p_data_begin; p_target++) + fputc(*p_target, output_file); + // 隔断 + fprintf(output_file, "\",\""); + + // 输出剩余Payload内容 + for (int count = strlen(p_data_begin); count > 0; count--) + { + // 按空格分列 + if (*p_data_begin == ' ') + fprintf(output_file, "\",\""); + else + fputc(*p_data_begin, output_file); + + p_data_begin++; + } + + // 记录行号 + sscanf(segment_buff + 1, "%d", &line_count); + } + + printf("[- OK -] [Total Line: %d] \"%s\"\n", line_count, target_file_node->file_name_str); + } + + // 关闭文件及切换下一个文件节点 + target_file_node = target_file_node->p_next; + fclose(input_file); + fclose(output_file); + } +} \ No newline at end of file