base ast + coloriage

This commit is contained in:
Nathan Lebrun
2025-01-27 19:49:48 +01:00
parent 1efc094d19
commit 5c3cd44e5a
9 changed files with 175 additions and 42 deletions

26
tests/drawio/drawio.h Normal file
View File

@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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"
// 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

83
tests/drawio/drawio_ast.c Normal file
View File

@@ -0,0 +1,83 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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 = 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 ;
}

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,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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);
}