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 167ac2f900d034bebc685eab640ca4b1f6de0ce7
parent ee5c225278d9ef4376277cbe74580814c093dc2a
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Thu, 28 Apr 2016 17:56:26 +0200

Work(Save): Beginning of saving

Diffstat:
Minc/scale.h | 4++++
Msrcs/graphic.c | 19+++++++++++++++++--
Msrcs/main.c | 2++
Asrcs/save.c | 42++++++++++++++++++++++++++++++++++++++++++
Msrcs/yaml.c | 2+-
5 files changed, 66 insertions(+), 3 deletions(-)

diff --git a/inc/scale.h b/inc/scale.h @@ -75,6 +75,7 @@ struct s_sections { }; struct s_scale { + char *o_file; scale_entry name; // Name of the subject scale_entry lang; // Language of the scale (Enum value LANG_*) scale_entry comment; // Comment about the scale @@ -106,4 +107,7 @@ scale *read_scale(FILE *fd); /* GRAPHIC (graphic.c) */ void window(scale *s); +/* SAVE (save.c) */ +void save_scale(scale *s); + #endif diff --git a/srcs/graphic.c b/srcs/graphic.c @@ -310,13 +310,27 @@ void handle_input(struct nk_context *ctx, GLFWwindow *win) { } void handle_window(scale *s, struct nk_context *ctx) { - struct nk_panel layout; + struct nk_panel layout, menu; static const float ratio[] = {120, 150}, ratio2[] = {300, 150}; - const char *lang[] = {"English", "French", "LANG_RU"}; + const char *lang[] = {"French", "English", "LANG_RU"}; int w_flag = NK_WINDOW_TITLE | NK_WINDOW_BORDER | NK_WINDOW_CLOSABLE | NK_WINDOW_SCALABLE | NK_WINDOW_MOVABLE; if (nk_begin(ctx, &layout, "Main", nk_rect(10, 10, 600, 900), w_flag)) { + nk_menubar_begin(ctx); + nk_layout_row_begin(ctx, NK_STATIC, 25, 2); + nk_layout_row_push(ctx, 45); + if (nk_menu_begin_label(ctx, &menu, "Menu", NK_TEXT_LEFT, 120)) { + nk_layout_row_dynamic(ctx, 25, 1); + if (nk_menu_item_label(ctx, "Save", NK_TEXT_LEFT)) { + save_scale(s); + } + if (nk_menu_item_label(ctx, "Quit", NK_TEXT_LEFT)) { + _exit(0); + } + nk_menu_end(ctx); + } + nk_menubar_end(ctx); nk_layout_row(ctx, NK_STATIC, 25, 2, ratio); nk_label(ctx, "Subject name", NK_TEXT_LEFT); nk_edit_string(ctx, NK_EDIT_FIELD, s->name.buf, &s->name.len, 64, nk_filter_default); @@ -349,6 +363,7 @@ void handle_window(scale *s, struct nk_context *ctx) { "Network & system administration", "Object-oriented programming", "Organization", "Parallel computing", "Rigor", "Security", "Technology integration", "Unix", "Web"}; int j = 0, q = 0, sk_totals_sd[30] = {0}, sk_totals_bs[30] = {0}; + if (!new_section) { new_section = malloc(64); } diff --git a/srcs/main.c b/srcs/main.c @@ -12,6 +12,8 @@ int main(int ac, char **av) { ERROR("Failed to read file %s\n", av[1]); } s = read_scale(fd); + s->o_file = malloc(sizeof(char) * strlen(av[1]) + 1); + strcpy(s->o_file, av[1]); window(s); return 0; } diff --git a/srcs/save.c b/srcs/save.c @@ -0,0 +1,42 @@ +# include <scale.h> + +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); + else + fprintf(fd, "%s: %s\n\n", name, string); +} + +void write_i(FILE *fd, char *name, int val) { + fprintf(fd, "%s: %d\n\n", name, val); +} + +void save_scale(scale *s) { + FILE *fd = fopen("/tmp/test.yml", "w+"); + + if (!fd) { + ERROR("Cannot open %s for writing\n", s->o_file); + } + fprintf(fd, "#########################################\nThis 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); + } else if (s->lang.val == LANG_FR) { + write_s(fd, "lg", "fr", 0); + } else if (s->lang.val == LANG_RU) { + write_s(fd, "lg", "ru", 0); + } + fprintf(fd, "is_primary: true\n\n"); + write_s(fd, "comment", s->comment.buf, 0); + write_s(fd, "introduction_md", s->intro.buf, 1); + write_s(fd, "disclaimer_md", s->disclaimer.buf, 1); + 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); + fclose(fd); +} diff --git a/srcs/yaml.c b/srcs/yaml.c @@ -30,7 +30,7 @@ void read_base_infos(yaml_parser_t *parser, scale *res) { res->disclaimer = m_strcpy(token); else if (!strcmp(key, "guidelines_md")) res->guidelines = m_strcpy(token); - else if (!strcmp(key, "lang")) { + else if (!strcmp(key, "lg")) { tmp = m_strcpy(token).buf; if (!strcmp(tmp, "en")) res->lang.val = LANG_EN;