This commit is contained in:
Nathan Lebrun
2025-01-29 11:40:30 +01:00
parent 9bce0bf9cb
commit 64100fe0f4
19 changed files with 29 additions and 91 deletions

44
test/drawio/drawio.h Normal file
View File

@@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* drawio.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
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);
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);
#endif

35
test/drawio/drawio_ast.c Normal file
View File

@@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* drawio_ast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 = 120;
}
void gen_dio_ast(t_ast_n *head, int fd)
{
t_elems e;
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 ;
}

View File

@@ -0,0 +1,69 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* drawio_ast_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 : < &#10;";
else if (redir == _RED_R)
out = "redir : > &#10;";
else if (redir == _RED_DR)
out = "redir : >> &#10;";
else
out = "Not redirected &#10;";
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);
}

View File

@@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* drawio_linklist.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
}
}

View File

@@ -0,0 +1,73 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* drawio_print_ast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View File

@@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* drawio_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/27 15:09:16 by nalebrun #+# #+# */
/* Updated: 2025/01/27 15:09:16 by nalebrun ### ########.fr */
/* */
/* ************************************************************************** */
#include "drawio.h"
static 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);
}