42_solong/xpm.c
Camille Chauvet 975e0c2ef5 kekw
2023-01-05 19:04:29 +01:00

100 lines
2.7 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* xpm.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/04 13:53:03 by cchauvet #+# #+# */
/* Updated: 2023/01/05 13:47:30 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "solong.h"
static char *name_generator(t_shape shape)
{
char *out;
char *size;
char *color;
char *temp;
color = ft_strdup(shape.color);
if (color == NULL)
return (NULL);
size = ft_itoa(shape.size);
if (size == NULL)
return (NULL);
temp = ft_strmerger(5, XPM_PATH, shape.shape, "_", color, "_");
if (temp == NULL)
return (NULL);
out = ft_strmerger(5, temp, 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 *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);
content = ft_strmerger(5, temp, size, " ", size, " 2 1\",\n\"");
free(temp);
if (content == NULL)
return (NULL);
map = ft_map_gen(shape);
if (map == NULL)
return (NULL);
temp = ft_strmerger(4, content, "a c ", shape.color, "\",\n\"");
if (temp == NULL)
return (NULL);
free(content);
content = ft_strmerger(6, temp, "b c ", shape.bcolor, "\",\n", map, "};");
free(temp);
return (content);
}
char *ft_xpm_gen_file(t_shape shape)
{
char *file_content;
char *path;
int fd;
file_content = ft_gen_xpm_content(shape);
if (file_content == NULL)
return (NULL);
path = name_generator(shape);
if (path == NULL)
{
free(file_content);
return (NULL);
}
fd = open(path, O_RDONLY);
if (fd == -1)
{
close(fd);
fd = open(path, O_WRONLY | O_CREAT, 0644);
if (fd == -1)
return (NULL);
write(fd, file_content, ft_strlen(file_content));
close(fd);
}
return (path);
}