94 lines
2.6 KiB
C
94 lines
2.6 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* xpm.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2023/01/04 13:53:03 by cchauvet #+# #+# */
|
|
/* Updated: 2023/01/04 19:48:36 by cchauvet ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "solong.h"
|
|
|
|
static char *name_generator(t_shape shape)
|
|
{
|
|
char *out;
|
|
char *size;
|
|
char *color;
|
|
|
|
color = ft_strdup(shape.color);
|
|
if (color == NULL)
|
|
return (NULL);
|
|
size = ft_itoa(shape.size);
|
|
if (size == NULL)
|
|
return (NULL);
|
|
out = ft_strmerger(8, shape.shape, "_", color, "_", size, "x", size, ".xpm");
|
|
return (out);
|
|
}
|
|
|
|
static char *ft_map_gen(t_shape shape)
|
|
{
|
|
if (ft_strcmp(shape.shape, "square") == 0)
|
|
return (ft_square('a', shape.size, shape.size));
|
|
if (ft_strcmp(shape.shape, "triangle") == 0)
|
|
return (ft_triangle('a', 'b', shape.size, shape.size));
|
|
return (NULL);
|
|
}
|
|
|
|
char *ft_gen_xpm_content(t_shape shape)
|
|
{
|
|
char *xpm_content;
|
|
char *temp;
|
|
char *size;
|
|
char *map;
|
|
|
|
size = ft_itoa(shape.size);
|
|
if (size == NULL)
|
|
return (NULL);
|
|
temp = ft_strdup("/* XPM */\nstatic char * XFACE[] = {" "\"\n");
|
|
if (temp == NULL)
|
|
return (NULL);
|
|
xpm_content = ft_strmerger(5, temp, size, " ", size, " 2 1\",\n\"");
|
|
free(temp);
|
|
if (xpm_content == NULL)
|
|
return (NULL);
|
|
map = ft_map_gen(shape);
|
|
if (map == NULL)
|
|
return (NULL);
|
|
temp = ft_strmerger(5, xpm_content, "a c ", shape.color, "\",\n", map);
|
|
if (temp == NULL)
|
|
return (NULL);
|
|
free(xpm_content);
|
|
xpm_content = ft_strmerger(2, temp, "\"b c ", shape.bcolor, "\",\n};");
|
|
free(temp);
|
|
return (xpm_content);
|
|
}
|
|
|
|
char *ft_xpm_gen_file(t_shape shape)
|
|
{
|
|
char *file_content;
|
|
char *name;
|
|
int fd;
|
|
|
|
file_content = ft_gen_xpm_content(shape);
|
|
if (file_content == NULL)
|
|
return (NULL);
|
|
name = name_generator(shape);
|
|
if (name == NULL)
|
|
return (NULL);
|
|
name = ft_strfjoin(XPM_PATH, name);
|
|
if (name == NULL)
|
|
return (NULL);
|
|
fd = open(name, O_RDONLY);
|
|
if (fd == -1)
|
|
{
|
|
close(fd);
|
|
fd = open(name, O_WRONLY | O_CREAT, 0644);
|
|
write(fd, file_content, ft_strlen(file_content));
|
|
close(fd);
|
|
}
|
|
return (name);
|
|
}
|