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-16 12:38:47 -05:00
|
|
|
/* Updated: 2023/01/16 18:14:56 by cchauvet ### ########.fr */
|
2023-01-04 14:07:13 -05:00
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
|
|
#include "solong.h"
|
|
|
|
|
2023-01-09 14:41:06 -05:00
|
|
|
static char *ft_square(size_t size)
|
2023-01-08 12:03:40 -05:00
|
|
|
{
|
|
|
|
char *map;
|
|
|
|
size_t x;
|
|
|
|
size_t y;
|
|
|
|
|
2023-01-09 14:41:06 -05:00
|
|
|
map = malloc(((size + 4) * size) * sizeof(char));
|
2023-01-08 12:03:40 -05:00
|
|
|
if (map == NULL)
|
|
|
|
return (map);
|
|
|
|
y = 0;
|
2023-01-09 14:41:06 -05:00
|
|
|
while (y < size)
|
2023-01-08 12:03:40 -05:00
|
|
|
{
|
2023-01-09 14:41:06 -05:00
|
|
|
map[(size + 4) * y] = '"';
|
2023-01-08 12:03:40 -05:00
|
|
|
x = 0;
|
2023-01-09 14:41:06 -05:00
|
|
|
while (x < size)
|
2023-01-08 12:03:40 -05:00
|
|
|
{
|
2023-01-09 14:41:06 -05:00
|
|
|
map[y * (size + 4) + x + 1] = 'a';
|
2023-01-08 12:03:40 -05:00
|
|
|
x++;
|
|
|
|
}
|
2023-01-09 14:41:06 -05:00
|
|
|
map[(size + 4) * y + size + 1] = '"';
|
|
|
|
map[(size + 4) * y + size + 2] = ',';
|
|
|
|
map[(size + 4) * y + size + 3] = '\n';
|
2023-01-08 12:03:40 -05:00
|
|
|
y++;
|
|
|
|
}
|
2023-01-09 14:41:06 -05:00
|
|
|
map[(size + 4) * size - 2] = '\0';
|
2023-01-08 12:03:40 -05:00
|
|
|
return (map);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *name_generator(t_square square)
|
2023-01-04 14:07:13 -05:00
|
|
|
{
|
|
|
|
char *out;
|
|
|
|
char *size;
|
2023-01-05 13:04:29 -05:00
|
|
|
char *temp;
|
2023-01-04 14:07:13 -05:00
|
|
|
|
2023-01-08 12:03:40 -05:00
|
|
|
size = ft_itoa(square.size);
|
2023-01-04 14:07:13 -05:00
|
|
|
if (size == NULL)
|
|
|
|
return (NULL);
|
2023-01-08 12:03:40 -05:00
|
|
|
temp = ft_strmerger(3, XPM_PATH, square.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);
|
|
|
|
}
|
|
|
|
|
2023-01-08 12:03:40 -05:00
|
|
|
static char *ft_gen_xpm_content(t_square square)
|
2023-01-04 14:07:13 -05:00
|
|
|
{
|
2023-01-05 13:04:29 -05:00
|
|
|
char *content;
|
2023-01-04 14:07:13 -05:00
|
|
|
char *temp;
|
|
|
|
char *map;
|
|
|
|
|
2023-01-08 12:03:40 -05:00
|
|
|
temp = ft_itoa(square.size);
|
2023-01-04 14:07:13 -05:00
|
|
|
if (temp == NULL)
|
|
|
|
return (NULL);
|
2023-01-08 12:03:40 -05:00
|
|
|
content = ft_strmerger(5, XPM_HEADER, temp, " ", temp, " 1 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-08 12:03:40 -05:00
|
|
|
temp = ft_strmerger(5, "/* XPM */", content, "a c ", square.color, "\",\n");
|
|
|
|
free(content);
|
2023-01-04 14:07:13 -05:00
|
|
|
if (temp == NULL)
|
|
|
|
return (NULL);
|
2023-01-09 14:41:06 -05:00
|
|
|
map = ft_square(square.size);
|
2023-01-06 13:37:36 -05:00
|
|
|
if (map == NULL)
|
2023-01-08 12:03:40 -05:00
|
|
|
{
|
2023-01-06 13:37:36 -05:00
|
|
|
free(temp);
|
|
|
|
return (NULL);
|
2023-01-08 12:03:40 -05:00
|
|
|
}
|
|
|
|
content = ft_strmerger(3, temp, 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
|
|
|
}
|
|
|
|
|
2023-01-11 13:10:28 -05:00
|
|
|
char *ft_xpm_gen_file(t_square square)
|
2023-01-04 14:07:13 -05:00
|
|
|
{
|
|
|
|
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-08 12:03:40 -05:00
|
|
|
path = name_generator(square);
|
2023-01-05 13:04:29 -05:00
|
|
|
if (path == NULL)
|
2023-01-11 13:10:28 -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-08 12:03:40 -05:00
|
|
|
file_content = ft_gen_xpm_content(square);
|
2023-01-05 13:04:29 -05:00
|
|
|
fd = open(path, O_WRONLY | O_CREAT, 0644);
|
2023-01-16 12:38:47 -05:00
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
free(path);
|
|
|
|
return (NULL);
|
|
|
|
}
|
2023-01-04 14:07:13 -05:00
|
|
|
write(fd, file_content, ft_strlen(file_content));
|
2023-01-10 13:04:46 -05:00
|
|
|
if (fd != -1)
|
|
|
|
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-11 13:10:28 -05:00
|
|
|
return (path);
|
2023-01-04 14:07:13 -05:00
|
|
|
}
|