diff --git a/.Makefile.swn b/.Makefile.swn deleted file mode 100644 index e91197a..0000000 Binary files a/.Makefile.swn and /dev/null differ diff --git a/Makefile b/Makefile index 8268051..cd5c1d5 100644 --- a/Makefile +++ b/Makefile @@ -2,26 +2,22 @@ # variables names # # - - - - - - - - - # -NAME = libft.a -CC = gcc -VPATH = srcs +NAME = libft.a +CC = gcc +VPATH = srcs/part1/ \ + srcs/part2/ \ + srcs/bonus/ \ + srcs/add/ -IDIR = includes -_DEP = libft.h -DEPS = $(_DEP:%.h=$(IDIR)/%.h) -# - $(_DEP:%.h=$(IDIR)/%.h) is a built-in function called -# a "substitute reference", an abbreviation for the -# expansion function "patsubst" : -# $(patsubst %.h,$(IDIR)/%.h,$(_DEP)) -# $(patsubst pattern,replacement,text) -# - % match everything, the value of the first % in -# "replacement" is replaced by the text matched by the -# first % in "pattern", it only works for the firsts +IDIR = includes +_DEP = libft.h \ + libonnus.h +DEPS = $(_DEP:%.h=$(IDIR)/%.h) -CFLAGS = -I$(IDIR) -CFLAGS += -Wall -Wextra -Werror +CFLAGS = -I$(IDIR) +CFLAGS += -Wall -Wextra -Werror -SRCS = ft_memset.c \ +SRCS = ft_memset.c \ ft_bzero.c \ ft_memcpy.c \ ft_memmove.c \ @@ -47,38 +43,35 @@ SRCS = ft_memset.c \ \ ft_substr.c \ ft_strjoin.c \ - ft_strtrim.c \ #fonction un peu changee - ft_strsplit.c \ #ft_split.c + ft_strtrim.c \ + ft_split.c \ ft_itoa.c \ ft_strmapi.c \ ft_putchar_fd.c \ - ft_putstr_fd.c \ #change const - ft_putendl_fd.c \ #change const + ft_putstr_fd.c \ + ft_putendl_fd.c \ ft_putnbr_fd.c \ - \ - ft_lstnew.c \ #fonction un peu changee + +ODIR = ./builds +OBJS = $(SRCS:%.c=$(ODIR)/%.o) + +SRCB = ft_lstnew.c \ #fonction un peu changee ft_lstadd.c \ #ft_lstadd_front.c lstsize \ #a creer lstlast \ #a creer lstadd_back \ #a creer - eart_lstdelone.c \ #fonction un peu changee + ft_lstdelone.c \ #fonction un peu changee ft_lstdel.c \ #lstclear et un peu changee ft_lstiter.c \ #fonction un peu changee ft_lstmap.c \ #fonction un peu changee \ - ft_memccpy.c \ ft_strcat.c \ ft_strcmp.c \ ft_strcpy.c \ ft_strncat.c \ ft_strncpy.c \ ft_strstr.c \ - ft_memalloc.c \ - ft_memdel.c \ - ft_putchar.c \ - ft_putendl.c \ - ft_putnbr.c \ - ft_putstr.c \ + ft_strjoinfree.c \ ft_strclr.c \ ft_strdel.c \ ft_strequ.c \ @@ -87,16 +80,25 @@ SRCS = ft_memset.c \ ft_strmap.c \ ft_strnequ.c \ ft_strnew.c \ + ft_memalloc.c \ + ft_memccpy.c \ + ft_memdel.c \ + ft_putchar.c \ + ft_putendl.c \ + ft_putnbr.c \ + ft_putnbrendl.c \ + ft_putnbrendl_fd.c \ + ft_putnbrbase.c \ + ft_putstr.c \ ft_any.c \ ft_atoibase.c \ ft_convertbase.c \ ft_foreach.c \ ft_issort.c \ ft_arraymap.c \ - ft_putnbrbase.c \ ft_strmultisplit.c -ODIR = ./builds -OBJ = $(SRCS:%.c=$(ODIR)/%.o) + +OBJB = $(SRCB:%.c=$(ODIR)/%.o) # - - - - - - - - - - - # @@ -105,91 +107,30 @@ OBJ = $(SRCS:%.c=$(ODIR)/%.o) all: $(ODIR) $(NAME) -# - when you write "make" in command line it will execute -# the first rule wrote in the Makefile, wich is "all" by -# convention because usually when you type "make" you -# expect it to build all -# - it first verify if ODIR is created and if not create -# it, then it calls NAME to create the library - $(ODIR): mkdir -p $(ODIR) -# create the folder where all the .o files will be stored - $(NAME): $(OBJS) $(DEPS) ar -rc $@ $(OBJS) @ranlib $@ -# - NAME will create the library libft.a -# - first it checks if any OBJ (files.o) have more recent -# date of modification than NAME (libft.a), and for each -# it will execute -# - $(OBJ) will expand in the list of files.o and each of -# them will call the rule "$(ODIR)/%.o:" below because it -# has its exact pattern -# - NAME will execute only for the .c that has been -# modified since last creation of NAME -# - ranlib is precedeed by a "@" to avoid being print -# - if DEP (libft.h) is modified the library is remade -# - $@ means name of the rule -# - $< means first argument right to ":" +bonnus: $(OBJB) + ar rc $(NAME) $(OBJB) + @ranlib $(NAME) $(ODIR)/%.o: %.c $(CC) $(CFLAGS) -c -o $@ $< -# - this rule depend of the list of files.c -# - if any of these files are more recent than the file -# builds/file.o equivalent then the rule will rebuild -# this .o file -# - the flag -o is there to put explicit name of the .o -# files since they must be written within a directory -# path (builds/) - clean: /bin/rm -rf $(ODIR) -# clean the objects file -# rm is use with its native path to avoid using an alias - fclean: clean /bin/rm -f $(NAME) -# clean the objects and executable files - re: fclean all -# remake the compilation - - -.PHONY: clean fclean re all -# all rules that are not names of a file are declared to -# be phony so that makefile knows he has to execute them -# even if a file name similarly exist and would prevent -# the rule to execute because it has not been changed - - - - - - - - - - - - - - - - - - - - - - +.PHONY: bonnus clean fclean re all diff --git a/a.out b/a.out index 9f2559e..a4b34af 100755 Binary files a/a.out and b/a.out differ diff --git a/srcs/a.out b/srcs/a.out deleted file mode 100755 index b2001cc..0000000 Binary files a/srcs/a.out and /dev/null differ diff --git a/srcs/add/ft_any.c b/srcs/add/ft_any.c new file mode 100644 index 0000000..349e3a9 --- /dev/null +++ b/srcs/add/ft_any.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_any.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/16 15:14:49 by hulamy #+# #+# */ +/* Updated: 2018/11/16 15:14:53 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_any(char **tab, int (*f)(char*)) +{ + int i; + + i = -1; + if (!tab) + return (0); + while (tab[++i]) + if (f(tab[i]) == 1) + return (1); + return (0); +} diff --git a/srcs/add/ft_arraymap.c b/srcs/add/ft_arraymap.c new file mode 100644 index 0000000..267510a --- /dev/null +++ b/srcs/add/ft_arraymap.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_arraymap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/srcs/add/ft_atoibase.c b/srcs/add/ft_atoibase.c new file mode 100644 index 0000000..fdb92d1 --- /dev/null +++ b/srcs/add/ft_atoibase.c @@ -0,0 +1,75 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoibase.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/srcs/add/ft_convertbase.c b/srcs/add/ft_convertbase.c new file mode 100644 index 0000000..7fe9bad --- /dev/null +++ b/srcs/add/ft_convertbase.c @@ -0,0 +1,86 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_convertbase.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/16 15:15:55 by hulamy #+# #+# */ +/* Updated: 2019/04/17 17:09:35 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int ft_malloc_size(int decimal, int length, int i) +{ + if (decimal <= 0) + i++; + while (decimal) + { + decimal /= length; + i++; + } + return (i); +} + +static char *ft_decimal_to_base(int decimal, char *base, char *res, int size) +{ + long nb; + int i; + + nb = decimal; + i = 0; + while (base[i]) + i++; + if (nb < 0) + nb = -nb; + while (--size >= 0) + { + res[size] = base[nb % i]; + nb /= i; + } + return (res); +} + +static int ft_base_to_decimal(char *nbr, char *base, int length, int i) +{ + long decimal; + int j; + + decimal = 0; + if (nbr[i] == '-') + i++; + while (nbr[i]) + { + j = 0; + while (nbr[i] != base[j] && base[j]) + j++; + decimal = (decimal * length) + j; + i++; + } + if (nbr[0] == '-') + decimal = -decimal; + return (decimal); +} + +char *ft_convertbase(char *nbr, char *base_from, char *base_to) +{ + int length; + int size; + int decimal; + char *res; + + res = 0; + length = 0; + while (base_from[length]) + length++; + decimal = ft_base_to_decimal(nbr, base_from, length, 0); + length = 0; + while (base_to[length]) + length++; + size = ft_malloc_size(decimal, length, 0); + res = (char *)malloc(sizeof(char) * (size + 1)); + res[size] = '\0'; + return (ft_decimal_to_base(decimal, base_to, res, size)); +} diff --git a/srcs/add/ft_foreach.c b/srcs/add/ft_foreach.c new file mode 100644 index 0000000..aaf7649 --- /dev/null +++ b/srcs/add/ft_foreach.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_foreach.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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++]); +} diff --git a/srcs/add/ft_issort.c b/srcs/add/ft_issort.c new file mode 100644 index 0000000..842195b --- /dev/null +++ b/srcs/add/ft_issort.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_issort.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/srcs/ft_memalloc.c b/srcs/add/ft_memalloc.c similarity index 100% rename from srcs/ft_memalloc.c rename to srcs/add/ft_memalloc.c diff --git a/srcs/add/ft_memccpy.c b/srcs/add/ft_memccpy.c new file mode 100644 index 0000000..a8bf1ee --- /dev/null +++ b/srcs/add/ft_memccpy.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memccpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 21:12:10 by hulamy #+# #+# */ +/* Updated: 2019/04/03 15:42:41 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); +} diff --git a/srcs/add/ft_memdel.c b/srcs/add/ft_memdel.c new file mode 100644 index 0000000..f057043 --- /dev/null +++ b/srcs/add/ft_memdel.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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; + } +} diff --git a/srcs/add/ft_putchar.c b/srcs/add/ft_putchar.c new file mode 100644 index 0000000..b0aa9cb --- /dev/null +++ b/srcs/add/ft_putchar.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/srcs/add/ft_putendl.c b/srcs/add/ft_putendl.c new file mode 100644 index 0000000..c1d9a6a --- /dev/null +++ b/srcs/add/ft_putendl.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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'); +} diff --git a/srcs/add/ft_putnbr.c b/srcs/add/ft_putnbr.c new file mode 100644 index 0000000..bb8e2d7 --- /dev/null +++ b/srcs/add/ft_putnbr.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/srcs/add/ft_putnbrbase.c b/srcs/add/ft_putnbrbase.c new file mode 100644 index 0000000..bf0d627 --- /dev/null +++ b/srcs/add/ft_putnbrbase.c @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbrbase.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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]); + } +} diff --git a/srcs/add/ft_putnbrendl.c b/srcs/add/ft_putnbrendl.c new file mode 100644 index 0000000..ddd05f6 --- /dev/null +++ b/srcs/add/ft_putnbrendl.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbrendl.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/srcs/add/ft_putnbrendl_fd.c b/srcs/add/ft_putnbrendl_fd.c new file mode 100644 index 0000000..266dc55 --- /dev/null +++ b/srcs/add/ft_putnbrendl_fd.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbrendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/srcs/add/ft_putstr.c b/srcs/add/ft_putstr.c new file mode 100644 index 0000000..78617eb --- /dev/null +++ b/srcs/add/ft_putstr.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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++]); +} diff --git a/srcs/add/ft_strcat.c b/srcs/add/ft_strcat.c new file mode 100644 index 0000000..d78543c --- /dev/null +++ b/srcs/add/ft_strcat.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/srcs/add/ft_strclr.c b/srcs/add/ft_strclr.c new file mode 100644 index 0000000..5e3952c --- /dev/null +++ b/srcs/add/ft_strclr.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strclr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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)); +} diff --git a/srcs/add/ft_strcmp.c b/srcs/add/ft_strcmp.c new file mode 100644 index 0000000..f6603c3 --- /dev/null +++ b/srcs/add/ft_strcmp.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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]); +} diff --git a/srcs/add/ft_strcpy.c b/srcs/add/ft_strcpy.c new file mode 100644 index 0000000..7d2a45b --- /dev/null +++ b/srcs/add/ft_strcpy.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/srcs/add/ft_strdel.c b/srcs/add/ft_strdel.c new file mode 100644 index 0000000..82cbc2e --- /dev/null +++ b/srcs/add/ft_strdel.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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; + } +} diff --git a/srcs/add/ft_strequ.c b/srcs/add/ft_strequ.c new file mode 100644 index 0000000..fa4d4e4 --- /dev/null +++ b/srcs/add/ft_strequ.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strequ.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/srcs/add/ft_striter.c b/srcs/add/ft_striter.c new file mode 100644 index 0000000..9d3b21f --- /dev/null +++ b/srcs/add/ft_striter.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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++); +} diff --git a/srcs/add/ft_striteri.c b/srcs/add/ft_striteri.c new file mode 100644 index 0000000..60fd7f6 --- /dev/null +++ b/srcs/add/ft_striteri.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striteri.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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++); +} diff --git a/srcs/add/ft_strjoinfree.c b/srcs/add/ft_strjoinfree.c new file mode 100644 index 0000000..8e0247b --- /dev/null +++ b/srcs/add/ft_strjoinfree.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoinfree.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 freeing them +*/ + +#include "libft.h" + +char *ft_strjoinfree(char *s1, char *s2) +{ + char *str; + + if (!(str = ft_strjoin(s1, s2))) + return (NULL); + free(s1); + return (str); +} diff --git a/srcs/add/ft_strmap.c b/srcs/add/ft_strmap.c new file mode 100644 index 0000000..ef82f97 --- /dev/null +++ b/srcs/add/ft_strmap.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/srcs/add/ft_strmultisplit.c b/srcs/add/ft_strmultisplit.c new file mode 100644 index 0000000..ed2ed98 --- /dev/null +++ b/srcs/add/ft_strmultisplit.c @@ -0,0 +1,83 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmultisplit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/srcs/add/ft_strncat.c b/srcs/add/ft_strncat.c new file mode 100644 index 0000000..cf52aae --- /dev/null +++ b/srcs/add/ft_strncat.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/srcs/add/ft_strncpy.c b/srcs/add/ft_strncpy.c new file mode 100644 index 0000000..91ea1b3 --- /dev/null +++ b/srcs/add/ft_strncpy.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/srcs/add/ft_strnequ.c b/srcs/add/ft_strnequ.c new file mode 100644 index 0000000..668f3fa --- /dev/null +++ b/srcs/add/ft_strnequ.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnequ.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/srcs/add/ft_strnew.c b/srcs/add/ft_strnew.c new file mode 100644 index 0000000..c7855ad --- /dev/null +++ b/srcs/add/ft_strnew.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 21:19:08 by hulamy #+# #+# */ +/* Updated: 2019/03/25 15:25:38 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); +} diff --git a/srcs/add/ft_strstr.c b/srcs/add/ft_strstr.c new file mode 100644 index 0000000..c7a3535 --- /dev/null +++ b/srcs/add/ft_strstr.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/srcs/bonus/ft_lstadd.c b/srcs/bonus/ft_lstadd.c new file mode 100644 index 0000000..d0bed31 --- /dev/null +++ b/srcs/bonus/ft_lstadd.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 21:10:33 by hulamy #+# #+# */ +/* Updated: 2018/11/16 13:58:54 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd(t_list **alst, t_list *new) +{ + new->next = *alst; + *alst = new; +} diff --git a/srcs/bonus/ft_lstdel.c b/srcs/bonus/ft_lstdel.c new file mode 100644 index 0000000..bbe792d --- /dev/null +++ b/srcs/bonus/ft_lstdel.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 21:10:49 by hulamy #+# #+# */ +/* Updated: 2018/11/16 13:59:10 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstdel(t_list **alst, void (*del)(void *, size_t)) +{ + if ((*alst)->next) + ft_lstdel(&(*alst)->next, del); + ft_lstdelone(alst, del); +} diff --git a/srcs/bonus/ft_lstdelone.c b/srcs/bonus/ft_lstdelone.c new file mode 100644 index 0000000..e2b85c2 --- /dev/null +++ b/srcs/bonus/ft_lstdelone.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 21:10:59 by hulamy #+# #+# */ +/* Updated: 2018/11/16 13:59:22 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstdelone(t_list **alst, void (*del)(void *, size_t)) +{ + del((*alst)->content, (*alst)->content_size); + free(*alst); + *alst = NULL; +} diff --git a/srcs/bonus/ft_lstiter.c b/srcs/bonus/ft_lstiter.c new file mode 100644 index 0000000..0f791be --- /dev/null +++ b/srcs/bonus/ft_lstiter.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstiter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 21:11:14 by hulamy #+# #+# */ +/* Updated: 2018/11/16 14:01:10 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstiter(t_list *lst, void (*f)(t_list *elem)) +{ + if (!lst) + return ; + if (lst->next) + ft_lstiter(lst->next, f); + f(lst); +} diff --git a/srcs/bonus/ft_lstmap.c b/srcs/bonus/ft_lstmap.c new file mode 100644 index 0000000..6b9a2c8 --- /dev/null +++ b/srcs/bonus/ft_lstmap.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 21:11:20 by hulamy #+# #+# */ +/* Updated: 2018/11/16 14:01:23 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)) +{ + t_list *new; + t_list *tmp; + + if (!lst) + return (NULL); + tmp = f(lst); + new = tmp; + while (lst->next) + { + lst = lst->next; + if (!(tmp->next = f(lst))) + { + free(tmp->next); + return (NULL); + } + tmp = tmp->next; + } + return (new); +} diff --git a/srcs/bonus/ft_lstnew.c b/srcs/bonus/ft_lstnew.c new file mode 100644 index 0000000..6d00337 --- /dev/null +++ b/srcs/bonus/ft_lstnew.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 21:11:42 by hulamy #+# #+# */ +/* Updated: 2018/11/16 14:01:36 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstnew(void const *content, size_t content_size) +{ + t_list *lst; + + if (!(lst = (t_list *)malloc(sizeof(*lst)))) + return (NULL); + if (!content) + { + lst->content = NULL; + lst->content_size = 0; + } + else + { + if (!(lst->content = malloc(content_size))) + return (NULL); + ft_memcpy(lst->content, content, content_size); + lst->content_size = content_size; + } + lst->next = NULL; + return (lst); +} diff --git a/srcs/part1/ft_atoi.c b/srcs/part1/ft_atoi.c new file mode 100644 index 0000000..5c10b59 --- /dev/null +++ b/srcs/part1/ft_atoi.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 21:09:04 by hulamy #+# #+# */ +/* Updated: 2018/11/14 21:38:12 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] == 32) || (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); +} diff --git a/srcs/part1/ft_bzero.c b/srcs/part1/ft_bzero.c new file mode 100644 index 0000000..ed2c7eb --- /dev/null +++ b/srcs/part1/ft_bzero.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_bzero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 21:09:19 by hulamy #+# #+# */ +/* Updated: 2018/11/15 21:43:05 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'; + } +} diff --git a/srcs/ft_calloc.c b/srcs/part1/ft_calloc.c similarity index 85% rename from srcs/ft_calloc.c rename to srcs/part1/ft_calloc.c index 19ad171..d6a9d4b 100644 --- a/srcs/ft_calloc.c +++ b/srcs/part1/ft_calloc.c @@ -6,7 +6,7 @@ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/19 14:56:09 by hulamy #+# #+# */ -/* Updated: 2019/11/19 15:21:25 by hulamy ### ########.fr */ +/* Updated: 2019/11/19 19:27:38 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,5 +24,10 @@ void *ft_calloc(size_t count, size_t size) { - return (ft_memalloc(count * size)); + void *tmp; + + if (!count || !size || !(tmp = malloc(count * size))) + return (NULL); + ft_bzero(tmp, count * size); + return (tmp); } diff --git a/srcs/part1/ft_isalnum.c b/srcs/part1/ft_isalnum.c new file mode 100644 index 0000000..21bfad0 --- /dev/null +++ b/srcs/part1/ft_isalnum.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 21:09:33 by hulamy #+# #+# */ +/* Updated: 2018/11/14 21:09:37 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isalnum(int c) +{ + return (ft_isalpha(c) || ft_isdigit(c)); +} diff --git a/srcs/part1/ft_isalpha.c b/srcs/part1/ft_isalpha.c new file mode 100644 index 0000000..b754b0b --- /dev/null +++ b/srcs/part1/ft_isalpha.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 21:09:44 by hulamy #+# #+# */ +/* Updated: 2018/11/14 21:09:46 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isalpha(int c) +{ + return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')); +} diff --git a/srcs/part1/ft_isascii.c b/srcs/part1/ft_isascii.c new file mode 100644 index 0000000..054f504 --- /dev/null +++ b/srcs/part1/ft_isascii.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 21:09:53 by hulamy #+# #+# */ +/* Updated: 2018/11/14 21:09:55 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isascii(int c) +{ + return (c >= 0 && c <= 127); +} diff --git a/srcs/part1/ft_isdigit.c b/srcs/part1/ft_isdigit.c new file mode 100644 index 0000000..1dd5840 --- /dev/null +++ b/srcs/part1/ft_isdigit.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 21:10:01 by hulamy #+# #+# */ +/* Updated: 2018/11/14 21:10:05 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isdigit(int c) +{ + return (c >= '0' && c <= '9'); +} diff --git a/srcs/part1/ft_isprint.c b/srcs/part1/ft_isprint.c new file mode 100644 index 0000000..f45cbaf --- /dev/null +++ b/srcs/part1/ft_isprint.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 21:10:19 by hulamy #+# #+# */ +/* Updated: 2018/11/14 21:10:20 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isprint(int c) +{ + return (c >= 32 && c < 127); +} diff --git a/srcs/part1/ft_memchr.c b/srcs/part1/ft_memchr.c new file mode 100644 index 0000000..f6254ed --- /dev/null +++ b/srcs/part1/ft_memchr.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 21:12:32 by hulamy #+# #+# */ +/* Updated: 2019/04/03 15:43:14 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* +** locate character in string and return its position +*/ + +#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); +} diff --git a/srcs/part1/ft_memcmp.c b/srcs/part1/ft_memcmp.c new file mode 100644 index 0000000..d881cba --- /dev/null +++ b/srcs/part1/ft_memcmp.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 21:13:04 by hulamy #+# #+# */ +/* Updated: 2019/04/03 15:43:41 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]); +} diff --git a/srcs/part1/ft_memcpy.c b/srcs/part1/ft_memcpy.c new file mode 100644 index 0000000..d21a57c --- /dev/null +++ b/srcs/part1/ft_memcpy.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 21:13:17 by hulamy #+# #+# */ +/* Updated: 2019/04/03 15:43:56 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* +** copy n characters from src to dst and return dst +*/ + +#include "libft.h" + +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); +} diff --git a/srcs/part1/ft_memmove.c b/srcs/part1/ft_memmove.c new file mode 100644 index 0000000..51af09b --- /dev/null +++ b/srcs/part1/ft_memmove.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memmove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 21:13:34 by hulamy #+# #+# */ +/* Updated: 2019/04/03 15:44:28 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* +** copy n characters from src to dst in a non destructive way and return dst +*/ + +#include "libft.h" + +void *ft_memmove(void *dst, const void *src, size_t len) +{ + int i; + char *source; + char *dest; + + i = -1; + source = (char *)src; + dest = (char *)dst; + if (source < dest) + while ((int)(--len) >= 0) + dest[len] = source[len]; + else + while (++i < (int)len) + dest[i] = source[i]; + return (dst); +} diff --git a/srcs/part1/ft_memset.c b/srcs/part1/ft_memset.c new file mode 100644 index 0000000..b3df393 --- /dev/null +++ b/srcs/part1/ft_memset.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 21:13:41 by hulamy #+# #+# */ +/* Updated: 2019/04/03 15:44:44 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); +} diff --git a/srcs/part1/ft_strchr.c b/srcs/part1/ft_strchr.c new file mode 100644 index 0000000..095c87e --- /dev/null +++ b/srcs/part1/ft_strchr.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 21:15:48 by hulamy #+# #+# */ +/* Updated: 2019/04/16 17:28:46 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); +} diff --git a/srcs/part1/ft_strdup.c b/srcs/part1/ft_strdup.c new file mode 100644 index 0000000..6fc8594 --- /dev/null +++ b/srcs/part1/ft_strdup.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 21:16:32 by hulamy #+# #+# */ +/* Updated: 2019/03/25 15:20:22 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); +} diff --git a/srcs/ft_strlcat.c b/srcs/part1/ft_strlcat.c similarity index 95% rename from srcs/ft_strlcat.c rename to srcs/part1/ft_strlcat.c index b381f99..7499b22 100644 --- a/srcs/ft_strlcat.c +++ b/srcs/part1/ft_strlcat.c @@ -6,7 +6,7 @@ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/19 15:09:51 by hulamy #+# #+# */ -/* Updated: 2019/11/19 15:09:54 by hulamy ### ########.fr */ +/* Updated: 2019/11/19 18:18:30 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,9 +17,7 @@ */ /* -** #include -** #include -** #include +** #include ** ** size_t ft_strlcat(char *dest, const char *src, size_t size); ** diff --git a/srcs/ft_strlcpy.c b/srcs/part1/ft_strlcpy.c similarity index 92% rename from srcs/ft_strlcpy.c rename to srcs/part1/ft_strlcpy.c index 289c870..d6284a2 100644 --- a/srcs/ft_strlcpy.c +++ b/srcs/part1/ft_strlcpy.c @@ -6,7 +6,7 @@ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/19 15:10:04 by hulamy #+# #+# */ -/* Updated: 2019/11/19 15:10:07 by hulamy ### ########.fr */ +/* Updated: 2019/11/19 18:18:54 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,10 +19,7 @@ */ /* -** #include -** #include -** #include -** #include +** #include ** ** size_t ft_strlcpy(char *dest, const char *src, size_t size); ** diff --git a/srcs/part1/ft_strlen.c b/srcs/part1/ft_strlen.c new file mode 100644 index 0000000..59cc818 --- /dev/null +++ b/srcs/part1/ft_strlen.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 21:17:32 by hulamy #+# #+# */ +/* Updated: 2019/03/25 15:23:00 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); +} diff --git a/srcs/part1/ft_strncmp.c b/srcs/part1/ft_strncmp.c new file mode 100644 index 0000000..5d51c2d --- /dev/null +++ b/srcs/part1/ft_strncmp.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 21:18:34 by hulamy #+# #+# */ +/* Updated: 2019/03/25 15:24:35 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); +} diff --git a/srcs/part1/ft_strnstr.c b/srcs/part1/ft_strnstr.c new file mode 100644 index 0000000..5dbb8c0 --- /dev/null +++ b/srcs/part1/ft_strnstr.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 21:19:16 by hulamy #+# #+# */ +/* Updated: 2019/03/25 15:25:57 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); +} diff --git a/srcs/part1/ft_strrchr.c b/srcs/part1/ft_strrchr.c new file mode 100644 index 0000000..012535a --- /dev/null +++ b/srcs/part1/ft_strrchr.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/11/19 19:35:39 by hulamy #+# #+# */ +/* Updated: 2019/11/19 19:35:45 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); +} diff --git a/srcs/part1/ft_tolower.c b/srcs/part1/ft_tolower.c new file mode 100644 index 0000000..e8f2b0f --- /dev/null +++ b/srcs/part1/ft_tolower.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_tolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 21:20:19 by hulamy #+# #+# */ +/* Updated: 2018/11/14 21:20:20 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_tolower(int c) +{ + if (c >= 'A' && c <= 'Z') + return (c + 32); + return (c); +} diff --git a/srcs/part1/ft_toupper.c b/srcs/part1/ft_toupper.c new file mode 100644 index 0000000..e0e0271 --- /dev/null +++ b/srcs/part1/ft_toupper.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_toupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 21:20:26 by hulamy #+# #+# */ +/* Updated: 2018/11/14 21:20:27 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_toupper(int c) +{ + if (c >= 'a' && c <= 'z') + return (c - 32); + return (c); +} diff --git a/srcs/part2/ft_itoa.c b/srcs/part2/ft_itoa.c new file mode 100644 index 0000000..65fbfa7 --- /dev/null +++ b/srcs/part2/ft_itoa.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 21:10:25 by hulamy #+# #+# */ +/* Updated: 2018/11/14 21:36:38 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_itoa(int n) +{ + char *str; + int len; + long int nbis; + + len = (n < 0) ? 2 : 1; + nbis = n; + while (nbis /= 10) + len++; + nbis = n; + nbis *= (nbis < 0) ? -1 : 1; + if (!(str = ft_strnew(len))) + return (NULL); + str[--len] = nbis % 10 + '0'; + while (nbis /= 10) + str[--len] = nbis % 10 + '0'; + if (n < 0) + str[0] = '-'; + return (str); +} diff --git a/srcs/part2/ft_putchar_fd.c b/srcs/part2/ft_putchar_fd.c new file mode 100644 index 0000000..e12e154 --- /dev/null +++ b/srcs/part2/ft_putchar_fd.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 21:14:14 by hulamy #+# #+# */ +/* Updated: 2018/11/14 21:14:15 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putchar_fd(char c, int fd) +{ + write(fd, &c, 1); +} diff --git a/srcs/part2/ft_putendl_fd.c b/srcs/part2/ft_putendl_fd.c new file mode 100644 index 0000000..2f82783 --- /dev/null +++ b/srcs/part2/ft_putendl_fd.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/11/19 18:38:05 by hulamy #+# #+# */ +/* Updated: 2019/11/19 18:39:05 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); +} diff --git a/srcs/part2/ft_putnbr_fd.c b/srcs/part2/ft_putnbr_fd.c new file mode 100644 index 0000000..66a4a9d --- /dev/null +++ b/srcs/part2/ft_putnbr_fd.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 21:15:09 by hulamy #+# #+# */ +/* Updated: 2018/11/14 21:15:10 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); +} diff --git a/srcs/part2/ft_putstr_fd.c b/srcs/part2/ft_putstr_fd.c new file mode 100644 index 0000000..42719e5 --- /dev/null +++ b/srcs/part2/ft_putstr_fd.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/11/19 18:35:48 by hulamy #+# #+# */ +/* Updated: 2019/11/19 18:37:07 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); +} diff --git a/srcs/part2/ft_split.c b/srcs/part2/ft_split.c new file mode 100644 index 0000000..caa1c5d --- /dev/null +++ b/srcs/part2/ft_split.c @@ -0,0 +1,62 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/11/19 18:22:41 by hulamy #+# #+# */ +/* Updated: 2019/11/19 18:28:01 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* +** return an array of string with each word found in str, with c as separator +*/ + +#include "libft.h" + +static int ft_count_word(char const *s, char c) +{ + int i; + int len; + + i = -1; + len = 0; + while (s[++i]) + if (s[i] != c) + { + len++; + while (s[i] && s[i] != c) + i++; + } + return (len); +} + +char **ft_strsplit(char const *s, char c) +{ + char **array; + int i; + int j; + int len; + + i = -1; + j = 0; + if (!s || !c) + return (0); + if (!(array = (char **)malloc(sizeof(char *) * (ft_count_word(s, c) + 1)))) + return (NULL); + while (s[++i]) + { + if (s[i] != c) + { + len = 0; + while (s[i + len] && s[i + len] != c) + len++; + array[j++] = ft_strsub(s, i, len); + i = i + len - 1; + } + } + array[j] = 0; + return (array); +} diff --git a/srcs/part2/ft_strjoin.c b/srcs/part2/ft_strjoin.c new file mode 100644 index 0000000..48f95a1 --- /dev/null +++ b/srcs/part2/ft_strjoin.c @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 21:17:12 by hulamy #+# #+# */ +/* Updated: 2019/03/25 15:31:03 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* +** create a new string by concatenating the two strings s1 and s2 +*/ + +#include "libft.h" + +static char *ft_doit(char const *s1, char const *s2, char *dest) +{ + int j; + int i; + + j = 0; + i = 0; + while (s1[j] != '\0') + { + dest[i] = s1[j]; + i++; + j++; + } + j = 0; + while (s2[j] != '\0') + { + dest[i] = s2[j]; + i++; + j++; + } + dest[i] = '\0'; + return (dest); +} + +char *ft_strjoin(char const *s1, char const *s2) +{ + char *str; + + if (!s1 || !s2) + return (NULL); + if (!(str = (char *)malloc(sizeof(char) * + (ft_strlen(s1) + ft_strlen(s2) + 1)))) + return (NULL); + str = ft_doit(s1, s2, str); + return (str); +} diff --git a/srcs/part2/ft_strmapi.c b/srcs/part2/ft_strmapi.c new file mode 100644 index 0000000..d9fb527 --- /dev/null +++ b/srcs/part2/ft_strmapi.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 21:18:03 by hulamy #+# #+# */ +/* Updated: 2019/03/25 15:28:21 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* +** create a new array with the result of function f +** on every element of s by index i +*/ + +#include "libft.h" + +char *ft_strmapi(char const *s, char (*f)(unsigned int, 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(i, s[i]); + return (str); +} diff --git a/srcs/ft_strtrim.c b/srcs/part2/ft_strtrim.c similarity index 100% rename from srcs/ft_strtrim.c rename to srcs/part2/ft_strtrim.c diff --git a/srcs/ft_substr.c b/srcs/part2/ft_substr.c similarity index 100% rename from srcs/ft_substr.c rename to srcs/part2/ft_substr.c diff --git a/test/.Makefile.swp b/test/.Makefile.swp new file mode 100644 index 0000000..7876ce8 Binary files /dev/null and b/test/.Makefile.swp differ diff --git a/test/Makefile b/test/Makefile index e920490..82fb285 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,22 +1,22 @@ #-------------# # VARIABLES # #-------------# -NAME = test +NAME = libtest.a CC = gcc VPATH = srcs -# HEADERS # + IDIR = includes _DEPS = test.h DEPS = $(_DEPS:%.h=$(IDIR)/%.h) -# LIBRAIRIES # -LDIR = ./ + +LDIR = . _LIBS = libtest.a LIBS = $(_LIBS:lib%.a=%) -# FUNCTIONS et OBJETS # + SRCS = main.c to_uppercase.c putchar.c transform.c ODIR = ./builds OBJS = $(SRCS:%.c=$(ODIR)/%.o) -# OPTIONS de GCC # + CFLAGS = -I$(IDIR) LFLAGS = -L$(LDIR) -l$(LIBS) diff --git a/test/builds/main.o b/test/builds/main.o index fa0b8d0..78735ef 100644 Binary files a/test/builds/main.o and b/test/builds/main.o differ diff --git a/test/builds/putchar.o b/test/builds/putchar.o index 27a2a24..824021b 100644 Binary files a/test/builds/putchar.o and b/test/builds/putchar.o differ diff --git a/test/builds/to_uppercase.o b/test/builds/to_uppercase.o index eb31766..e8f61a5 100644 Binary files a/test/builds/to_uppercase.o and b/test/builds/to_uppercase.o differ diff --git a/test/builds/transform.o b/test/builds/transform.o index d35bfb8..3c17dae 100644 Binary files a/test/builds/transform.o and b/test/builds/transform.o differ diff --git a/test/libdeux.a b/test/libdeux.a new file mode 100644 index 0000000..961c189 Binary files /dev/null and b/test/libdeux.a differ diff --git a/test/libtest.a b/test/libtest.a new file mode 100644 index 0000000..7318fbd Binary files /dev/null and b/test/libtest.a differ diff --git a/test/libtrois.a b/test/libtrois.a new file mode 100644 index 0000000..ab70bce Binary files /dev/null and b/test/libtrois.a differ diff --git a/test/test b/test/test old mode 100644 new mode 100755 index c7262cb..8b65d74 Binary files a/test/test and b/test/test differ