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 64e705f99ab45a93ae9d7b2c06bc6c6538a05c3a
parent 167ac2f900d034bebc685eab640ca4b1f6de0ce7
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Fri, 29 Apr 2016 18:38:20 +0200

Add(Save): Now working !

Diffstat:
Msrcs/graphic.c | 2+-
Msrcs/save.c | 87+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
Msrcs/yaml.c | 2+-
3 files changed, 83 insertions(+), 8 deletions(-)

diff --git a/srcs/graphic.c b/srcs/graphic.c @@ -358,7 +358,7 @@ void handle_window(scale *s, struct nk_context *ctx) { static int ns_len = 0, nq_len[30], nq_active[30], sk_add[64] = {0}; const char *rating[] = {"Boolean", "Multi"}; const char *kind[] = {"Standard", "Bonus"}; - const char *skills[] = {"Adaptation & Creativity", "Algorithms & AI", "Company experience", "DB & Data", + const char *skills[] = {"Adaptation & creativity", "Algorithms & AI", "Company experience", "DB & Data", "Functionnal programming", "Graphics", "Group & interpersonal", "Imperative programming", "Network & system administration", "Object-oriented programming", "Organization", "Parallel computing", "Rigor", "Security", "Technology integration", "Unix", "Web"}; diff --git a/srcs/save.c b/srcs/save.c @@ -1,28 +1,100 @@ # include <scale.h> +int tab; + void write_s(FILE *fd, char *name, char *string, int is_md) { if (!strlen(string)) { string = malloc(3); strcpy(string, "''"); is_md = 0; } - if (is_md) - fprintf(fd, "%s: |\n\n%s\n\n", name, string); + if (tab) { + for (int i = 0; i < tab; fprintf(fd, " "), i++); + } + if (is_md) { + fprintf(fd, "%s: |\n", name); + for (int i = 0; string[i]; i++) { + if (!i && string[i] != '\n') + fprintf(fd, "\n"); + if ((i > 1 && string[i - 1] == '\n') || !i) { + if (string[i] == '\n' && !i) + fprintf(fd, "\n"); + for (int j = 0; j < tab + 2; fprintf(fd, " "), j++); + if (string[i] == '\n' && !i) + i++; + } + fprintf(fd, "%c", string[i]); + } + if (string[strlen(string) - 1] != '\n') + fprintf(fd, "\n"); + fprintf(fd, "\n"); + } else - fprintf(fd, "%s: %s\n\n", name, string); + fprintf(fd, "%s: %s\n", name, string); } void write_i(FILE *fd, char *name, int val) { - fprintf(fd, "%s: %d\n\n", name, val); + if (tab) { + for (int i = 0; i < tab; fprintf(fd, " "), i++); + } + fprintf(fd, "%s: %d\n", name, val); +} + +void print_sections(scale *s, FILE *fd) { + int position = 1, pos2; + scale_sections *it; + scale_questions *qt; + scale_skills *sk; + + for (it = s->sections; it; it = it->next) { + fprintf(fd, "# BEGINNING OF SECTION %s #\n", it->name.buf); + tab = 0; + write_s(fd, "- name", it->name.buf, 0); + tab = 2; + write_i(fd, "position", position++); + write_s(fd, "description", it->description.buf, 1); + for (int i = 0; i < tab; fprintf(fd, " "), i++); + fprintf(fd, "questions:\n"); + pos2 = 1; + for (qt = it->questions; qt; qt = qt->next) { + fprintf(fd, " # Question %s #\n", qt->name.buf); + tab = 2; + write_s(fd, "- name", qt->name.buf, 0); + tab = 4; + write_i(fd, "position", pos2++); + qt->guidelines.buf[qt->guidelines.len] = 0; + write_s(fd, "guidelines", qt->guidelines.buf, 1); + if (qt->rating.val == R_BOOL) { + write_s(fd, "rating", "bool", 0); + } else if (qt->rating.val == R_MULTI) { + write_s(fd, "rating", "multi", 0); + } + if (qt->kind.val == R_STAND) { + write_s(fd, "kind", "standard", 0); + } else if (qt->kind.val == R_BONUS) { + write_s(fd, "kind", "bonus", 0); + } + for (int i = 0; i < tab; fprintf(fd, " "), i++); + fprintf(fd, "questions_skills:\n"); + for (sk = qt->skills; sk; sk = sk->next) { + write_i(fd, "- percentage", sk->percent.val); + tab = 6; + write_s(fd, "name", sk->name.buf, 0); + tab = 4; + } + } + fprintf(fd, "\n# END OF SECTION #\n\n"); + } } void save_scale(scale *s) { - FILE *fd = fopen("/tmp/test.yml", "w+"); + FILE *fd = fopen(s->o_file, "w+"); + tab = 0; if (!fd) { ERROR("Cannot open %s for writing\n", s->o_file); } - fprintf(fd, "#########################################\nThis scale has been generated by 42_scale\n#########################################\n"); + fprintf(fd, "#############################################\n# This scale has been generated by 42_scale #\n#############################################\n"); write_s(fd, "name", s->name.buf, 0); if (s->lang.val == LANG_EN) { write_s(fd, "lg", "en", 0); @@ -38,5 +110,8 @@ void save_scale(scale *s) { write_s(fd, "guidelines_md", s->guidelines.buf, 1); write_i(fd, "correction_number", s->correction_n.val); write_i(fd, "duration", s->duration.val); + fprintf(fd, "\n# **************************************************************************** #\n\n"); + fprintf(fd, "sections:\n"); + print_sections(s, fd); fclose(fd); } diff --git a/srcs/yaml.c b/srcs/yaml.c @@ -63,7 +63,7 @@ void read_skills(yaml_parser_t *parser, scale_questions *question) { scale_skills *sk, *it; int cur = 0, ct = 0, j; char *key = 0x0, *tmp = 0x0; - const char *skills[] = {"Adaptation & Creativity", "Algorithms & AI", "Company experience", "DB & Data", + const char *skills[] = {"Adaptation & creativity", "Algorithms & AI", "Company experience", "DB & Data", "Functionnal programming", "Graphics", "Group & interpersonal", "Imperative programming", "Network & system administration", "Object-oriented programming", "Organization", "Parallel computing", "Rigor", "Security", "Technology integration", "Unix", "Web"};