From 7033187abaf42772097377c0a1ffc2cd4cefdada Mon Sep 17 00:00:00 2001 From: JSDurand Date: Fri, 4 Aug 2023 10:12:04 +0800 Subject: minor adjustments Not bug deals but adjustments of details. --- src/helper.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) (limited to 'src/helper.c') diff --git a/src/helper.c b/src/helper.c index d52fa5f..3b02c81 100644 --- a/src/helper.c +++ b/src/helper.c @@ -1,3 +1,4 @@ +#include #include "helper.h" #include "big_endian.h" @@ -82,3 +83,75 @@ print_node(struct CForest *forest, uint64_t node) print_forest_node(forest, node_ptr); } + +void print_forest_file(char *filename, char *output_filename) +{ + unsigned char error_vec_len[8] = { 0 }; + unsigned char error_vec_cap[8] = { 0 }; + + struct SignedVec error_vec = { 0 }; + + error_vec.len = error_vec_len; + error_vec.capacity = error_vec_cap; + + /* Now read the file into bytes and pack inside a struct UnsignedVec + and pass to the function. */ + + FILE *file = fopen(filename, "r"); + + if (file == NULL) { + fprintf(stderr, "Cannot open file %s\n", filename); + + return; + } + + fseek(file, 0, SEEK_END); + + uint64_t file_size = ftell(file); + + fseek(file, 0, SEEK_SET); + + unsigned char *file_buffer = malloc(sizeof(unsigned char) * file_size); + + if (file_buffer == NULL) { + fprintf(stderr, "%s:%d: Cannot allocate %llu memory\n", + __FILE__, __LINE__, + file_size); + + return; + } + + fread(file_buffer, 1, file_size, file); + + fclose(file); + + printf("file size = %llu\n", file_size); + + unsigned char forest_len[8] = { 0 }; + + struct UnsignedVec forest = { 0 }; + + forest.len = forest_len; + + to_big_endian(file_size, forest.len); + + forest.data = file_buffer; + + print_forest(&forest, &error_vec, output_filename); + + uint64_t error_len = from_big_endian(error_vec.len); + + if (error_len) { + fprintf(stderr, "error: "); + + for (uint64_t i = 0; i < error_len; i++) { + fprintf(stderr, "%c", *(error_vec.data+i)); + } + + fprintf(stderr, "\n"); + + clean_signed(&error_vec, 4); + } + + free(file_buffer); +} -- cgit v1.2.3-18-g5258