From 9bce0bf9cb55e839bb3ca4779b4b8cc0e5fcb15e Mon Sep 17 00:00:00 2001 From: Nathan Lebrun Date: Wed, 29 Jan 2025 10:05:16 +0100 Subject: [PATCH] drawio done and at norm --- lib/libft/libft.h | 1 + lib/libft/srcs/format/ft_tabstr.c | 36 +++++++++ tests/drawio/drawio.h | 24 +++++- tests/drawio/drawio_ast.c | 125 ++---------------------------- tests/drawio/drawio_ast_utils.c | 69 +++++++++++++++++ tests/drawio/drawio_linklist.c | 6 +- tests/drawio/drawio_print_ast.c | 73 +++++++++++++++++ tests/drawio/drawio_utils.c | 2 - 8 files changed, 210 insertions(+), 126 deletions(-) create mode 100644 lib/libft/srcs/format/ft_tabstr.c create mode 100644 tests/drawio/drawio_ast_utils.c create mode 100644 tests/drawio/drawio_print_ast.c diff --git a/lib/libft/libft.h b/lib/libft/libft.h index 4ef1713..9712ee0 100644 --- a/lib/libft/libft.h +++ b/lib/libft/libft.h @@ -64,6 +64,7 @@ void ft_bzero(void *s, size_t n); char **ft_split(const char *s, char *set); char **ft_split_keep(const char *s, char *set); +char *ft_tabstr(char **tab); int is_charset(char c, char *set); void free_tab(char **tab); char **free_all(char **tab, int count); diff --git a/lib/libft/srcs/format/ft_tabstr.c b/lib/libft/srcs/format/ft_tabstr.c new file mode 100644 index 0000000..1cd9f9c --- /dev/null +++ b/lib/libft/srcs/format/ft_tabstr.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_tabstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/29 08:54:00 by nalebrun #+# #+# */ +/* Updated: 2025/01/29 08:54:00 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +char *ft_tabstr(char **tab) +{ + int i; + int alloc_count; + char *out; + char *tmp; + + i = -1; + alloc_count = 0; + while (tab[++i]) + alloc_count += ft_strlen(tab[i]) + 1; + i = 0; + out = tab[0]; + while (tab[++i]) + { + tmp = out; + out = ft_sprintf("%s %s", out, tab[i]); + free(tmp); + } + out[alloc_count] = 0; + return (out); +} diff --git a/tests/drawio/drawio.h b/tests/drawio/drawio.h index 3175b0e..98cbdd5 100644 --- a/tests/drawio/drawio.h +++ b/tests/drawio/drawio.h @@ -17,10 +17,28 @@ # include "../tokenizer/tokenizer.h" # include "../ast/ast.h" +typedef struct s_dio_node +{ + const char *st; + const char *redir; + char *cmd; + char *args; + char *inf; + char *outf; +} t_dio_node; + +typedef struct s_elems +{ + t_dio_elem rect; + t_dio_elem arrow; +} t_elems; + // internal -char* replace_ampercent(char *src); +char *replace_ampercent(char *src); +t_dio_node get_cmd_txt(t_ast_n *node); +int print_ast(t_ast_n *node, t_elems *e, int fd); // external -void gen_dio_linked_list(t_node *head, int fd); -void gen_dio_ast(t_ast_n *head, int fd); +void gen_dio_linked_list(t_node *head, int fd); +void gen_dio_ast(t_ast_n *head, int fd); #endif diff --git a/tests/drawio/drawio_ast.c b/tests/drawio/drawio_ast.c index b0ded76..e0595ac 100644 --- a/tests/drawio/drawio_ast.c +++ b/tests/drawio/drawio_ast.c @@ -19,128 +19,17 @@ void set_ast_rect(t_dio_elem *rect) rect->x = 50; rect->y = 150; rect->w = 150; - rect->h = 150; + rect->h = 120; } -const char *translate_state(t_state state) -{ - const char *out; - - if (state == _AND) - out = "CMD_AND"; - else if (state == _OR) - out = "CMD_OR"; - else if (state == _PLINE) - out = "CMD_PIPELINE"; - else if (state == _CMD) - out = "SIMPLE_CMD"; - else - out = "UNDEFINED"; - return (out); -} - -const char *translate_redir(t_redir redir) -{ - const char *out; - - if (redir == _RED_L) - out = "redir : < "; - else if (redir == _RED_R) - out = "redir : > "; - else if (redir == _RED_DR) - out = "redir : >> "; - else - out = "Not redirected "; - return (out); -} - -char *get_node_txt(t_ast_n *node) -{ - const char *st; - char *out; - char *cmd; - char *args; - const char *redir; - char *inf; - char *outf; - - st = translate_state(node->state); - if (node->state == _CMD) - { - cmd = ft_sprintf("%s%s%s", NL, node->cmd, NL); - args = ft_sprintf("future args", NL); - redir = translate_redir(node->redir); - inf = ft_sprintf("Infile : %s%s", node->infile, NL); - outf = ft_sprintf("Outfile : %s%s", node->outfile, NL); - } - else - { - cmd = ft_calloc(1, 1); - args = ft_calloc(1, 1); - redir = ""; - inf = ft_calloc(1, 1); - outf = ft_calloc(1, 1); - } - - out = ft_sprintf("%s%s%s%s%s%s%s", st, cmd, args, NL, redir, inf, outf); - free(cmd); - free(args); - free(inf); - free(outf); - return (out); -} - -int print_ast(t_ast_n *node, t_dio_elem *rect, t_dio_elem *arrow, int fd) -{ - int i; - int node_id; - - if (!node || !rect) - return -1; - - rect->text = get_node_txt(node); - node_id = drawio_create_elem(fd, rect); - - if (node->state != _PLINE) - { - rect->y += rect->h + 50; - arrow->id_dst = print_ast(node->left, rect, arrow, fd); - arrow->id_src = node_id; - drawio_create_elem(fd, arrow); - - rect->x += rect->w + 50; - arrow->id_dst = print_ast(node->right, rect, arrow, fd); - arrow->id_src = node_id; - drawio_create_elem(fd, arrow); - - rect->y -= rect->h + 50; - if (node->state == _CMD) - rect->x -= rect->w + 50; - } - else - { - i = 0; - rect->y += rect->h + 50; - while (node->pline[i]) - { - arrow->id_dst = print_ast(node->pline[i++], rect, arrow, fd); - arrow->id_src = node_id; - drawio_create_elem(fd, arrow); - rect->x += rect->w + 50; - } - } - return (node_id); -} void gen_dio_ast(t_ast_n *head, int fd) { - t_dio_elem rect; - t_dio_elem arrow; + t_elems e; - set_ast_rect(&rect); - arrow.type = ARROW; - arrow.id_src = 0; - arrow.id_dst = 0; - - print_ast(head, &rect, &arrow, fd); + set_ast_rect(&e.rect); + e.arrow.type = ARROW; + e.arrow.id_src = 0; + e.arrow.id_dst = 0; + print_ast(head, &e, fd); return ; } diff --git a/tests/drawio/drawio_ast_utils.c b/tests/drawio/drawio_ast_utils.c new file mode 100644 index 0000000..fd823c5 --- /dev/null +++ b/tests/drawio/drawio_ast_utils.c @@ -0,0 +1,69 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* drawio_ast_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/29 09:21:09 by nalebrun #+# #+# */ +/* Updated: 2025/01/29 09:21:09 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "drawio.h" + +const char *translate_state(t_state state) +{ + const char *out; + + if (state == _AND) + out = "CMD_AND"; + else if (state == _OR) + out = "CMD_OR"; + else if (state == _PLINE) + out = "CMD_PIPELINE"; + else if (state == _CMD) + out = "SIMPLE_CMD"; + else + out = "UNDEFINED"; + return (out); +} + +const char *translate_redir(t_redir redir) +{ + const char *out; + + if (redir == _RED_L) + out = "redir : < "; + else if (redir == _RED_R) + out = "redir : > "; + else if (redir == _RED_DR) + out = "redir : >> "; + else + out = "Not redirected "; + return (out); +} + +t_dio_node get_cmd_txt(t_ast_n *node) +{ + t_dio_node txt; + + txt.st = translate_state(node->state); + if (node->state == _CMD) + { + txt.cmd = ft_sprintf("%s%s%s", NL, node->cmd, NL); + txt.args = ft_sprintf(ft_tabstr(node->args), NL); + txt.redir = translate_redir(node->redir); + txt.inf = ft_sprintf("Infile : %s%s", node->infile, NL); + txt.outf = ft_sprintf("Outfile : %s", node->outfile); + } + else + { + txt.cmd = ft_calloc(1, 1); + txt.args = ft_calloc(1, 1); + txt.redir = ""; + txt.inf = ft_calloc(1, 1); + txt.outf = ft_calloc(1, 1); + } + return (txt); +} diff --git a/tests/drawio/drawio_linklist.c b/tests/drawio/drawio_linklist.c index 80bd842..b5be403 100644 --- a/tests/drawio/drawio_linklist.c +++ b/tests/drawio/drawio_linklist.c @@ -24,9 +24,9 @@ void set_ll_rect(t_dio_elem *rect) void gen_dio_linked_list(t_node *head, int fd) { - t_dio_elem rect; - t_dio_elem arrow; - t_node *current; + t_dio_elem rect; + t_dio_elem arrow; + t_node *current; current = head; set_ll_rect(&rect); diff --git a/tests/drawio/drawio_print_ast.c b/tests/drawio/drawio_print_ast.c new file mode 100644 index 0000000..ef0533e --- /dev/null +++ b/tests/drawio/drawio_print_ast.c @@ -0,0 +1,73 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* drawio_print_ast.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/29 09:54:31 by nalebrun #+# #+# */ +/* Updated: 2025/01/29 09:54:31 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "drawio.h" + +char *get_node_txt(t_ast_n *node) +{ + t_dio_node txt; + char *out; + + txt = get_cmd_txt(node); + out = ft_sprintf("%s%s%s%s%s%s%s", txt.st, txt.cmd, txt.args, + NL, txt.redir, txt.inf, txt.outf); + free(txt.cmd); + free(txt.args); + free(txt.inf); + free(txt.outf); + return (out); +} + +void draw_bin_part(t_ast_n *node, t_elems *e, int fd, int node_id) +{ + e->rect.y += e->rect.h + 50; + e->arrow.id_dst = print_ast(node->left, e, fd); + e->arrow.id_src = node_id; + drawio_create_elem(fd, &e->arrow); + e->rect.x += e->rect.w + 50; + e->arrow.id_dst = print_ast(node->right, e, fd); + e->arrow.id_src = node_id; + drawio_create_elem(fd, &e->arrow); + e->rect.y -= e->rect.h + 50; + if (node->state == _CMD) + e->rect.x -= e->rect.w + 50; +} + +void draw_pline_part(t_ast_n *node, t_elems *e, int fd, int node_id) +{ + int i; + + i = 0; + e->rect.y += e->rect.h + 50; + while (node->pline[i]) + { + e->arrow.id_dst = print_ast(node->pline[i++], e, fd); + e->arrow.id_src = node_id; + drawio_create_elem(fd, &e->arrow); + e->rect.x += e->rect.w + 50; + } +} + +int print_ast(t_ast_n *node, t_elems *e, int fd) +{ + int node_id; + + if (!node) + return (-1); + e->rect.text = get_node_txt(node); + node_id = drawio_create_elem(fd, &e->rect); + if (node->state != _PLINE) + draw_bin_part(node, e, fd, node_id); + else + draw_pline_part(node, e, fd, node_id); + return (node_id); +} diff --git a/tests/drawio/drawio_utils.c b/tests/drawio/drawio_utils.c index f9577dc..ff43b7c 100644 --- a/tests/drawio/drawio_utils.c +++ b/tests/drawio/drawio_utils.c @@ -56,5 +56,3 @@ char *replace_ampercent(char *src) out[j] = 0; return (out); } - -