mise a jour de plusieurs fonctions

This commit is contained in:
Hugo LAMY
2019-11-19 18:16:42 +01:00
parent c612c9bae7
commit 966241a6df
11 changed files with 333 additions and 7 deletions

Binary file not shown.

View File

@@ -38,14 +38,14 @@ SRCS = ft_memset.c \
ft_strchr.c \
ft_strrchr.c \
ft_strncmp.c \
strlcpy \ #a creer
ft_strlcpy.c \
ft_strlcat.c \
ft_strnstr.c \
ft_atoi.c \
ft_strdup.c \
calloc \ #a creer
ft_calloc.c \
\
ft_substr.c \ #ft_substr.C
ft_substr.c \
ft_strjoin.c \
ft_strtrim.c \ #fonction un peu changee
ft_strsplit.c \ #ft_split.c

View File

@@ -6,14 +6,85 @@
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/14 21:17:22 by hulamy #+# #+# */
/* Updated: 2019/03/25 15:22:42 by hulamy ### ########.fr */
/* Updated: 2019/11/19 14:40:09 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** append src to sized dest and return size of final dest
** append src to sized dest and return size of final dest
**
** TESTS :
*/
/*
** #include <string.h>
** #include <stdio.h>
** #include <stdlib.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)

BIN
a.out

Binary file not shown.

BIN
srcs/a.out Executable file

Binary file not shown.

28
srcs/ft_calloc.c Normal file
View File

@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/19 14:56:09 by hulamy #+# #+# */
/* Updated: 2019/11/19 15:21:25 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** 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));
**
** allocate count * size byte of memory and
** return a pointer to the allocated memory
*/
#include "libft.h"
void *ft_calloc(size_t count, size_t size)
{
return (ft_memalloc(count * size));
}

27
srcs/ft_memalloc.c Normal file
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);
}

109
srcs/ft_strlcat.c Normal file
View File

@@ -0,0 +1,109 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/19 15:09:51 by hulamy #+# #+# */
/* Updated: 2019/11/19 15:09:54 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** append src to sized dest and return size of final dest
**
** TESTS :
*/
/*
** #include <string.h>
** #include <stdio.h>
** #include <stdlib.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

@@ -5,8 +5,8 @@
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/19 13:03:47 by hulamy #+# #+# */
/* Updated: 2019/11/19 14:11:39 by hulamy ### ########.fr */
/* Created: 2019/11/19 15:10:04 by hulamy #+# #+# */
/* Updated: 2019/11/19 15:10:07 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */

59
srcs/ft_strtrim.c Normal file
View File

@@ -0,0 +1,59 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/19 17:26:06 by hulamy #+# #+# */
/* Updated: 2019/11/19 18:15:32 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** create a copy of s without the firsts and lasts empty characters
*/
/*
** #include <libc.h>
**
** char *ft_strtrim(char const *s1, char const *set);
**
** int main(int ac, char **av)
** {
** if (ac == 3)
** printf("%s\n",ft_strtrim(av[1], av[2]));
**
** return (0);
** }
*/
#include "libft.h"
char *ft_strtrim(char const *s1, char const *set)
{
int len;
char *str;
(void)set;
if (!s1)
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_strsub(s1, 0, len)))
return (NULL);
return (str);
}

32
srcs/ft_substr.c Normal file
View File

@@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/19 17:20:51 by hulamy #+# #+# */
/* Updated: 2019/11/19 17:24:10 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** create a copy of a portion of s, begining at start and of length len
*/
#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 (!(str = ft_strnew(len)))
return (NULL);
i = 0;
while (i < len)
str[i++] = s[start++];
return (str);
}