diff options
author | Joursoir <chat@joursoir.net> | 2021-04-28 16:49:03 +0000 |
---|---|---|
committer | Joursoir <chat@joursoir.net> | 2021-04-28 16:49:03 +0000 |
commit | 34f21050dd5947ebe5a61d02f43f9a57cb967108 (patch) | |
tree | f3aaf315bffd8ad6ac5095d9b8c9ad8e57e0b031 /ctimeline.c | |
parent | 72770394480e1dadeb6a7a23f30c5e0fe5416c64 (diff) | |
download | ctimeline-34f21050dd5947ebe5a61d02f43f9a57cb967108.tar.gz ctimeline-34f21050dd5947ebe5a61d02f43f9a57cb967108.tar.bz2 ctimeline-34f21050dd5947ebe5a61d02f43f9a57cb967108.zip |
add sort by age, refactor store of branch; remove 'branch.position', add 'branch.age_from' and 'branch.age_to' config parametres
Diffstat (limited to 'ctimeline.c')
-rw-r--r-- | ctimeline.c | 99 |
1 files 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<ul class=\"timeline\">\n"); - struct branch_ll *ptr; - for(ptr = first_branch; ptr; ptr = ptr->prev) { + for(i = 0; i < branches.count; i++) { + ptr = &branches.list[i]; printf( "\t<li>\n" "\t\t<div class=\"direction-%s\">\n" "\t\t\t<div class=\"flag-wrapper\">\n" "\t\t\t\t<span class=\"flag\">%s</span>\n" - "\t\t\t\t<span class=\"time-wrapper\"><span class=\"time\">%s</span></span>\n" + "\t\t\t\t<span class=\"time-wrapper\"><span class=\"time\">%d-%d</span></span>\n" "\t\t\t</div>\n" "\t\t\t<div class=\"desc\">%s</div>\n" "\t\t</div>\n" - "\t</li>\n", ptr->branch->position->s, ptr->branch->name->s, - ptr->branch->age->s, ptr->branch->desc->s); + "\t</li>\n", (i % 2) ? "left" : "right", ptr->name->s, + ptr->age_from, ptr->age_to, ptr->desc->s); + string_release(ptr->name); + string_release(ptr->desc); } printf("\t</ul>\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; } |