Added libft and mlx

This commit is contained in:
Théo Champion 2025-05-03 22:54:55 +02:00
parent 8448ae3a23
commit 82d06d234a
122 changed files with 10400 additions and 0 deletions

40
libft/Makefile Normal file
View file

@ -0,0 +1,40 @@
CC=cc
CFLAGS=-Wall -Wextra -Werror -c
SOURCEFILES=src/str/ft_atoi.c src/mem/ft_bzero.c src/mem/ft_calloc.c src/cond/ft_isalnum.c \
src/cond/ft_isalpha.c src/cond/ft_isascii.c src/cond/ft_isdigit.c \
src/cond/ft_isprint.c src/str/ft_itoa.c src/lst/ft_lstadd_back_bonus.c \
src/lst/ft_lstadd_front_bonus.c src/lst/ft_lstclear_bonus.c \
src/lst/ft_lstdelone_bonus.c src/lst/ft_lstiter_bonus.c \
src/lst/ft_lstlast_bonus.c src/lst/ft_lstmap_bonus.c \
src/lst/ft_lstnew_bonus.c src/lst/ft_lstsize_bonus.c src/mem/ft_memchr.c \
src/mem/ft_memcmp.c src/mem/ft_memcpy.c src/mem/ft_memmove.c src/mem/ft_memset.c \
src/printf/ft_printf.c src/io/ft_putchar_fd.c src/io/ft_putendl_fd.c \
src/io/ft_putnbr_fd.c src/io/ft_putstr_fd.c src/str/ft_split.c \
src/str/ft_strchr.c src/str/ft_strdup.c src/str/ft_striteri.c \
src/str/ft_strjoin.c src/str/ft_strlcat.c src/str/ft_strlcpy.c \
src/str/ft_strlen.c src/str/ft_strmapi.c src/str/ft_strncmp.c \
src/str/ft_strnstr.c src/str/ft_strrchr.c src/str/ft_strtrim.c \
src/str/ft_substr.c src/str/ft_tolower.c src/str/ft_toupper.c \
src/gnl/get_next_line.c src/gnl/get_next_line_utils.c src/printf/printhex.c \
src/printf/printnumbers.c src/printf/printptr.c
OBJECTS=$(patsubst src/%.c,objects/%.o,$(SOURCEFILES))
OBJDIR=objects
NAME=libft.a
all: $(OBJECTS) $(NAME)
$(NAME): $(OBJECTS)
ar -rcs libft.a $(OBJECTS)
$(OBJDIR)/%.o: src/%.c
$(CC) $(CFLAGS) $< -o $@
clean:
rm -f $(OBJECTS)
fclean: clean
rm -f $(NAME)
re: fclean all
.PHONY: all clean fclean re

11
libft/README.md Normal file
View file

@ -0,0 +1,11 @@
# Updated LibFT
For my projects @ 42 Le Havre.
# Todo
- [x] Add ft_printf
- [x] Add get_next_line
- [ ] Rework the file structure (ie. src/string src/list src/unga_bunga)
- [ ] Add a way to manage memory in a more simple way
- [ ] Add other libc functions
- [ ] Add test suite
- [ ] idk, continue this list I guess.

View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/31 16:29:36 by tchampio #+# #+# */
/* Updated: 2024/11/04 15:30:25 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_PRINTF_H
# define FT_PRINTF_H
# include <stdarg.h>
# include <stdlib.h>
# include <stdint.h>
# include <unistd.h>
# include "../includes/libft.h"
# include "../includes/ft_printf.h"
int ft_printf(const char *format, ...);
char *dec_to_hexa_conversion(int decimal_Number);
int ft_print_ptr_nb(uintptr_t decimal_Number);
int ft_print_ptr(void *ptr);
int ft_print_integer(int i);
int ft_print_uinteger(unsigned int i);
int ft_print_hex(unsigned int num, const char format);
void ft_put_hex(unsigned int num, const char format);
int ft_hex_len(unsigned int num);
#endif

View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/25 10:08:13 by tchampio #+# #+# */
/* Updated: 2024/12/02 17:20:24 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef GET_NEXT_LINE_H
# define GET_NEXT_LINE_H
# include <unistd.h>
# include <stddef.h>
# include <stdlib.h>
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 69
# endif
char *_get_next_line(int fd);
char *_ft_strchr(const char *s, int c);
void *_ft_memcpy(void *dest, const void *src, size_t n);
size_t _ft_strlen(const char *s);
void _ft_strcat(char **dst_ptr, const char *src);
#endif

75
libft/includes/libft.h Normal file
View file

@ -0,0 +1,75 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/14 12:40:57 by tchampio #+# #+# */
/* Updated: 2024/12/18 04:40:53 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFT_H
# define LIBFT_H
# include <stddef.h>
# include <stdarg.h>
# include <stdlib.h>
# include <stdint.h>
# include <stdbool.h>
# include <unistd.h>
typedef struct s_list
{
void *content;
struct s_list *next;
} t_list;
int ft_isalpha(int c);
int ft_isdigit(int c);
int ft_isalnum(int c);
int ft_isascii(int c);
int ft_isprint(int c);
size_t ft_strlen(const char *s);
void *ft_memset(void *s, int c, size_t n);
void ft_bzero(void *s, size_t n);
void *ft_memcpy(void *dest, const void *src, size_t n);
void *ft_memmove(void *dest, const void *src, size_t n);
size_t ft_strlcpy(char *dest, const char *src, size_t n);
size_t ft_strlcat(char *dest, const char *src, size_t n);
char *ft_strchr(const char *str, int c);
char *ft_strrchr(char *str, int c);
int ft_toupper(int c);
int ft_tolower(int c);
int ft_strncmp(const char *s1, const char *s2, size_t n);
void *ft_memchr(void *s, int c, size_t n);
int ft_memcmp(const void *s1, const void *s2, size_t n);
char *ft_strnstr(const char *big, const char *little, size_t n);
int ft_atoi(const char *s);
void *ft_calloc(size_t memsize, size_t count);
char *ft_strdup(const char *s);
char *ft_substr(const char *s, unsigned int start, size_t len);
char *ft_strjoin(const char *s1, const char *s2);
char *ft_strtrim(const char *s1, const char *set);
char **ft_split(const char *s, char separator);
char *ft_itoa(int n);
char *ft_strmapi(const char *s, char (*f)(unsigned int, char));
void ft_striteri(char *s, void (*f)(unsigned int, char *));
void ft_putchar_fd(char c, int fd);
void ft_putendl_fd(char *s, int fd);
void ft_putstr_fd(char *s, int fd);
void ft_putnbr_fd(int s, int fd);
t_list *ft_lstnew(void *content);
void ft_lstadd_front(t_list **lst, t_list *new);
int ft_lstsize(t_list *lst);
t_list *ft_lstlast(t_list *lst);
void ft_lstadd_back(t_list **lst, t_list *new);
void ft_lstdelone(t_list *lst, void (*del)(void *));
void ft_lstclear(t_list **lst, void (*del)(void *));
void ft_lstiter(t_list *lst, void (*f)(void *));
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
char *get_next_line(int fd);
int ft_printf(const char *format, ...);
#endif

0
libft/objects/.gitkeep Normal file
View file

View file

View file

View file

View file

View file

View file

View file

View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalnum.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/14 14:56:25 by tchampio #+# #+# */
/* Updated: 2024/10/14 15:03:40 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isalnum(int c)
{
if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z'))
return (1);
return (0);
}

View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalpha.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/14 13:38:13 by tchampio #+# #+# */
/* Updated: 2024/10/14 13:55:09 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isalpha(int c)
{
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
return (1);
else
return (0);
}

View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isascii.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/14 15:07:15 by tchampio #+# #+# */
/* Updated: 2024/10/14 15:27:27 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isascii(int c)
{
if (c >= 0 && c <= 127)
return (1);
return (0);
}

View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/14 13:57:00 by tchampio #+# #+# */
/* Updated: 2024/10/14 14:52:41 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isdigit(int c)
{
if (c >= '0' && c <= '9')
return (1);
return (0);
}

View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isprint.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/14 15:07:15 by tchampio #+# #+# */
/* Updated: 2024/10/14 15:30:57 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isprint(int c)
{
if (c >= 32 && c <= 126)
return (1);
return (0);
}

View file

@ -0,0 +1,106 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/30 11:15:38 by tchampio #+# #+# */
/* Updated: 2024/12/02 17:19:36 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/get_next_line.h"
void getlinefrombuffer(char **line, char *buffer)
{
size_t remaining;
char *newline_pos;
char saved_char;
newline_pos = _ft_strchr(buffer, '\n');
if (newline_pos)
{
saved_char = newline_pos[1];
newline_pos[1] = '\0';
_ft_strcat(line, buffer);
if (*line == NULL)
return ;
newline_pos[1] = saved_char;
newline_pos++;
}
else
{
_ft_strcat(line, buffer);
if (*line == NULL)
return ;
newline_pos = buffer + _ft_strlen(buffer);
}
remaining = _ft_strlen(newline_pos);
_ft_memcpy(buffer, newline_pos, remaining);
buffer[remaining] = '\0';
}
char *rereadbuffer(char *buffer)
{
size_t len;
char *line;
line = _ft_strchr(buffer, '\n');
if (line == NULL)
line = _ft_strchr(buffer, '\0');
len = line - buffer + (*line == '\n');
line = (char *)malloc(len + 1);
if (line == NULL)
return (NULL);
_ft_memcpy(line, buffer, len);
line[len] = '\0';
_ft_memcpy(buffer, buffer + len, BUFFER_SIZE - len);
buffer[BUFFER_SIZE - len] = '\0';
return (line);
}
char *readfd(int fd, char *buffer)
{
ssize_t bytes;
char *line;
line = rereadbuffer(buffer);
while (!_ft_strchr(line, '\n'))
{
bytes = read(fd, buffer, BUFFER_SIZE);
if (bytes < 0)
{
free(line);
return (NULL);
}
buffer[bytes] = '\0';
if (bytes == 0 || _ft_strchr(buffer, '\n'))
{
getlinefrombuffer(&line, buffer);
return (line);
}
_ft_strcat(&line, buffer);
if (line == NULL)
return (NULL);
}
return (line);
}
char *get_next_line(int fd)
{
static char buffer[BUFFER_SIZE + 1] = {0};
char *line;
if (fd < 0 || BUFFER_SIZE < 0)
return (NULL);
line = readfd(fd, buffer);
if (line == NULL)
return (NULL);
if (_ft_strlen(line) == 0)
{
free(line);
return (NULL);
}
return (line);
}

View file

@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/25 10:09:25 by tchampio #+# #+# */
/* Updated: 2024/12/02 17:20:10 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/get_next_line.h"
char *_ft_strchr(const char *s, int c)
{
while (*s != '\0' && *s != c)
++s;
if (*s != '\0' || c == '\0')
return ((char *)s);
return (NULL);
}
void *_ft_memcpy(void *dest, const void *src, size_t n)
{
size_t i;
i = 0;
while (i++ < n)
*(char *)dest++ = *(char *)src++;
return ((void *)((char *)dest - i));
}
size_t _ft_strlen(const char *s)
{
const char *org = s;
while (*s)
++s;
return (s - org);
}
void _ft_strcat(char **dst_ptr, const char *src)
{
size_t dst_len;
size_t src_len;
char *ans;
dst_len = _ft_strlen(*dst_ptr);
src_len = _ft_strlen(src);
ans = malloc(dst_len + src_len + 1);
if (ans)
{
_ft_memcpy(ans, *dst_ptr, dst_len);
_ft_memcpy(ans + dst_len, src, src_len);
ans[dst_len + src_len] = '\0';
}
free(*dst_ptr);
*dst_ptr = ans;
}

View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/18 10:13:00 by tchampio #+# #+# */
/* Updated: 2024/10/18 10:21:39 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void ft_putchar_fd(char c, int fd)
{
write(fd, &c, 1);
}

View file

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putendl_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/18 10:26:53 by tchampio #+# #+# */
/* Updated: 2024/10/18 10:40:11 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "../../includes/libft.h"
void ft_putendl_fd(char *s, int fd)
{
if (!s)
return ;
ft_putstr_fd(s, fd);
ft_putchar_fd('\n', fd);
}

View file

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/18 10:31:07 by tchampio #+# #+# */
/* Updated: 2024/10/18 10:34:25 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "../../includes/libft.h"
#define MIN_INT -2147483648
void ft_putnbr_fd(int nb, int fd)
{
if (nb == MIN_INT)
{
ft_putchar_fd('-', fd);
ft_putchar_fd('2', fd);
nb = 147483648;
}
if (nb < 0)
{
ft_putchar_fd('-', fd);
nb = -nb;
}
if (nb >= 10)
{
ft_putnbr_fd(nb / 10, fd);
ft_putnbr_fd(nb % 10, fd);
}
else
ft_putchar_fd('0' + nb, fd);
}

View file

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/18 10:23:40 by tchampio #+# #+# */
/* Updated: 2024/10/18 10:28:57 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "../../includes/libft.h"
void ft_putstr_fd(char *s, int fd)
{
size_t size;
if (!s)
return ;
size = ft_strlen(s);
write(fd, s, size);
}

View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_back.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/22 12:56:49 by tchampio #+# #+# */
/* Updated: 2024/10/22 13:21:51 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
void ft_lstadd_back(t_list **lst, t_list *new)
{
t_list *last;
if (lst)
{
if (*lst)
{
last = ft_lstlast(*lst);
last->next = new;
}
else
*lst = new;
}
}

View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_front_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/21 13:27:19 by tchampio #+# #+# */
/* Updated: 2024/10/22 13:02:46 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
#include <stdlib.h>
void ft_lstadd_front(t_list **lst, t_list *new)
{
new->next = *lst;
*lst = new;
}

View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstclear_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/22 14:31:30 by tchampio #+# #+# */
/* Updated: 2024/10/22 14:44:42 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
#include <stdlib.h>
void ft_lstclear(t_list **lst, void (*del)(void *))
{
t_list *next;
if (!lst || !del || !*lst)
return ;
while (lst && *lst)
{
next = (*lst)->next;
ft_lstdelone(*lst, del);
*lst = next;
}
}

View file

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstdelone_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/22 13:33:02 by tchampio #+# #+# */
/* Updated: 2024/10/22 14:29:59 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
#include <stdlib.h>
void ft_lstdelone(t_list *lst, void (*del)(void *))
{
if (!lst || !del)
return ;
(*del)(lst->content);
free(lst);
}

View file

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstiter_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/22 14:46:27 by tchampio #+# #+# */
/* Updated: 2024/10/22 15:12:21 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
void ft_lstiter(t_list *lst, void (*f)(void *))
{
t_list *current;
if (!lst || !f)
return ;
current = lst;
while (current)
{
(*f)(current->content);
current = current->next;
}
}

View file

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstlast_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/22 12:43:20 by tchampio #+# #+# */
/* Updated: 2024/10/22 13:03:40 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
t_list *ft_lstlast(t_list *lst)
{
t_list *current;
if (!lst)
return (NULL);
current = lst;
while (current->next != NULL)
current = current->next;
return (current);
}

View file

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/23 15:52:26 by tchampio #+# #+# */
/* Updated: 2024/10/23 17:24:57 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *newlist;
t_list *newnode;
void *content;
if (!lst || !f || !del)
return (NULL);
newlist = NULL;
while (lst)
{
content = f(lst->content);
newnode = ft_lstnew(content);
if (!newnode)
{
ft_lstclear(&newlist, del);
del(content);
return (NULL);
}
ft_lstadd_back(&newlist, newnode);
lst = lst->next;
}
return (newlist);
}

View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstnew_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/21 12:51:42 by tchampio #+# #+# */
/* Updated: 2024/10/21 14:12:56 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
#include <stdlib.h>
t_list *ft_lstnew(void *content)
{
t_list *newlist;
newlist = (t_list *)malloc(sizeof(t_list));
if (!newlist)
return (NULL);
newlist->content = content;
newlist->next = NULL;
return (newlist);
}

View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstsize_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/22 11:57:52 by tchampio #+# #+# */
/* Updated: 2024/10/22 13:12:56 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
int ft_lstsize(t_list *lst)
{
int i;
i = 0;
while (lst)
{
i++;
lst = lst->next;
}
return (i);
}

18
libft/src/mem/ft_bzero.c Normal file
View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_bzero.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 09:39:47 by tchampio #+# #+# */
/* Updated: 2024/10/15 10:15:31 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
void ft_bzero(void *s, size_t n)
{
ft_memset(s, '\0', n);
}

31
libft/src/mem/ft_calloc.c Normal file
View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/16 10:10:41 by tchampio #+# #+# */
/* Updated: 2024/10/18 16:02:48 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "../../includes/libft.h"
#include <limits.h>
#include <stdint.h>
void *ft_calloc(size_t memsize, size_t count)
{
size_t total;
void *allocated;
total = memsize * count;
if (memsize != 0 && total / memsize != count)
return (NULL);
allocated = malloc(total);
if (allocated == NULL)
return (NULL);
ft_bzero(allocated, total);
return (allocated);
}

27
libft/src/mem/ft_memchr.c Normal file
View file

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 13:30:59 by tchampio #+# #+# */
/* Updated: 2024/10/15 16:22:14 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
char *ft_memchr(void *s, int c, size_t n)
{
size_t i;
i = 0;
while (i < n)
{
if (((unsigned char *)s)[i] == (unsigned char)c)
return ((void *)(s + i));
i++;
}
return (NULL);
}

29
libft/src/mem/ft_memcmp.c Normal file
View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 15:02:03 by tchampio #+# #+# */
/* Updated: 2024/10/23 10:42:19 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
int ft_memcmp(const void *s1, const void *s2, size_t n)
{
size_t i;
i = 0;
while (((unsigned char *)s1 || (unsigned char *)s2) && i < n)
{
if (*(unsigned char *)s1 != *(unsigned char *)s2)
return (*(unsigned char *)s1 - *(unsigned char *)s2);
s1++;
s2++;
i++;
}
return (0);
}

28
libft/src/mem/ft_memcpy.c Normal file
View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 09:58:47 by tchampio #+# #+# */
/* Updated: 2024/10/19 14:24:53 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
void *ft_memcpy(void *dest, const void *src, size_t n)
{
size_t i;
if (!dest && !src)
return (NULL);
i = 0;
while (i < n)
{
((unsigned char *)dest)[i] = ((unsigned char *)src)[i];
i++;
}
return (dest);
}

130
libft/src/mem/ft_memmove.c Normal file
View file

@ -0,0 +1,130 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memmove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 10:17:04 by tchampio #+# #+# */
/* Updated: 2024/10/21 09:44:05 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
void *ft_memmove(void *dest, const void *src, size_t n)
{
size_t i;
if (n == 0 || dest == src)
return (dest);
if (src < dest)
{
i = n;
while (i > 0)
{
i--;
((unsigned char *)dest)[i] = ((unsigned char *)src)[i];
}
}
else
{
i = 0;
while (i < n)
{
((unsigned char *)dest)[i] = ((unsigned char *)src)[i];
i++;
}
}
return (dest);
}
/*
#include <stdio.h>
#include <string.h>
// Prototype de la fonction ft_memmove que tu devras implémenter
void *ft_memmove(void *dest, const void *src, size_t n);
// Macros pour les couleurs
#define RESET "\033[0m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define BOLDWHITE "\033[1m\033[37m"
// Fonction de test
void test_ft_memmove(void *dest, const void *src, size_t n,
const void *expected, int *success_count) {
// Utilisation de ft_memmove
ft_memmove(dest, src, n);
// Comparaison des résultats
if (memcmp(dest, expected, n) == 0) {
printf(GREEN "Test passed: ft_memmove(%p, %p, %zu)\n" RESET,
dest, src, n);
(*success_count)++;
} else {
printf(RED "Test failed: ft_memmove(%p, %p, %zu)\nExpected: ",
dest, src, n);
for (size_t i = 0; i < n; i++) {
printf("%02x ", ((unsigned char *)expected)[i]);
}
printf("\nGot: ");
for (size_t i = 0; i < n; i++) {
printf("%02x ", ((unsigned char *)dest)[i]);
}
printf("\n" RESET);
}
}
int main() {
int success_count = 0;
int total_tests = 0;
// Déclaration des buffers
char buffer1[50], buffer2[50];
// Test 1 : Copier une petite chaîne de caractères
memset(buffer1, 0, sizeof(buffer1)); // Nettoyage du buffer
const char *src1 = "Hello";
test_ft_memmove(buffer1, src1, strlen(src1) + 1, src1, &success_count);
total_tests++; // Copie de "Hello" avec '\0'
// Test 2 : Copier une chaîne vide
memset(buffer1, 0, sizeof(buffer1)); // Nettoyage du buffer
const char *src2 = "";
test_ft_memmove(buffer1, src2, 1, src2, &success_count);
total_tests++; // Copie de la chaîne vide
// Test 3 : Copier un tableau de chiffres
memset(buffer1, 0, sizeof(buffer1)); // Nettoyage du buffer
const char src3[] = {1, 2, 3, 4, 5};
const char expected3[] = {1, 2, 3, 4, 5};
test_ft_memmove(buffer1, src3, sizeof(src3), expected3, &success_count);
total_tests++; // Copie du tableau {1, 2, 3, 4, 5}
// Test 4 : Cas de chevauchement (src avant dest)
memset(buffer1, 0, sizeof(buffer1)); // Nettoyage du buffer
const char overlap_src1[] = "Overlap Test";
memcpy(buffer1, overlap_src1, sizeof(overlap_src1));
test_ft_memmove(buffer1 + 5, buffer1, strlen(overlap_src1) + 1,
buffer1 + 5, &success_count);
total_tests++; // Chevauchement : src est avant dest
// Test 5 : Cas de chevauchement (dest avant src)
memset(buffer1, 0, sizeof(buffer1)); // Nettoyage du buffer
memcpy(buffer1, overlap_src1, sizeof(overlap_src1));
test_ft_memmove(buffer1, buffer1 + 5, 10, buffer1, &success_count);
total_tests++; // Chevauchement : dest est avant src
// Test 6 : Copier 0 octets (ne doit rien changer)
strcpy(buffer2, "No change");
test_ft_memmove(buffer2, "Nothing", 0, "No change", &success_count);
total_tests++; // Copier 0 octets, doit rester inchangé
// Résumé des résultats
printf(BOLDWHITE "\nTotal tests: %d, Success: %d, Failures: %d\n" RESET,
total_tests, success_count, total_tests - success_count);
return 0;
}
*/

105
libft/src/mem/ft_memset.c Normal file
View file

@ -0,0 +1,105 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/14 15:56:53 by tchampio #+# #+# */
/* Updated: 2024/10/14 17:27:38 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
void *ft_memset(void *s, int c, size_t n)
{
size_t i;
i = 0;
while (i < n)
{
((char *)s)[i] = c;
i++;
}
return (s);
}
/*
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
// Prototype de la fonction ft_memset que tu devras implémenter
void *ft_memset(void *s, int c, size_t n);
// Macros pour les couleurs
#define RESET "\033[0m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define BOLDWHITE "\033[1m\033[37m"
// Fonction de test
void test_ft_memset(void *s, int c, size_t n, const char *expected,
int *success_count) {
// Utilisation de ft_memset
ft_memset(s, c, n);
// Comparaison des résultats
if (memcmp(s, expected, n) == 0) {
printf(GREEN "Test passed: ft_memset(%p, %d, %zu)\n" RESET, s, c, n);
(*success_count)++;
} else {
printf(RED "Test failed: ft_memset(%p, %d, %zu)\nExpected: %s,
Got: ", s, c, n, expected);
for (size_t i = 0; i < n; i++) {
printf("%02x ", ((unsigned char *)s)[i]);
}
printf("\n" RESET);
}
}
int main() {
int success_count = 0;
int total_tests = 0;
// Tests de ft_memset
char buffer[50];
// Test 1 : Remplir un tampon avec un seul caractère
memset(buffer, 0, sizeof(buffer)); // Reset buffer
test_ft_memset(buffer, 'A', 5, "AAAAA", &success_count);
total_tests++;
// Test 2 : Remplir avec un caractère null
memset(buffer, 0, sizeof(buffer)); // Reset buffer
test_ft_memset(buffer, '\0', 5, "\0\0\0\0\0", &success_count);
total_tests++;
// Test 3 : Remplir une partie du tampon
memset(buffer, 0, sizeof(buffer)); // Reset buffer
test_ft_memset(buffer, 'B', 3, "BBB", &success_count);
total_tests++;
// Test 4 : Remplir avec un caractère à la limite de l'ASCII
memset(buffer, 0, sizeof(buffer)); // Reset buffer
test_ft_memset(buffer, 255, 4, "\xFF\xFF\xFF\xFF", &success_count);
total_tests++;
// Test 5 : Remplir tout le tampon
memset(buffer, 0, sizeof(buffer)); // Reset buffer
test_ft_memset(buffer, 'C', sizeof(buffer),
"CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC", &success_count);
total_tests++;
// Test 6 : Remplir 0 octets
memset(buffer, 0, sizeof(buffer)); // Reset buffer
test_ft_memset(buffer, 'D', 0, "", &success_count);
total_tests++;
// Résumé des résultats
printf(BOLDWHITE "\nTotal tests: %d, Success: %d, Failures: %d\n" RESET,
total_tests, success_count, total_tests - success_count);
return 0;
}
*/

View file

@ -0,0 +1,78 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/31 15:01:45 by tchampio #+# #+# */
/* Updated: 2024/11/04 15:28:21 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/ft_printf.h"
static int ft_print_string(char *s)
{
int size;
if (!s)
{
ft_putstr_fd("(null)", 1);
return (6);
}
size = (int)ft_strlen(s);
ft_putstr_fd(s, 1);
return (size);
}
static int ft_check_flag(char flag, va_list args)
{
int added;
added = 0;
if (flag == 'c')
return (ft_putchar_fd(va_arg(args, int), 1), 1);
else if (flag == 's')
added += ft_print_string((char *)va_arg(args, char *));
else if (flag == 'p')
added += ft_print_ptr((void *)va_arg(args, void *));
else if (flag == 'i' || flag == 'd')
added += ft_print_integer((int)va_arg(args, int));
else if (flag == 'u')
added += ft_print_uinteger((unsigned int)va_arg(args, unsigned int));
else if (flag == 'x')
added += ft_print_hex((int)va_arg(args, int), 'x');
else if (flag == 'X')
added += ft_print_hex((int)va_arg(args, int), 'X');
else if (flag == '%')
return (ft_putchar_fd('%', 1), 1);
return (added);
}
int ft_printf(const char *format, ...)
{
int counter;
va_list args;
size_t index;
index = 0;
counter = 0;
va_start(args, format);
while (format[index])
{
if (format[index] == '%')
{
index++;
counter += ft_check_flag(format[index], args);
index++;
}
else
{
ft_putchar_fd(format[index], 1);
counter++;
index++;
}
}
return (counter);
}

View file

@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* printhex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/04 15:25:57 by tchampio #+# #+# */
/* Updated: 2024/11/04 16:46:28 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/ft_printf.h"
#include "../../includes/libft.h"
int ft_hex_len(unsigned int num)
{
int len;
len = 0;
while (num != 0)
{
len++;
num = num / 16;
}
return (len);
}
void ft_put_hex(unsigned int num, const char format)
{
if (num >= 16)
{
ft_put_hex(num / 16, format);
ft_put_hex(num % 16, format);
}
else
{
if (num <= 9)
ft_putchar_fd((num + '0'), 1);
else
{
if (format == 'x')
ft_putchar_fd((num - 10 + 'a'), 1);
if (format == 'X')
ft_putchar_fd((num - 10 + 'A'), 1);
}
}
}
int ft_print_hex(unsigned int num, const char format)
{
if (num == 0)
return (write(1, "0", 1));
else
ft_put_hex(num, format);
return (ft_hex_len(num));
}

View file

@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* printnumbers.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/02 05:50:37 by tchampio #+# #+# */
/* Updated: 2024/11/04 16:00:42 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/ft_printf.h"
int ft_print_integer(int i)
{
int size;
char *str;
str = ft_itoa(i);
if (str == NULL)
return (0);
size = ft_strlen(str);
ft_putstr_fd(str, 1);
free(str);
return (size);
}
static int get_number_size(unsigned int i)
{
int size;
if (i == 0)
return (1);
size = 0;
while (i != 0)
{
i /= 10;
size++;
}
return (size);
}
int ft_print_uinteger(unsigned int i)
{
unsigned int nb;
nb = i;
if (nb >= 10)
{
ft_putnbr_fd(nb / 10, 1);
ft_putnbr_fd(nb % 10, 1);
}
else
ft_putchar_fd('0' + nb, 1);
return (get_number_size(i));
}

View file

@ -0,0 +1,55 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* printhex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/02 05:52:06 by tchampio #+# #+# */
/* Updated: 2024/11/04 15:25:12 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/ft_printf.h"
int ft_print_ptr_nb(uintptr_t decimal_number)
{
long i;
long j;
uintptr_t temp;
char hexa_number[100];
i = 1;
while (decimal_number != 0)
{
temp = decimal_number % 16;
if (temp < 10)
temp = temp + 48;
else
temp = temp + 55;
hexa_number[i++] = temp;
decimal_number = decimal_number / 16;
}
j = i - 1;
while (j > 0)
{
ft_putchar_fd(ft_tolower(hexa_number[j--]), 1);
}
return (i - 1);
}
int ft_print_ptr(void *ptr)
{
int size;
uintptr_t addr;
if (ptr == NULL)
{
ft_putstr_fd("(nil)", 1);
return (5);
}
addr = (uintptr_t)ptr;
ft_putstr_fd("0x", 1);
size = ft_print_ptr_nb(addr) + 2;
return (size);
}

37
libft/src/str/ft_atoi.c Normal file
View file

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 17:02:23 by tchampio #+# #+# */
/* Updated: 2024/10/15 17:23:50 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
int ft_atoi(const char *s)
{
int isnegative;
int res;
res = 0;
isnegative = 1;
while (*s == ' ' || *s == '\f' || *s == '\r' || *s == '\v' || *s == '\t'
|| *s == '\n')
s++;
if (*s == '-' || *s == '+')
{
if (*s == '-')
isnegative = -1;
s++;
}
while (*s >= '0' && *s <= '9')
{
res = (res * 10) + (*s - '0');
s++;
}
return (res * isnegative);
}

72
libft/src/str/ft_itoa.c Normal file
View file

@ -0,0 +1,72 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 11:29:14 by tchampio #+# #+# */
/* Updated: 2024/10/21 10:13:23 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
#include <stdlib.h>
static size_t getnumberoffigures(int n)
{
size_t totalsize;
long nn;
totalsize = 0;
nn = n;
if (n == 0)
return (1);
if (nn < 0)
{
totalsize++;
nn = -nn;
}
while (nn > 0)
{
nn /= 10;
totalsize++;
}
return (totalsize);
}
static void populate_array(char *s, int n, size_t size)
{
int isneg;
long nn;
size_t index;
nn = n;
if (nn < 0)
nn = -nn;
isneg = (n < 0);
index = size - 1;
while (nn > 0)
{
s[index] = (nn % 10) + '0';
nn /= 10;
index--;
}
if (isneg == 1)
s[index] = '-';
}
char *ft_itoa(int n)
{
char *s;
size_t size;
size = getnumberoffigures(n);
s = (char *)ft_calloc(sizeof(*s), size + 1);
if (!s)
return (NULL);
if (n == 0)
ft_strlcpy(s, "0", 2);
populate_array(s, n, size);
return (s);
}

87
libft/src/str/ft_split.c Normal file
View file

@ -0,0 +1,87 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 09:23:50 by tchampio #+# #+# */
/* Updated: 2024/10/23 13:16:12 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
#include <stdlib.h>
static size_t count_words(const char *s, char sep)
{
size_t count;
count = 0;
if (!s)
return (0);
while (*s)
{
while (*s == sep)
s++;
if (*s)
count++;
while (*s != sep && *s)
s++;
}
return (count);
}
static int free_all(char **split, size_t words)
{
size_t i;
i = 0;
while (i < words)
free(split[i++]);
free(split);
return (1);
}
static int populate(char **split, const char *s, char sep)
{
size_t wordlength;
size_t i;
wordlength = 0;
i = 0;
while (*s)
{
while (*s == sep && *s)
s++;
if (*s)
{
if (!ft_strchr(s, sep))
wordlength = ft_strlen(s);
else
wordlength = ft_strchr(s, sep) - s;
split[i] = ft_substr(s, 0, wordlength);
if (split[i] == NULL)
return (free_all(split, i));
i++;
s += wordlength;
}
}
split[i] = NULL;
return (0);
}
char **ft_split(const char *s, char separator)
{
char **split;
if (!s)
return (NULL);
split = (char **)malloc(sizeof(char *)
* (count_words(s, separator) + 1));
if (!split)
return (NULL);
if (populate(split, s, separator) == 1)
return (NULL);
return (split);
}

31
libft/src/str/ft_strchr.c Normal file
View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 13:30:59 by tchampio #+# #+# */
/* Updated: 2024/10/19 13:41:02 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
char *ft_strchr(const char *str, int c)
{
char *s;
char tofind;
tofind = (char)c;
s = (char *)str;
while (*s)
{
if (*s == tofind)
return (s);
s++;
}
if (*s == tofind)
return (s);
return (NULL);
}

27
libft/src/str/ft_strdup.c Normal file
View file

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/16 10:34:42 by tchampio #+# #+# */
/* Updated: 2024/10/19 14:52:38 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
#include <stdlib.h>
char *ft_strdup(const char *s)
{
size_t length;
char *str;
length = ft_strlen(s) + 1;
str = (char *)malloc(sizeof(*str) * length);
if (!str)
return (NULL);
ft_memcpy((char *)str, (const char *)s, length);
return (str);
}

View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_striteri.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/18 09:54:48 by tchampio #+# #+# */
/* Updated: 2024/10/18 10:09:15 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
void ft_striteri(char *s, void (*f)(unsigned int, char*))
{
size_t i;
if (!s || !f)
return ;
i = 0;
while (*s)
{
(*f)(i, s);
i++;
s++;
}
}

View file

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/16 15:40:16 by tchampio #+# #+# */
/* Updated: 2024/10/18 15:47:12 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
#include <stdlib.h>
char *ft_strjoin(const char *s1, const char *s2)
{
size_t lengths1;
size_t lengths2;
size_t index;
char *str;
lengths1 = ft_strlen(s1);
lengths2 = ft_strlen(s2);
index = 0;
str = (char *)ft_calloc(sizeof(*str), (lengths1 + lengths2 + 1));
if (str == NULL)
return (NULL);
while (*s1)
{
str[index] = *s1;
s1++;
index++;
}
while (*s2)
{
str[index] = *s2;
s2++;
index++;
}
return (str);
}

View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 12:31:51 by tchampio #+# #+# */
/* Updated: 2024/10/18 11:18:01 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
size_t ft_strlcat(char *dest, const char *src, size_t size)
{
size_t i;
size_t destsize;
size_t srcsize;
i = 0;
destsize = ft_strlen(dest);
srcsize = ft_strlen(src);
if (size <= 0 || destsize >= size)
return (srcsize + size);
while (i < size - destsize - 1 && src[i])
{
dest[destsize + i] = src[i];
i++;
}
dest[destsize + i] = '\0';
return (destsize + srcsize);
}

View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 11:03:52 by tchampio #+# #+# */
/* Updated: 2024/10/17 12:33:16 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
#include "../../includes/libft.h"
size_t ft_strlcpy(char *dest, const char *src, size_t size)
{
size_t i;
size_t srcsize;
i = 0;
srcsize = ft_strlen(src);
if (size == 0)
return (srcsize);
while (i < size - 1 && src[i] != '\0')
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
return (srcsize);
}

81
libft/src/str/ft_strlen.c Normal file
View file

@ -0,0 +1,81 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/14 15:36:45 by tchampio #+# #+# */
/* Updated: 2024/10/14 15:53:37 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
size_t ft_strlen(const char *s)
{
int len;
len = 0;
while (*s)
{
s++;
len++;
}
return (len);
}
/*
#include <stdio.h>
#include <string.h>
#include <limits.h>
// Prototype de la fonction ft_strlen que tu devras implémenter
size_t ft_strlen(const char *s);
// Macros pour les couleurs
#define RESET "\033[0m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define BOLDWHITE "\033[1m\033[37m"
// Fonction de test
void test_ft_strlen(const char *s, size_t expected, int *success_count) {
size_t result = ft_strlen(s);
if (result == expected) {
printf(GREEN "Test passed: ft_strlen(\"%s\") == %zu\n" RESET, s, result);
(*success_count)++;
} else {
printf(RED "Test failed: ft_strlen(\"%s\") == %zu (expected %zu)\n"
RESET, s, result, expected);
}
}
int main() {
int success_count = 0;
int total_tests = 0;
// Tests de chaînes avec des longueurs différentes
test_ft_strlen("", 0, &success_count); total_tests++;
test_ft_strlen("a", 1, &success_count); total_tests++;
test_ft_strlen("Hello", 5, &success_count); total_tests++;
test_ft_strlen("Hello, World!", 13, &success_count); total_tests++;
// Tests de chaînes avec des caractères spéciaux
test_ft_strlen("12345", 5, &success_count); total_tests++;
test_ft_strlen("!@#$%^&*()", 10, &success_count); total_tests++;
test_ft_strlen(" \t\n\r", 4, &success_count); total_tests++;
// Tests de longues chaînes
const char long_string[] = "This is a long string to test the
function ft_strlen properly.";
test_ft_strlen(long_string, strlen(long_string), &success_count);
total_tests++;
// Résumé des résultats
printf(BOLDWHITE "\nTotal tests: %d, Success: %d, Failures: %d\n" RESET,
total_tests, success_count, total_tests - success_count);
return 0;
}
*/

View file

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 15:58:52 by tchampio #+# #+# */
/* Updated: 2024/10/18 10:09:39 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
#include <stdlib.h>
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
size_t i;
char *str;
if (!s || !f)
return (NULL);
i = 0;
str = (char *)malloc(ft_strlen(s) + 1);
if (str == NULL)
return (NULL);
while (s[i])
{
str[i] = (*f)(i, s[i]);
i++;
}
str[i] = '\0';
return (str);
}

View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 14:16:56 by tchampio #+# #+# */
/* Updated: 2024/10/19 14:49:00 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
int ft_strncmp(const char *s1, const char *s2, size_t n)
{
size_t i;
i = 0;
while ((*s1 || *s2) && i < n)
{
if (*s1 != *s2)
return ((unsigned char)*s1 - (unsigned char)*s2);
s1++;
s2++;
i++;
}
return (0);
}

View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 16:33:24 by tchampio #+# #+# */
/* Updated: 2024/10/18 12:39:00 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
char *ft_strnstr(const char *big, const char *little, size_t len)
{
size_t littlesize;
size_t i;
if (little[0] == '\0')
return ((char *)big);
littlesize = ft_strlen(little);
i = 0;
while (*big && i < len)
{
if (ft_strncmp(big, little, littlesize) == 0
&& (i + littlesize) <= len)
return ((char *)big);
big++;
i++;
}
return (NULL);
}

View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strrchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 13:44:15 by tchampio #+# #+# */
/* Updated: 2024/10/18 10:55:22 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
char *ft_strrchr(char *str, int c)
{
char *s;
char *last;
char tofind;
s = str;
last = NULL;
tofind = (char)c;
while (*s)
{
if (*s == tofind)
last = s;
s++;
}
if (*s == tofind)
last = s;
return (last);
}

View file

@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/16 15:54:04 by tchampio #+# #+# */
/* Updated: 2024/10/19 11:06:07 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
#include <stdio.h>
#include <stdlib.h>
static int is_in_set(char c, const char *set)
{
while (*set)
{
if (*set == c)
return (1);
set++;
}
return (0);
}
char *ft_strtrim(const char *s1, const char *set)
{
size_t start;
size_t end;
size_t s1_len;
char *trim;
if (!s1 || !set)
return (NULL);
start = 0;
while (is_in_set(s1[start], set))
start++;
s1_len = ft_strlen(s1);
end = s1_len - 1;
if (start == s1_len)
return (ft_strdup(""));
while (is_in_set(s1[end], set))
end--;
trim = malloc((end - start + 2) * sizeof(char));
if (!trim)
return (NULL);
ft_strlcpy(trim, (s1 + start), (end - start + 2));
return (trim);
}

36
libft/src/str/ft_substr.c Normal file
View file

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/16 10:57:55 by tchampio #+# #+# */
/* Updated: 2024/10/19 15:35:35 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "../../includes/libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *str;
size_t strlen;
size_t allocated;
if (!s)
return (NULL);
strlen = ft_strlen(s);
if (start > strlen)
return (ft_strdup(""));
if (strlen - start >= len)
allocated = len + 1;
else
allocated = (strlen - start) + 1;
str = (char *)malloc(sizeof(*str) * allocated);
if (!str)
return (NULL);
ft_strlcpy(str, s + start, allocated);
return (str);
}

View file

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_tolower.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 13:17:58 by tchampio #+# #+# */
/* Updated: 2024/10/16 16:51:33 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
int ft_tolower(int c)
{
if (c >= 'A' && c <= 'Z')
return (c + 32);
else
return (c);
}

View file

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_toupper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 13:17:58 by tchampio #+# #+# */
/* Updated: 2024/10/15 13:27:35 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/libft.h"
int ft_toupper(int c)
{
if (c >= 'a' && c <= 'z')
return (c - 32);
else
return (c);
}