42_solong/mandatory/xpm.c

121 lines
2.8 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-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)
{
char *map;
size_t x;
size_t y;
2023-01-09 14:41:06 -05:00
map = malloc(((size + 4) * size) * sizeof(char));
if (map == NULL)
return (map);
y = 0;
2023-01-09 14:41:06 -05:00
while (y < size)
{
2023-01-09 14:41:06 -05:00
map[(size + 4) * y] = '"';
x = 0;
2023-01-09 14:41:06 -05:00
while (x < size)
{
2023-01-09 14:41:06 -05:00
map[y * (size + 4) + x + 1] = 'a';
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';
y++;
}
2023-01-09 14:41:06 -05:00
map[(size + 4) * size - 2] = '\0';
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
size = ft_itoa(square.size);
2023-01-04 14:07:13 -05:00
if (size == NULL)
return (NULL);
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);
}
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;
temp = ft_itoa(square.size);
2023-01-04 14:07:13 -05:00
if (temp == NULL)
return (NULL);
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);
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-06 13:37:36 -05:00
free(temp);
return (NULL);
}
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;
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)
{
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
}