This commit is contained in:
2024-01-14 23:36:45 +01:00
parent 2fd2c96579
commit b378e60269
44 changed files with 3041 additions and 7203 deletions

61
utils/ft_utils_convert.c Normal file
View File

@@ -0,0 +1,61 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_utils_convert.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 22:21:26 by fgras-ca #+# #+# */
/* Updated: 2024/01/11 22:23:53 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
static size_t ft_intlen(long n)
{
size_t len;
len = 0;
if (n == 0)
len++;
if (n < 0)
{
n *= -1;
len++;
}
while (n > 0)
{
n = n / 10;
len++;
}
return (len);
}
char *ft_itoa(int nb)
{
char *dst;
int i;
long n;
n = nb;
i = ft_intlen(n);
dst = (char *)malloc((i + 1) * sizeof(char));
if (!dst)
return (NULL);
dst[i--] = '\0';
if (n == 0)
dst[0] = '0';
if (n < 0)
{
dst[0] = '-';
n = n * -1;
}
while (n > 0)
{
dst[i] = '0' + (n % 10);
n = n / 10;
i--;
}
return (dst);
}

106
utils/ft_utils_gnl.c Normal file
View File

@@ -0,0 +1,106 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_utils_gnl.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 22:21:44 by fgras-ca #+# #+# */
/* Updated: 2024/01/11 22:24:01 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
char *ft_get_line(char *left_str)
{
int i;
char *str;
i = 0;
if (!left_str[i])
return (NULL);
while (left_str[i] && left_str[i] != '\n')
i++;
str = (char *)malloc(sizeof(char) * (i + 2));
if (!str)
return (NULL);
i = 0;
while (left_str[i] && left_str[i] != '\n')
{
str[i] = left_str[i];
i++;
}
if (left_str[i] == '\n')
{
str[i] = left_str[i];
i++;
}
str[i] = '\0';
return (str);
}
char *ft_new_left_str(char *left_str)
{
int i;
int j;
char *str;
i = 0;
while (left_str[i] && left_str[i] != '\n')
i++;
if (!left_str[i])
{
free(left_str);
return (NULL);
}
str = (char *)malloc(sizeof(char) * (ft_strlen(left_str) - i + 1));
if (!str)
return (NULL);
i++;
j = 0;
while (left_str[i])
str[j++] = left_str[i++];
str[j] = '\0';
free(left_str);
return (str);
}
char *ft_read_to_left_str(int fd, char *left_str)
{
char *buff;
int rd_bytes;
buff = malloc((BUFFER_SIZE + 1) * sizeof(char));
if (!buff)
return (NULL);
rd_bytes = 1;
while (!ft_strchr(left_str, '\n') && rd_bytes != 0)
{
rd_bytes = read(fd, buff, BUFFER_SIZE);
if (rd_bytes == -1)
{
free(buff);
return (NULL);
}
buff[rd_bytes] = '\0';
left_str = ft_strjoin(left_str, buff);
}
free(buff);
return (left_str);
}
char *get_next_line(int fd)
{
char *line;
static char *left_str;
if (fd < 0 || BUFFER_SIZE <= 0)
return (0);
left_str = ft_read_to_left_str(fd, left_str);
if (!left_str)
return (NULL);
line = ft_get_line(left_str);
left_str = ft_new_left_str(left_str);
return (line);
}

104
utils/ft_utils_split.c Normal file
View File

@@ -0,0 +1,104 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_utils_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 22:21:53 by fgras-ca #+# #+# */
/* Updated: 2024/01/11 22:24:06 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
static size_t words_count(char *s, char c)
{
size_t i;
size_t j;
i = 0;
j = 0;
while (*s)
{
if (*s != c)
i++;
else if (*s == c && i != 0)
{
j++;
i = 0;
}
s++;
}
if (i != 0)
j++;
return (j);
}
static char *word(char *s, char c)
{
char *buf;
while (*s == c)
s++;
buf = s;
while (*buf && *buf != c)
buf++;
*buf = '\0';
return (ft_strdup(s));
}
static char **free_arr(char **arr, char *s)
{
size_t i;
i = 0;
while (arr[i])
{
free(arr[i]);
i++;
}
free(arr);
free(s);
return (NULL);
}
static char **worker(char **arr, char *s1, char c, size_t j)
{
size_t i;
char *str;
str = s1;
i = 0;
while (i < j)
{
if (*s1 != c)
{
arr[i] = word(s1, c);
if (!arr[i])
return (free_arr(arr, s1));
s1 = s1 + ft_strlen(arr[i]);
i++;
}
s1++;
}
arr[i] = NULL;
free(str);
return (arr);
}
char **ft_split(char const *s, char c)
{
char **w_arr;
char *s1;
size_t j;
s1 = ft_strdup(s);
if (!s1)
return (NULL);
j = words_count(s1, c);
w_arr = (char **)malloc(sizeof(char *) * (j + 1));
if (!w_arr)
return (NULL);
return (worker(w_arr, s1, c, j));
}

88
utils/ft_utils_str_1.c Normal file
View File

@@ -0,0 +1,88 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_utils_str_1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 22:22:01 by fgras-ca #+# #+# */
/* Updated: 2024/01/11 22:24:10 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
size_t ft_strlen(const char *s)
{
size_t i;
i = 0;
if (s)
while (s[i])
i++;
return (i);
}
char *ft_strdup(const char *src)
{
char *cp;
size_t i;
i = 0;
cp = (char *)malloc(ft_strlen(src) + 1);
if (!cp)
return (NULL);
while (src[i])
{
cp[i] = src[i];
i++;
}
cp[i] = '\0';
return (cp);
}
char *ft_strjoin(char *left_str, char *buff)
{
size_t i;
size_t j;
char *str;
if (!left_str)
{
left_str = (char *)malloc(1 * sizeof(char));
left_str[0] = '\0';
}
if (!left_str || !buff)
return (NULL);
str = malloc(sizeof(char) * ((ft_strlen(left_str) + ft_strlen(buff)) + 1));
if (str == NULL)
return (NULL);
i = -1;
j = 0;
if (left_str)
while (left_str[++i] != '\0')
str[i] = left_str[i];
while (buff[j] != '\0')
str[i++] = buff[j++];
str[ft_strlen(left_str) + ft_strlen(buff)] = '\0';
free(left_str);
return (str);
}
char *ft_strchr(const char *s, int c)
{
int i;
i = 0;
if (!s)
return (0);
if (c == '\0')
return ((char *)&s[ft_strlen(s)]);
while (s[i])
{
if (s[i] == (char)c)
return ((char *)&s[i]);
i++;
}
return (0);
}