diff --git a/lib/libft/libft.h b/lib/libft/libft.h index 977ac93..4ef1713 100644 --- a/lib/libft/libft.h +++ b/lib/libft/libft.h @@ -24,7 +24,7 @@ # include "colors.h" # include "general.h" -# include "srcs/drawio/drawio.h" +# include "srcs/drawio/lib_drawio.h" int ft_isalpha(int c); int ft_isdigit(int c); diff --git a/lib/libft/srcs/drawio/drawio.h b/lib/libft/srcs/drawio/lib_drawio.h similarity index 96% rename from lib/libft/srcs/drawio/drawio.h rename to lib/libft/srcs/drawio/lib_drawio.h index ad1da83..70d72cd 100644 --- a/lib/libft/srcs/drawio/drawio.h +++ b/lib/libft/srcs/drawio/lib_drawio.h @@ -10,8 +10,8 @@ /* */ /* ************************************************************************** */ -#ifndef DRAWIO_H -# define DRAWIO_H +#ifndef LIB_DRAWIO_H +# define LIB_DRAWIO_H # include "../../libft.h" diff --git a/tests/ast/ast.c b/tests/ast/ast.c index ba58118..0681640 100644 --- a/tests/ast/ast.c +++ b/tests/ast/ast.c @@ -30,6 +30,9 @@ t_ast_n *created_ast_n(t_state st, t_ast_n *prt, t_ast_n *he) node->redir = _NR; node->infile = NULL; node->outfile = NULL; + node->left = NULL; + node->right = NULL; + node->pline = NULL; if (prt) node->shlvl = prt->shlvl; else diff --git a/tests/drawio/drawio.h b/tests/drawio/drawio.h new file mode 100644 index 0000000..3175b0e --- /dev/null +++ b/tests/drawio/drawio.h @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* drawio.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/27 14:20:35 by nalebrun #+# #+# */ +/* Updated: 2025/01/27 14:20:35 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef DRAWIO_H +# define DRAWIO_H + +# include "../../lib/libft/libft.h" +# include "../tokenizer/tokenizer.h" +# include "../ast/ast.h" + +// internal +char* replace_ampercent(char *src); + +// external +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 new file mode 100644 index 0000000..5cc49e4 --- /dev/null +++ b/tests/drawio/drawio_ast.c @@ -0,0 +1,83 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* drawio_ast.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/27 17:15:39 by nalebrun #+# #+# */ +/* Updated: 2025/01/27 17:15:39 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "drawio.h" + +void set_ast_rect(t_dio_elem *rect) +{ + rect->type = RECT; + rect->rounded = 0; + rect->x = 50; + rect->y = 150; + rect->w = 150; + rect->h = 100; +} + +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); + +} + +void print_ast(t_ast_n *node, t_dio_elem *rect, int fd) +{ + int i; + const char *state; + + if (!node || !rect) + return; + + state = translate_state(node->state); + rect->text = ft_sprintf("%s\ntest", state); + drawio_create_elem(fd, rect); + + if (node->state != _PLINE) + { + rect->y += rect->h + 50; + print_ast(node->left, rect, fd); + rect->x += rect->w + 50; + print_ast(node->right, rect, fd); + rect->y -= rect->h + 50; + rect->x -= rect->w + 50; + } + else + { + i = 0; + rect->y += rect->h + 50; + while (node->pline[i]) + { + print_ast(node->pline[i++], rect, fd); + rect->x += rect->w + 50; + } + } +} +void gen_dio_ast(t_ast_n *head, int fd) +{ + t_dio_elem rect; + + set_ast_rect(&rect); + print_ast(head, &rect, fd); + return ; +} diff --git a/tests/drawio/drawio_linklist.c b/tests/drawio/drawio_linklist.c new file mode 100644 index 0000000..80bd842 --- /dev/null +++ b/tests/drawio/drawio_linklist.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* drawio_linklist.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/24 14:41:02 by nalebrun #+# #+# */ +/* Updated: 2025/01/27 17:16:53 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "drawio.h" + +void set_ll_rect(t_dio_elem *rect) +{ + rect->type = RECT; + rect->rounded = 0; + rect->x = 50; + rect->y = 50; + rect->w = 100; + rect->h = 50; +} + +void gen_dio_linked_list(t_node *head, int fd) +{ + t_dio_elem rect; + t_dio_elem arrow; + t_node *current; + + current = head; + set_ll_rect(&rect); + arrow.id_src = 0; + arrow.type = ARROW; + while (current != NULL) + { + rect.text = ft_sprintf("%s", current->val); + rect.text = replace_ampercent(rect.text); + arrow.id_dst = drawio_create_elem(fd, &rect); + drawio_create_elem(fd, &arrow); + arrow.id_src = arrow.id_dst; + rect.x += 150; + free(rect.text); + current = current->next; + } +} diff --git a/tests/drawio.c b/tests/drawio/drawio_utils.c similarity index 56% rename from tests/drawio.c rename to tests/drawio/drawio_utils.c index 4dafb0b..f9577dc 100644 --- a/tests/drawio.c +++ b/tests/drawio/drawio_utils.c @@ -1,19 +1,18 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* drawio.c :+: :+: :+: */ +/* drawio_utils.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: nalebrun +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2025/01/24 14:41:02 by nalebrun #+# #+# */ -/* Updated: 2025/01/24 14:41:02 by nalebrun ### ########.fr */ +/* Created: 2025/01/27 15:09:16 by nalebrun #+# #+# */ +/* Updated: 2025/01/27 15:09:16 by nalebrun ### ########.fr */ /* */ /* ************************************************************************** */ -#include "parsing.h" -#include "tokenizer/tokenizer.h" +#include "drawio.h" -int get_amp_count(char *str) +static int get_amp_count(char *str) { int i; int count; @@ -29,7 +28,7 @@ int get_amp_count(char *str) return (count); } -char* replace_ampercent(char *src) +char *replace_ampercent(char *src) { int i; int j; @@ -58,33 +57,4 @@ char* replace_ampercent(char *src) return (out); } -void gen_dio_linked_list(t_node *head, char *fp) -{ - int fd; - t_dio_elem rect; - t_dio_elem arrow; - t_node *current; - current = head; - fd = drawio_init(fp); - rect.type = RECT; - rect.rounded = 0; - rect.x = 50; - rect.y = 50; - rect.w = 100; - rect.h = 50; - arrow.id_src = 0; - arrow.type = ARROW; - while (current != NULL) - { - rect.text = ft_sprintf("|||%s|||", current->val); - rect.text = replace_ampercent(rect.text); - arrow.id_dst = drawio_create_elem(fd, &rect); - drawio_create_elem(fd, &arrow); - arrow.id_src = arrow.id_dst; - rect.x += 150; - free(rect.text); - current = current->next; - } - drawio_end_file(fd); -} diff --git a/tests/parsing.h b/tests/parsing.h index a8bcb06..ddb328f 100644 --- a/tests/parsing.h +++ b/tests/parsing.h @@ -15,9 +15,9 @@ # include "tokenizer/tokenizer.h" # include "ast/ast.h" +# include "drawio/drawio.h" // drawio -void gen_dio_linked_list(t_node *head, char *fp); // tmp_env char **init_env(char **envp); diff --git a/tests/test.c b/tests/test.c index b964f30..6b9190b 100644 --- a/tests/test.c +++ b/tests/test.c @@ -49,6 +49,8 @@ void truncate_comment(char *str) int main(int ac, char **av, char **envp) { t_node *lst; + t_ast_n *ast; + int dio; /*t_data data;*/ if (ac != 3) @@ -61,8 +63,11 @@ int main(int ac, char **av, char **envp) lst = tokenize(av[2]); if (!lst) return (1); - gen_dio_linked_list(lst, av[1]); + dio = drawio_init(av[1]); + gen_dio_linked_list(lst, dio); /*debug_linked_list(lst, "ff");*/ - return_hardcode_ast(envp); + ast = return_hardcode_ast(envp); + gen_dio_ast(ast, dio); + drawio_end_file(dio); free_linked_list(lst); }