From 34f21050dd5947ebe5a61d02f43f9a57cb967108 Mon Sep 17 00:00:00 2001 From: Joursoir Date: Wed, 28 Apr 2021 16:49:03 +0000 Subject: add sort by age, refactor store of branch; remove 'branch.position', add 'branch.age_from' and 'branch.age_to' config parametres --- ctimeline.c | 99 ++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 59 insertions(+), 40 deletions(-) diff --git a/ctimeline.c b/ctimeline.c index ce0331d..505a683 100644 --- a/ctimeline.c +++ b/ctimeline.c @@ -6,39 +6,71 @@ #include "xstring.h" #define COMMENT_CHAR '#' +#define INIT_CAPACITY_LIST 8 struct branch { string *name; - string *position; - string *age; + int age_from; + int age_to; string *desc; }; +struct branch_list { + struct branch *list; + int count; + int capacity; +}; + struct branch_ll { struct branch *branch; struct branch_ll *prev; }; -struct branch_ll *first_branch; +struct branch_list branches = { NULL, 0, 0 }; struct branch *current_branch; +void sort_branches_by_age() +{ + int i, j; + struct branch *i_ptr, *j_ptr; + struct branch tmp; + + for(i = 0; i < branches.count; i++) { + i_ptr = &branches.list[i]; + for(j = i+1; j < branches.count; j++) { + j_ptr = &branches.list[j]; + if((j_ptr->age_from > i_ptr->age_from) || + (j_ptr->age_to > i_ptr->age_to) ) { + tmp = *i_ptr; + *i_ptr = *j_ptr; + *j_ptr = tmp; + } + } + } +} + void emit_timeline_html() { + int i; + struct branch *ptr; + printf("\t\n"); @@ -47,33 +79,22 @@ void emit_timeline_html() struct branch *add_branch(const char *name) { struct branch *br; - - if(!first_branch) { - first_branch = malloc(sizeof(struct branch_ll)); - first_branch->branch = malloc(sizeof(struct branch)); - first_branch->prev = NULL; - - br = first_branch->branch; - } - else { - struct branch_ll *ptr; - for(ptr = first_branch; ptr; ptr = ptr->prev) { - if(!ptr->prev) { - ptr->prev = malloc(sizeof(struct branch_ll)); - ptr->prev->branch = malloc(sizeof(struct branch)); - ptr->prev->prev = NULL; - - br = ptr->prev->branch; - break; - } - } + branches.count++; + + if(branches.count > branches.capacity) { + if(branches.capacity == 0) + branches.capacity = INIT_CAPACITY_LIST; + else + branches.capacity *= 2; + branches.list = realloc(branches.list, + sizeof(struct branch) * branches.capacity); } + br = &branches.list[branches.count-1]; br->name = string_alloc(name); - br->position = string_alloc("right"); - br->age = string_alloc("No age"); + br->age_from = 0; + br->age_to = 0; br->desc = string_alloc("No description"); - return br; } @@ -82,22 +103,17 @@ void handle_config_context(const char *name, const char *value) if(strcmp(name, "branch.name") == 0) { current_branch = add_branch(value); } - else if(strcmp(name, "branch.position") == 0) { + else if(strcmp(name, "branch.age_from") == 0) { if(!current_branch) return; - if(strcmp(value, "right") != 0 && strcmp(value, "left") != 0) - return; - - string_release(current_branch->position); - current_branch->position = string_alloc(value); + current_branch->age_from = atoi(value); } - else if(strcmp(name, "branch.age") == 0) { + else if(strcmp(name, "branch.age_to") == 0) { if(!current_branch) return; - string_release(current_branch->age); - current_branch->age = string_alloc(value); + current_branch->age_to = atoi(value); } else if(strcmp(name, "branch.desc") == 0) { if(!current_branch) @@ -200,6 +216,9 @@ int main(int argc, char **argv) return 1; } + sort_branches_by_age(); + emit_timeline_html(); + free(branches.list); return 0; } -- cgit v1.2.3-18-g5258