drawio generator

This commit is contained in:
Nathan Lebrun
2025-01-24 14:10:50 +01:00
parent 4764c4a679
commit 035bf45ad0
14 changed files with 238 additions and 55 deletions

View File

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

View File

@@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* dio_private.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

View File

@@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* create_elem.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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, "<mxCell id=\"%d\" ", id);
ft_fprintf(fd, "value=\"%s\" ", elem->text);
ft_fprintf(fd, "style=\"rounded=%d;whiteSpace=wrap;html=1;\" ", elem->rounded);
ft_fprintf(fd, "vertex=\"1\" parent=\"1\"> \n");
ft_fprintf(fd, "<mxGeometry x=\"%d\" y=\"%d\" ", elem->x, elem->y);
ft_fprintf(fd, "width=\"%d\" height=\"%d\" ", elem->w, elem->h);
ft_fprintf(fd, "as=\"geometry\"/>\n");
ft_fprintf(fd, "</mxCell>\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, "<mxCell id=\"%d\" ", id);
ft_fprintf(fd, "edge=\"1\" parent=\"1\" ");
ft_fprintf(fd, "source=\"%d\" target=\"%d\">\n", elem->id_src, elem->id_dst);
ft_fprintf(fd, "<mxGeometry relative=\"1\" as=\"geometry\"/>\n</mxCell>\n");
return (id);
}

View File

@@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* drawio.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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, "<mxfile host=\"app.diagrams.net\" ");
ft_fprintf(fd, "modified=\"2025-01-24T12:00:00Z\" ");
ft_fprintf(fd, "agent=\"c_generator\" version=\"15.8.7\" ");
ft_fprintf(fd, "type=\"device\">\n");
ft_fprintf(fd, "<diagram id=\"GEN\" name=\"GEN-Diagram\">\n");
ft_fprintf(fd, "<mxGraphModel>\n");
ft_fprintf(fd, "<root>\n");
ft_fprintf(fd, "<mxCell id=\"0\"/>\n");
ft_fprintf(fd, "<mxCell id=\"1\" parent=\"0\"/>\n");
return(fd);
}
void drawio_end_file(int fd)
{
ft_fprintf(fd, "</root>\n</mxGraphModel>\n</diagram>\n</mxfile>");
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);
}

View File

@@ -0,0 +1,67 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_sprintf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 13:46:45 by lderidde #+# #+# */
/* Updated: 2025/01/24 13:46:45 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_sprintf.h"
static int is_spec(char c)
{
if (c == 'c' || c == 's' || c == 'd')
return (1);
return (0);
}
static char *add_arg(char c, char *str, va_list args)
{
char *out;
if (c == 'c')
out = ft_strfjoinc(str, va_arg(args, int));
else if (c == 's')
out = ft_strfjoin(str, va_arg(args, char *));
else if (c == 'd')
out = ft_strfjoind(str, ft_itoa(va_arg(args, int)));
return (out);
}
static char *build_str(const char *str, va_list args)
{
char *out;
int i;
out = NULL;
i = 0;
while (str[i])
{
if (str[i] == '%' && str[i + 1])
{
if (is_spec(str[++i]))
out = add_arg(str[i], out, args);
else
out = ft_strfjoinc(out, '%');
}
else
out = ft_strfjoinc(out, str[i]);
i++;
}
return (out);
}
char *ft_sprintf(const char *str, ...)
{
va_list args;
char *out;
va_start(args, str);
out = build_str(str, args);
va_end(args);
return (out);
}

View File

@@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_sprintf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 13:44:08 by lderidde #+# #+# */
/* Updated: 2025/01/24 13:47:01 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_SPRINTF_H
# define FT_SPRINTF_H
# include "../../libft.h"
char *ft_strfjoin(char *s1, char *s2);
char *ft_strfjoinc(char *s1, char c);
char *ft_strfjoind(char *s1, char *s2);
#endif

View File

@@ -0,0 +1,92 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sprintf_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 13:44:22 by lderidde #+# #+# */
/* Updated: 2025/01/24 13:47:10 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_sprintf.h"
char *ft_strfjoin(char *s1, char *s2)
{
char *out;
unsigned int i;
unsigned int j;
if (!s1 && !s2)
return (NULL);
if (!s1)
return (ft_strdup(s2));
if (!s2)
return (ft_strdup(s1));
out = ft_calloc(ft_strlen(s1) + ft_strlen(s2) + 1, sizeof(char));
if (!out)
return (NULL);
i = 0;
j = 0;
while (s1[j])
out[i++] = s1[j++];
j = 0;
while (s2[j])
out[i++] = s2[j++];
ft_free(&s1);
return (out);
}
char *ft_strfjoinc(char *s1, char c)
{
char *out;
unsigned int i;
unsigned int j;
if (!s1)
{
out = ft_calloc(2, sizeof(char));
if (!out)
return (NULL);
out[0] = c;
return (out);
}
out = ft_calloc(ft_strlen(s1) + 2, sizeof(char));
if (!out)
return (NULL);
i = 0;
j = 0;
while (s1[j])
out[i++] = s1[j++];
out[i] = c;
ft_free(&s1);
return (out);
}
char *ft_strfjoind(char *s1, char *s2)
{
char *out;
unsigned int i;
unsigned int j;
if (!s1 && !s2)
return (NULL);
if (!s1)
return (ft_strdup(s2));
if (!s2)
return (ft_strdup(s1));
out = ft_calloc(ft_strlen(s1) + ft_strlen(s2) + 1, sizeof(char));
if (!out)
return (NULL);
i = 0;
j = 0;
while (s1[j])
out[i++] = s1[j++];
j = 0;
while (s2[j])
out[i++] = s2[j++];
ft_free(&s1);
ft_free(&s2);
return (out);
}