From 035bf45ad07fd9467d56c5000075b9ac44c191d9 Mon Sep 17 00:00:00 2001 From: Nathan Lebrun Date: Fri, 24 Jan 2025 14:10:50 +0100 Subject: [PATCH] drawio generator --- Makefile | 2 +- ast.drawio.xml | 42 ---------------- lib/libft/libft.h | 3 ++ lib/libft/srcs/drawio/drawio.h | 42 ++++++++++++++++ lib/libft/srcs/drawio/header/dio_private.h | 19 +++++++ lib/libft/srcs/drawio/srcs/create_elem.c | 38 ++++++++++++++ lib/libft/srcs/drawio/srcs/drawio.c | 50 +++++++++++++++++++ .../libft/srcs/sprintf}/ft_sprintf.c | 9 ++-- .../libft/srcs/sprintf}/ft_sprintf.h | 5 +- .../libft/srcs/sprintf}/sprintf_utils.c | 0 tests/ast/ast.c | 20 ++++++++ tests/ast/ast.h | 19 +++++++ tests/{parse.c => test.c} | 40 ++++++++++++++- tests/tokenizer/tokenizer.c | 4 -- 14 files changed, 238 insertions(+), 55 deletions(-) delete mode 100644 ast.drawio.xml create mode 100644 lib/libft/srcs/drawio/drawio.h create mode 100644 lib/libft/srcs/drawio/header/dio_private.h create mode 100644 lib/libft/srcs/drawio/srcs/create_elem.c create mode 100644 lib/libft/srcs/drawio/srcs/drawio.c rename {sprintf => lib/libft/srcs/sprintf}/ft_sprintf.c (90%) rename {sprintf => lib/libft/srcs/sprintf}/ft_sprintf.h (91%) rename {sprintf => lib/libft/srcs/sprintf}/sprintf_utils.c (100%) create mode 100644 tests/ast/ast.c create mode 100644 tests/ast/ast.h rename tests/{parse.c => test.c} (71%) diff --git a/Makefile b/Makefile index 9f304da..95000b1 100644 --- a/Makefile +++ b/Makefile @@ -46,7 +46,7 @@ $(TEST_OBJDIR)/%.o: $(TEST_SRCDIR)/%.c @mkdir -p $(dir $@) @$(CC) $(WFLAGS) -MMD -MP -I$(INCDIR) -c $< -g3 -ggdb -o $@ $(LINK) -test: $(LIBFT) $(TEST_OBJS) +tests: $(LIBFT) $(TEST_OBJS) @$(CC) $(WFLAGS) $(TEST_OBJS) $(LIBFT) -o test $(LINK) @echo "$(CYAN)Test build completed: test$(RESET)" diff --git a/ast.drawio.xml b/ast.drawio.xml deleted file mode 100644 index a3b9337..0000000 --- a/ast.drawio.xml +++ /dev/null @@ -1,42 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/lib/libft/libft.h b/lib/libft/libft.h index 9617aff..977ac93 100644 --- a/lib/libft/libft.h +++ b/lib/libft/libft.h @@ -19,10 +19,12 @@ # include # include # include +# include # include # include "colors.h" # include "general.h" +# include "srcs/drawio/drawio.h" int ft_isalpha(int c); int ft_isdigit(int c); @@ -80,6 +82,7 @@ void ft_error(char *e); int ft_printf(const char *fstr, ...); int ft_fprintf(int fd, const char *str, ...); +char *ft_sprintf(const char *str, ...); int ft_debug(const char *fstr, ...); char *get_next_line(int fd); diff --git a/lib/libft/srcs/drawio/drawio.h b/lib/libft/srcs/drawio/drawio.h new file mode 100644 index 0000000..781ecd0 --- /dev/null +++ b/lib/libft/srcs/drawio/drawio.h @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* drawio.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/24 09:34:41 by nalebrun #+# #+# */ +/* Updated: 2025/01/24 09:34:41 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef DRAWIO_H +# define DRAWIO_H + +#include "../../libft.h" + +typedef enum e_dio_elemtype +{ + RECT, + ARROW, +} t_dio_elemtype; + +typedef struct s_dio_elem +{ + t_dio_elemtype type; + char *text; + int x; + int y; + int w; + int h; + int id_src; + int id_dst; + int rounded; + +} t_dio_elem; + +int drawio_init(char *file_path); +int drawio_create_elem(int fd, t_dio_elem *elem); +void drawio_end_file(int fd); + +#endif diff --git a/lib/libft/srcs/drawio/header/dio_private.h b/lib/libft/srcs/drawio/header/dio_private.h new file mode 100644 index 0000000..3a533b7 --- /dev/null +++ b/lib/libft/srcs/drawio/header/dio_private.h @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* dio_private.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/24 11:06:54 by nalebrun #+# #+# */ +/* Updated: 2025/01/24 11:06:54 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef DIO_PRIVATE +# define DIO_PRIVATE + +int draw_rect(int fd, int id, t_dio_elem *elem); +int draw_arrow(int fd, int id, t_dio_elem *elem); + +#endif diff --git a/lib/libft/srcs/drawio/srcs/create_elem.c b/lib/libft/srcs/drawio/srcs/create_elem.c new file mode 100644 index 0000000..02838c9 --- /dev/null +++ b/lib/libft/srcs/drawio/srcs/create_elem.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* create_elem.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/24 10:06:11 by nalebrun #+# #+# */ +/* Updated: 2025/01/24 10:06:11 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../drawio.h" +#include "../header/dio_private.h" + +int draw_rect(int fd, int id, t_dio_elem *elem) +{ + ft_fprintf(fd, "text); + ft_fprintf(fd, "style=\"rounded=%d;whiteSpace=wrap;html=1;\" ", elem->rounded); + ft_fprintf(fd, "vertex=\"1\" parent=\"1\"> \n"); + ft_fprintf(fd, "x, elem->y); + ft_fprintf(fd, "width=\"%d\" height=\"%d\" ", elem->w, elem->h); + ft_fprintf(fd, "as=\"geometry\"/>\n"); + ft_fprintf(fd, "\n"); + return (id); +} + +int draw_arrow(int fd, int id, t_dio_elem *elem) +{ + if (!elem->id_src || !elem->id_dst) + return (0); + ft_fprintf(fd, "\n", elem->id_src, elem->id_dst); + ft_fprintf(fd, "\n\n"); + return (id); +} diff --git a/lib/libft/srcs/drawio/srcs/drawio.c b/lib/libft/srcs/drawio/srcs/drawio.c new file mode 100644 index 0000000..e442717 --- /dev/null +++ b/lib/libft/srcs/drawio/srcs/drawio.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* drawio.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/24 08:47:41 by nalebrun #+# #+# */ +/* Updated: 2025/01/24 08:47:41 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../drawio.h" +#include "../header/dio_private.h" + +int drawio_init(char *file_path) +{ + int fd; + + fd = open(file_path, O_WRONLY | O_CREAT | O_TRUNC, 0666); + + ft_fprintf(fd, "\n"); + ft_fprintf(fd, "\n"); + ft_fprintf(fd, "\n"); + ft_fprintf(fd, "\n"); + ft_fprintf(fd, "\n"); + ft_fprintf(fd, "\n"); + return(fd); +} + +void drawio_end_file(int fd) +{ + ft_fprintf(fd, "\n\n\n"); + close(fd); +} + +int drawio_create_elem(int fd, t_dio_elem *elem) +{ + static int id = 1; + + id ++; + if (elem->type == ARROW) + return (draw_arrow(fd, id, elem)); + if (elem->type == RECT || elem->type) + return (draw_rect(fd, id, elem)); + return (0); +} diff --git a/sprintf/ft_sprintf.c b/lib/libft/srcs/sprintf/ft_sprintf.c similarity index 90% rename from sprintf/ft_sprintf.c rename to lib/libft/srcs/sprintf/ft_sprintf.c index 831679a..0ed60ce 100644 --- a/sprintf/ft_sprintf.c +++ b/lib/libft/srcs/sprintf/ft_sprintf.c @@ -21,12 +21,15 @@ static int is_spec(char c) static char *add_arg(char c, char *str, va_list args) { + char *out; + if (c == 'c') - return (ft_strfjoinc(str, va_arg(args, int))); + out = ft_strfjoinc(str, va_arg(args, int)); else if (c == 's') - return (ft_strfjoin(str, va_arg(args, char *))); + out = ft_strfjoin(str, va_arg(args, char *)); else if (c == 'd') - return (ft_strfjoind(str, ft_itoa(va_arg(args, int)))); + out = ft_strfjoind(str, ft_itoa(va_arg(args, int))); + return (out); } static char *build_str(const char *str, va_list args) diff --git a/sprintf/ft_sprintf.h b/lib/libft/srcs/sprintf/ft_sprintf.h similarity index 91% rename from sprintf/ft_sprintf.h rename to lib/libft/srcs/sprintf/ft_sprintf.h index 0f68a06..87342e7 100644 --- a/sprintf/ft_sprintf.h +++ b/lib/libft/srcs/sprintf/ft_sprintf.h @@ -13,10 +13,7 @@ #ifndef FT_SPRINTF_H # define FT_SPRINTF_H -# include -# include -# include -# include "../lib/libft/libft.h" +# include "../../libft.h" char *ft_strfjoin(char *s1, char *s2); char *ft_strfjoinc(char *s1, char c); diff --git a/sprintf/sprintf_utils.c b/lib/libft/srcs/sprintf/sprintf_utils.c similarity index 100% rename from sprintf/sprintf_utils.c rename to lib/libft/srcs/sprintf/sprintf_utils.c diff --git a/tests/ast/ast.c b/tests/ast/ast.c new file mode 100644 index 0000000..f7084ba --- /dev/null +++ b/tests/ast/ast.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ast.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/24 08:22:16 by nalebrun #+# #+# */ +/* Updated: 2025/01/24 08:22:16 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ast.h" + +/*void ast(t_node *token_lst)*/ +/*{*/ +/* t_node *head = token_lst;*/ +/**/ +/**/ +/*}*/ diff --git a/tests/ast/ast.h b/tests/ast/ast.h new file mode 100644 index 0000000..dfedc78 --- /dev/null +++ b/tests/ast/ast.h @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ast.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/24 08:23:27 by nalebrun #+# #+# */ +/* Updated: 2025/01/24 08:23:27 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef AST_H +# define AST_H + +#include "../../includes/minishell.h" +#include "../tokenizer/tokenizer.h" + +#endif diff --git a/tests/parse.c b/tests/test.c similarity index 71% rename from tests/parse.c rename to tests/test.c index 7249baf..7441d7a 100644 --- a/tests/parse.c +++ b/tests/test.c @@ -37,6 +37,36 @@ void truncate_comment(char *str) } } +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); + 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); +} + /*static t_data *init_data(char **envp)*/ /*{*/ /* t_data *data;*/ @@ -52,13 +82,21 @@ int main(int ac, char **av, char **envp) (void)ac; (void)envp; + + if (ac != 3) + { + ft_error("./test drawio_file command_str\n"); + return (1); + } + /*t_data data;*/ /*data = init_data(envp);*/ truncate_comment(av[1]); - lst = tokenize(av[1]); + lst = tokenize(av[2]); if (!lst) return (1); debug_linked_list(lst, "Tokenized"); + gen_dio_linked_list(lst, av[1]); free_linked_list(lst); } diff --git a/tests/tokenizer/tokenizer.c b/tests/tokenizer/tokenizer.c index e9d198f..108e1d9 100644 --- a/tests/tokenizer/tokenizer.c +++ b/tests/tokenizer/tokenizer.c @@ -111,16 +111,12 @@ t_node *tokenize(char *str) head = tokenize_base(str); if (!head) return (NULL); - debug_linked_list(head, "Base_cut"); if (!trim_nodes(head)) return (NULL); - debug_linked_list(head, "Trimed"); if (!unstick_nodes(head)) return (NULL); - debug_linked_list(head, "Nodes Unsticked"); stick_quote_node(head, 39); stick_quote_node(head, '"'); - debug_linked_list(head, "Quote Sticked"); set_token(head); return (head); }