makefile a la norme
This commit is contained in:
55
srcs/part2/ft_itoa.c
Normal file
55
srcs/part2/ft_itoa.c
Normal file
@@ -0,0 +1,55 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_itoa.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/11/25 13:59:01 by hulamy #+# #+# */
|
||||
/* Updated: 2019/11/25 14:21:01 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
/*
|
||||
** take an integer and give a string
|
||||
*/
|
||||
|
||||
/*
|
||||
** #include <libc.h>
|
||||
**
|
||||
** char *ft_itoa(int n)
|
||||
**
|
||||
** int main(int ac, char **av)
|
||||
** {
|
||||
** if (ac == 2)
|
||||
** {
|
||||
** printf("%s\n",ft_itoa(atoi(av[1])));
|
||||
** }
|
||||
** return 0;
|
||||
** }
|
||||
*/
|
||||
|
||||
#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 = (char *)malloc(sizeof(char) * (len + 1))))
|
||||
return (NULL);
|
||||
str[len] = '\0';
|
||||
str[--len] = nbis % 10 + '0';
|
||||
while (nbis /= 10)
|
||||
str[--len] = nbis % 10 + '0';
|
||||
if (n < 0)
|
||||
str[0] = '-';
|
||||
return (str);
|
||||
}
|
||||
18
srcs/part2/ft_putchar_fd.c
Normal file
18
srcs/part2/ft_putchar_fd.c
Normal 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);
|
||||
}
|
||||
23
srcs/part2/ft_putendl_fd.c
Normal file
23
srcs/part2/ft_putendl_fd.c
Normal 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);
|
||||
}
|
||||
28
srcs/part2/ft_putnbr_fd.c
Normal file
28
srcs/part2/ft_putnbr_fd.c
Normal 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);
|
||||
}
|
||||
23
srcs/part2/ft_putstr_fd.c
Normal file
23
srcs/part2/ft_putstr_fd.c
Normal 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);
|
||||
}
|
||||
128
srcs/part2/ft_split.c
Normal file
128
srcs/part2/ft_split.c
Normal file
@@ -0,0 +1,128 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_split.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/11/25 14:00:13 by hulamy #+# #+# */
|
||||
/* Updated: 2019/12/02 17:48:50 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
/*
|
||||
** return an array of string with each word found in str, with c as separator
|
||||
*/
|
||||
|
||||
/*
|
||||
** #include <libc.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);
|
||||
**
|
||||
** int main(int ac, char **av)
|
||||
** //int main(void)
|
||||
** {
|
||||
** char **str;
|
||||
** int i;
|
||||
**
|
||||
** char *s;
|
||||
** char c;
|
||||
**
|
||||
** if (ac == 3)
|
||||
** {
|
||||
** i = 0;
|
||||
** s = av[1];
|
||||
** c = av[2][0];
|
||||
** str = ft_split(s, c);
|
||||
** while (str[i])
|
||||
** printf("%s\n", str[i++]);
|
||||
** }
|
||||
** return (0);
|
||||
** }
|
||||
*/
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
static int ft_count_word(char const *s, char c)
|
||||
{
|
||||
int i;
|
||||
int words;
|
||||
|
||||
i = 0;
|
||||
words = 0;
|
||||
while (s[i])
|
||||
{
|
||||
if (s[i] != c)
|
||||
{
|
||||
words++;
|
||||
while (s[i] && s[i] != c)
|
||||
i++;
|
||||
}
|
||||
else
|
||||
i++;
|
||||
}
|
||||
return (words);
|
||||
}
|
||||
|
||||
void *ft_free(char **array)
|
||||
{
|
||||
while (array)
|
||||
free(array++);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
char **ft_split(char const *s, char c)
|
||||
{
|
||||
char **array;
|
||||
int i;
|
||||
int j;
|
||||
int len;
|
||||
|
||||
i = ft_count_word(s, c);
|
||||
if (!s || !c || !(array = malloc(sizeof(char *) * (i + 1))))
|
||||
return (NULL);
|
||||
array[i] = 0;
|
||||
i = -1;
|
||||
j = 0;
|
||||
while (s[++i])
|
||||
{
|
||||
if (s[i] != c)
|
||||
{
|
||||
len = 0;
|
||||
while (s[i + len] && s[i + len] != c)
|
||||
len++;
|
||||
if (!(array[j++] = ft_substr(s, i, len)))
|
||||
return (ft_free(array));
|
||||
i = i + len - 1;
|
||||
}
|
||||
}
|
||||
return (array);
|
||||
}
|
||||
54
srcs/part2/ft_strjoin.c
Normal file
54
srcs/part2/ft_strjoin.c
Normal file
@@ -0,0 +1,54 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strjoin.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/11/25 14:01:26 by hulamy #+# #+# */
|
||||
/* Updated: 2019/11/25 14:01:34 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);
|
||||
}
|
||||
72
srcs/part2/ft_strmapi.c
Normal file
72
srcs/part2/ft_strmapi.c
Normal file
@@ -0,0 +1,72 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strmapi.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/11/25 14:01:40 by hulamy #+# #+# */
|
||||
/* Updated: 2019/11/25 14:19:43 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 -= 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);
|
||||
** str = av[1];
|
||||
** printf("%s\n",str);
|
||||
** str = ft_strmapi(str, touppercase);
|
||||
** 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)
|
||||
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);
|
||||
}
|
||||
90
srcs/part2/ft_strtrim.c
Normal file
90
srcs/part2/ft_strtrim.c
Normal file
@@ -0,0 +1,90 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strtrim.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/11/25 14:01:49 by hulamy #+# #+# */
|
||||
/* Updated: 2019/11/25 14:20:41 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
/*
|
||||
** create a copy of s without the firsts and lasts empty 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)
|
||||
** {
|
||||
** 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;
|
||||
|
||||
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_substr(s1, 0, len)))
|
||||
return (NULL);
|
||||
return (str);
|
||||
}
|
||||
93
srcs/part2/ft_substr.c
Normal file
93
srcs/part2/ft_substr.c
Normal file
@@ -0,0 +1,93 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_substr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/12/01 16:00:10 by hulamy #+# #+# */
|
||||
/* Updated: 2019/12/01 16:00:12 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
/*
|
||||
** create a copy of a portion of s, begining at start and of length len
|
||||
*/
|
||||
|
||||
/*
|
||||
** #include <libc.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;
|
||||
**
|
||||
** str = "";
|
||||
** size_t size = 0;
|
||||
** char *ret = ft_substr(str, 5, size);
|
||||
** if (!ft_strncmp(ret, str + 5, size))
|
||||
** printf("gloups\n");
|
||||
** 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);
|
||||
}
|
||||
Reference in New Issue
Block a user