Pushing from the vogsphere to the github
This commit is contained in:
parent
d9986a5674
commit
c86fe1ad3d
33 changed files with 2093 additions and 0 deletions
64
libft/ft_printf.c
Normal file
64
libft/ft_printf.c
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_printf.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/14 17:27:44 by rparodi #+# #+# */
|
||||
/* Updated: 2024/03/06 11:25:24 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/so_long.h"
|
||||
|
||||
int ft_check_args(char c, va_list args, int *ret_value)
|
||||
{
|
||||
if (c == 'c')
|
||||
ft_putchar((char)va_arg(args, int), ret_value);
|
||||
else if (c == 's')
|
||||
ft_putstr((char *)va_arg(args, char *), ret_value);
|
||||
else if (c == 'i' || c == 'd')
|
||||
ft_putnbr((int)va_arg(args, int), ret_value);
|
||||
else if (c == '%')
|
||||
ft_putchar('%', ret_value);
|
||||
else if (c == 'u')
|
||||
ft_putnbr_unsigned((unsigned int)va_arg(args, unsigned int), ret_value);
|
||||
else if (c == 'x')
|
||||
ft_putnbr_base((unsigned int)va_arg(args, unsigned int),
|
||||
"0123456789abcdef", ret_value, c);
|
||||
else if (c == 'X')
|
||||
ft_putnbr_base((unsigned int)va_arg(args, unsigned int),
|
||||
"0123456789ABCDEF", ret_value, c);
|
||||
else if (c == 'p')
|
||||
ft_putnbr_base((unsigned long long)va_arg(args, unsigned long long),
|
||||
"0123456789abcdef", ret_value, c);
|
||||
va_end(args);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int ft_printf(const char *s, ...)
|
||||
{
|
||||
size_t i;
|
||||
va_list args;
|
||||
char *str;
|
||||
int ret_value;
|
||||
|
||||
ret_value = 0;
|
||||
str = ft_strdup(s);
|
||||
va_start(args, s);
|
||||
i = 0;
|
||||
while (str[i])
|
||||
{
|
||||
if (str[i] == '%')
|
||||
{
|
||||
ft_check_args(str[i + 1], args, &ret_value);
|
||||
i++;
|
||||
}
|
||||
else
|
||||
ft_putchar(str[i], &ret_value);
|
||||
i++;
|
||||
}
|
||||
free(str);
|
||||
return (ret_value);
|
||||
}
|
||||
82
libft/ft_put.c
Normal file
82
libft/ft_put.c
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_put.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/16 12:13:14 by rparodi #+# #+# */
|
||||
/* Updated: 2024/03/06 11:25:24 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/so_long.h"
|
||||
|
||||
void ft_putchar(char c, int *ret_value)
|
||||
{
|
||||
write(1, &c, 1);
|
||||
(*ret_value)++;
|
||||
}
|
||||
|
||||
void ft_putnbr(int nb, int *ret_value)
|
||||
{
|
||||
if (nb < 0)
|
||||
{
|
||||
if (nb == INT_MIN)
|
||||
{
|
||||
write(1, "-2147483648", 11);
|
||||
*ret_value += 11;
|
||||
return ;
|
||||
}
|
||||
nb = -nb;
|
||||
ft_putchar('-', ret_value);
|
||||
}
|
||||
if (nb >= 10)
|
||||
{
|
||||
ft_putnbr(nb / 10, ret_value);
|
||||
nb = nb % 10;
|
||||
}
|
||||
if (nb < 10)
|
||||
ft_putchar(nb + 48, ret_value);
|
||||
}
|
||||
|
||||
void ft_putnbr_base(unsigned long long nbr, char *base, int *ret_value,
|
||||
char c)
|
||||
{
|
||||
if (c == 'p')
|
||||
{
|
||||
if (nbr != 0)
|
||||
ft_putstr("0x", ret_value);
|
||||
if (nbr == 0)
|
||||
{
|
||||
ft_putstr("(nil)", ret_value);
|
||||
return ;
|
||||
}
|
||||
c++;
|
||||
}
|
||||
if (c != 'p')
|
||||
{
|
||||
if (nbr >= 16)
|
||||
ft_putnbr_base(nbr / 16, base, ret_value, c);
|
||||
ft_putchar(base[nbr % 16], ret_value);
|
||||
}
|
||||
}
|
||||
|
||||
void ft_putnbr_unsigned(unsigned int nb, int *ret_value)
|
||||
{
|
||||
if (nb >= 10)
|
||||
{
|
||||
ft_putnbr_unsigned(nb / 10, ret_value);
|
||||
nb = nb % 10;
|
||||
}
|
||||
if (nb < 10)
|
||||
ft_putchar(nb + 48, ret_value);
|
||||
}
|
||||
|
||||
void ft_putstr(char *str, int *ret_value)
|
||||
{
|
||||
if (!str)
|
||||
*ret_value += write(1, "(null)", 6);
|
||||
else
|
||||
*ret_value += write(1, str, ft_strlen(str));
|
||||
}
|
||||
114
libft/ft_reverse_split.c
Normal file
114
libft/ft_reverse_split.c
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_reverse_split.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/22 11:18:33 by rparodi #+# #+# */
|
||||
/* Updated: 2024/03/10 18:28:07 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/so_long.h"
|
||||
|
||||
size_t ft_count_line(char *path)
|
||||
{
|
||||
char *str;
|
||||
size_t count;
|
||||
int fd;
|
||||
|
||||
count = 0;
|
||||
fd = open(path, O_RDONLY);
|
||||
str = get_next_line(fd);
|
||||
while (str != NULL)
|
||||
{
|
||||
free(str);
|
||||
count++;
|
||||
str = get_next_line(fd);
|
||||
}
|
||||
free(str);
|
||||
close(fd);
|
||||
return (count);
|
||||
}
|
||||
|
||||
char **ft_init_map(t_mlx *mlx, char *path)
|
||||
{
|
||||
int check;
|
||||
size_t total_size;
|
||||
size_t j;
|
||||
|
||||
j = 0;
|
||||
check = 1;
|
||||
total_size = ft_count_line(path);
|
||||
if (mlx->map != NULL)
|
||||
ft_free_maps(mlx->map, NULL);
|
||||
mlx->map = (char **)malloc(sizeof(char *) * (total_size + 1));
|
||||
if (!mlx->map)
|
||||
ft_exit(mlx, NULL, 0, "The allocation of the map crashed");
|
||||
while (check != 0 && mlx->map != NULL)
|
||||
{
|
||||
mlx->map[j] = get_next_line(mlx->fd_map);
|
||||
if (mlx->map[j] == NULL)
|
||||
check = 0;
|
||||
j++;
|
||||
}
|
||||
return (mlx->map);
|
||||
}
|
||||
|
||||
size_t ft_strslen(char **strs)
|
||||
{
|
||||
size_t counter;
|
||||
size_t i;
|
||||
size_t j;
|
||||
|
||||
counter = 0;
|
||||
i = 0;
|
||||
j = 0;
|
||||
while (strs[j] != NULL)
|
||||
{
|
||||
while (strs[j][i] != '\0')
|
||||
{
|
||||
counter++;
|
||||
i++;
|
||||
}
|
||||
i = 0;
|
||||
j++;
|
||||
}
|
||||
return (counter);
|
||||
}
|
||||
|
||||
char *ft_convert_strs_to_str(char *str, char **strs)
|
||||
{
|
||||
size_t i;
|
||||
size_t j;
|
||||
size_t k;
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
k = 0;
|
||||
while (strs[j] != NULL)
|
||||
{
|
||||
while (strs[j][i] != '\0')
|
||||
{
|
||||
str[k] = strs[j][i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
i = 0;
|
||||
j++;
|
||||
}
|
||||
str[k] = '\0';
|
||||
return (str);
|
||||
}
|
||||
|
||||
char *ft_reverse_split(t_mlx *mlx)
|
||||
{
|
||||
const size_t total_size = ft_strslen(mlx->map);
|
||||
char *str;
|
||||
|
||||
str = (char *)malloc(total_size + 1);
|
||||
if (str == NULL)
|
||||
ft_exit(mlx, NULL, 0, "The allocation of the string crashed");
|
||||
return (ft_convert_strs_to_str(str, mlx->map));
|
||||
}
|
||||
93
libft/ft_split.c
Normal file
93
libft/ft_split.c
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_split.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/09 13:56:02 by rparodi #+# #+# */
|
||||
/* Updated: 2024/03/06 11:26:19 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/so_long.h"
|
||||
|
||||
static int count_words(const char *str, char sep)
|
||||
{
|
||||
int i;
|
||||
int count;
|
||||
|
||||
i = 0;
|
||||
count = 0;
|
||||
while (str[i])
|
||||
{
|
||||
while (str[i] == sep && str[i])
|
||||
i++;
|
||||
if (str[i] != sep && str[i])
|
||||
{
|
||||
while (str[i] != sep && str[i])
|
||||
i++;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return (count);
|
||||
}
|
||||
|
||||
static char *ft_strndup(const char *s, int j)
|
||||
{
|
||||
int i;
|
||||
char *str;
|
||||
|
||||
i = 0;
|
||||
str = (char *)malloc((j + 1));
|
||||
if (!str)
|
||||
return (NULL);
|
||||
while (s[i] && i < j)
|
||||
{
|
||||
str[i] = s[i];
|
||||
i++;
|
||||
}
|
||||
str[i] = '\0';
|
||||
return (str);
|
||||
}
|
||||
|
||||
static char **ext_w(char **words_array, const char *str, char sep, int size)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
while (j < size)
|
||||
{
|
||||
while (str[i] == sep && str[i])
|
||||
i++;
|
||||
str = str + i;
|
||||
i = 0;
|
||||
while (str[i] != sep && str[i])
|
||||
i++;
|
||||
words_array[j++] = ft_strndup(str, i);
|
||||
str = str + i;
|
||||
i = 0;
|
||||
}
|
||||
words_array[j] = 0;
|
||||
return (words_array);
|
||||
}
|
||||
|
||||
char **ft_split(char const *s, char c)
|
||||
{
|
||||
int size;
|
||||
char **words_array;
|
||||
|
||||
size = count_words(s, c);
|
||||
words_array = (char **)malloc(sizeof(char *) * (size + 1));
|
||||
if (!words_array)
|
||||
return (NULL);
|
||||
if (size == 0)
|
||||
{
|
||||
words_array[0] = NULL;
|
||||
return (words_array);
|
||||
}
|
||||
words_array = ext_w(words_array, s, c, size);
|
||||
return (words_array);
|
||||
}
|
||||
45
libft/ft_strdup.c
Normal file
45
libft/ft_strdup.c
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strdup.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/07 16:53:59 by rparodi #+# #+# */
|
||||
/* Updated: 2024/03/06 11:26:19 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/so_long.h"
|
||||
|
||||
char **ft_strsdup(char **s, t_mlx *mlx)
|
||||
{
|
||||
const size_t len = ft_count_line(mlx->path);
|
||||
size_t i;
|
||||
char **to_return;
|
||||
|
||||
i = 0;
|
||||
to_return = (char **)malloc(sizeof(char *) * mlx->size_map);
|
||||
if (!to_return)
|
||||
return (NULL);
|
||||
while (i < len)
|
||||
{
|
||||
to_return[i] = ft_strdup(s[i]);
|
||||
i++;
|
||||
}
|
||||
to_return[i] = NULL;
|
||||
return (to_return);
|
||||
}
|
||||
|
||||
char *ft_strdup(const char *s)
|
||||
{
|
||||
size_t len;
|
||||
char *to_return;
|
||||
|
||||
len = ft_strlen(s) + 1;
|
||||
to_return = (char *)malloc(sizeof(char) * len);
|
||||
if (!to_return)
|
||||
return (NULL);
|
||||
ft_strlcpy(to_return, s, len);
|
||||
return (to_return);
|
||||
}
|
||||
31
libft/ft_strlcpy.c
Normal file
31
libft/ft_strlcpy.c
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strlcpy.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/07 16:55:25 by rparodi #+# #+# */
|
||||
/* Updated: 2024/03/06 11:26:19 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/so_long.h"
|
||||
|
||||
size_t ft_strlcpy(char *dst, const char *src, size_t size)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (src[i] != '\0' && i + 1 < size)
|
||||
{
|
||||
dst[i] = src[i];
|
||||
i++;
|
||||
}
|
||||
if (size > 0)
|
||||
{
|
||||
dst[i] = '\0';
|
||||
i++;
|
||||
}
|
||||
return (ft_strlen(src));
|
||||
}
|
||||
78
libft/get_next_line.c
Normal file
78
libft/get_next_line.c
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/18 17:12:02 by rparodi #+# #+# */
|
||||
/* Updated: 2024/03/06 11:26:19 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/so_long.h"
|
||||
|
||||
char *ft_check(char *memory, int fd)
|
||||
{
|
||||
char *buffer;
|
||||
int bytescopy;
|
||||
|
||||
bytescopy = 1;
|
||||
buffer = (char *)malloc(sizeof(char) * (BUFFER_SIZE + 1));
|
||||
if (!buffer)
|
||||
return (NULL);
|
||||
while (ft_index_strchr(memory, '\n') <= 0 && bytescopy)
|
||||
{
|
||||
bytescopy = read(fd, buffer, BUFFER_SIZE);
|
||||
if (bytescopy == -1)
|
||||
return (ft_free(buffer));
|
||||
buffer[bytescopy] = '\0';
|
||||
memory = ft_strjoin(memory, buffer, 0, -1);
|
||||
}
|
||||
free(buffer);
|
||||
return (memory);
|
||||
}
|
||||
|
||||
char *file_converted(int fd)
|
||||
{
|
||||
size_t i;
|
||||
int read_bits;
|
||||
char *str;
|
||||
char c;
|
||||
|
||||
i = 0;
|
||||
c = 1;
|
||||
read_bits = 1;
|
||||
str = (char *)malloc(sizeof(char) * BUFFER_SIZE);
|
||||
if (!str)
|
||||
return (NULL);
|
||||
while (read_bits != 0 && i < BUFFER_SIZE)
|
||||
{
|
||||
read_bits = read(fd, &c, 1);
|
||||
str[i] = c;
|
||||
i++;
|
||||
}
|
||||
str[i] = '\0';
|
||||
return (str);
|
||||
}
|
||||
|
||||
char *ft_free(char *str)
|
||||
{
|
||||
free(str);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
char *get_next_line(int fd)
|
||||
{
|
||||
static char *memory = NULL;
|
||||
char *line;
|
||||
|
||||
if (fd < 0 || BUFFER_SIZE <= 0)
|
||||
return (NULL);
|
||||
memory = ft_check(memory, fd);
|
||||
if (!memory)
|
||||
return (ft_free(memory));
|
||||
line = ft_get_line(memory);
|
||||
memory = ft_get_next(memory);
|
||||
return (line);
|
||||
}
|
||||
119
libft/get_next_line_utils.c
Normal file
119
libft/get_next_line_utils.c
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/18 17:14:47 by rparodi #+# #+# */
|
||||
/* Updated: 2024/03/06 11:26:19 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/so_long.h"
|
||||
|
||||
size_t ft_strlen(char const *str)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
if (str)
|
||||
{
|
||||
while (str[i])
|
||||
i++;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
||||
char *ft_strjoin(char *s1, char *s2, int i, int j)
|
||||
{
|
||||
char *temp;
|
||||
|
||||
if (!s1)
|
||||
{
|
||||
s1 = (char *)malloc(1);
|
||||
s1[i] = '\0';
|
||||
}
|
||||
if (!s2 || !s1)
|
||||
return (NULL);
|
||||
temp = (char *)malloc((ft_strlen(s1) + ft_strlen(s2) + 1));
|
||||
if (!temp)
|
||||
return (NULL);
|
||||
while (s1[i])
|
||||
{
|
||||
temp[i] = s1[i];
|
||||
i++;
|
||||
}
|
||||
while (s2[++j])
|
||||
temp[i + j] = s2[j];
|
||||
temp[i + j] = '\0';
|
||||
free(s1);
|
||||
return (temp);
|
||||
}
|
||||
|
||||
char *ft_get_line(char *str)
|
||||
{
|
||||
char *temp;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (!str[i])
|
||||
return (NULL);
|
||||
while (str[i] && str[i] != '\n')
|
||||
i++;
|
||||
temp = (char *)malloc((i + 2));
|
||||
if (!temp)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
while (str[i] && str[i] != '\n')
|
||||
{
|
||||
temp[i] = str[i];
|
||||
i++;
|
||||
}
|
||||
if (str[i] == '\n')
|
||||
{
|
||||
temp[i] = str[i];
|
||||
i++;
|
||||
}
|
||||
temp[i] = '\0';
|
||||
return (temp);
|
||||
}
|
||||
|
||||
char *ft_get_next(char *str)
|
||||
{
|
||||
char *temp;
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = 0;
|
||||
while (str[i] && str[i] != '\n')
|
||||
i++;
|
||||
if (!str[i])
|
||||
return (ft_free(str));
|
||||
temp = (char *)malloc((ft_strlen(str) - i));
|
||||
if (!temp)
|
||||
return (NULL);
|
||||
j = i + 1;
|
||||
while (str[++i])
|
||||
temp[i - j] = str[i];
|
||||
temp[i - j] = '\0';
|
||||
free(str);
|
||||
return (temp);
|
||||
}
|
||||
|
||||
int ft_index_strchr(const char *s, char c)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (s)
|
||||
{
|
||||
while (s[i])
|
||||
{
|
||||
if (s[i] == c)
|
||||
return (i + 1);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue