42_Scale

a small software to create and edit 42 (.yaml) scale files easily
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/42_Scale

commit 1db7fbefe7bc0562984e97402daed394a2e7416b
parent 351a3eb2c5e25a8926be426be3af057b7f323550
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Tue, 17 May 2016 16:49:58 +0200

Add(Init): New template system:

Creating a file with a basic template by default
Fix #2
I will work on a basic template configuration, for comments, guidelines, etc.

Diffstat:
Minc/scale.h | 3++-
Msrcs/graphic.c | 8++++----
Msrcs/helpers.c | 25+++++++++++++++++++++----
Msrcs/main.c | 15+++++++++++++--
Msrcs/save.c | 2+-
5 files changed, 41 insertions(+), 12 deletions(-)

diff --git a/inc/scale.h b/inc/scale.h @@ -96,9 +96,10 @@ void print_trace(void); scale_entry m_strcpy(yaml_token_t token); void scale_debug(scale *res); int count_lines(char *s); -void add_section(scale_sections *s, char *name); +void add_section(scale *s, char *name); void add_question(scale_sections *s, char *name, int j); void add_skills(scale_questions *sk, int val, char *name); +scale *create_template(void); /* YAML (yaml.c) */ diff --git a/srcs/graphic.c b/srcs/graphic.c @@ -372,7 +372,7 @@ void handle_window(scale *s, struct nk_context *ctx) { active = nk_edit_string(ctx, NK_EDIT_FIELD|NK_EDIT_SIG_ENTER, new_section, &ns_len, 64, nk_filter_ascii); if (nk_button_label(ctx, "Add a Section", NK_BUTTON_DEFAULT) || (active & NK_EDIT_COMMITED)) { new_section[ns_len] = 0x0; - add_section(s->sections, new_section); + add_section(s, new_section); new_section = ""; ns_len = 0; } @@ -442,10 +442,10 @@ void handle_window(scale *s, struct nk_context *ctx) { nk_label(ctx, skills[j], NK_TEXT_LEFT); nk_property_int(ctx, "Total:", -10, &sk_totals_sd[j], 1000, 1, 1); } - nk_layout_row_end(ctx); } + nk_layout_row_end(ctx); + nk_tree_pop(ctx); } - nk_tree_pop(ctx); if (nk_tree_push(ctx, NK_TREE_NODE, "Bonus", NK_MAXIMIZED)) { nk_layout_row(ctx, NK_STATIC, 25, 2, ratio2); for (int j = 0; j < 30; j++) { @@ -454,8 +454,8 @@ void handle_window(scale *s, struct nk_context *ctx) { nk_property_int(ctx, "Total:", -10, &sk_totals_bs[j], 1000, 1, 1); } } + nk_tree_pop(ctx); } - nk_tree_pop(ctx); } nk_end(ctx); } diff --git a/srcs/helpers.c b/srcs/helpers.c @@ -103,7 +103,7 @@ int count_lines(char *s) { return res * 20; } -void add_section(scale_sections *s, char *name) { +void add_section(scale *s, char *name) { scale_sections *res, *it; if (!strlen(name)) @@ -116,10 +116,10 @@ void add_section(scale_sections *s, char *name) { res->description.len = 0; res->questions = 0x0; res->next = 0x0; - if (!s) { - s = res; + if (!s->sections) { + s->sections = res; } else { - for (it = s; it->next; it = it->next); + for (it = s->sections; it->next; it = it->next); it->next = res; } } @@ -178,3 +178,20 @@ void add_skills(scale_questions *sk, int val, char *name) { } } } + +scale *create_template(void) { + scale *s; + + s = malloc(sizeof(struct s_scale)); + s->name.buf = malloc(64); s->name.len = 0; + s->lang.val = LANG_EN; + s->comment.buf = malloc(2024); s->comment.len = 0; + s->intro.buf = malloc(2024); s->intro.len = 0; + s->disclaimer.buf = malloc(2024); s->disclaimer.len = 0; + s->guidelines.buf = malloc(2024); s->guidelines.len = 0; + s->correction_n.val = 5; + s->is_primary.bol = true; + s->duration.val = 5; + s->sections = 0x0; + return s; +} diff --git a/srcs/main.c b/srcs/main.c @@ -3,15 +3,26 @@ int main(int ac, char **av) { FILE *fd; scale *s; + char b[2]; if (ac == 1) { ERROR("Need an argument !\n"); } fd = fopen(av[1], "r"); if (!fd) { - ERROR("Failed to read file %s\n", av[1]); + printf("\033[1;33m>\033[0m File %s doesn't exist. Create one with a basic template ? [Y\\n] ", av[1]); + fflush(stdout); // Trick for the printf ^ + if (read(0, b, 1)) { + if (b[0] == 'y' || b[0] == 'Y' || b[0] == '\n') { + printf("Creating template ...\n"); + s = create_template(); + } else { + ERROR("Failed to read file %s\n", av[1]); + } + } + } else { + s = read_scale(fd); } - s = read_scale(fd); s->o_file = malloc(sizeof(char) * strlen(av[1]) + 1); strcpy(s->o_file, av[1]); window(s); diff --git a/srcs/save.c b/srcs/save.c @@ -93,7 +93,7 @@ void print_sections(scale *s, FILE *fd) { } void save_scale(scale *s) { - FILE *fd = fopen(s->o_file, "w+"); + FILE *fd = fopen(s->o_file, "wb+"); tab = 0; if (!fd) {