From 8a24b6817f79cdd21efe54ee56f7e01b39f2577a Mon Sep 17 00:00:00 2001 From: Nathan Lebrun Date: Mon, 27 Jan 2025 09:55:18 +0100 Subject: [PATCH] drawio update --- lib/libft/srcs/drawio/drawio.h | 8 +- lib/libft/srcs/drawio/header/dio_private.h | 4 +- lib/libft/srcs/drawio/srcs/create_elem.c | 14 ++-- lib/libft/srcs/drawio/srcs/drawio.c | 21 +++-- tests/drawio.c | 90 ++++++++++++++++++++++ tests/parsing.h | 21 +++++ tests/test.c | 34 +------- 7 files changed, 136 insertions(+), 56 deletions(-) create mode 100644 tests/drawio.c create mode 100644 tests/parsing.h diff --git a/lib/libft/srcs/drawio/drawio.h b/lib/libft/srcs/drawio/drawio.h index 781ecd0..ad1da83 100644 --- a/lib/libft/srcs/drawio/drawio.h +++ b/lib/libft/srcs/drawio/drawio.h @@ -13,7 +13,7 @@ #ifndef DRAWIO_H # define DRAWIO_H -#include "../../libft.h" +# include "../../libft.h" typedef enum e_dio_elemtype { @@ -35,8 +35,8 @@ typedef struct s_dio_elem } 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); +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 index 3a533b7..a0ed9b6 100644 --- a/lib/libft/srcs/drawio/header/dio_private.h +++ b/lib/libft/srcs/drawio/header/dio_private.h @@ -10,8 +10,8 @@ /* */ /* ************************************************************************** */ -#ifndef DIO_PRIVATE -# define DIO_PRIVATE +#ifndef DIO_PRIVATE_H +# define DIO_PRIVATE_H int draw_rect(int fd, int id, t_dio_elem *elem); int draw_arrow(int fd, int id, t_dio_elem *elem); diff --git a/lib/libft/srcs/drawio/srcs/create_elem.c b/lib/libft/srcs/drawio/srcs/create_elem.c index 02838c9..6728863 100644 --- a/lib/libft/srcs/drawio/srcs/create_elem.c +++ b/lib/libft/srcs/drawio/srcs/create_elem.c @@ -17,12 +17,12 @@ 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, "style=\"rounded=%d;whiteSpace=wrap;", elem->rounded); + ft_fprintf(fd, "html=1;\" 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"); + ft_fprintf(fd, "\n"); return (id); } @@ -31,8 +31,8 @@ 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"); + ft_fprintf(fd, "edge=\"1\" parent=\"1\" source="); + ft_fprintf(fd, "\"%d\" target=\"%d\">\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 index e442717..e97b122 100644 --- a/lib/libft/srcs/drawio/srcs/drawio.c +++ b/lib/libft/srcs/drawio/srcs/drawio.c @@ -13,33 +13,32 @@ #include "../drawio.h" #include "../header/dio_private.h" -int drawio_init(char *file_path) +int drawio_init(char *file_path) { - int fd; + 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"); + ft_fprintf(fd, "\n"); + ft_fprintf(fd, "\n"); ft_fprintf(fd, "\n"); - return(fd); + return (fd); } -void drawio_end_file(int 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) +int drawio_create_elem(int fd, t_dio_elem *elem) { - static int id = 1; + static int id = 1; id ++; if (elem->type == ARROW) diff --git a/tests/drawio.c b/tests/drawio.c new file mode 100644 index 0000000..4dafb0b --- /dev/null +++ b/tests/drawio.c @@ -0,0 +1,90 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* drawio.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/24 14:41:02 by nalebrun #+# #+# */ +/* Updated: 2025/01/24 14:41:02 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parsing.h" +#include "tokenizer/tokenizer.h" + +int get_amp_count(char *str) +{ + int i; + int count; + + i = 0; + count = 0; + while (str[i]) + { + if (str[i] == '&') + count++; + i++; + } + return (count); +} + +char* replace_ampercent(char *src) +{ + int i; + int j; + int amp_count; + char *out; + + i = -1; + j = 0; + amp_count = get_amp_count(src); + out = malloc(ft_strlen(src) + amp_count * 4); + while (src[++i]) + { + if (src[i] == '&') + { + out[j] = '&'; + out[j + 1] = 'a'; + out[j + 2] = 'm'; + out[j + 3] = 'p'; + out[j + 4] = ';'; + j += 5; + } + else + out[j++] = src[i]; + } + out[j] = 0; + 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 new file mode 100644 index 0000000..f6c207d --- /dev/null +++ b/tests/parsing.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parsing.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/24 14:45:28 by nalebrun #+# #+# */ +/* Updated: 2025/01/24 14:45:28 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PARSING_H +# define PARSING_H + +# include "tokenizer/tokenizer.h" + +// drawio +void gen_dio_linked_list(t_node *head, char *fp); + +#endif diff --git a/tests/test.c b/tests/test.c index 7441d7a..ad33661 100644 --- a/tests/test.c +++ b/tests/test.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "tokenizer/tokenizer.h" +#include "parsing.h" /*#include "../includes/env.h"*/ void truncate_comment(char *str) @@ -37,36 +37,6 @@ 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;*/ @@ -96,7 +66,7 @@ int main(int ac, char **av, char **envp) lst = tokenize(av[2]); if (!lst) return (1); - debug_linked_list(lst, "Tokenized"); gen_dio_linked_list(lst, av[1]); + debug_linked_list(lst, "ff"); free_linked_list(lst); }