Added memorybook to parsing and cleaned most of the files

This commit is contained in:
Philippe BLAGOJEVIC
2022-05-04 14:38:34 +02:00
parent e564e7c8e9
commit 362668fe35
231 changed files with 11474 additions and 405 deletions

154
libs/libft/testing/Makefile Normal file
View File

@@ -0,0 +1,154 @@
# - - - - - - - - - - - - - - # name = value \
# variables names # value
# - - - - - - - - - - - - - - # ! name is case sensitive
NAME = libftest
CC = gcc
VPATH = srcs
DEPS = ../includes/libft.h \
./includes/libftest.h
IDIR = $(dir $(DEPS))
# $(dir PATH/TO/FILE) expands to "PATH/TO/" -> the directory of a file
LDIR = ../
_LIBS = libft.a
LIBS = $(_LIBS:lib%.a=%)
SRCS = main.c \
test_memset.c \
test_bzero.c \
test_memcpy.c \
test_memccpy.c \
test_memmove.c \
test_memchr.c \
test_memcmp.c \
test_strlen.c \
test_isalpha.c \
test_isdigit.c \
test_isalnum.c \
test_isascii.c \
test_isprint.c \
test_toupper.c \
test_tolower.c \
test_strchr.c \
test_strrchr.c \
test_strchrset.c \
test_strncmp.c \
test_strlcpy.c \
test_strlcat.c \
test_strnstr.c \
test_atoi.c \
test_calloc.c \
test_strdup.c \
test_substr.c \
test_strjoin.c \
test_strtrim.c \
test_split.c \
test_itoa.c \
test_utoa.c \
test_strmapi.c \
test_putchar_fd.c \
test_putstr_fd.c \
test_putendl_fd.c \
test_putnbr_fd.c \
test_lstnew.c \
test_lstadd_front.c \
test_lstsize.c \
test_lstlast.c \
test_lstadd_back.c \
test_lstdelone.c \
test_lstclear.c \
test_lstiter.c \
test_lstmap.c \
test_strcat.c \
test_strcmp.c \
test_strcpy.c \
test_strncat.c \
test_strncpy.c \
test_strstr.c \
test_strjoinfree.c \
test_strclr.c \
test_strdel.c \
test_strequ.c \
test_striter.c \
test_striteri.c \
test_strmap.c \
test_strnequ.c \
test_strnew.c \
test_memalloc.c \
test_memdel.c \
test_putchar.c \
test_putendl.c \
test_putnbr.c \
test_putnbrendl.c \
test_putnbrendl_fd.c \
test_putnbrbase.c \
test_putstr.c \
test_any.c \
test_atoibase.c \
test_convertbase.c \
test_convertbase_free.c \
test_foreach.c \
test_issort.c \
test_arraymap.c \
test_strmultisplit.c \
test_gnl.c \
test_concat_free.c \
test_printf.c \
test_abs.c \
test_greater.c \
test_smaller.c \
test_sign.c \
test_sqrt.c
ODIR = ./builds
OBJS = $(SRCS:%.c=$(ODIR)/%.o)
CFLAGS = $(IDIR:%=-I%)
# flag -g generates debug informations, g3 is maximal version
CFLAGS += -g3 -Wall -Wextra -Werror
LFLAGS = -L$(LDIR) -l$(LIBS)
# - - - - - - - - - - - - - - # target: prerequisites | $@ : target
# rules to execute # recipe | $< : 1st prerequisite
# - - - - - - - - - - - - - - # recipe | $^ : all prerequisites
all: $(NAME)
$(NAME): $(ODIR) $(OBJS) $(DEPS)
make -C $(LDIR)
$(CC) $(CFLAGS) -o $@ $(OBJS) $(LFLAGS)
$(ODIR):
mkdir -p $@
$(ODIR)/%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
debug: CFLAGS += -fsanitize=address
debug: clean $(NAME)
leaks: $(NAME)
valgrind --leak-check=full --show-leak-kinds=all ./$(NAME) maps/42_color.fdf
clean:
/bin/rm -f $(OBJS) $(OBJS_B)
fclean: clean
/bin/rm -rf $(ODIR)
/bin/rm -f $(NAME)
/bin/rm -rf a.out a.out.dSYM
libfclean:
make fclean -C $(LDIR)
re: fclean all
relib: libfclean re
.PHONY: all clean fclean re gcc

View File

@@ -0,0 +1,104 @@
#ifndef LIBFTEST_H
# define LIBFTEST_H
# include "libft.h"
# include <unistd.h> // for write
# include <stdio.h> // for printf
# include <stdlib.h> // for malloc
# include <string.h> // for strdup and strlen and strcmp
# include <ctype.h> // for characters like isalpha or isdigit etc
# define RED write(1, "\033[91m", 5);
# define GREEN write(1, "\033[92m", 5);
# define COLOREND write(1, "\033[0m", 4);
void test_memset(void);
void test_bzero(void);
void test_memcpy(void);
void test_memccpy(void);
void test_memmove(void);
void test_memchr(void);
void test_memcmp(void);
void test_strlen(void);
void test_isalpha(void);
void test_isdigit(void);
void test_isalnum(void);
void test_isascii(void);
void test_isprint(void);
void test_toupper(void);
void test_tolower(void);
void test_strchr(void);
void test_strrchr(void);
void test_strchrset(void);
void test_strncmp(void);
void test_strlcpy(void);
void test_strlcat(void);
void test_strnstr(void);
void test_atoi(void);
void test_calloc(void);
void test_strdup(void);
void test_substr(void);
void test_strjoin(void);
void test_strtrim(void);
void test_split(void);
void test_itoa(void);
void test_utoa(void);
void test_strmapi(void);
void test_putchar_fd(void);
void test_putstr_fd(void);
void test_putendl_fd(void);
void test_putnbr_fd(void);
void test_lstnew(void);
void test_lstadd_front(void);
void test_lstsize(void);
void test_lstlast(void);
void test_lstadd_back(void);
void test_lstdelone(void);
void test_lstclear(void);
void test_lstiter(void);
void test_lstmap(void);
void test_strcat(void);
void test_strcmp(void);
void test_strcpy(void);
void test_strncat(void);
void test_strncpy(void);
void test_strstr(void);
void test_strjoinfree(void);
void test_strclr(void);
void test_strdel(void);
void test_strequ(void);
void test_striter(void);
void test_striteri(void);
void test_strmap(void);
void test_strnequ(void);
void test_strnew(void);
void test_memalloc(void);
void test_memdel(void);
void test_putchar(void);
void test_putendl(void);
void test_putnbr(void);
void test_putnbrendl(void);
void test_putnbrendl_fd(void);
void test_putnbrbase(void);
void test_putstr(void);
void test_any(void);
void test_atoibase(void);
void test_convertbase(void);
void test_convertbase_free(void);
void test_foreach(void);
void test_issort(void);
void test_arraymap(void);
void test_strmultisplit(void);
void test_gnl(void);
void test_concat_free(void);
void test_printf(void);
void test_next_word(void);
void test_convert(void);
void test_flag_transform(void);
void test_flag_transform_bonus(void);
void test_abs(void);
void test_greater(void);
void test_smaller(void);
void test_sign(void);
void test_sqrt(void);
#endif

BIN
libs/libft/testing/libftest Executable file

Binary file not shown.

95
libs/libft/testing/main.c Normal file
View File

@@ -0,0 +1,95 @@
#include "libftest.h"
// the functions precedes by # already exists in the system
int main()
{
test_abs();
test_any();
/*
test_arraymap.c
test_atoibase.c
# test_atoi.c
# test_bzero.c
# test_calloc.c
test_concat_free.c
test_convertbase.c
test_convertbase_free.c
test_foreach.c
test_gnl.c
test_greater.c
# test_isalnum.c
# test_isalpha.c
# test_isascii.c
# test_isdigit.c
# test_isprint.c
test_issort.c
test_itoa.c
test_lstadd_back.c
test_lstadd_front.c
test_lstclear.c
test_lstdelone.c
test_lstiter.c
test_lstlast.c
test_lstmap.c
test_lstnew.c
test_lstsize.c
test_memalloc.c
# test_memccpy.c
# test_memchr.c
# test_memcmp.c
# test_memcpy.c
test_memdel.c
# test_memmove.c
# test_memset.c
test_printf.c
test_putchar.c
test_putchar_fd.c
test_putendl.c
test_putendl_fd.c
test_putnbrbase.c
test_putnbr.c
test_putnbrendl.c
test_putnbrendl_fd.c
test_putnbr_fd.c
test_putstr.c
test_putstr_fd.c
test_sign.c
test_smaller.c
test_split.c
test_sqrt.c
test_strcat.c
# test_strchr.c
test_strchrset.c
test_strclr.c
test_strcmp.c
test_strcpy.c
test_strdel.c
# test_strdup.c
test_strequ.c
test_striter.c
test_striteri.c
test_strjoin.c
test_strjoinfree.c
# test_strlcat.c
# test_strlcpy.c
# test_strlen.c
test_strmap.c
test_strmapi.c
test_strmultisplit.c
test_strncat.c
# test_strncmp.c
test_strncpy.c
test_strnequ.c
test_strnew.c
# test_strnstr.c
# test_strrchr.c
test_strstr.c
test_strtrim.c
test_substr.c
# test_tolower.c
# test_toupper.c
test_utoa.c
*/
return 0;
}

View File

@@ -0,0 +1,23 @@
#include "libftest.h"
void compare_abs(int i, int ans)
{
if (ft_abs(i) != ans)
{
RED ft_printf("error: "); COLOREND
ft_printf("ft_abs(%i)", i);
RED ft_printf(" gives "); COLOREND
ft_printf("%i", ft_abs(i));
RED ft_printf(" instead of "); COLOREND
ft_printf("%i\n", ans);
}
}
void test_abs(void)
{
compare_abs(-1, 1);
compare_abs(0, 0);
compare_abs(-0, 0);
compare_abs(-1000, 1000);
compare_abs(-2147483647, 2147483647);
}

View File

@@ -0,0 +1,131 @@
#include "libftest.h"
char **create_tab(void)
{
char **tab;
tab = (char **)malloc(sizeof(char *) * (6 + 1));
tab[0] = strdup("premiere ligne de test");
tab[1] = strdup("deuxieme ligne de test");
tab[2] = strdup("troisieme ligne pour le fun");
tab[3] = strdup("quatrieme linge avec une erreur");
tab[4] = strdup("cinquieme peche a la ligne");
tab[5] = strdup("sixieme et derniere ligne");
return (tab);
}
void free_tab(char **tab)
{
free(tab[0]);
free(tab[1]);
free(tab[2]);
free(tab[3]);
free(tab[4]);
free(tab[5]);
free(tab);
}
int contain_a(char *s)
{
int i;
i = -1;
while (s[++i])
if (s[i] == 'a')
return (1);
return (0);
}
int contain_y(char *s)
{
int i;
i = -1;
while (s[++i])
if (s[i] == 'y')
return (1);
return (0);
}
int contain_z(char *s)
{
int i;
i = -1;
while (s[++i])
if (s[i] == 'z')
return (1);
return (0);
}
int contain_x(char *s)
{
int i;
i = -1;
while (s[++i])
if (s[i] == 'x')
return (1);
return (0);
}
int contain_u(char *s)
{
int i;
i = -1;
while (s[++i])
if (s[i] == 'u')
return (1);
return (0);
}
int contain_1(char *s)
{
int i;
i = -1;
while (s[++i])
if (s[i] == '1')
return (1);
return (0);
}
void compare_any(char **tab, int (*f)(char *), char *s, int solution)
{
int i;
i = ft_any(tab, f);
if (i != solution)
{
write(1, "\033[91m", 5);
printf("error: ");
write(1, "\033[0m", 4);
printf("ft_any(tab, %s) returned", s);
printf(" %i ", i);
printf("in tab :\n");
printf(" %s:\n", tab[0]);
printf(" %s:\n", tab[1]);
printf(" %s:\n", tab[2]);
printf(" %s:\n", tab[3]);
printf(" %s:\n", tab[4]);
printf(" %s:\n", tab[5]);
}
}
void test_any(void)
{
char **tab;
tab = create_tab();
compare_any(tab, contain_a, "contain_a", 1);
compare_any(tab, contain_y, "contain_y", 0);
compare_any(tab, contain_z, "contain_z", 0);
compare_any(tab, contain_x, "contain_x", 1);
compare_any(tab, contain_u, "contain_u", 1);
compare_any(tab, contain_1, "contain_1", 0);
free_tab(tab);
}

View File

@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_arraymap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/16 15:17:24 by hulamy #+# #+# */
/* Updated: 2018/11/16 15:17:27 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int *ft_arraymap(int *tab, int length, int (*f)(int))
{
int i;
int *newtab;
i = -1;
if (!tab)
return (NULL);
if (!(newtab = (int*)malloc(sizeof(*newtab) * (length + 1))))
return (NULL);
while (++i < length)
newtab[i] = (*f)(tab[i]);
return (newtab);
}

View File

@@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:54:29 by hulamy #+# #+# */
/* Updated: 2019/11/25 13:54:35 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_atoi(const char *str)
{
long long nbr;
int i;
int n;
i = 0;
n = 1;
nbr = 0;
while ((str[i] == ' ') || (str[i] > 8 && str[i] < 14))
i++;
if (str[i] == '-')
n = -1;
if (str[i] == '+' || str[i] == '-')
i++;
while (str[i] >= '0' && str[i] <= '9')
{
if ((nbr >= 922337203685477580
&& ((str[i] > 8 && n < 0) || (str[i] > 7 && n > 0))))
return ((n > 0) ? -1 : 0);
else
nbr = nbr * 10 + (str[i++] - '0');
}
return (nbr * n);
}

View File

@@ -0,0 +1,75 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoibase.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/16 15:15:31 by hulamy #+# #+# */
/* Updated: 2018/11/16 15:22:34 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int is_valid_base(char *base, int i, int j)
{
while (base[i])
{
j = i + 1;
while (base[j])
{
if (base[i] == base[j])
return (0);
j++;
}
if (base[i] == '-' || base[i] == '+')
return (0);
i++;
}
if (i >= 2)
return (1);
return (0);
}
static int skip(int i, char *str, int *n)
{
while ((str[i] == 32) || (str[i] > 8 && str[i] < 14))
i++;
if (str[i] == '+' || str[i] == '-')
{
if (str[i] == '-')
*n = -1;
i++;
}
return (i);
}
int ft_atoibase(char *str, char *base)
{
int i;
int j;
int length;
int res;
int n;
length = 0;
res = 0;
n = 1;
if (!is_valid_base(base, 0, 0))
return (0);
while (base[length])
length++;
i = skip(0, str, &n);
while (str[i] && str[i] > 32 && str[i] != '-' && str[i] != '+')
{
j = 0;
while (str[i] != base[j] && base[j])
j++;
if (base[j] == '\0')
return (0);
res = (res * length) + j;
i++;
}
return (res * n);
}

View File

@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_bzero.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:54:43 by hulamy #+# #+# */
/* Updated: 2019/11/25 13:54:44 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_bzero(void *s, size_t n)
{
size_t i;
unsigned char *ptr;
if (n)
{
ptr = (unsigned char *)s;
i = 0;
while (i < n)
ptr[i++] = '\0';
}
}

View File

@@ -0,0 +1,64 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:54:53 by hulamy #+# #+# */
/* Updated: 2019/12/01 16:04:12 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** allocate count * size byte of memory and
** return a pointer to the allocated memory
**
** exemple allocation for 5 integers with malloc then calloc :
** a = (int *)malloc(5 * sizeof(int)); //5*4bytes = 20 bytes
** free(a);
** a = (int *)calloc(5, sizeof(int));
*/
/*
** #include <libc.h>
**
** void ft_bzero(void *s, size_t n)
** {
** size_t i;
** unsigned char *ptr;
**
** if (n)
** {
** ptr = (unsigned char *)s;
** i = 0;
** while (i < n)
** ptr[i++] = '\0';
** }
** }
**
** void *ft_calloc(size_t count, size_t size);
**
** int main(void)
** {
** void *str;
**
** str = ft_calloc(0, 0);
** if (str == ((void *)0))
** printf("failed\n");
** free(str);
** return (0);
** }
*/
#include "libft.h"
void *ft_calloc(size_t count, size_t size)
{
void *tmp;
if (!(tmp = malloc(count * size)))
return (NULL);
ft_bzero(tmp, count * size);
return (tmp);
}

View File

@@ -0,0 +1,53 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_concat_free.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/27 18:04:09 by hulamy #+# #+# */
/* Updated: 2020/02/27 18:06:44 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** create a new string size of str1 + str2
** fill it with concated str1 and str2m as "str1str2"
** free the received strings str1 and str2
** return the new string
*/
/*
** #include <stdio.h>
** #include "libft.h"
**
** char *ft_concat_free(char *str1, char *str2);
**
** int main(int ac, char **av)
** {
** if (ac != 3)
** return (0);
** printf("%s\n", ft_concat_free(ft_strdup(av[1]), ft_strdup(av[2])));
** return (0);
** }
*/
#include "libft.h"
char *ft_concat_free(char *str1, char *str2)
{
char *cat;
int i;
int j;
cat = ft_memalloc(sizeof(char) * (ft_strlen(str1) + ft_strlen(str2) + 1));
i = -1;
j = 0;
while (str1[++i])
cat[i] = str1[i];
while (str2[j])
cat[i++] = str2[j++];
free(str1);
free(str2);
return (cat);
}

View File

@@ -0,0 +1,196 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_convertbase.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/26 20:19:54 by hulamy #+# #+# */
/* Updated: 2020/02/26 20:20:14 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** take a string that is a number in a certain base
** and convert it in another base
** it works with unsigned long int
** return the new string
*/
/*
** #include <stdio.h> // for printf
** #include <stdlib.h> // for atoi
**
** char *ft_convertbase(char *nbr, char *base_from, char *base_to);
**
** int main(int ac, char **av)
** {
** if (ac != 4)
** {
** printf("usage:\nchar *nbr, char *base_from, char *base_to\n");
** printf("try the max long unsigned int : 18446744073709551615\n");
** }
** else
** printf("[%s]\n",ft_convertbase(av[1], av[2], av[3]));
** return (0);
** }
*/
#include "libft.h"
/*
** check :
** -if the base has no characters that appear more than one time
** -if the signes '-' and '+' are not part of the set
** -if there are no invisible characters (inferior to 32 or equal to 127)
*/
int
is_valid_base(char *base)
{
int i;
int j;
i = 0;
while (base[i])
{
j = i + 1;
while (base[j])
if (base[i] == base[j++])
return (0);
if (base[i] == '-' || base[i] == '+' || base[i] < 33 || base[i] == 127)
return (0);
i++;
}
if (i >= 2)
return (1);
return (0);
}
/*
** check :
** -if base is valid
** -if nbr contain characters
** -if nbr is made of elements of base only
*/
int
is_valid_nbr(char *nbr, char *base)
{
int i;
int j;
i = 0;
if (!is_valid_base(base))
return (0);
while (nbr[i])
{
j = 0;
while (base[j] && nbr[i] != base[j])
j++;
if (base[j] == '\0')
return (0);
i++;
}
if (i == 0)
return (0);
return (1);
}
/*
** -transform a nbr written as a string into a decimal nbr
** -it's an unsigned nbr because the negativity is managed elsewhere
** -if the number is bigger than the max unsigned long int it's false
** as it's impossible to verify if a number is bigger than the biggest
** unsigned, we verify the difference before the multiplication
*/
unsigned long int
base_to_decimal(char *nbr, char *base, int *error)
{
unsigned long int decimal;
int i;
int j;
int length;
decimal = 0;
i = 0;
length = 0;
while (base[length])
length++;
while (nbr[i])
{
j = 0;
while (nbr[i] != base[j] && base[j])
j++;
if ((18446744073709551615U - j) / length < decimal)
return (*error = 1);
decimal = (decimal * length) + j;
i++;
}
return (decimal);
}
/*
** -it counts the size needed to be allocated
** -if the given nbr was a negative one it add the place for the '-'
** -then convert the nbr from decimal base to destination base
** into the string allocated
*/
char
*decimal_to_base(unsigned long int decimal, char *base, int malloc_size)
{
int base_size;
int neg;
char *result;
unsigned long int nb;
neg = malloc_size;
base_size = 0;
while (base[base_size])
base_size++;
nb = decimal;
while (nb /= base_size)
malloc_size++;
result = (char *)malloc(sizeof(char) * (malloc_size + 2));
result[malloc_size + 1] = '\0';
if (neg)
result[0] = '-';
while (malloc_size >= 0)
{
result[malloc_size--] = base[decimal % base_size];
decimal /= base_size;
}
return (result);
}
/*
** -main function to convert from one base to another
** -function base_to_decimal has an awfull int *error because it cannot
** return -1 in case of error, since it's an unsigned, and it cannot
** return 0 to check the error since it would be confusing with an actual
** return of 0 if the number to convert is 0
*/
char
*ft_convertbase(char *nbr, char *base_from, char *base_to)
{
int length;
unsigned long int decimal;
int error;
error = 0;
length = 0;
if (nbr[0] == '-')
{
nbr++;
length = 1;
}
if (!is_valid_nbr(nbr, base_from) || !is_valid_base(base_to))
return (NULL);
decimal = base_to_decimal(nbr, base_from, &error);
if (error == 1)
return (NULL);
return (decimal_to_base(decimal, base_to, length));
}

View File

@@ -0,0 +1,198 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_convertbase_free.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/26 20:19:54 by hulamy #+# #+# */
/* Updated: 2020/02/27 20:23:22 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** (just like ft_convert, but free the string it receive)
** take a string that is a number in a certain base
** and convert it in another base
** it works with unsigned long int
** return the new string
*/
/*
** #include <stdio.h> // for printf
** #include <stdlib.h> // for atoi
**
** char *ft_convertbase_free(char *nbr, char *base_from, char *base_to);
**
** int main(int ac, char **av)
** {
** if (ac != 4)
** {
** printf("usage:\nchar *nbr, char *base_from, char *base_to\n");
** printf("try the max long unsigned int : 18446744073709551615\n");
** }
** else
** printf("[%s]\n",ft_convertbase_free(av[1], av[2], av[3]));
** return (0);
** }
*/
#include "libft.h"
/*
** check :
** -if the base has no characters that appear more than one time
** -if the signes '-' and '+' are not part of the set
** -if there are no invisible characters (inferior to 32 or equal to 127)
*/
int
is_base_valid(char *base)
{
int i;
int j;
i = 0;
while (base[i])
{
j = i + 1;
while (base[j])
if (base[i] == base[j++])
return (0);
if (base[i] == '-' || base[i] == '+' || base[i] < 33 || base[i] == 127)
return (0);
i++;
}
if (i >= 2)
return (1);
return (0);
}
/*
** check :
** -if base is valid
** -if nbr contain characters
** -if nbr is made of elements of base only
*/
int
is_nbr_valid(char *nbr, char *base)
{
int i;
int j;
i = 0;
if (!is_base_valid(base))
return (0);
while (nbr[i])
{
j = 0;
while (base[j] && nbr[i] != base[j])
j++;
if (base[j] == '\0')
return (0);
i++;
}
if (i == 0)
return (0);
return (1);
}
/*
** -transform a nbr written as a string into a decimal nbr
** -it's an unsigned nbr because the negativity is managed elsewhere
** -if the number is bigger than the max unsigned long int it's false
** as it's impossible to verify if a number is bigger than the biggest
** unsigned, we verify the difference before the multiplication
*/
unsigned long int
base_2_decimal(char *nbr, char *base, int *error)
{
unsigned long int decimal;
int i;
int j;
int length;
decimal = 0;
i = 0;
length = 0;
while (base[length])
length++;
while (nbr[i])
{
j = 0;
while (nbr[i] != base[j] && base[j])
j++;
if ((18446744073709551615U - j) / length < decimal)
return (*error = 1);
decimal = (decimal * length) + j;
i++;
}
return (decimal);
}
/*
** -it counts the size needed to be allocated
** -if the given nbr was a negative one it add the place for the '-'
** -then convert the nbr from decimal base to destination base
** into the string allocated
*/
char
*decimal_2_base(unsigned long int decimal, char *base, int malloc_size)
{
int base_size;
int neg;
char *result;
unsigned long int nb;
neg = malloc_size;
base_size = 0;
while (base[base_size])
base_size++;
nb = decimal;
while (nb /= base_size)
malloc_size++;
result = (char *)malloc(sizeof(char) * (malloc_size + 2));
result[malloc_size + 1] = '\0';
if (neg)
result[0] = '-';
while (malloc_size >= 0)
{
result[malloc_size--] = base[decimal % base_size];
decimal /= base_size;
}
return (result);
}
/*
** -main function to convert from one base to another
** -function base_to_decimal has an awfull int *error because it cannot
** return -1 in case of error, since it's an unsigned, and it cannot
** return 0 to check the error since it would be confusing with an actual
** return of 0 if the number to convert is 0
*/
char
*ft_convertbase_free(char *nbr, char *base_from, char *base_to)
{
int length;
unsigned long int decimal;
int error;
error = 0;
length = 0;
if (nbr[0] == '-')
{
nbr++;
length = 1;
}
if (!is_nbr_valid(nbr, base_from) || !is_base_valid(base_to))
return (NULL);
decimal = base_2_decimal(nbr, base_from, &error);
if (error == 1)
return (NULL);
free(nbr);
return (decimal_2_base(decimal, base_to, length));
}

View File

@@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_foreach.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/16 15:16:10 by hulamy #+# #+# */
/* Updated: 2018/11/16 15:16:11 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_foreach(int *tab, int length, void (*f)(int))
{
int i;
i = 0;
while (i < length && tab && tab[i])
(*f)(tab[i++]);
}

View File

@@ -0,0 +1,135 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/31 17:05:53 by hulamy #+# #+# */
/* Updated: 2020/02/25 18:48:55 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
** #include <fcntl.h> //for open
**
** int main(int ac, char **av)
** {
** int *fd;
** int i = 0;
** int j = 0;
** int ret;
** char *line = NULL;
**
** fd = (int *)ft_calloc(ac, sizeof(int));
** while (++i <= ac - 1)
** fd[i - 1] = open(av[i], O_RDONLY);
** i = 0;
** while (j < ac - 1)
** {
** if ((ret = ft_gnl(fd[i], &line)) > 0)
** {
** ft_printf(" [fd%i-%i] %s\n", fd[i], ret, line);
** free(line);
** j = 0;
** }
** else if (ret == -1)
** {
** ft_printf("[fd%i-%i] *ERROR*\n", fd[i], ret);
** free(line);
** j++;
** }
** else if (*line != '\0')
** ft_printf(" [fd%i-%i] %s\n", fd[i], ret, line);
** else
** {
** ft_printf("[fd%i-%i] %s *FINI*\n", fd[i], ret, line);
** free(line);
** j++;
** }
** i++;
** if (i >= ac - 1)
** i = 0;
** }
** free(fd);
** //while (1);
** return (0);
** }
*/
int free_lst(t_gnlist **lst, int ret)
{
t_gnlist *tmp;
tmp = *lst;
while (tmp->next != *lst)
tmp = tmp->next;
tmp->next = (*lst)->next;
free((*lst)->str);
if (*lst == (*lst)->next)
{
free(*lst);
*lst = NULL;
}
else
{
free(*lst);
*lst = tmp;
}
return (ret);
}
int multi_fd(int fd, t_gnlist **lst)
{
t_gnlist *tmp;
tmp = *lst;
while (*lst && (*lst)->lfd != fd && (*lst)->next != tmp)
*lst = (*lst)->next;
if (!tmp || ((*lst)->next == tmp && (*lst)->lfd != fd))
{
if (!(tmp = (t_gnlist*)malloc(sizeof(*tmp))))
return (0);
tmp->lfd = fd;
if (!(tmp->str = ft_strdup("")))
return (0);
if (*lst)
{
tmp->next = (*lst)->next;
(*lst)->next = tmp;
}
else
tmp->next = tmp;
*lst = tmp;
}
return (1);
}
int ft_gnl(const int fd, char **line)
{
char buf[BUFFER_SIZE + 1];
int ret;
static t_gnlist *lst = NULL;
char *str;
ret = 1;
if (!(multi_fd(fd, &lst)) || !line || BUFFER_SIZE < 1)
return (free_lst(&lst, -1));
while (!(str = ft_strchr(lst->str, '\n')) && ret != 0)
{
if ((ret = read(fd, buf, BUFFER_SIZE)) < 0)
return (free_lst(&lst, -1));
buf[ret] = '\0';
if (!(lst->str = ft_strjoinfree(lst->str, ft_strdup(buf))))
return (free_lst(&lst, -1));
}
if (str != NULL)
str[0] = '\0';
if (!(*line = ft_strdup(lst->str)))
return (free_lst(&lst, -1));
if (str != NULL)
return (ft_memmove(lst->str, str + 1, ft_strlen(str + 1) + 1) != NULL);
return (free_lst(&lst, 0));
}

View File

@@ -0,0 +1,8 @@
#include "libft.h"
int ft_greater(int a, int b)
{
if (a < b)
return (b);
return (a);
}

View File

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalnum.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:55:05 by hulamy #+# #+# */
/* Updated: 2019/11/25 13:55:06 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalnum(int c)
{
return (ft_isalpha(c) || ft_isdigit(c));
}

View File

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalpha.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:55:15 by hulamy #+# #+# */
/* Updated: 2019/11/25 13:55:17 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalpha(int c)
{
return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
}

View File

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isascii.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:55:24 by hulamy #+# #+# */
/* Updated: 2019/11/25 13:55:25 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isascii(int c)
{
return (c >= 0 && c <= 127);
}

View File

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:55:32 by hulamy #+# #+# */
/* Updated: 2019/11/25 13:55:33 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isdigit(int c)
{
return (c >= '0' && c <= '9');
}

View File

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isprint.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:55:43 by hulamy #+# #+# */
/* Updated: 2019/11/25 13:55:44 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isprint(int c)
{
return (c >= 32 && c < 127);
}

View File

@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_issort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/16 15:18:14 by hulamy #+# #+# */
/* Updated: 2018/11/16 15:18:15 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_issort(int *tab, int length, int (*f)(int, int))
{
int i;
i = -1;
if (!tab)
return (0);
while (++i < length - 1)
if (f(tab[i], tab[i + 1]) > 0)
return (0);
return (1);
}

View File

@@ -0,0 +1,91 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:59:01 by hulamy #+# #+# */
/* Updated: 2020/02/19 15:44:04 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** take an integer and give a string
*/
/*
** #include <stdio.h> // for printf
** #include <stdlib.h> // for atoi
**
** char *ft_itoa(long int n);
**
** int main(int ac, char **av)
** {
** if (ac == 0)
** return (0);
** else if (ac == 2)
** printf("%s\n",ft_itoa(atoi(av[1])));
** else
** {
** long int i;
** i = 0;
** printf(" %li\n %s\n\n",i,ft_itoa(i));
** i = 1234567;
** printf(" %li\n %s\n\n",i,ft_itoa(i));
** i = -1234567;
** printf(" %li\n %s\n\n",i,ft_itoa(i));
** i = 237683;
** printf(" %li\n %s\n\n",i,ft_itoa(i));
** i = 2147483647;
** printf("| %li\n| %s\n\n",i,ft_itoa(i));
** i = i ^ 0; // create the opposite of a signed '0', which
** // is 0 followed by 31 '1', the signed int max
** printf("* %li\n* %s\n\n",i,ft_itoa(i));
** i = i ^ 0;
** i = 1 << 31; // change the most lefted bit from '0' (positive)
** // to '1' (negative), the signed int min
** printf("* %li\n* %s\n\n",i,ft_itoa(i));
** i = 2147483646;
** printf(" %li\n %s\n\n",i,ft_itoa(i));
** i = -2147483648;
** printf("| %li\n| %s\n\n",i,ft_itoa(i));
** i = -2147483647;
** printf(" %li\n %s\n\n",i,ft_itoa(i));
** i = 2147483648;
** printf(" %li\n %s\n\n",i,ft_itoa(i));
** i = -2147483649;
** printf(" %li\n %s\n\n",i,ft_itoa(i));
** i = 9223372036854775807;
** printf("| %li\n| %s\n\n",i,ft_itoa(i));
** i = -9223372036854775807;
** printf("| %li\n| %s\n\n",i,ft_itoa(i));
** }
** return 0;
** }
*/
#include "libft.h"
char *ft_itoa(long int n)
{
char *str;
int len;
long int cpy;
char rgt;
cpy = (n < 0) ? (n / 10) * -10 : (n / 10) * 10;
len = (n < 0) ? 2 : 1;
rgt = (n < 0) ? (n % 10) * -1 + '0' : n % 10 + '0';
while (n /= 10)
len++;
if (!(str = (char *)malloc(sizeof(char) * (len + 1))))
return (NULL);
str[len] = '\0';
str[--len] = rgt;
while (cpy /= 10)
str[--len] = cpy % 10 + '0';
if (len)
str[0] = '-';
return (str);
}

View File

@@ -0,0 +1,90 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_back.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:11:53 by hulamy #+# #+# */
/* Updated: 2019/11/25 14:36:12 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** add an element to the end of a list
** or first if list has no element so far
*/
/*
** #include <libc.h>
**
** typedef struct s_list
** {
** void *content;
** struct s_list *next;
** } t_list;
**
** t_list *ft_lstnew(void *content)
** {
** t_list *lst;
**
** if (!(lst = (t_list *)malloc(sizeof(*lst))))
** return (NULL);
** if (!content)
** lst->content = NULL;
** else
** lst->content = content;
** lst->next = NULL;
** return (lst);
** }
**
** void ft_lstadd_back(t_list **alst, t_list *new);
**
** int main(void)
** {
** char tresor;
** char matos;
** char friends;
** t_list *toto;
** t_list *tmp;
**
** tresor = 'a';
** matos = 'b';
** friends = 'c';
** toto = ft_lstnew(&tresor);
** printf("toto->data :%c\n",*(char*)(toto->content));
** tmp = ft_lstnew(&matos);
** ft_lstadd_back(&toto, tmp);
** printf("----------------------\n");
** printf("toto->data :%c\n",*(char*)(toto->content));
** printf("toto->nxt->data :%c\n",*(char*)(toto->next->content));
** tmp = ft_lstnew(&friends);
** ft_lstadd_back(&toto, tmp);
** printf("----------------------\n");
** printf("toto->data :%c\n",*(char*)(toto->content));
** printf("toto->nxt->data :%c\n",*(char*)(toto->next->content));
** printf("toto->nxt->nxt->data:%c\n",*(char*)(toto->next->next->content));
** printf("toto->nxt->nxt->nxt :%s\n",(char*)(toto->next->next->next));
** return (0);
** }
*/
#include "libft.h"
void ft_lstadd_back(t_list **alst, t_list *new)
{
t_list *tmp;
if (alst)
{
tmp = *alst;
if (!tmp)
*alst = new;
else
{
while (tmp->next)
tmp = tmp->next;
tmp->next = new;
}
}
}

View File

@@ -0,0 +1,94 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_front.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:12:02 by hulamy #+# #+# */
/* Updated: 2019/11/25 14:36:54 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** add an element to the begining of a list
*/
/*
** #include <libc.h>
**
** typedef struct s_list
** {
** void *content;
** struct s_list *next;
** } t_list;
**
** void *ft_memcpy(void *dst, const void *src, size_t n)
** {
** size_t i;
** char *ptr;
** char *ptr2;
**
** ptr = (char *)dst;
** ptr2 = (char *)src;
** i = -1;
** while (++i < n)
** ptr[i] = ptr2[i];
** return (dst);
** }
**
** t_list *ft_lstnew(void *content)
** {
** t_list *lst;
**
** if (!(lst = (t_list *)malloc(sizeof(*lst))))
** return (NULL);
** if (!content)
** lst->content = NULL;
** else
** {
** if (!(lst->content = malloc(sizeof(content))))
** return (NULL);
** ft_memcpy(lst->content, content, sizeof(content));
** }
** lst->next = NULL;
** return (lst);
** }
**
** void ft_lstadd_front(t_list **alst, t_list *new);
**
** int main(void)
** {
** char tresor;
** char matos;
** char friends;
** t_list *toto;
** t_list *tmp;
**
** tresor = 'a';
** matos = 'b';
** friends = 'c';
** toto = ft_lstnew(&tresor);
** printf("toto->data :%c\n",*(char*)(toto->content));
** tmp = ft_lstnew(&matos);
** ft_lstadd_front(&toto, tmp);
** printf("----------------------\n");
** printf("toto->data :%c\n",*(char*)(toto->content));
** printf("toto->nxt->data :%c\n",*(char*)(toto->next->content));
** tmp = ft_lstnew(&friends);
** ft_lstadd_front(&toto, tmp);
** printf("----------------------\n");
** printf("toto->data :%c\n",*(char*)(toto->content));
** printf("toto->nxt->data :%c\n",*(char*)(toto->next->content));
** printf("toto->nxt->nxt->dqta:%c\n",*(char*)(toto->next->next->content));
** return (0);
** }
*/
#include "libft.h"
void ft_lstadd_front(t_list **alst, t_list *new)
{
new->next = *alst;
*alst = new;
}

View File

@@ -0,0 +1,107 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstclear.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:13:30 by hulamy #+# #+# */
/* Updated: 2019/11/28 17:06:48 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** delete and free an element of the list and all the followings
*/
/*
** #include <libc.h>
**
** typedef struct s_list
** {
** void *content;
** struct s_list *next;
** } t_list;
**
** t_list *ft_lstnew(void *content)
** {
** t_list *lst;
**
** if (!(lst = (t_list *)malloc(sizeof(*lst))))
** return (NULL);
** if (!content)
** lst->content = NULL;
** else
** lst->content = content;
** lst->next = NULL;
** return (lst);
** }
**
** void ft_lstadd_back(t_list **alst, t_list *new)
** {
** t_list *tmp;
**
** if (alst)
** {
** tmp = *alst;
** if (!tmp)
** *alst = new;
** else
** {
** while (tmp->next)
** tmp = tmp->next;
** tmp->next = new;
** }
** new->next = NULL;
** }
** }
**
** void ft_delete(void *element)
** {
** *(char*)element = '\0';
** }
**
** void ft_lstdelone(t_list *lst, void (*del)(void *))
** {
** del(lst->content);
** free(lst);
** lst = NULL;
** }
**
** void ft_lstclear(t_list **lst, void (*del)(void *));
**
** int main(void)
** {
** t_list *toto;
** void (ft_delete)(void*);
**
** printf("sizeof(t_list)%lu\n",sizeof(t_list));
** toto = ft_lstnew("a");
** toto->next = ft_lstnew("b");
** toto->next->next = ft_lstnew("c");
** printf("toto->data :%c\n",*(char*)(toto->content));
** printf("toto->nxt->data :%c\n",*(char*)(toto->next->content));
** printf("toto->nxt->nxt->data:%c\n",*(char*)(toto->next->next->content));
** printf("toto->nxt->nxt->nxt :%s\n",(char*)(toto->next->next->next));
** ft_lstclear(&(toto->next), ft_delete);
** printf("----------------------\n");
** printf("toto->data :%c\n",*(char*)(toto->content));
** printf("toto->nxt->data :%c\n",*(char*)(toto->next->content));
** printf("toto->nxt->nxt :%s\n",(char*)(toto->next->next));
** return (0);
** }
*/
#include "libft.h"
void ft_lstclear(t_list **lst, void (*del)(void *))
{
t_list *next;
while (*lst != NULL)
{
next = (*lst)->next;
ft_lstdelone(*lst, del);
*lst = next;
}
}

View File

@@ -0,0 +1,97 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstdelone.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:14:03 by hulamy #+# #+# */
/* Updated: 2019/11/25 14:35:53 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** free an element and delete its content with del
** next is not free
*/
/*
** #include <libc.h>
**
** typedef struct s_list
** {
** void *content;
** struct s_list *next;
** } t_list;
**
** t_list *ft_lstnew(void *content)
** {
** t_list *lst;
**
** if (!(lst = (t_list *)malloc(sizeof(*lst))))
** return (NULL);
** if (!content)
** lst->content = NULL;
** else
** lst->content = content;
** lst->next = NULL;
** return (lst);
** }
**
** void ft_lstadd_back(t_list **alst, t_list *new)
** {
** t_list *tmp;
**
** if (alst)
** {
** tmp = *alst;
** if (!tmp)
** *alst = new;
** else
** {
** while (tmp->next)
** tmp = tmp->next;
** tmp->next = new;
** }
** new->next = NULL;
** }
** }
**
** void ft_delete(void *element)
** {
** *(char*)element = '\0';
** }
**
** void ft_lstdelone(t_list *lst, void (*del)(void *));
**
** int main(void)
** {
** t_list *toto;
** void (ft_delete)(void*);
**
** toto = ft_lstnew("a");
** toto->next = ft_lstnew("b");
** toto->next->next = ft_lstnew("c");
** printf("toto->data :%c\n",*(char*)(toto->content));
** printf("toto->nxt->data :%c\n",*(char*)(toto->next->content));
** printf("toto->nxt->nxt->data:%c\n",*(char*)(toto->next->next->content));
** printf("toto->nxt->nxt->nxt :%s\n",(char*)(toto->next->next->next));
** ft_lstdelone(toto->next, ft_delete);
** printf("----------------------\n");
** printf("toto->data :%c\n",*(char*)(toto->content));
** printf("toto->nxt->data :%c\n",*(char*)(toto->next->content));
** printf("toto->nxt->nxt->data:%c\n",*(char*)(toto->next->next->content));
** printf("toto->nxt->nxt->nxt :%s\n",(char*)(toto->next->next->next));
** return (0);
** }
*/
#include "libft.h"
void ft_lstdelone(t_list *lst, void (*del)(void *))
{
if (lst->content && del)
del(lst->content);
free(lst);
lst = NULL;
}

View File

@@ -0,0 +1,85 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstiter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:14:11 by hulamy #+# #+# */
/* Updated: 2019/12/01 16:03:40 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** go through all elements of the list and apply the function f to each of them
*/
/*
** #include <libc.h>
**
** typedef struct s_list
** {
** void *content;
** struct s_list *next;
** } t_list;
**
** t_list *ft_lstnew(void *content)
** {
** t_list *lst;
**
** if (!(lst = (t_list *)malloc(sizeof(*lst))))
** return (NULL);
** if (!content)
** lst->content = NULL;
** else
** lst->content = content;
** lst->next = NULL;
** return (lst);
** }
**
** void ft_lstiter(t_list *lst, void (*f)(void*));
**
** void to_uppercase(void *element)
** {
** // *(char*)(((t_list*)element)->content) -= 32;
** // or :
** t_list *tmp;
**
** tmp = (t_list*)element;
** *(char*)(tmp->content) -= 32;
** }
**
** int main(void)
** {
** t_list *toto;
** void to_uppercase(void*);
**
** toto = ft_lstnew("a");
** toto->next = ft_lstnew("b");
** toto->next->next = ft_lstnew("c");
** printf("toto->data :%c\n",*(char*)(toto->content));
** printf("toto->nxt->data :%c\n",*(char*)(toto->next->content));
** printf("toto->nxt->nxt->data:%c\n",*(char*)(toto->next->next->content));
** printf("toto->nxt->nxt->nxt :%s\n",(char*)(toto->next->next->next));
** printf("---------------------------\n");
** ft_lstiter(toto, to_uppercase);
** printf("toto->data :%c\n",*(char*)(toto->content));
** printf("toto->nxt->data :%c\n",*(char*)(toto->next->content));
** printf("toto->nxt->nxt->data:%c\n",*(char*)(toto->next->next->content));
** printf("toto->nxt->nxt->nxt :%s\n",(char*)(toto->next->next->next));
** return (0);
** }
*/
#include "libft.h"
void ft_lstiter(t_list *lst, void (*f)(void *))
{
if (!f)
return ;
while (lst)
{
f(lst->content);
lst = lst->next;
}
}

View File

@@ -0,0 +1,100 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstlast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:14:49 by hulamy #+# #+# */
/* Updated: 2019/11/28 16:43:18 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** return a pointer to the last element of a list
*/
/*
** #include <libc.h>
**
** typedef struct s_list
** {
** void *content;
** struct s_list *next;
** } t_list;
**
** void *ft_memcpy(void *dst, const void *src, size_t n)
** {
** size_t i;
** char *ptr;
** char *ptr2;
**
** ptr = (char *)dst;
** ptr2 = (char *)src;
** i = -1;
** while (++i < n)
** ptr[i] = ptr2[i];
** return (dst);
** }
**
** t_list *ft_lstnew(void *content)
** {
** t_list *lst;
**
** if (!(lst = (t_list *)malloc(sizeof(*lst))))
** return (NULL);
** if (!content)
** lst->content = NULL;
** else
** {
** if (!(lst->content = malloc(sizeof(content))))
** return (NULL);
** ft_memcpy(lst->content, content, sizeof(content));
** }
** lst->next = NULL;
** return (lst);
** }
**
** void ft_lstadd_front(t_list **alst, t_list *new)
** {
** new->next = *alst;
** *alst = new;
** }
**
** t_list *ft_lstlast(t_list *lst);
**
** int main(void)
** {
** char tresor;
** t_list *toto;
** t_list *tmp;
**
** tresor = 'a';
** toto = ft_lstnew(&tresor);
** tresor = 'b';
** tmp = ft_lstnew(&tresor);
** ft_lstadd_front(&toto, tmp);
** tresor = 'c';
** tmp = ft_lstnew(&tresor);
** ft_lstadd_front(&toto, tmp);
** printf("toto->data :%c\n",*(char*)(toto->content));
** printf("toto->nxt->data :%c\n",*(char*)(toto->next->content));
** printf("toto->nxt->nxt->data:%c\n",*(char*)(toto->next->next->content));
** tmp = ft_lstlast(toto);
** printf("%c\n",*(char*)(tmp->content));
** printf("toto->data :%c\n",*(char*)(toto->content));
** printf("toto->nxt->data :%c\n",*(char*)(toto->next->content));
** printf("toto->nxt->nxt->data:%c\n",*(char*)(toto->next->next->content));
** return (0);
** }
*/
#include "libft.h"
t_list *ft_lstlast(t_list *lst)
{
if (lst)
while (lst->next)
lst = lst->next;
return (lst);
}

View File

@@ -0,0 +1,145 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:15:42 by hulamy #+# #+# */
/* Updated: 2019/12/01 16:02:13 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** iterate trhough linked list and apply to each element a function f
** if necessary the function del is used to delete an element
*/
/*
** #include <stdlib.h>
** #include <unistd.h>
** #include <stdio.h>
**
** typedef struct s_list
** {
** void *content;
** struct s_list *next;
** } t_list;
**
** t_list *ft_lstnew(void *content)
** {
** t_list *lst;
**
** if (!(lst = (t_list *)malloc(sizeof(*lst))))
** return (NULL);
** if (!content)
** lst->content = NULL;
** else
** lst->content = content;
** lst->next = NULL;
** return (lst);
** }
**
** void ft_lstadd_back(t_list **alst, t_list *new)
** {
** t_list *tmp;
**
** if (alst)
** {
** tmp = *alst;
** if (!tmp)
** *alst = new;
** else
** {
** while (tmp->next)
** tmp = tmp->next;
** tmp->next = new;
** }
** new->next = NULL;
** }
** }
**
** char *ft_strdup(const char *src)
** {
** int i;
** char *str;
**
** i = 0;
** while (src[i] != '\0')
** i++;
** if (!(str = (char*)malloc(sizeof(*str) * (i + 1))))
** return (NULL);
** while (i-- >= 0)
** str[i + 1] = src[i + 1];
** return (str);
** }
**
** void *to_uppercase(void *element)
** {
** char *i;
**
** if (!(i = ft_strdup((char*)element)))
** return (NULL);
** *i -= 32;
** return ((void *)i);
** }
**
** void ft_delete(void *element)
** {
** *(char*)element = '\0';
** }
**
** t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
**
** int main(void)
** {
** t_list *toto;
** void *(to_uppercase)(void *);
** void (ft_delete)(void*);
**
** toto = ft_lstnew("aa");
** toto->next = ft_lstnew("b");
** toto->next->next = ft_lstnew("c");
** printf("toto->data :%s\n",(char*)(toto->content));
** printf("toto->nxt->data :%s\n",(char*)(toto->next->content));
** printf("toto->nxt->nxt->data:%s\n",(char*)(toto->next->next->content));
** printf("toto->nxt->nxt->nxt :%s\n",(char*)(toto->next->next->next));
** toto = ft_lstmap(toto, to_uppercase, ft_delete);
** printf("----------------------\n");
** printf("toto->data :%s\n",(char*)(toto->content));
** printf("toto->nxt->data :%s\n",(char*)(toto->next->content));
** printf("toto->nxt->nxt->data:%s\n",(char*)(toto->next->next->content));
** printf("toto->nxt->nxt->nxt :%s\n",(char*)(toto->next->next->next));
** return (0);
** }
*/
#include "libft.h"
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *new;
t_list *tmp;
if (!lst)
return (NULL);
if (!(tmp = ft_lstnew(f(lst->content))))
{
del(tmp->content);
free(tmp);
return (NULL);
}
new = tmp;
while (lst->next)
{
lst = lst->next;
if (!(tmp->next = ft_lstnew(f(lst->content))))
{
del(tmp->next->content);
free(tmp->next);
return (NULL);
}
tmp = tmp->next;
}
return (new);
}

View File

@@ -0,0 +1,59 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstnew.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:16:20 by hulamy #+# #+# */
/* Updated: 2019/11/25 14:29:46 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** create a new list
*/
/*
** #include <libc.h>
**
** typedef struct s_list
** {
** void *content;
** struct s_list *next;
** } t_list;
**
** t_list *ft_lstnew(void *content);
**
** int main(void)
** {
** char tresor;
** t_list *toto;
**
** tresor = 'd';
** printf("tresor : %c\n",tresor);
** toto = ft_lstnew(&tresor);
** //toto->content was alocated as void* so it need cast
** printf("toto->content : %c\n",*(char*)(toto->content));
** tresor = 'D';
** printf("transform tresor : %c\n",tresor);
** printf("and also toto->content: %c\n",*(char*)(toto->content));
** return (0);
** }
*/
#include "libft.h"
t_list *ft_lstnew(void *content)
{
t_list *lst;
if (!(lst = (t_list *)malloc(sizeof(*lst))))
return (NULL);
if (!content)
lst->content = NULL;
else
lst->content = content;
lst->next = NULL;
return (lst);
}

View File

@@ -0,0 +1,86 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstsize.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:31:48 by hulamy #+# #+# */
/* Updated: 2019/11/25 16:06:41 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** return the size of the linked list
*/
/*
** #include <libc.h>
**
** typedef struct s_list
** {
** void *content;
** struct s_list *next;
** } t_list;
**
** t_list *ft_lstnew(void *content)
** {
** t_list *lst;
**
** if (!(lst = (t_list *)malloc(sizeof(*lst))))
** return (NULL);
** if (!content)
** lst->content = NULL;
** else
** lst->content = content;
** lst->next = NULL;
** return (lst);
** }
**
** void ft_lstadd_front(t_list **alst, t_list *new)
** {
** new->next = *alst;
** *alst = new;
** }
**
** int ft_lstsize(t_list *lst);
**
** int main(void)
** {
** char tresor;
** t_list *toto;
** t_list *tmp;
**
** tresor = 'a';
** toto = ft_lstnew(&tresor);
** tresor = 'b';
** tmp = ft_lstnew(&tresor);
** ft_lstadd_front(&toto, tmp);
** tresor = 'c';
** tmp = ft_lstnew(&tresor);
** ft_lstadd_front(&toto, tmp);
** printf("toto->data :%c\n",*(char*)(toto->content));
** printf("toto->nxt->data :%c\n",*(char*)(toto->next->content));
** printf("toto->nxt->nxt->dqta:%c\n",*(char*)(toto->next->next->content));
** printf("%i\n",ft_lstsize(toto));
** printf("toto->data :%c\n",*(char*)(toto->content));
** printf("toto->nxt->data :%c\n",*(char*)(toto->next->content));
** printf("toto->nxt->nxt->dqta:%c\n",*(char*)(toto->next->next->content));
** return (0);
** }
*/
#include "libft.h"
int ft_lstsize(t_list *lst)
{
int size;
size = 0;
while (lst)
{
size++;
lst = lst->next;
}
return (size);
}

View File

@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memalloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/19 15:21:44 by hulamy #+# #+# */
/* Updated: 2019/11/19 15:23:17 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** allocate size byte of memory and return a pointer to the allocated memory
*/
#include "libft.h"
void *ft_memalloc(size_t size)
{
void *tmp;
if (!size || !(tmp = malloc(size)))
return (NULL);
ft_bzero(tmp, size);
return (tmp);
}

View File

@@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memccpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 15:24:51 by hulamy #+# #+# */
/* Updated: 2019/11/25 15:25:09 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** copy string until character is found and place cursor in dst
** after last byte copied
*/
#include "libft.h"
void *ft_memccpy(void *dst, const void *src, int c, size_t n)
{
unsigned char *dest;
unsigned char *sourc;
size_t i;
i = -1;
dest = (unsigned char *)dst;
sourc = (unsigned char *)src;
while (++i < n)
{
dest[i] = sourc[i];
if (sourc[i] == (unsigned char)c)
return (dst + i + 1);
}
return (NULL);
}

View File

@@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:55:51 by hulamy #+# #+# */
/* Updated: 2019/12/12 21:50:32 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** locate character in string and return its position
*/
/*
** #include <libc.h>
**
** void *ft_memchr(const void *s, int c, size_t n);
**
** int main(void)
** {
** const char *str;
**
** char *pouet = "z";
** char *lolzer = (char *)&pouet[2];
** lolzer = "aaaaaaaaaa";
** str = ft_memchr(pouet, 'a', 50);
** if (!str)
** printf("NULL");
** else
** printf("%s\n", str);
** return (0);
** }
*/
#include "libft.h"
void *ft_memchr(const void *s, int c, size_t n)
{
unsigned char *sbis;
size_t i;
sbis = (unsigned char *)s;
i = -1;
while (++i < n)
if (sbis[i] == (unsigned char)c)
return ((void *)sbis + i);
return (NULL);
}

View File

@@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:56:05 by hulamy #+# #+# */
/* Updated: 2019/11/25 13:56:07 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** compare two bytes strings (doesnt recognize a null terminated string)
** and return value of difference between first two different character
*/
#include "libft.h"
int ft_memcmp(const void *s1, const void *s2, size_t n)
{
unsigned char *frst;
unsigned char *scnd;
size_t i;
i = 0;
frst = (unsigned char *)s1;
scnd = (unsigned char *)s2;
while (i < n && frst[i] == scnd[i])
i++;
return ((i == n) ? 0 : frst[i] - scnd[i]);
}

View File

@@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:56:16 by hulamy #+# #+# */
/* Updated: 2019/12/01 14:54:14 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** copy n characters from src to dst and return dst
*/
/*
** #include <libc.h>
**
** void *ft_memcpy(void *dst, const void *src, size_t n);
**
** int main(int ac, char **av)
** {
** if (ac == 4)
** printf("%s\n", ft_memcpy(av[1], av[2], atoi(av[3])));
** return (0);
** }
*/
#include "libft.h"
void *ft_memcpy(void *dst, const void *src, size_t n)
{
int i;
char *ptr;
char *ptr2;
i = -1;
ptr = (char *)dst;
ptr2 = (char *)src;
if (dst == src)
return (dst);
while (++i < (int)n)
ptr[i] = ptr2[i];
return (dst);
}

View File

@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memdel.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/14 21:13:26 by hulamy #+# #+# */
/* Updated: 2019/04/03 15:44:12 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** free memory
*/
#include "libft.h"
void ft_memdel(void **ap)
{
if (ap && *ap)
{
free(*ap);
*ap = 0;
}
}

View File

@@ -0,0 +1,68 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memmove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:56:25 by hulamy #+# #+# */
/* Updated: 2019/12/10 23:53:40 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** copy n characters from src to dst in a non destructive way and return dst
*/
/*
** #include <libc.h>
**
** size_t ft_strlen(const char *str)
** {
** size_t i;
**
** i = 0;
** while (str[i])
** i++;
** return (i);
** }
**
** void *ft_memmove(void *dst, const void *src, size_t len);
**
** //int main(int ac, char **av)
** int main(void)
** {
** char *src = "this is a good nyancat !\r\n";
** char dst[0xF0];
** int size = strlen(src);
**
** // if (ac == 4)
** // printf("%s\n", ft_memmove(av[1], av[2], atoi(av[3])));
**
** ft_memmove(dst, src, size);
** printf("%s", dst);
** return (0);
** }
*/
#include "libft.h"
void *ft_memmove(void *dst, const void *src, size_t len)
{
size_t i;
char *cpsrc;
char *cpdst;
i = -1;
cpsrc = (char *)src;
cpdst = (char *)dst;
if (dst == src)
return (dst);
if (cpsrc < cpdst)
while (len--)
cpdst[len] = cpsrc[len];
else
while (++i < len)
cpdst[i] = cpsrc[i];
return (dst);
}

View File

@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:56:37 by hulamy #+# #+# */
/* Updated: 2019/11/25 13:56:38 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** copy n time a character in a string and return the string
*/
#include "libft.h"
void *ft_memset(void *b, int c, size_t len)
{
char *ptr;
size_t i;
ptr = (char *)b;
i = 0;
while (i < len)
ptr[i++] = c;
return (b);
}

View File

View File

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/14 21:14:00 by hulamy #+# #+# */
/* Updated: 2018/11/14 21:14:01 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putchar(char c)
{
write(1, &c, 1);
}

View File

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:59:40 by hulamy #+# #+# */
/* Updated: 2019/11/25 13:59:42 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putchar_fd(char c, int fd)
{
write(fd, &c, 1);
}

View File

@@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putendl.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/14 21:14:32 by hulamy #+# #+# */
/* Updated: 2018/11/14 21:14:33 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putendl(char const *s)
{
ft_putstr(s);
ft_putchar('\n');
}

View File

@@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putendl_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:59:47 by hulamy #+# #+# */
/* Updated: 2019/11/25 13:59:48 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** write the string s on the given file descriptor fd, followed by a newline
*/
#include "libft.h"
void ft_putendl_fd(char *s, int fd)
{
ft_putstr_fd(s, fd);
ft_putchar_fd('\n', fd);
}

View File

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/14 21:14:57 by hulamy #+# #+# */
/* Updated: 2018/11/14 21:14:58 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putnbr(int n)
{
ft_putnbr_fd(n, 1);
}

View File

@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:59:56 by hulamy #+# #+# */
/* Updated: 2019/11/25 13:59:57 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putnbr_fd(int n, int fd)
{
long l;
l = n;
if (l < 0)
{
ft_putchar_fd('-', fd);
l *= -1;
}
if (l >= 10)
ft_putnbr_fd(l / 10, fd);
ft_putchar_fd((l % 10) + '0', fd);
}

View File

@@ -0,0 +1,59 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbrbase.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/16 15:17:00 by hulamy #+# #+# */
/* Updated: 2018/11/16 15:23:43 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int check(char *base)
{
int i;
int j;
i = 0;
while (base[i])
{
j = i + 1;
while (base[j])
{
if (base[i] == base[j])
return (0);
j++;
}
if (base[i] == '-' || base[i] == '+')
return (0);
i++;
}
if (i >= 2)
return (1);
return (0);
}
void ft_putnbrbase(int nbr, char *base)
{
int i;
long n;
i = 0;
n = nbr;
if (check(base))
{
if (n < 0)
{
ft_putchar('-');
n = -n;
}
while (base[i])
i++;
if (n >= i)
ft_putnbrbase(n / i, base);
ft_putchar(base[n % i]);
}
}

View File

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbrendl.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/19 10:38:07 by hulamy #+# #+# */
/* Updated: 2019/02/19 10:42:46 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putnbrendl(int n)
{
ft_putnbrendl_fd(n, 1);
}

View File

@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbrendl_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/19 10:37:58 by hulamy #+# #+# */
/* Updated: 2019/02/19 10:42:48 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putnbrendl_fd(int n, int fd)
{
long l;
l = n;
if (l < 0)
{
ft_putchar_fd('-', fd);
l *= -1;
}
if (l >= 10)
ft_putnbr_fd(l / 10, fd);
ft_putchar_fd((l % 10) + '0', fd);
ft_putchar_fd('\n', fd);
}

View File

@@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/14 21:15:19 by hulamy #+# #+# */
/* Updated: 2018/11/14 21:15:19 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putstr(char const *s)
{
int i;
i = 0;
while (s && s[i])
ft_putchar(s[i++]);
}

View File

@@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:00:04 by hulamy #+# #+# */
/* Updated: 2019/11/25 14:00:05 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** write the string s on the given file descritor fd
*/
#include "libft.h"
void ft_putstr_fd(char *s, int fd)
{
while (s && *s)
ft_putchar_fd(*s++, fd);
}

View File

@@ -0,0 +1,8 @@
#include "libft.h"
int ft_sign(int i)
{
if (i < 0)
return (-1);
return (1);
}

View File

@@ -0,0 +1,8 @@
#include "libft.h"
int ft_smaller(int a, int b)
{
if (a > b)
return (b);
return (a);
}

View File

@@ -0,0 +1,137 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/12 22:41:54 by hulamy #+# #+# */
/* Updated: 2019/12/13 01:35:15 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** return an array of string with each word found in str, with c as separator
*/
/*
** #include <stdio.h>
** #include <stdlib.h>
** #include <unistd.h>
**
** size_t ft_strlen(const char *str)
** {
** size_t i;
**
** i = 0;
** while (str[i])
** i++;
** return (i);
** }
**
** char *ft_substr(char const *s, unsigned int start, size_t len)
** {
** char *str;
** size_t i;
**
** if (!s)
** return (NULL);
** if (ft_strlen(s) < start)
** return ("");
** if (!(str = (char *)malloc(sizeof(char) * (len + 1))))
** return (NULL);
** i = 0;
** while (i < len && s[start])
** str[i++] = s[start++];
** str[i] = '\0';
** return (str);
** }
**
** char **ft_split(char const *s, char c);
** char **ft_strsplit(char const *s, char c);
**
** int main(void)
** {
** char **str;
** int i;
**
** char *s;
** char c;
**
** i = -1;
** s = NULL;
** c = ' ';
** str = ft_split(s, c);
** //if (str)
** //{
** printf("s : '%s'\n", s);
** printf("*str : '%p'\n", str[0]);
** while (str[++i])
** printf("str[%i] : '%s'\n", i, str[i]);
** //}
** return (0);
** }
*/
#include "libft.h"
static int count(char const *s, char c)
{
int i;
int words;
i = -1;
words = 0;
while (s[++i] != '\0')
if (s[i] != c && ++words)
while (s[i + 1] != '\0' && s[i + 1] != c)
i++;
return (words);
}
void *ft_free(char **array, int w)
{
int i;
i = 0;
while (array[i] != NULL && i < w)
free(array[i++]);
free(array);
return (NULL);
}
char **empty_s(char **empty)
{
if (!(empty = (char **)malloc(sizeof(char *) * 1)))
return (NULL);
empty[0] = NULL;
return (empty);
}
char **ft_split(char const *s, char c)
{
char **array;
int w;
int len;
if (!s)
return (empty_s(NULL));
if (!(array = (char **)malloc(sizeof(char *) * (count(s, c) + 1))))
return (NULL);
w = 0;
while (*s != '\0')
{
len = 0;
if (*s != c)
{
while (s[len] != '\0' && s[len] != c)
len++;
if (!(array[w++] = ft_substr(s, 0, len)))
return (ft_free(array, w));
s += len - 1;
}
s++;
}
array[w] = NULL;
return (array);
}

View File

@@ -0,0 +1,15 @@
#include "libft.h"
/*
** return the square root of nb
** or the integer value of it
*/
int ft_sqrt(int nb)
{
int i;
i = 0;
while ((i*i) < nb)
i++;
return (i);
}

View File

@@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/14 21:15:40 by hulamy #+# #+# */
/* Updated: 2019/03/25 15:12:58 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** append src to dest (dest must have sufficient space) and return dest
*/
#include "libft.h"
char *ft_strcat(char *dest, const char *src)
{
int i;
int j;
i = 0;
j = 0;
while (dest[i])
i++;
while (src[j])
dest[i++] = src[j++];
dest[i] = '\0';
return (dest);
}

View File

@@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:56:46 by hulamy #+# #+# */
/* Updated: 2019/11/25 13:56:47 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** locate the first occurence of character c in string s
** and return pointer to its location
*/
#include "libft.h"
char *ft_strchr(const char *s, int c)
{
int i;
int j;
i = 0;
j = -1;
while (s[i])
i++;
while (++j < i + 1)
if (s[j] == c)
return ((char *)s + j);
return (NULL);
}

View File

@@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strchrset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/16 15:07:11 by hulamy #+# #+# */
/* Updated: 2020/03/10 15:24:14 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** if any character of the character set is found in s
** return a pointer to the first found, else return 0
*/
#include "libft.h"
char *ft_strchrset(const char *s, const char *set)
{
int i;
i = 0;
while (set[i] != '\0')
{
if (ft_strchr(s, set[i]) != NULL)
return ((char *)set + i);
i++;
}
return (NULL);
}

View File

@@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strclr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/14 21:15:58 by hulamy #+# #+# */
/* Updated: 2019/03/25 15:17:42 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** fill string with zeros
*/
#include "libft.h"
void ft_strclr(char *s)
{
if (s)
ft_bzero(s, ft_strlen(s));
}

View File

@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/14 21:16:08 by hulamy #+# #+# */
/* Updated: 2019/03/25 15:18:30 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** compare two null terminated strings and return value
** of difference between first two different character
*/
#include "libft.h"
int ft_strcmp(const char *s1, const char *s2)
{
int i;
i = 0;
while (s1[i] && s1[i] == s2[i])
i++;
return ((unsigned char)s1[i] - (unsigned char)s2[i]);
}

View File

@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/14 21:16:17 by hulamy #+# #+# */
/* Updated: 2019/03/25 15:19:19 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** copy string src to dst including '\0' and return dst
*/
#include "libft.h"
char *ft_strcpy(char *dest, const char *src)
{
int i;
i = -1;
while (src[++i])
dest[i] = src[i];
dest[i] = '\0';
return (dest);
}

View File

@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdel.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/14 21:16:25 by hulamy #+# #+# */
/* Updated: 2019/03/25 15:19:54 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** free memory
*/
#include "libft.h"
void ft_strdel(char **as)
{
if (as && *as)
{
free(*as);
*as = 0;
}
}

View File

@@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:56:54 by hulamy #+# #+# */
/* Updated: 2019/11/25 13:56:55 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** save a copy of string src by allocating memory and return pointer to copy
*/
#include "libft.h"
char *ft_strdup(const char *src)
{
int i;
char *str;
i = 0;
while (src[i] != '\0')
i++;
if (!(str = (char*)malloc(sizeof(*str) * (i + 1))))
return (NULL);
while (i-- >= 0)
str[i + 1] = src[i + 1];
return (str);
}

View File

@@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strequ.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/14 21:16:44 by hulamy #+# #+# */
/* Updated: 2019/03/25 15:21:02 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** return 0 if strings s1 and s2 are identical and 1 if not
*/
#include "libft.h"
int ft_strequ(char const *s1, char const *s2)
{
if (!s1 || !s2)
return (0);
return (ft_strcmp(s1, s2) == 0);
}

View File

@@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_striter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/14 21:16:53 by hulamy #+# #+# */
/* Updated: 2019/03/25 15:21:14 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** apply function f to each element of string s
*/
#include "libft.h"
void ft_striter(char *s, void (*f)(char *))
{
while (s && *s && f)
f(s++);
}

View File

@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_striteri.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/14 21:17:04 by hulamy #+# #+# */
/* Updated: 2019/03/25 15:21:27 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** apply function f to each element of string s with index specified
*/
#include "libft.h"
void ft_striteri(char *s, void (*f)(unsigned int, char *))
{
int i;
i = 0;
while (s && *s && f)
f(i++, s++);
}

View File

@@ -0,0 +1,79 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:01:26 by hulamy #+# #+# */
/* Updated: 2019/12/09 21:38:35 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** create a new string by concatenating the two strings s1 and s2
*/
/*
** #include <libc.h>
**
** size_t ft_strlen(const char *str)
** {
** size_t i;
**
** i = 0;
** while (str[i])
** i++;
** return (i);
** }
**
** char *ft_strjoin(char const *s1, char const *s2);
**
** int main(int ac, char **av)
** {
** char *s1;
** char *s2;
** char *str;
**
** if (ac == 0)
** return (0);
** else if (ac == 3)
** {
** s1 = strdup(av[1]);
** s2 = strdup(av[2]);
** }
** else
** {
** s1 = malloc(sizeof(char*) * 100);
** s1 = "sdf";
** s2 = "tref";
** }
** str = ft_strjoin(s1, s2);
** printf("%s\n", str);
** return (0);
** }
*/
#include "libft.h"
char *ft_strjoin(char const *s1, char const *s2)
{
char *str;
int len;
int i;
if (!s1 || !s2)
return (NULL);
len = ft_strlen(s1) + ft_strlen(s2);
if (!(str = (char *)malloc(sizeof(char) * (len + 1))))
return (NULL);
len = 0;
i = 0;
while (s1[i] != '\0')
str[len++] = s1[i++];
i = 0;
while (s2[i] != '\0')
str[len++] = s2[i++];
str[len] = '\0';
return (str);
}

View File

@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoinfree.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/05 15:05:28 by hulamy #+# #+# */
/* Updated: 2019/03/25 15:22:28 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** create a new string by concatenating the two strings s1 and s2
** then free s1 and s2
*/
#include "libft.h"
char *ft_strjoinfree(char *s1, char *s2)
{
char *str;
if (!(str = ft_strjoin(s1, s2)))
return (NULL);
free(s1);
free(s2);
return (str);
}

View File

@@ -0,0 +1,108 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:57:02 by hulamy #+# #+# */
/* Updated: 2019/11/25 14:23:18 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** append src to sized dest and return size of final dest
*/
/*
** #include <libc.h>
**
** size_t ft_strlcat(char *dest, const char *src, size_t size);
**
** size_t ft_strlcat2(char *dest, char *src, size_t size);
**
** int ft_strlen(char *str)
** {
** int i;
**
** i = 0;
** while (str[i])
** i++;
** return (i);
** }
**
** int main(int ac, char **av)
** {
** char tmp1[100];
** char tmp2[100];
** int i;
**
** i = atoi(av[3]);
** strcpy(tmp1, av[1]);
** strcpy(tmp2, av[2]);
**
** if (ac == 4)
** {
** printf("----strlcat: %zu - %s - %s\n", strlcat(tmp1, tmp2, i),
** tmp1, tmp2);
**
** strcpy(tmp1, av[1]);
** strcpy(tmp2, av[2]);
**
** printf("-ft_strlcat: %zu - %s - %s\n", ft_strlcat(tmp1, tmp2, i),
** tmp1, tmp2);
**
** strcpy(tmp1, av[1]);
** strcpy(tmp2, av[2]);
**
** printf("ft_strlcat2: %zu - %s - %s\n", ft_strlcat2(tmp1, tmp2, i),
** tmp1, tmp2);
** }
** }
**
** size_t ft_strlcat2(char *dest, char *src, size_t size)
** {
** size_t i;
** size_t dest_length;
** size_t src_length;
**
** i = 0;
** dest_length = ft_strlen(dest);
** src_length = ft_strlen(src);
** if (size > dest_length + 1)
** {
** while (i < (size - dest_length - 1))
** {
** dest[i + dest_length] = src[i];
** i++;
** }
** dest[dest_length + i] = '\0';
** }
** if (size >= dest_length)
** return (dest_length + src_length);
** return (src_length + size);
** }
*/
#include "libft.h"
size_t ft_strlcat(char *dest, const char *src, size_t size)
{
size_t i;
size_t j;
i = 0;
j = 0;
while (dest[i] && i < size)
i++;
while (src[j])
{
if (j + i < size - 1 && size)
{
dest[i + j] = src[j];
dest[i + j + 1] = '\0';
}
j++;
}
return (i + j);
}

View File

@@ -0,0 +1,70 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:57:19 by hulamy #+# #+# */
/* Updated: 2019/12/01 16:12:57 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** copy size - 1 length of src into dest,
** terminate it with a '\0'
** and return size of src
** this way, if you try to copy a name in a variable with an available size
** of 5 for exemple, if you use ft_strlcpy(variable, name, sizeof(variable))
** you will know if the name was too long for the variable by looking at the
** return value (which is size of name)
*/
/*
** #include <libc.h>
**
** size_t ft_strlcpy(char *dest, const char *src, size_t size);
**
** int main(int argc, char **argv)
** {
** char str[100];
** int i;
** unsigned int u;
** unsigned int v;
**
** i = atoi(argv[3]);
** strcpy(str, argv[2]);
** if (argc > 3)
** {
** u = strlcpy(argv[2], argv[1], i);
** printf("strlcpy : %s - %s - %d",argv[1], argv[2], i);
** printf(" - return:%d\n",u);
** strcpy(argv[2], str);
** printf("(re-init : %s - %s - %d)\n",argv[1], argv[2], i);
** v = ft_strlcpy(argv[2], argv[1], i);
** printf("ft_strlcpy: %s - %s - %d",argv[1], argv[2], i);
** printf(" - return:%d\n",v);
** }
** return (0);
** }
*/
#include "libft.h"
size_t ft_strlcpy(char *dest, const char *src, size_t size)
{
size_t i;
size_t j;
i = 0;
j = 0;
while (src[i] != '\0')
{
if (i + 1 < size)
dest[i] = src[j++];
i++;
}
if (size > 0)
dest[j] = '\0';
return (i);
}

View File

@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:57:48 by hulamy #+# #+# */
/* Updated: 2019/11/25 13:57:49 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** return length of of string
*/
#include "libft.h"
size_t ft_strlen(const char *str)
{
size_t i;
i = 0;
while (str[i])
i++;
return (i);
}

View File

@@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/14 21:17:49 by hulamy #+# #+# */
/* Updated: 2019/03/25 15:23:12 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** create a new array with the result of function f on every element of s
*/
#include "libft.h"
char *ft_strmap(char const *s, char (*f)(char))
{
char *str;
int i;
if (!s)
return (NULL);
if (!(str = ft_strnew(ft_strlen(s))))
return (NULL);
i = -1;
while (s[++i])
str[i] = f(s[i]);
return (str);
}

View File

@@ -0,0 +1,75 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:01:40 by hulamy #+# #+# */
/* Updated: 2019/12/09 21:44:07 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** create a new array with the result of function f on every element of
** s by index i
*/
/*
** #include <libc.h>
**
** size_t ft_strlen(const char *str)
** {
** size_t i;
**
** i = 0;
** while (str[i])
** i++;
** return (i);
** }
** char touppercase(unsigned int i, char c)
** {
** if (i < 3 && c >= 'a' && c <= 'z')
** c -= 32;
** return (c);
** }
**
** char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
**
** int main(int ac, char **av)
** {
** char *str;
** char touppercase(unsigned int, char);
**
** if (ac > 2)
** return (0);
** if (ac == 2)
** str = strdup(av[1]);
** if (ac == 1)
** str = NULL;
** //str = ft_strmapi(str, touppercase);
** str = ft_strmapi(str, NULL);
** printf("%s\n",str);
** return (0);
** }
*/
#include "libft.h"
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
char *str;
int i;
int size;
if (!s || !f)
return (NULL);
size = ft_strlen(s);
if (!(str = (char *)malloc(sizeof(char) * (size + 1))))
return (NULL);
str[size] = '\0';
i = -1;
while (s[++i])
str[i] = f(i, s[i]);
return (str);
}

View File

@@ -0,0 +1,83 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmultisplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/16 15:18:29 by hulamy #+# #+# */
/* Updated: 2019/03/25 15:23:57 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** return an array of string with each word found in str
** with any character of charset difining a separator
*/
#include "libft.h"
static int ft_is_separator(char c, char *charset, int i)
{
while (charset[i])
{
if (c == charset[i])
return (1);
i++;
}
c = charset[i];
return (0);
}
static int ft_count(char *str, int word, char **tab, char *charset)
{
int i;
int j;
int k;
k = 0;
i = 0;
while (ft_is_separator(str[k], charset, 0) == 1)
k++;
while (str[k] != '\0' && i != word)
{
j = 0;
while (!ft_is_separator(str[k + j], charset, 0) && str[k + j] != '\0')
{
if (word == -2)
tab[i][j] = str[k + j];
j++;
}
k += j;
while (ft_is_separator(str[k], charset, 0))
k++;
i++;
}
if (word == -1)
return (i);
return (j);
}
char **ft_strmultisplit(char *str, char *charset)
{
char **tab;
int i;
int j;
int k;
k = 0;
tab = 0;
i = ft_count(str, -1, tab, charset);
if (!(tab = (char**)malloc(sizeof(tab) * (i + 1))))
return (NULL);
tab[i] = 0;
while (k < i)
{
j = ft_count(str, k + 1, tab, charset);
tab[k] = (char*)malloc(sizeof(*tab) * (j + 1));
tab[k][j] = '\0';
k++;
}
ft_count(str, -2, tab, charset);
return (tab);
}

View File

@@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/14 21:18:24 by hulamy #+# #+# */
/* Updated: 2019/03/25 15:24:11 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** append n character of src to dest and return dest
*/
#include "libft.h"
char *ft_strncat(char *dest, const char *src, size_t nb)
{
size_t i;
size_t j;
i = 0;
j = 0;
while (dest[i])
i++;
while (src[j] && j < nb)
dest[i++] = src[j++];
dest[i] = '\0';
return (dest);
}

View File

@@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:57:59 by hulamy #+# #+# */
/* Updated: 2019/11/25 13:58:00 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** compare size first character of two null terminated strings
** and return value of difference between first two different character
*/
#include "libft.h"
int ft_strncmp(const char *s1, const char *s2, size_t n)
{
size_t i;
int res;
i = 0;
res = 0;
while (s1[i] && s1[i] == s2[i] && i < n - 1)
i++;
if (n != 0)
res = (unsigned char)s1[i] - (unsigned char)s2[i];
return (res);
}

View File

@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/14 21:18:44 by hulamy #+# #+# */
/* Updated: 2019/03/25 15:24:59 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** copy n characters from string src to dst including '\0'
** if space remain it's filled zith '\0', and return dst
*/
#include "libft.h"
char *ft_strncpy(char *dest, const char *src, size_t n)
{
size_t i;
i = -1;
while (++i < n && src[i])
dest[i] = src[i];
while (i < n)
dest[i++] = '\0';
return (dest);
}

View File

@@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnequ.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/14 21:18:55 by hulamy #+# #+# */
/* Updated: 2019/03/25 15:25:20 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** return 0 if n first character of strings s1 and s2 are identical
** and 1 if not
*/
#include "libft.h"
int ft_strnequ(char const *s1, char const *s2, size_t n)
{
if (!s1 || !s2)
return (0);
return (ft_strncmp(s1, s2, n) == 0);
}

View File

@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnew.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/14 21:19:08 by hulamy #+# #+# */
/* Updated: 2019/11/21 17:00:30 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** create a new string of length size, fill with zero, and return pointer to it
*/
#include "libft.h"
char *ft_strnew(size_t size)
{
char *str;
if (!(str = (char *)malloc(sizeof(char) * (size + 1))))
return (NULL);
ft_bzero(str, size + 1);
return (str);
}

View File

@@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:58:10 by hulamy #+# #+# */
/* Updated: 2019/11/25 13:58:11 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** locate the first occurence of the string little in len first characters
** of big and return a pointer to this occurence if found
*/
#include "libft.h"
char *ft_strnstr(const char *big, const char *little, size_t len)
{
size_t i;
size_t j;
j = 0;
i = 0;
if (!ft_strlen(little))
return ((char *)big);
while (i == 0 && j <= len)
{
while (little[i] == big[j + i] && little[i] && j + i <= len)
i++;
if (little[i])
{
j++;
if (!big[j] || j >= len)
return (0);
i = 0;
}
}
return ((char *)big + j);
}

View File

@@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strrchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:58:20 by hulamy #+# #+# */
/* Updated: 2019/11/25 13:58:21 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** locate the last occurence of character c in string s
** and return pointer to its location
*/
#include "libft.h"
char *ft_strrchr(const char *s, int c)
{
int i;
i = 0;
while (s[i])
i++;
i++;
while (i--)
if (s[i] == c)
return ((char *)s + i);
return (NULL);
}

View File

@@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/14 21:19:45 by hulamy #+# #+# */
/* Updated: 2019/03/25 15:26:59 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** locate the first occurence of the string little in big
** and return a pointer to this occurence if found
*/
#include "libft.h"
char *ft_strstr(const char *str, const char *to_find)
{
int i;
int j;
j = 0;
i = 0;
if (!ft_strlen(to_find))
return ((char *)str);
while (i == 0)
{
while (to_find[i] && to_find[i] == str[j + i])
i++;
if (to_find[i])
{
j++;
if (str[j] == '\0' && to_find[i])
return (0);
i = 0;
}
}
return ((char *)str + j);
}

View File

@@ -0,0 +1,101 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:01:49 by hulamy #+# #+# */
/* Updated: 2019/12/09 21:46:54 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** create a copy of s without the firsts and lasts set of characters
*/
/*
** #include <libc.h>
**
** char *ft_substr(char const *s, unsigned int start, size_t len)
** {
** char *str;
** size_t i;
**
** if (!s)
** return (NULL);
** if (!(str = (char *)malloc(sizeof(char) * (len + 1))))
** return (NULL);
** str[len] = '\0';
** i = 0;
** while (i < len)
** str[i++] = s[start++];
** return (str);
** }
**
** size_t ft_strlen(const char *str)
** {
** size_t i;
**
** i = 0;
** while (str[i])
** i++;
** return (i);
** }
**
** char *ft_strchr(const char *s, int c)
** {
** int i;
** int j;
**
** i = 0;
** j = -1;
** while (s[i])
** i++;
** while (++j < i + 1)
** if (s[j] == c)
** return ((char *)s + j);
** return (NULL);
** }
**
** char *ft_strtrim(char const *s1, char const *set);
**
** int main(int ac, char **av)
** {
** char *s1;
** char *s2;
**
** if (ac == 3)
** {
** s1 = strdup(av[1]);
** s2 = strdup(av[2]);
** }
** if (ac == 1)
** {
** s1 = "fuehf";
** s2 = NULL;
** }
** printf("%s\n",ft_strtrim(s1, s2));
** return (0);
** }
*/
#include "libft.h"
char *ft_strtrim(char const *s1, char const *set)
{
int len;
char *str;
if (!s1 || !set)
return (NULL);
while (s1[0] && ft_strchr(set, s1[0]))
s1++;
len = ft_strlen(s1) - 1;
while (len >= 0 && ft_strchr(set, s1[len]))
len--;
len++;
if (!(str = ft_substr(s1, 0, len)))
return (NULL);
return (str);
}

View File

@@ -0,0 +1,104 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/01 16:00:10 by hulamy #+# #+# */
/* Updated: 2019/12/09 22:04:26 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** create a copy of a portion of s, begining at start and of length len
*/
/*
** #include <unistd.h>
** #include <stdlib.h>
** #include <stdio.h>
**
** char *ft_substr(char const *s, unsigned int start, size_t len);
**
** char *ft_strdup(const char *src)
** {
** int i;
** char *str;
**
** i = 0;
** while (src[i] != '\0')
** i++;
** if (!(str = (char*)malloc(sizeof(*str) * (i + 1))))
** return (NULL);
** while (i-- >= 0)
** str[i + 1] = src[i + 1];
** return (str);
** }
**
** int ft_strncmp(const char *s1, const char *s2, size_t n)
** {
** size_t i;
** int res;
**
** i = 0;
** res = 0;
** while (s1[i] && s1[i] == s2[i] && i < n - 1)
** i++;
** if (n != 0)
** res = (unsigned char)s1[i] - (unsigned char)s2[i];
** return (res);
** }
**
** size_t ft_strlen(const char *str)
** {
** size_t i;
**
** i = 0;
** while (str[i])
** i++;
** return (i);
** }
**
** int main(int ac, char **av)
** {
** char *str;
** size_t size;
** int start;
**
** str = "frgehr";
** start = 2;
** size = 5;
** if (ac == 4)
** {
** str = strdup(av[1]);
** start = atoi(av[2]);
** size = atoi(av[3]);
** }
** char *ret = ft_substr(str, start, size);
** printf("%s, %i, %zu\n", str, start, size);
** printf("%s\n", ret);
** free(ret);
** return (0);
** }
*/
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *str;
size_t i;
if (!s)
return (NULL);
if (ft_strlen(s) < start)
return (ft_strdup(""));
if (!(str = (char *)malloc(sizeof(char) * (len + 1))))
return (NULL);
i = 0;
while (i < len && s[start])
str[i++] = s[start++];
str[i] = '\0';
return (str);
}

View File

@@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_tolower.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:58:30 by hulamy #+# #+# */
/* Updated: 2019/11/25 13:58:32 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_tolower(int c)
{
if (c >= 'A' && c <= 'Z')
return (c + 32);
return (c);
}

View File

@@ -0,0 +1,7 @@
char touppercase(unsigned int i, char c)
{
if (i < 3 && c >= 'a' && c <= 'z')
c -= 32;
return (c);
}

View File

@@ -0,0 +1,74 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_utoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/03/12 22:36:50 by hulamy #+# #+# */
/* Updated: 2020/03/12 22:36:56 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** take an unsigned integer and give a string
*/
/*
** #include <stdio.h> // for printf
** #include <stdlib.h> // for atoi
**
** char *ft_utoa(unsigned long int n);
**
** int main(int ac, char **av)
** {
** if (ac == 0)
** return (0);
** else if (ac == 2)
** printf("%s\n",ft_utoa(atoi(av[1])));
** else
** {
** unsigned long int i;
** i = 0;
** printf(" %lu\n %s\n\n",i,ft_utoa(i));
** i = 237683;
** printf(" %lu\n %s\n\n",i,ft_utoa(i));
** i = 1234567;
** printf(" %lu\n %s\n\n",i,ft_utoa(i));
** i = 12345678;
** printf(" %lu\n %s\n\n",i,ft_utoa(i));
** i = 2147483646;
** printf(" %lu\n %s\n\n",i,ft_utoa(i));
** i = 2147483647;
** printf(" %lu\n %s\n\n",i,ft_utoa(i));
** i = 2147483648;
** printf(" %lu\n %s\n\n",i,ft_utoa(i));
** i = 2147483649;
** printf(" %lu\n %s\n\n",i,ft_utoa(i));
** i = 9223372036854775807;
** printf("| %lu\n| %s\n\n",i,ft_utoa(i));
** }
** return 0;
** }
*/
#include "libft.h"
char *ft_utoa(unsigned long int n)
{
char *str;
int len;
unsigned long int cpy;
cpy = n;
len = 1;
while (n /= 10)
len++;
if (!(str = (char *)malloc(sizeof(char) * (len + 1))))
return (NULL);
str[len] = '\0';
str[--len] = cpy % 10 + '0';
while (cpy /= 10)
str[--len] = cpy % 10 + '0';
return (str);
}