42_solong/xpm.c

106 lines
2.7 KiB
C
Raw Normal View History

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