├── README.md
├── ft_putchar_fd.c
├── ft_putstr_fd.c
├── ft_tolower.c
├── ft_toupper.c
├── ft_isalnum.c
├── ft_isascii.c
├── ft_isdigit.c
├── ft_isprint.c
├── ft_memccpy.c
├── ft_putendl_fd.c
├── ft_lstlast.c
├── ft_isalpha.c
├── ft_lstnew.c
├── ft_lstiter.c
├── ft_strlen.c
├── ft_lstdelone.c
├── ft_putnbr_fd.c
├── ft_calloc.c
├── ft_lstadd_front.c
├── ft_strdup.c
├── ft_striteri.c
├── ft_lstclear.c
├── ft_strrchr.c
├── ft_bzero.c
├── ft_memset.c
├── ft_strjoin.c
├── ft_lstsize.c
├── ft_memchr.c
├── ft_substr.c
├── ft_itoa.c
├── ft_strnstr.c
├── ft_strlcpy.c
├── ft_memcmp.c
├── ft_lstmap.c
├── ft_strncmp.c
├── ft_atoi.c
├── ft_strtrim.c
├── ft_memmove.c
├── ft_strchr.c
├── ft_strmapi.c
├── ft_split.c
├── libft.h
├── ft_lstadd_back.c
├── ft_memcpy.c
├── ft_strlcat.c
└── Makefile
/README.md:
--------------------------------------------------------------------------------
1 | # libft
2 |
3 |
4 | SPLIT
5 |
6 | [](https://youtu.be/rn0B-ALufrw)
7 |
8 |
9 |
10 | BONUS explanation
11 | [](https://youtu.be/mkZYMKwKkvI)
12 |
13 | As a test i used this.
14 | https://github.com/FranFrau/Supreme-Tester-Libft
15 |
16 | #enjoy
17 |
--------------------------------------------------------------------------------
/ft_putchar_fd.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_putchar_fd.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/20 12:07:30 by Oceano #+# #+# */
9 | /* Updated: 2023/01/24 19:57:42 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | void ft_putchar_fd(char c, int fd)
16 | {
17 | if (c >= 0 && c <= 127)
18 | write(fd, &c, 1);
19 | }
20 |
--------------------------------------------------------------------------------
/ft_putstr_fd.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_putstr_fd.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/20 12:12:01 by Oceano #+# #+# */
9 | /* Updated: 2023/01/20 12:12:08 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | void ft_putstr_fd(char *s, int fd)
16 | {
17 | if (s)
18 | {
19 | while (*s)
20 | write(fd, s++, 1);
21 | }
22 | }
23 |
24 | /*
25 | int main()
26 | {
27 | ft_putstr_fd("h", 1);
28 | }
29 | */
30 |
--------------------------------------------------------------------------------
/ft_tolower.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_tolower.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/18 16:38:42 by Oceano #+# #+# */
9 | /* Updated: 2023/01/24 14:46:08 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | //#include
14 |
15 | #include "libft.h"
16 |
17 | int ft_tolower(int c)
18 | {
19 | if (c >= 65 && c <= 90)
20 | c += 32;
21 | return (c);
22 | }
23 |
24 | /*
25 | int main(void)
26 | {
27 | for (int i = 32; i <= 122; ++i)
28 | printf("%c\n", ft_lower(i));
29 | }
30 | */
31 |
--------------------------------------------------------------------------------
/ft_toupper.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_toupper.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/18 16:35:45 by Oceano #+# #+# */
9 | /* Updated: 2023/01/18 16:37:19 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | //#include
14 |
15 | #include "libft.h"
16 |
17 | int ft_toupper(int c)
18 | {
19 | if (c >= 97 && c <= 122)
20 | c -= 32;
21 | return (c);
22 | }
23 |
24 | /*
25 | int main(void)
26 | {
27 | for (int i = 0; i <= 122; ++i)
28 | printf("%c\n", ft_toupper(i));
29 | }
30 | */
31 |
--------------------------------------------------------------------------------
/ft_isalnum.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_isalnum.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/22 21:15:29 by Oceano #+# #+# */
9 | /* Updated: 2023/01/22 21:15:58 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 | //#include
15 |
16 | int ft_isalnum(int c)
17 | {
18 | return (ft_isalpha(c) || ft_isdigit(c));
19 | }
20 |
21 | /*
22 | int main(void)
23 | {
24 | for (int i = 0; i < 127; ++i)
25 | {
26 | if (ft_isalnum(i))
27 | printf("%c is alnum\n", i);
28 | }
29 | }
30 | */
31 |
--------------------------------------------------------------------------------
/ft_isascii.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_isascii.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/18 11:26:51 by Oceano #+# #+# */
9 | /* Updated: 2023/01/22 21:16:27 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | //#include
14 | //#include
15 | #include "libft.h"
16 |
17 | int ft_isascii(int c)
18 | {
19 | return (c >= 0 && c <= 127);
20 | }
21 |
22 | /*
23 | int main(void)
24 | {
25 | for (int i = 0; i < 256; ++i)
26 | {
27 | if (ft_isascii(i))
28 | printf("My function %d-> %c\n", i, i);
29 | if (isascii(i))
30 | printf("Real function %d-> %c\n\n\n", i, i);
31 | }
32 | }
33 | */
34 |
--------------------------------------------------------------------------------
/ft_isdigit.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_isdigit.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/18 09:48:23 by Oceano #+# #+# */
9 | /* Updated: 2023/01/30 09:34:21 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | //#include
14 | //#include
15 | #include "libft.h"
16 |
17 | int ft_isdigit(int c)
18 | {
19 | return (c >= 48 && c <= 57);
20 | }
21 | /*
22 | int main(void)
23 | {
24 | for (int i = 0; i < 127; ++i)
25 | {
26 | if (ft_isdigit(i))
27 | printf("My function-> %c is a digit\n", i);
28 | if (isdigit(i))
29 | printf("Real function-> %c is a digit\n\n\n", i);
30 | }
31 |
32 | }
33 | */
34 |
--------------------------------------------------------------------------------
/ft_isprint.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_isprint.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/22 21:12:21 by Oceano #+# #+# */
9 | /* Updated: 2023/01/22 21:13:08 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | //#include
14 | //#include
15 | #include "libft.h"
16 |
17 | int ft_isprint(int c)
18 | {
19 | return (c >= 32 && c < 127);
20 | }
21 |
22 | /*
23 | int main(void)
24 | {
25 | for (int i = 0; i < 256; ++i)
26 | {
27 | if (ft_isprint(i))
28 | printf("From my function ->%c is printable\n", i);
29 | if (isprint(i))
30 | printf("From real function ->%c is printable\n\n\n", i);
31 | }
32 | }
33 | */
34 |
--------------------------------------------------------------------------------
/ft_memccpy.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_memccpy.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/27 21:19:20 by Oceano #+# #+# */
9 | /* Updated: 2023/01/30 10:52:51 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | void *ft_memccpy(void *dst, const void *src, int c, size_t n)
16 | {
17 | size_t i;
18 | unsigned char *ptr_dst;
19 | unsigned char *ptr_src;
20 |
21 | ptr_dst = (unsigned char *)dst;
22 | ptr_src = (unsigned char *)src;
23 | i = 0;
24 | while (i < n)
25 | {
26 | ptr_dst[i] = ptr_src[i];
27 | if (ptr_dst[i] == (unsigned char)c)
28 | return ((void *)(dst + i + 1));
29 | ++i;
30 | }
31 | return (NULL);
32 | }
33 |
--------------------------------------------------------------------------------
/ft_putendl_fd.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_putendl_fd.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/20 12:17:14 by Oceano #+# #+# */
9 | /* Updated: 2023/01/24 19:59:47 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | //Outputs the string ’s’ to the given file descriptor
14 | //followed by a newline.
15 |
16 | #include "libft.h"
17 |
18 | void ft_putendl_fd(char *s, int fd)
19 | {
20 | if (s)
21 | {
22 | while (*s)
23 | write(fd, s++, 1);
24 | write(fd, "\n", 1);
25 | }
26 | }
27 |
28 | /*
29 | int main()
30 | {
31 | ft_putendl_fd("hello", 0);
32 | ft_putendl_fd("hello", 1);
33 | ft_putendl_fd("hello", 2);
34 | //3 not in terminal
35 | ft_putendl_fd("hello", 3);
36 | }
37 | */
38 |
--------------------------------------------------------------------------------
/ft_lstlast.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_lstlast.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/26 12:47:56 by Oceano #+# #+# */
9 | /* Updated: 2023/01/31 10:41:14 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | /*
14 | * Returns the last node of a linked list data structure.
15 | * The function takes one parameter,
16 | * ~lst, which is a pointer to the first node of the list.
17 | *
18 | * The function uses a loop to iterate through the list,
19 | * following the next pointers of each node,
20 | * until it reaches the end of the list, and returns the last node.
21 | *
22 | */
23 |
24 | #include "libft.h"
25 |
26 | t_list *ft_lstlast(t_list *lst)
27 | {
28 | if (NULL == lst)
29 | return (NULL);
30 | while (lst->next)
31 | lst = lst->next;
32 | return (lst);
33 | }
34 |
--------------------------------------------------------------------------------
/ft_isalpha.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_isalpha.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/18 09:36:49 by Oceano #+# #+# */
9 | /* Updated: 2023/01/22 21:15:07 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | /*
14 | *It's a good practice to include the library header file in every
15 | implementation file of the library functions,
16 | because it ensures that the function implementation adheres to the
17 | function prototype defined in the header file.
18 | *
19 | */
20 | //#include
21 | #include "libft.h"
22 |
23 | int ft_isalpha(int c)
24 | {
25 | return ((c >= 65 && c <= 90) || (c >= 97 && c <= 122));
26 | }
27 |
28 | /*
29 | int main(void)
30 | {
31 | for (int i = 0; i <= 127; ++i)
32 | if (ft_isalpha(i))
33 | printf("The char %c is alpha\n", i);
34 | }
35 | */
36 |
--------------------------------------------------------------------------------
/ft_lstnew.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_lstnew.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/21 12:28:04 by Oceano #+# #+# */
9 | /* Updated: 2023/01/26 12:11:11 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | /*
14 | * Creates a new node for a linked list,
15 | * and it takes one parameter
16 | * ~content, will be stored in the node's content member variable.
17 | * Allocates memory for the new node using malloc
18 | * initializes the content member variable
19 | * with the value of the content parameter.
20 | * The next member variable is initialized to NULL.
21 | */
22 |
23 | #include "libft.h"
24 |
25 | t_list *ft_lstnew(void *content)
26 | {
27 | t_list *node;
28 |
29 | node = malloc(sizeof(t_list));
30 | if (NULL == node)
31 | return (NULL);
32 | node->content = content;
33 | node->next = NULL;
34 | return (node);
35 | }
36 |
--------------------------------------------------------------------------------
/ft_lstiter.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_lstiter.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/21 15:00:01 by Oceano #+# #+# */
9 | /* Updated: 2023/01/27 14:32:31 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | /*
14 | *Takes in
15 | ~a pointer to a t_list
16 | * ~a pointer to a function that takes in a void pointer
17 | *
18 | *It iterates through the list, starting with the first node,
19 | *and applies the function to the content of each node
20 | * by dereferencing the content pointer
21 | * and passing it as a parameter to the function.
22 | *
23 | *It then moves on to the next node
24 | *in the list until it reaches the end.
25 | */
26 |
27 | #include "libft.h"
28 |
29 | void ft_lstiter(t_list *lst, void (*f)(void *))
30 | {
31 | if (!lst || !f)
32 | return ;
33 | while (lst)
34 | {
35 | f(lst->content);
36 | lst = lst->next;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/ft_strlen.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strlen.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/24 11:09:33 by Oceano #+# #+# */
9 | /* Updated: 2023/01/31 19:11:26 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | //#include
14 | /*
15 | * The strlen() function returns the number of characters that precede the
16 | terminating NUL character.
17 | *
18 | * size_t is not a primitive, is defined into
19 | * or , and is inside
20 | * it is an unsigned char
21 | */
22 |
23 | #include "libft.h"
24 |
25 | size_t ft_strlen(const char *s)
26 | {
27 | size_t len;
28 |
29 | len = 0;
30 | while (s[len])
31 | ++len;
32 | return (len);
33 | }
34 |
35 | /*
36 | int main(void)
37 | {
38 | char *s = "hello";
39 | char *s1 = "";
40 |
41 | printf("Len of %s -> %zu\n", s, ft_strlen(s));
42 | printf("Len of %s -> %zu", s1, ft_strlen(s1));
43 |
44 | }
45 | */
46 |
--------------------------------------------------------------------------------
/ft_lstdelone.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_lstdelone.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/27 13:03:21 by Oceano #+# #+# */
9 | /* Updated: 2023/01/31 18:53:32 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | /*
14 | *Function that frees the memory of a single node
15 | *of a linked list data structure.
16 | *The function takes two parameters: lst and del.
17 | * ~lst is a pointer to the node that needs to be freed,
18 | * ~del is a pointer to a function that will be
19 | * used to free the memory of the node's content.
20 | */
21 |
22 | /*
23 | #include "libft.h"
24 |
25 | void ft_lstdelone(t_list *lst, void (*del)(void*))
26 | {
27 | if (NULL == lst || NULL == del)
28 | return ;
29 | del(lst->content);
30 | free(lst);
31 | }
32 | */
33 | #include "libft.h"
34 |
35 | void ft_lstdelone(t_list *lst, void (*del)(void*))
36 | {
37 | if (!lst || !del)
38 | return ;
39 | del(lst->content);
40 | free(lst);
41 | }
42 |
--------------------------------------------------------------------------------
/ft_putnbr_fd.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_putnbr_fd.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/20 12:50:46 by Oceano #+# #+# */
9 | /* Updated: 2023/01/24 20:04:16 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 |
15 | /*
16 | * Outputs the integer ’n’ to the given file descriptor.
17 | *
18 | */
19 |
20 | void ft_putnbr_fd(int n, int fd)
21 | {
22 | if (n < 0)
23 | {
24 | if (-2147483648 == n)
25 | {
26 | ft_putstr_fd("-2147483648", fd);
27 | return ;
28 | }
29 | n *= -1;
30 | write(fd, "-", 1);
31 | }
32 | if (0 == n)
33 | write(fd, "0", 1);
34 | else if (n > 0 && n < 10)
35 | ft_putchar_fd((n + 48), fd);
36 | else
37 | {
38 | ft_putnbr_fd((n / 10), fd);
39 | ft_putchar_fd((n % 10) + 48, fd);
40 | }
41 | }
42 |
43 | /*
44 | int main()
45 | {
46 | ft_putnbr_fd(404, 1);
47 | write(1, "\n", 1);
48 | ft_putnbr_fd(-2147483648, 1);
49 | write(1, "\n", 1);
50 | ft_putnbr_fd(2147483647, 1);
51 | write(1, "\n", 1);
52 | ft_putnbr_fd(0, 1);
53 | }
54 | */
55 |
--------------------------------------------------------------------------------
/ft_calloc.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_calloc.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/19 12:27:33 by Oceano #+# #+# */
9 | /* Updated: 2023/02/01 12:12:45 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | /*
14 | *
15 | * The calloc() function contiguously allocates enough space for
16 | * ~count objects elements
17 | * ~that are size bytes of memory each.
18 | * returns a pointer to the allocated memory.
19 | The allocated memory is filled with bytes of value zero.
20 |
21 | If error return NULL
22 | and set errno to ENOMEM.
23 |
24 | In the C standard library, calloc is expected to return a
25 | valid pointer when both nmemb and size are 0.
26 |
27 | */
28 |
29 | #include "libft.h"
30 |
31 | void *ft_calloc(size_t nmemb, size_t size)
32 | {
33 | size_t bytes;
34 | void *ptr;
35 |
36 | bytes = nmemb * size;
37 | if (size && ((bytes / size) != nmemb))
38 | return (NULL);
39 | ptr = malloc(bytes);
40 | if (NULL == ptr)
41 | return (NULL);
42 | ft_bzero(ptr, bytes);
43 | return (ptr);
44 | }
45 |
--------------------------------------------------------------------------------
/ft_lstadd_front.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_lstadd_front.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/21 12:42:57 by Oceano #+# #+# */
9 | /* Updated: 2023/01/26 12:42:21 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | /*
14 | *Takes in two parameters:
15 | * ~a pointer to a pointer of the first link of a list (lst),
16 | * ~a pointer to the node to be added to the list (new).
17 | * Adds the node "new" to the beginning of the list,
18 | * and does not return any value.
19 | * No external functions are used.
20 | *
21 | * IMPLEMENTATION
22 | * 1)Checks if both the "lst" and "new" pointers are not NULL.
23 | * 2)If they are not,
24 | * it sets the next pointer of the "new" node
25 | * to the current first node of the list (pointed to by "lst").
26 | * 3)Then it updates the "lst" pointer to point to the "new" node,
27 | * making it the new first node of the list.
28 | */
29 |
30 | #include "libft.h"
31 |
32 | void ft_lstadd_front(t_list **lst, t_list *new)
33 | {
34 | if (lst && new)
35 | {
36 | new->next = *lst;
37 | *lst = new;
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/ft_strdup.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strdup.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/19 13:02:58 by Oceano #+# #+# */
9 | /* Updated: 2023/02/01 12:09:49 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | /*
14 | #include
15 | #include
16 | #include
17 | */
18 |
19 | /*
20 | *
21 | * strdup is a C library function that creates a new,
22 | * duplicate string from an existing string.
23 | * The function takes a single argument,
24 | * ~a pointer to the string to be duplicated,
25 | * and returns a pointer to the new string.
26 | *
27 | * The new string is created using malloc and is
28 | * guaranteed to be null-terminated.
29 | * The caller is responsible for freeing the memory
30 | * allocated by the function using free.
31 | */
32 |
33 | #include "libft.h"
34 |
35 | char *ft_strdup(const char *src)
36 | {
37 | char *str;
38 | int buf_size;
39 |
40 | buf_size = ft_strlen(src) + 1;
41 | str = malloc(buf_size);
42 | if (NULL == str)
43 | return (NULL);
44 | ft_strlcpy(str, src, buf_size);
45 | return (str);
46 | }
47 |
--------------------------------------------------------------------------------
/ft_striteri.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_striteri.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/20 11:35:43 by Oceano #+# #+# */
9 | /* Updated: 2023/01/31 19:23:53 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | /*
14 | * Applies the function ’f’ on each character of
15 | the string passed as argument, passing its index
16 | as first argument. Each character is passed by
17 | address to ’f’ to be modified if necessary.
18 | *
19 | */
20 |
21 | //#include
22 |
23 | /*
24 | void ft_capitalize(unsigned int i, char *c)
25 | {
26 | if (0 == i)
27 | {
28 | if (*c >= 97 && *c <= 122)
29 | *c -= 32;
30 | }
31 | }
32 | */
33 |
34 | #include "libft.h"
35 |
36 | void ft_striteri(char *s, void (*f)(unsigned int, char*))
37 | {
38 | int i;
39 |
40 | i = 0;
41 | if (NULL == s || NULL == f)
42 | return ;
43 | while (s[i])
44 | {
45 | f(i, s + i);
46 | ++i;
47 | }
48 | }
49 |
50 | /*
51 | int main(int argc, char **argv)
52 | {
53 | void (*f)(unsigned int, char *);
54 |
55 | f = ft_capitalize;
56 | ft_striteri(argv[1], f);
57 | printf("%s\n", argv[1]);
58 | }
59 | */
60 |
--------------------------------------------------------------------------------
/ft_lstclear.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_lstclear.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/21 14:52:02 by Oceano #+# #+# */
9 | /* Updated: 2023/01/27 14:29:17 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | /*
14 | *Deletes and frees the memory of a linked list
15 | *starting from a given node.
16 | *
17 | * The function takes two parameters, lst and del.
18 | * ~lst, pointer to a pointer to the first node of the list,
19 | * ~del is a pointer to a function that will be used
20 | * to free the memory of the node's content.
21 | *
22 | * DESCRIPTION
23 | * Deletes and frees the given node and every
24 | * SUCCESSOR of that node, using the function ’del’
25 | * and free(3).
26 | * Finally, the pointer to the list must be set to NULL.
27 | */
28 |
29 | #include "libft.h"
30 |
31 | void ft_lstclear(t_list **lst, void (*del)(void*))
32 | {
33 | t_list *current;
34 | t_list *tmp;
35 |
36 | if (NULL == lst || NULL == del)
37 | return ;
38 | current = *lst;
39 | while (current)
40 | {
41 | tmp = current;
42 | current = current->next;
43 | ft_lstdelone(tmp, del);
44 | }
45 | *lst = NULL;
46 | }
47 |
--------------------------------------------------------------------------------
/ft_strrchr.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strrchr.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/18 17:08:22 by Oceano #+# #+# */
9 | /* Updated: 2023/01/31 19:13:00 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | //#include
14 | //#include
15 |
16 | /*
17 | *
18 | * locates the last occurrence of c 🚨(converted to a char)🚨
19 | * in the string pointed to by s. The terminating null character
20 | * is considered to be part of the string;
21 | * therefore if c is `\0', the functions locate the terminating `\0'.
22 | *
23 | * TLDR
24 | * The strrchr() function is identical to strchr(),
25 | * except it locates the last occurrence of c.
26 | *
27 | */
28 |
29 | #include "libft.h"
30 |
31 | char *ft_strrchr(const char *s, int c)
32 | {
33 | int len;
34 |
35 | len = ft_strlen(s);
36 | while (len >= 0)
37 | {
38 | if ((char)c == *(s + len))
39 | return ((char *)(s + len));
40 | --len;
41 | }
42 | return (NULL);
43 | }
44 |
45 | /*
46 | int main(void)
47 | {
48 | char v[] = "Hello world";
49 | printf("%p\n", ft_strrchr(v, '\0'));
50 | printf("%p\n", strrchr(v, '\0'));
51 | }
52 | */
53 |
--------------------------------------------------------------------------------
/ft_bzero.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_bzero.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/18 13:24:57 by Oceano #+# #+# */
9 | /* Updated: 2023/01/22 21:04:36 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | /*
14 | * bzero is a C library function that is similar to the memset function.
15 | * It is used to set a block of memory to the value of zero.
16 | * The function takes two arguments:
17 | * ~a pointer to the memory block to be set,
18 | * ~and the number of bytes to set.
19 | *
20 | * DESCRIPTION
21 | * The bzero() function writes n zeroed bytes to the string s.
22 | * If n is zero, bzero() does nothing.
23 | */
24 |
25 | //#include
26 | //#include
27 | #include "libft.h"
28 |
29 | void ft_bzero(void *s, size_t n)
30 | {
31 | while (n-- > 0)
32 | *((unsigned char *)s + n) = 0;
33 | }
34 |
35 | /*
36 | int main(void)
37 | {
38 | int v[20];
39 |
40 | for (int i = 0; i < 20; ++i)
41 | v[i] = 42;
42 |
43 | for (int i = 0; i < 20; ++i)
44 | printf("%d\n", v[i]);
45 | puts("");
46 |
47 | ft_bzero(v, 9);
48 |
49 | for (int i = 0; i < 20; ++i)
50 | printf("%d\n", v[i]);
51 | }
52 | */
53 |
--------------------------------------------------------------------------------
/ft_memset.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_memset.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/18 13:03:03 by Oceano #+# #+# */
9 | /* Updated: 2023/01/22 23:58:05 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | //#include
14 | //#include
15 |
16 | /*
17 | * The memset function is a C library function that is used
18 | * to set a block of memory to a specific value.
19 | * The function takes three arguments:
20 | * ~a pointer to the memory block to be set,
21 | * ~the value to set it to,
22 | * ~and the number of bytes to set.
23 | */
24 |
25 | #include "libft.h"
26 |
27 | void *ft_memset(void *b, int c, size_t len)
28 | {
29 | typedef unsigned char *byte;
30 | while (len-- > 0)
31 | *((byte)b + len) = c;
32 | return (b);
33 | }
34 |
35 | /*
36 | int main(void)
37 | {
38 | char v[10];
39 |
40 | ft_memset(v, 42, 5);
41 |
42 | for (int i = 0; i < 10; ++i)
43 | printf("%d\n", v[i]);
44 | printf("ft_memset-> %p\n\n", ft_memset(v, 42, 5));
45 |
46 |
47 |
48 | memset(v, 21, 5);
49 |
50 | for (int i = 0; i < 10; ++i)
51 | printf("%d\n", v[i]);
52 | printf("Real memset-> %p\n", memset(v, 21, 5));
53 | }
54 | */
55 |
--------------------------------------------------------------------------------
/ft_strjoin.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strjoin.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/19 15:59:37 by Oceano #+# #+# */
9 | /* Updated: 2023/01/31 19:31:40 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | /*
14 | * ARGUMENTS
15 | * s1: The prefix string.
16 | * s2: The suffix string.
17 | *
18 | * DESCRIPTION
19 | * Allocates (with malloc(3)) and returns a new
20 | string, which is the result of the concatenation
21 | of ’s1’ and ’s2’.
22 | *
23 | * RETURN
24 | * The new string.
25 | * NULL if the allocation fails.
26 | */
27 |
28 | #include "libft.h"
29 |
30 | char *ft_strjoin(char const *s1, char const *s2)
31 | {
32 | size_t buffer;
33 | size_t i;
34 | char *ptr;
35 |
36 | i = 0;
37 | if (NULL == s1 || NULL == s2)
38 | return (NULL);
39 | buffer = ft_strlen(s1) + ft_strlen(s2) + 1;
40 | ptr = malloc(buffer);
41 | if (NULL == ptr)
42 | return (NULL);
43 | while (*s1)
44 | *(ptr + i++) = *s1++;
45 | while (*s2)
46 | *(ptr + i++) = *s2++;
47 | *(ptr + i) = '\0';
48 | return (ptr);
49 | }
50 |
51 | /*
52 |
53 | int main(int argc, char **argv)
54 | {
55 | char *s = "Hello";
56 | char *s1 = "World";
57 |
58 | printf("%s\n\n", ft_strjoin(s, s1));
59 | }
60 | */
61 |
--------------------------------------------------------------------------------
/ft_lstsize.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_lstsize.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/26 12:43:34 by Oceano #+# #+# */
9 | /* Updated: 2023/01/26 12:46:47 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | /*
14 | * DESCRIPTION
15 | * Counts the number of nodes in a linked list data structure.
16 | * The function takes one parameter
17 | * ~lst, which is a pointer to the first node of the list.
18 | *
19 | * The function uses a loop to iterate through the list,
20 | * following the next pointers of each node,
21 | * until it reaches the end of the list,
22 | * and returns the number of nodes it has encountered.
23 | *
24 | * This code allows me to move in the linked list
25 | *
26 | * 👀 current = current->next; 👀
27 | *
28 | */
29 |
30 | //#include
31 | #include "libft.h"
32 |
33 | int ft_lstsize(t_list *lst)
34 | {
35 | int counter;
36 |
37 | counter = 0;
38 | while (lst)
39 | {
40 | ++counter;
41 | lst = lst->next;
42 | }
43 | return (counter);
44 | }
45 |
46 | /*
47 | int main(void) {
48 | t_list *head;
49 | head = ft_lstnew("first node");
50 | head->next = ft_lstnew("second node");
51 | head->next->next = ft_lstnew("third node");
52 |
53 | printf("The number of nodes in the list is: %d\n", ft_lstsize(head));
54 |
55 | return 0;
56 | }
57 | */
58 |
--------------------------------------------------------------------------------
/ft_memchr.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_memchr.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/22 23:30:22 by Oceano #+# #+# */
9 | /* Updated: 2023/01/22 23:39:27 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | //#include
14 | //#include
15 | //#include
16 | //#include
17 |
18 | /*
19 | * memchr() is a function in the C standard library that
20 | * searches for the first occurrence of a given character in a memory block.
21 | * It takes in three arguments:
22 | * ~a pointer to the memory block to be searched,
23 | * ~the character to search for,
24 | * ~the size of the memory block.
25 | * The function returns a pointer to the first occurrence of the character
26 | * in the memory block, or a null pointer if the character is not found.
27 | */
28 |
29 | #include "libft.h"
30 |
31 | void *ft_memchr(const void *s, int c, size_t n)
32 | {
33 | size_t i;
34 |
35 | i = 0;
36 | while (i < n)
37 | {
38 | if (*((unsigned char *)s + i) == (unsigned char)c)
39 | return ((void *)((unsigned char *)s + i));
40 | ++i;
41 | }
42 | return (NULL);
43 | }
44 |
45 | /*
46 |
47 | //gcc ft_memchr.c && ./a.out s c n
48 | int main(int argc, char **argv)
49 | {
50 | char c;
51 | char *s;
52 | int n;
53 |
54 | s = argv[1];
55 | c = argv[2][0];
56 | n = atoi(argv[3]);
57 | printf("%p\n\n", ft_memchr(s, c, n));
58 | printf("%p\n\n", memchr(s, c, n));
59 | }
60 | */
61 |
--------------------------------------------------------------------------------
/ft_substr.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_substr.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/19 15:22:24 by Oceano #+# #+# */
9 | /* Updated: 2023/01/31 17:41:51 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | /*
14 | * Allocates (with malloc(3)) and returns a substring
15 | from the string ’s’.
16 | The substring begins at index ’start’ and is of
17 | maximum size ’len’.
18 | *
19 | * ARGUMENTS
20 | * s: The string from which to create the substring.
21 | start: The start index of the substring in the string ’s’.
22 | len: The maximum length of the substring.
23 | *
24 | * RETURN
25 | * The substring
26 | * NULL if allocation fails
27 | *
28 | */
29 |
30 | //#include
31 | //#include
32 |
33 | #include"libft.h"
34 |
35 | char *ft_substr(char const *s, unsigned int start, size_t len)
36 | {
37 | char *str;
38 | size_t s_len;
39 | size_t substring_len;
40 |
41 | if (NULL == s)
42 | return (NULL);
43 | s_len = ft_strlen(s);
44 | if (start >= s_len)
45 | return (ft_strdup(""));
46 | substring_len = s_len - start;
47 | if (len > substring_len)
48 | len = substring_len;
49 | str = malloc(len + 1);
50 | if (NULL == str)
51 | return (NULL);
52 | ft_strlcpy(str, s + start, len + 1);
53 | return (str);
54 | }
55 |
56 | /*
57 | #include
58 | int main()
59 | {
60 | char *str;
61 |
62 | str = "hello";
63 | printf("%s\n",ft_substr(str, 0, 20));
64 | }
65 | */
66 |
--------------------------------------------------------------------------------
/ft_itoa.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_itoa.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/20 10:04:58 by Oceano #+# #+# */
9 | /* Updated: 2023/01/30 19:35:09 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | /*
14 | *
15 | * Allocates with malloc(3) and returns a string
16 | * representing the integer received as an argument.
17 | * Negative numbers must be handled.
18 | *
19 | */
20 |
21 | //#include
22 | //#include
23 | #include "libft.h"
24 |
25 | static int ft_len(int n)
26 | {
27 | size_t count;
28 |
29 | count = 0;
30 | if (n <= 0)
31 | ++count;
32 | while (n)
33 | {
34 | ++count;
35 | n /= 10;
36 | }
37 | return (count);
38 | }
39 |
40 | char *ft_itoa(int n)
41 | {
42 | char *ptr;
43 | int len;
44 |
45 | len = ft_len(n);
46 | ptr = malloc(len + 1);
47 | if (NULL == ptr)
48 | return (NULL);
49 | ptr[len] = '\0';
50 | if (0 == n)
51 | ptr[0] = '0';
52 | else if (n < 0)
53 | ptr[0] = '-';
54 | while (n)
55 | {
56 | if (n < 0)
57 | ptr[--len] = (~(n % 10) + 1) + 48;
58 | else
59 | ptr[--len] = (n % 10) + 48;
60 | n /= 10;
61 | }
62 | return (ptr);
63 | }
64 |
65 | /*
66 | int main()
67 | {
68 | char *str = ft_itoa(-2134);
69 | printf("%s\n", str);
70 |
71 | str = ft_itoa(42);
72 | printf("%s\n", str);
73 | str = ft_itoa(707);
74 | printf("%s\n", str);
75 |
76 | str = ft_itoa(INT32_MIN);
77 | printf("%s\n", str);
78 |
79 | str = ft_itoa(INT32_MAX);
80 | printf("%s\n", str);
81 |
82 | str = ft_itoa(0);
83 | printf("%s\n", str);
84 |
85 | return 0;
86 | }
87 | */
88 |
--------------------------------------------------------------------------------
/ft_strnstr.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strnstr.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/18 19:01:14 by Oceano #+# #+# */
9 | /* Updated: 2023/02/01 10:18:07 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include
14 | #include
15 |
16 | /*
17 | * The strnstr() function locates the first occurrence
18 | * of the null-terminated string needle in the string haystack,
19 | * where not more than len characters are searched.
20 | *
21 | * Characters that appear after a `\0' character are not searched.
22 | * Since the strnstr() function is a FreeBSD specific API,
23 | * it should only be used when portability is not a concern.
24 | *
25 | * RETURN VALUES
26 | * If needle is an empty string, haystack is returned; ✅
27 | * if needle occurs nowhere in haystack, NULL is returned;
28 | * otherwise a pointer to thefirst character ✅
29 | * of the first occurrence of needle is returned. ✅
30 |
31 | */
32 |
33 | #include "libft.h"
34 |
35 | char *ft_strnstr(const char *haystack, const char *needle, size_t len)
36 | {
37 | size_t j;
38 | size_t k;
39 |
40 | j = 0;
41 | if (!*needle || (NULL == haystack && !len))
42 | return ((char *)haystack);
43 | while (*(haystack + j) && j < len)
44 | {
45 | k = 0;
46 | while (*(haystack + j + k) == *(needle + k) && (k + j) < len)
47 | {
48 | if (0 == *(needle + k + 1))
49 | return ((char *)haystack + j);
50 | ++k;
51 | }
52 | ++j;
53 | }
54 | return (NULL);
55 | }
56 |
57 | /*
58 | int main()
59 | {
60 | // ->NULL back
61 | printf("%s\n", ft_strnstr("lorem ipsum dolor sit amet", "dolor", 15));
62 |
63 | }
64 | */
65 |
--------------------------------------------------------------------------------
/ft_strlcpy.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strlcpy.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/18 16:22:55 by Oceano #+# #+# */
9 | /* Updated: 2023/01/24 11:07:34 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | //#include
14 | //#include
15 | //#include
16 |
17 | /*
18 | * strlcpy function.
19 | * It copies at most dstsize - 1 characters from
20 | * the string src to the string dst
21 | * and ensures that dst is null-terminated.
22 | *
23 | * RETURN
24 | * The function also returns the length of src,
25 | * which is the number of characters that would
26 | * have been copied if dstsize was not limited.
27 | *
28 | * The strlcpy() and strlcat() functions return
29 | * the total length of the string they
30 | * tried to create.
31 | * For strlcpy() that means the length of src.
32 | *
33 | * For strlcat() that means the initial
34 | * length of dst plus the length of src.
35 | */
36 |
37 | #include "libft.h"
38 |
39 | size_t ft_strlcpy(char *dst, const char *src, size_t dstsize)
40 | {
41 | size_t src_len;
42 |
43 | src_len = ft_strlen(src);
44 | if (NULL == dst || NULL == src || !dstsize)
45 | return (src_len);
46 | while (*src && --dstsize)
47 | *dst++ = *src++;
48 | *dst = '\0';
49 | return (src_len);
50 | }
51 |
52 | /*
53 | #define DEST_SIZE 5
54 |
55 | int main(void)
56 | {
57 | char dst[DEST_SIZE];
58 | char src[] = "rrrrrr\0\0\0\0\0\0";
59 |
60 |
61 | printf("%s-> %lu\n", dst, strlcpy(dst, "lorem", DEST_SIZE));
62 |
63 | char dst1[DEST_SIZE];
64 |
65 | printf("My function -> %s-> %lu\n", dst, ft_strlcpy(dst1, "lorem", DEST_SIZE));
66 |
67 | }
68 | */
69 |
--------------------------------------------------------------------------------
/ft_memcmp.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_memcmp.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/19 10:43:42 by Oceano #+# #+# */
9 | /* Updated: 2023/01/22 23:45:07 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | /*
14 | * memcmp() is a function in the C standard library
15 | * that compares two memory blocks.
16 | * It takes in three arguments:
17 | * ~pointers to the two memory blocks to be compared,
18 | * ~and the size of the memory blocks.
19 | * The function returns an integer value indicating the relationship
20 | * between the two memory blocks:
21 | * ~0 if they are equal,
22 | * ~a positive value if the first memory block is greater than the second,
23 | * ~and a negative value if the first memory block is less than the second.
24 | */
25 |
26 | //#include
27 | //#include
28 | //#include
29 | #include "libft.h"
30 |
31 | int ft_memcmp(const void *s1, const void *s2, size_t n)
32 | {
33 | size_t i;
34 |
35 | i = 0;
36 | while (i < n)
37 | {
38 | if (*((unsigned char *)s1 + i) != *((unsigned char *)s2 + i))
39 | return (*((unsigned char *)s1 + i) - *((unsigned char *)s2 + i));
40 | ++i;
41 | }
42 | return (0);
43 | }
44 |
45 | /*
46 |
47 | int main(int argc, char **argv)
48 | {
49 | char *s = argv[1];
50 | char *s1 = argv[2];
51 | int n = atoi(argv[3]);
52 |
53 | if (!ft_memcmp(s, s1, n))
54 | printf("The string are equal up to %d letters\n\n\n", n);
55 | else
56 | printf("The strings are different\n\n\n");
57 |
58 | if (!memcmp(s, s1, n))
59 | printf("The string are equal up to %d letters\n", n);
60 | else
61 | printf("The strings are different\n");
62 |
63 |
64 | }
65 | */
66 |
--------------------------------------------------------------------------------
/ft_lstmap.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_lstmap.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: ncolomer +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/10/09 20:25:31 by ncolomer #+# #+# */
9 | /* Updated: 2023/01/27 16:09:25 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | /*
14 | Parameters lst:
15 | ~The address of a pointer to a node.
16 | ~f: The address of the function used
17 | to iterate on the list.
18 | ~del: The address of the function used to delete
19 | the content of a node if needed.
20 | Return value
21 | ~The new list.
22 | ~NULL if the allocation fails.
23 |
24 | External functs. malloc, free
25 |
26 | DESCRIPTION
27 | Iterates the list ’lst’ and applies the function
28 | ’f’ on the content of each node.
29 | Returns a new list resulting of the
30 | successive applications of the function ’f’.
31 | The ’del’ function is used to
32 | delete the content of a node if needed.
33 |
34 | IMPLEMENTATION
35 | ~ft_lstnew-> t_list *ft_lstnew(void *content)
36 | ~ft_lstclear->void ft_lstclear(t_list **lst, void (*del)(void*))
37 | ~ft_lstadd_back->void ft_lstadd_back(t_list **lst, t_list *new)
38 | ~ft_lstiter->void ft_lstiter(t_list *lst, void (*f)(void *))
39 | */
40 |
41 | #include "libft.h"
42 |
43 | t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
44 | {
45 | t_list *new_list;
46 | t_list *new_node;
47 |
48 | if (!lst)
49 | return (NULL);
50 | new_list = NULL;
51 | while (lst)
52 | {
53 | if (f)
54 | new_node = ft_lstnew(f(lst->content));
55 | else
56 | new_node = ft_lstnew(lst->content);
57 | if (!new_node)
58 | {
59 | ft_lstclear(&new_list, del);
60 | return (NULL);
61 | }
62 | ft_lstadd_back(&new_list, new_node);
63 | lst = lst->next;
64 | }
65 | return (new_list);
66 | }
67 |
--------------------------------------------------------------------------------
/ft_strncmp.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strncmp.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/19 11:34:09 by Oceano #+# #+# */
9 | /* Updated: 2023/01/31 20:05:12 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | /*
14 | #include
15 | #include
16 | #include
17 | #include
18 | */
19 |
20 | /*
21 | *
22 | * The strncmp() function compares not more than n characters.
23 | *
24 | * RETURN is the difference of the chars
25 | *
26 | * The strcmp() and strncmp() functions return an integer
27 | * ~Greater than,
28 | * ~equal to,
29 | * ~less than 0, according as the string s1 is greater than,
30 | * equal to,
31 | * or less than the string s2.
32 | *
33 | * 🚨The comparison is done using unsigned characters,
34 | * so that `\200' is greater than `\0'🚨
35 | *
36 | * [h e l l o] [h i] [h e l l o]
37 | *
38 | * [h e l l o] [h e l l o] [h i]
39 | */
40 |
41 | #include "stdlib.h"
42 |
43 | int ft_strncmp(const char *s1, const char *s2, size_t n)
44 | {
45 | if (!n)
46 | return (0);
47 | while (*s1 == *s2 && --n && *s1)
48 | {
49 | ++s1;
50 | ++s2;
51 | }
52 | return (*(unsigned char *)s1 - *(unsigned char *)s2);
53 | }
54 |
55 | /*
56 |
57 | int main(int argc, char **argv)
58 | {
59 | char *s;
60 | char *s1;
61 | int n;
62 |
63 | s = argv[1];
64 | s1 = argv[2];
65 | n = atoi(argv[3]);
66 | if (ft_strncmp(s, s1, n))
67 | printf("The strings are not equal\n");
68 | else
69 | printf("The strings are equal\n");
70 | if (ft_strncmp(s, s1, n))
71 | printf("The strings are not equal\n");
72 | else
73 | printf("The strings are equal\n");
74 |
75 | //CORNER CASE
76 | ft_strncmp("test\200", "test\0", 6);
77 |
78 | }
79 | */
80 |
--------------------------------------------------------------------------------
/ft_atoi.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_atoi.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/18 17:36:14 by Oceano #+# #+# */
9 | /* Updated: 2023/02/01 11:23:34 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | //#include
14 | //#include
15 | #include "libft.h"
16 |
17 | /*
18 | * 🚨According to the standard atoi function specification,
19 | * multiple signs in the input string are considered invalid
20 | * and the function should return 0 in such cases.
21 | * 🚨
22 | */
23 |
24 | int ft_atoi(const char *str)
25 | {
26 | int num;
27 | int isneg;
28 | int i;
29 |
30 | num = 0;
31 | isneg = 1;
32 | i = 0;
33 | while (str[i] && (str[i] == ' ' || str[i] == '\t'
34 | || str[i] == '\n' || str[i] == '\r'
35 | || str[i] == '\v' || str[i] == '\f'))
36 | i++;
37 | if (str[i] == '+')
38 | i++;
39 | else if (str[i] == '-')
40 | {
41 | isneg *= -1;
42 | i++;
43 | }
44 | while (ft_isdigit(str[i]))
45 | {
46 | num = (num * 10) + (str[i] - '0');
47 | i++;
48 | }
49 | return (num * isneg);
50 | }
51 |
52 | /*
53 | static int ft_isspace(char c)
54 | {
55 | if ((c >= 9 && c <= 13) || 32 == c)
56 | return (1);
57 | return (0);
58 | }
59 |
60 | int ft_atoi(const char *str)
61 | {
62 | int sign;
63 | int nb;
64 |
65 | nb = 0;
66 | sign = 1;
67 | if (NULL == str)
68 | return (0);
69 | while (ft_isspace(*str))
70 | ++str;
71 | if (*str == '+' || *str == '-')
72 | if ((*str++) == '-')
73 | sign = -1;
74 | while (*str >= 48 && *str <= 57)
75 | nb = (10 * nb) + ((*str++) - 48);
76 | return (nb * sign);
77 | }
78 | */
79 | /*
80 | int main(void)
81 | {
82 | char *s;
83 | char *s1;
84 |
85 | s1 = " ++ 2413";
86 | s = " ++ 2413";
87 | printf("%d\n", ft_atoi(s));
88 | printf("%d\n", atoi(s));
89 | }
90 | */
91 |
--------------------------------------------------------------------------------
/ft_strtrim.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strtrim.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/20 13:02:23 by Oceano #+# #+# */
9 | /* Updated: 2023/01/31 10:10:42 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | /*
14 | * DESCRIPTION
15 | * Allocates (with malloc(3)) and returns a copy of
16 | ’s1’ with the characters specified in ’set’ removed
17 | from the beginning and the end of the string.
18 | *
19 | * ARGUMENTS
20 | * s1: The string to be trimmed.
21 | set: The reference set of characters to trim.
22 | *
23 | * RETURN
24 | * The trimmed string.
25 | NULL if the allocation fails.
26 | *
27 | * For example, let's say 's1' is equal to "Hello, World!"
28 | * and 'set' is equal to ",! ".
29 | * The function would create a new string in memory,
30 | * and copy "Hello, World!" into it.
31 | * Then it would remove the comma, exclamation mark
32 | * and space from the beginning and end of the new string,
33 | * so it would be "Hello, World" after function execution.
34 | */
35 |
36 | //#include
37 | #include "libft.h"
38 |
39 | char *ft_strtrim(char const *s1, char const *set)
40 | {
41 | int start;
42 | int end;
43 | char *trimmed;
44 |
45 | if (NULL == s1 || NULL == set)
46 | return (NULL);
47 | if (!*s1)
48 | return (ft_strdup(s1));
49 | start = 0;
50 | end = ft_strlen(s1) - 1;
51 | while (*(s1 + start) && ft_strchr(set, *(s1 + start)))
52 | ++start;
53 | while (end >= 0 && ft_strchr(set, *(s1 + end)))
54 | --end;
55 | if (start > end)
56 | return (ft_strdup(""));
57 | trimmed = malloc((end - start) + 2);
58 | if (NULL == trimmed)
59 | return (NULL);
60 | ft_strlcpy(trimmed, s1 + start, (end - start) + 2);
61 | return (trimmed);
62 | }
63 |
64 | /*
65 | int main(void)
66 | {
67 | printf("%s\n", ft_strtrim(" ", " "));
68 |
69 | }
70 | */
71 |
--------------------------------------------------------------------------------
/ft_memmove.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_memmove.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: smbaabu +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2019/02/19 16:07:39 by smbaabu #+# #+# */
9 | /* Updated: 2023/01/31 19:09:31 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | /*
14 | * Move block of memory
15 | * Copies the values of
16 | * ~len bytes from the location
17 | * ~pointed by source to the memory block
18 | * ~pointed by destination.
19 | *
20 | * Copying takes place as if an intermediate buffer were used,
21 | * allowing the destination and source to overlap.
22 | *
23 | *
24 | if memcpy copies "front to back" and the memory blocks are aligned as this
25 | [---- src ----]
26 | [---- dst ---]
27 | copying the first byte of src to dst already destroys the content
28 | of the last bytes of src before these have been copied.
29 | Only copying "back to front" will lead to correct results.
30 |
31 | Now swap src and dst:
32 |
33 | [---- dst ----]
34 | [---- src ---]
35 | *
36 | * TL;DR
37 | * memmove, is typically implemented as a slower, safer version of memcpy
38 | * that can handle overlapping memory regions.
39 | * memmove copies the data from the source to a temporary buffer,
40 | * and then copies the data from the temporary buffer to the destination.
41 | * This ensures that the data is copied correctly
42 | * even if the source and destination memory regions overlap.
43 | */
44 |
45 | #include "libft.h"
46 | //#include "stdio.h"
47 |
48 | void *ft_memmove(void *dst, const void *src, size_t len)
49 | {
50 | typedef unsigned char byte;
51 | if ((NULL == src && NULL == dst) || len < 0)
52 | return (dst);
53 | if (dst > src)
54 | while (len--)
55 | *((byte *)dst + len) = *((byte *)src + len);
56 | else
57 | ft_memcpy(dst, src, len);
58 | return (dst);
59 | }
60 | /*
61 | int main()
62 | {
63 | char src[] = "lorem ipsum dolor sit amet";
64 | char *dest;
65 |
66 | dest = src + 1;
67 | printf("%s\n", (char *)ft_memmove(src, dest, 8));
68 | }
69 | */
70 |
--------------------------------------------------------------------------------
/ft_strchr.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strchr.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/18 16:41:32 by Oceano #+# #+# */
9 | /* Updated: 2023/01/31 19:12:40 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | /*
14 | * DESCRIPTION
15 | * The strchr() function locates the first occurrence
16 | * of c 🚨(converted to a char)🚨 in the string pointed to by s.
17 | * The terminating null character is considered to be
18 | * part of the string; therefore if c is `\0',
19 | * the functions locate the terminating `\0'.
20 | *
21 | *
22 | * strchr is a C library function that searches for the first occurrence
23 | * of a character in a string. It takes two arguments:
24 | * ~a pointer to the string to search and
25 | * ~the character to search for.
26 | *
27 | * 🚨
28 | * The terminating null character is considered to be part
29 | * of the string; therefore if c is `\0', the func-
30 | * tions locate the terminating `\0'.
31 | * 🚨
32 | *
33 | * TLDR
34 | * It returns a pointer to the first occurrence of the character
35 | * in the string, or a null pointer if the character is not found.
36 | */
37 |
38 | //#include
39 | //#include
40 | #include "libft.h"
41 |
42 | char *ft_strchr(const char *s, int c)
43 | {
44 | while (*s != (char)c && *s)
45 | ++s;
46 | if (*s)
47 | return ((char *)s);
48 | else if (!*s && 0 == (char)c)
49 | return ((char *)s);
50 | else
51 | return (NULL);
52 | }
53 | /*
54 | int main(void)
55 | {
56 | char *s = "Bonjour";
57 |
58 | printf("%p\n", strchr(s, 'b'));
59 | printf("my func-> %p\n\n", ft_strchr(s, 'b'));
60 |
61 | printf("%p\n", strchr(s, 'j'));
62 | printf("%p\n\n", ft_strchr(s, 'j'));
63 |
64 | printf("%p\n", strchr(s, 's'));
65 | printf("%p\n\n", ft_strchr(s, 's'));
66 |
67 | printf("%p\n", strchr(s, '\0'));
68 | printf("%p\n\n", ft_strchr(s, '\0'));
69 |
70 | char *s1 = "";
71 | printf("%p\n", strchr(s1, '\0'));
72 | printf("%p\n\n", ft_strchr(s1, '\0'));
73 | }
74 | */
75 |
--------------------------------------------------------------------------------
/ft_strmapi.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strmapi.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/20 11:12:10 by Oceano #+# #+# */
9 | /* Updated: 2023/01/31 19:26:00 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | /*
14 | * DESCRIPTION
15 | * Applies the function ’f’ to each character of the
16 | string ’s’, passing its index as first argument
17 | to create a new string (with malloc(3)) resulting
18 | from successive applications of ’f’.
19 | *
20 | * BETTER DESCRIPTION
21 | * ft_strmapi stands for "string map indexed."
22 | * It is a function that applies a given function to each
23 | * character of a string, while also passing the index of the
24 | * character to the function. The result is a new string
25 | * with the modified characters. Essentially, it takes the
26 | * string, applies a function to each element of the string,
27 | * and then returns a new string with the modified elements.
28 | *
29 | * RETURN
30 | * The string created from the successive applications
31 | of ’f’.
32 | Returns NULL if the allocation fails.
33 | */
34 |
35 | #include "libft.h"
36 | //#include
37 |
38 | /*
39 | char my_func(unsigned int i, char c) {
40 | // example function that capitalizes every even indexed character
41 | if (i % 2 == 0) {
42 | return c - 32;
43 | }
44 | return c;
45 | }
46 | */
47 |
48 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
49 | {
50 | char *ptr;
51 | int i;
52 |
53 | i = 0;
54 | if (NULL == s)
55 | return (NULL);
56 | ptr = malloc(ft_strlen(s) + 1);
57 | if (NULL == ptr)
58 | return (NULL);
59 | if (NULL == f)
60 | {
61 | ft_strlcpy(ptr, s, ft_strlen(s) + 1);
62 | return (ptr);
63 | }
64 | while (s[i])
65 | {
66 | ptr[i] = f(i, s[i]);
67 | ++i;
68 | }
69 | ptr[i] = '\0';
70 | return (ptr);
71 | }
72 |
73 | /*
74 | *
75 | int main(int argc, char **argv)
76 | {
77 | char *s;
78 | char (*f)(unsigned int, char);
79 |
80 | f = my_func;
81 | s = "hello";
82 | printf("%s\n", ft_strmapi(s, f));
83 | }
84 | */
85 |
--------------------------------------------------------------------------------
/ft_split.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_split.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/25 14:11:13 by Oceano #+# #+# */
9 | /* Updated: 2023/01/30 20:14:19 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #include "libft.h"
14 | //#include
15 | //#include
16 |
17 | /*
18 | * DESCRIPTION
19 | * Allocates (with malloc(3)) and returns an array of strings obtained
20 | * by splitting ’s’ using the character ’c’ as a delimiter.
21 | * The array must end with a NULL pointer.
22 | *
23 | * RETURN
24 | * The array of new strings resulting from the split.
25 | * NULL if the allocation fails.
26 | *
27 | * INPUT
28 | * s: The string to be split.
29 | c: The delimiter character.
30 | */
31 | int safe_malloc(char **token_v, int position, size_t buffer)
32 | {
33 | int i;
34 |
35 | i = 0;
36 | token_v[position] = malloc(buffer);
37 | if (NULL == token_v[position])
38 | {
39 | while (i < position)
40 | free(token_v[i++]);
41 | free(token_v);
42 | return (1);
43 | }
44 | return (0);
45 | }
46 |
47 | // return 0 if all mallocs went fine, otherwise 1
48 | int fill(char **token_v, char const *s, char delimeter)
49 | {
50 | size_t len;
51 | int i;
52 |
53 | i = 0;
54 | while (*s)
55 | {
56 | len = 0;
57 | while (*s == delimeter && *s)
58 | ++s;
59 | while (*s != delimeter && *s)
60 | {
61 | ++len;
62 | ++s;
63 | }
64 | if (len)
65 | {
66 | if (safe_malloc(token_v, i, len + 1))
67 | return (1);
68 | ft_strlcpy(token_v[i], s - len, len + 1);
69 | }
70 | ++i;
71 | }
72 | return (0);
73 | }
74 |
75 | size_t count_tokens(char const *s, char delimeter)
76 | {
77 | size_t tokens;
78 | int inside_token;
79 |
80 | tokens = 0;
81 | while (*s)
82 | {
83 | inside_token = 0;
84 | while (*s == delimeter && *s)
85 | ++s;
86 | while (*s != delimeter && *s)
87 | {
88 | if (!inside_token)
89 | {
90 | ++tokens;
91 | inside_token = 42;
92 | }
93 | ++s;
94 | }
95 | }
96 | return (tokens);
97 | }
98 |
99 | /*
100 | * INPUT
101 | *
102 | * "___Hello_there,_dude!!"
103 | *
104 | * v--->[0]------->"Hello"
105 | * |-->[1]------->"there,"
106 | * |-->[2]------->"dude!!"
107 | * |-->[NULL]
108 | **/
109 | char **ft_split(char const *s, char c)
110 | {
111 | size_t tokens;
112 | char **token_v;
113 |
114 | if (NULL == s)
115 | return (NULL);
116 | tokens = 0;
117 | tokens = count_tokens(s, c);
118 | token_v = malloc((tokens + 1) * sizeof(char *));
119 | if (NULL == token_v)
120 | return (NULL);
121 | token_v[tokens] = NULL;
122 | if (fill(token_v, s, c))
123 | return (NULL);
124 | return (token_v);
125 | }
126 |
--------------------------------------------------------------------------------
/libft.h:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* libft.h :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/22 17:08:01 by Oceano #+# #+# */
9 | /* Updated: 2023/01/26 13:29:14 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | #ifndef LIBFT_H
14 | # define LIBFT_H
15 | //size_t, malloc, write
16 | # include
17 | # include
18 | # include
19 |
20 | //String Prototypes
21 | int ft_isdigit(int c);
22 | int ft_isalpha(int c);
23 | int ft_isalnum(int c);
24 | int ft_isascii(int c);
25 | int ft_isprint(int c);
26 | int ft_toupper(int c);
27 | int ft_tolower(int c);
28 | size_t ft_strlen(const char *s);
29 | size_t ft_strlcpy(char *dst, const char *src, size_t dstsize);
30 | size_t ft_strlcat(char *dst, const char *src, size_t dstsize);
31 | char *ft_strchr(const char *s, int c);
32 | char *ft_strrchr(const char *s, int c);
33 | int ft_strncmp(const char *s1, const char *s2, size_t n);
34 | char *ft_strnstr(const char *haystack, const char *needle, size_t len);
35 |
36 | //Memory manipulation
37 | void *ft_memset(void *b, int c, size_t len);
38 | void ft_bzero(void *s, size_t n);
39 | void *ft_memchr(const void *s, int c, size_t n);
40 | int ft_memcmp(const void *s1, const void *s2, size_t n);
41 | void *ft_memcpy(void *dst, const void *src, size_t n);
42 | void *ft_memmove(void *dst, const void *src, size_t len);
43 | int ft_atoi(const char *str);
44 |
45 | //🚨cancel
46 | void *ft_memccpy(void *dst, const void *src, int c, size_t n);
47 |
48 | //malloc
49 | void *ft_calloc(size_t count, size_t size);
50 | char *ft_strdup(const char *s1);
51 |
52 | //2) Additional functions
53 | char *ft_substr(char const *s, unsigned int start, size_t len);
54 | char *ft_strjoin(char const *s1, char const *s2);
55 | char *ft_strtrim(char const *s1, char const *set);
56 | char **ft_split(char const *s, char c);
57 | char *ft_itoa(int n);
58 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
59 | void ft_striteri(char *s, void (*f)(unsigned int, char*));
60 |
61 | //File descriptor functions
62 | void ft_putchar_fd(char c, int fd);
63 | void ft_putstr_fd(char *s, int fd);
64 | void ft_putendl_fd(char *s, int fd);
65 | void ft_putnbr_fd(int n, int fd);
66 |
67 | //3)Linked list struct
68 | typedef struct s_list
69 | {
70 | void *content;
71 | struct s_list *next;
72 | } t_list;
73 |
74 | //BONUS Linked list Prototypes
75 | t_list *ft_lstnew(void *content);
76 | void ft_lstadd_front(t_list **lst, t_list *new);
77 | int ft_lstsize(t_list *lst);
78 | t_list *ft_lstlast(t_list *lst);
79 | void ft_lstadd_back(t_list **lst, t_list *new);
80 | void ft_lstdelone(t_list *lst, void (*del)(void*));
81 | void ft_lstclear(t_list **lst, void (*del)(void*));
82 | void ft_lstiter(t_list *lst, void (*f)(void *));
83 | t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
84 |
85 | #endif
86 |
--------------------------------------------------------------------------------
/ft_lstadd_back.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_lstadd_back.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/26 11:37:20 by Oceano #+# #+# */
9 | /* Updated: 2023/01/31 18:48:06 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | /*
14 | * Adds a new node to the end of a linked list data structure.
15 | *
16 | * The function takes two parameters, lst and new.
17 | * ~lst is a pointer to a pointer to the first node of the list,
18 | * ~new is a pointer to the new node that needs to be added to the list.
19 | *
20 | * 1)Uses the ft_lstlast function to find the last node of the list,
21 | * 2)Then sets the next pointer of the last node to point to the new node,
22 | * and the next pointer of the new node to NULL,
23 |
24 | Here is a breakdown of how it works:
25 |
26 | ~The ft_lstlast(*lst) call returns a pointer
27 | to the last node of the list.
28 | ~last->next = new adds the new node to the end
29 | of the list by making the last node point to it.
30 | ~new->next = NULL ensures that the new node is the
31 | last node of the list by setting its next pointer to NULL.
32 | *
33 | * CONTROLS
34 | *if (!lst) check if the pointer lst is null,
35 | if it's true the function exits.
36 | if (!new) check if the pointer new is null,
37 | if it's true the function exits.
38 | TL;DR
39 | In summary, adding these control statements to check
40 | for null pointers before proceeding with the function
41 | makes it more robust and less prone to errors
42 | and it's a best practice to always check the
43 | validity of the pointers before dereferencing them.
44 | */
45 |
46 | #include "libft.h"
47 | //#include
48 |
49 | /*
50 | void ft_lstadd_back(t_list **lst, t_list *new)
51 | {
52 | t_list *curr;
53 |
54 | if (!lst)
55 | return ;
56 | if (*lst)
57 | {
58 | curr = ft_lstlast(*lst);
59 | curr->next = new;
60 | }
61 | else
62 | *lst = new;
63 | }
64 | */
65 |
66 | void ft_lstadd_back(t_list **lst, t_list *new)
67 | {
68 | t_list *last;
69 |
70 | if (NULL == lst)
71 | return ;
72 | if (NULL == *lst)
73 | {
74 | *lst = new;
75 | return ;
76 | }
77 | last = ft_lstlast(*lst);
78 | last->next = new;
79 | }
80 |
81 | /*
82 | int main()
83 | {
84 | t_list *head, *node_1, *node_2;
85 |
86 | head = malloc(sizeof(t_list));
87 | node_1 = malloc(sizeof(t_list));
88 | node_2 = malloc(sizeof(t_list));
89 |
90 | head->content = (void *)"hello";
91 | node_1->content = (void *)"world";
92 | node_2->content = (void *)"!";
93 |
94 | ft_lstadd_back(&head, node_1);
95 | ft_lstadd_back(&head, node_2);
96 |
97 | t_list *current;
98 |
99 | current = head;
100 | while (current)
101 | {
102 | printf("%s\n", (char *)(current->content));
103 | current = current->next;
104 | }
105 | }
106 | */
107 |
--------------------------------------------------------------------------------
/ft_memcpy.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_memcpy.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/18 13:55:17 by Oceano #+# #+# */
9 | /* Updated: 2023/01/31 19:06:34 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | //#include
14 | //#include
15 |
16 | /*
17 | *
18 | * memcpy is a C library function that is used to copy a
19 | * block of memory from one location to another.
20 | The function takes three arguments:
21 | ~a pointer to the destination memory block,
22 | ~a pointer to the source memory block,
23 | ~and the number of bytes to copy.
24 | *
25 | * RETURN VALUES
26 | * The memcpy() function returns the original value of dst.
27 | *
28 | * MEMCPY vs MEMMOVE
29 | * video-> https://www.youtube.com/watch?v=nFl1cNXk85s&t=397s
30 | * if the pointers overlap, the function memcpy may overwrite the original data,
31 | * that's why the function memmove should be used to
32 | * handle overlapping memory regions.
33 | *
34 | * IMPLEMENTATIONS
35 | void memmove ( void * dst, const void * src, size_t count )
36 | {
37 | if ((uintptr_t)src < (uintptr_t)dst) {
38 | // Copy from back to front
39 |
40 | } else if ((uintptr_t)dst < (uintptr_t)src) {
41 | // Copy from front to back
42 | }
43 | }
44 |
45 | void memcpy ( void * dst, const void * src, size_t count )
46 | {
47 | if ((uintptr_t)src != (uintptr_t)dst) {
48 | // Copy in any way you want
49 | }
50 | }
51 |
52 | if memcpy copies "front to back" and the memory blocks are aligned as this
53 | [---- src ----]
54 | [---- dst ---]
55 | copying the first byte of src to dst already destroys the content
56 | of the last bytes of src before these have been copied.
57 |
58 | Only copying "back to front" will lead to correct results.
59 | Now swap src and dst:
60 |
61 | [---- dst ----]
62 | [---- src ---]
63 |
64 | * TLDR
65 | * memcpy is typically implemented as a fast, raw memory copy operation
66 | * that simply copies data from one memory location to another,
67 | * without any checks for overlap between source
68 | * and destination memory regions.
69 | * This can lead to incorrect results if the source
70 | * and destination memory regions overlap.
71 | */
72 |
73 | #include "libft.h"
74 |
75 | void *ft_memcpy(void *dst, const void *src, size_t n)
76 | {
77 | int i;
78 |
79 | i = 0;
80 | if (NULL == src && NULL == dst)
81 | return (NULL);
82 | while (n--)
83 | *((unsigned char *)dst + i++) = *(unsigned char *)src++;
84 | return (dst);
85 | }
86 |
87 | /*
88 | void *ft_memcpy(void *dest, const void *src, size_t n)
89 | {
90 | unsigned char *ptr;
91 | const unsigned char *ptr2;
92 |
93 | if (dest == NULL && src == NULL)
94 | return (NULL);
95 | ptr = (unsigned char *)dest;
96 | ptr2 = (unsigned char *)src;
97 | while (n-- > 0)
98 | *(ptr++) = *(ptr2++);
99 | return ((void *)dest);
100 | }
101 | */
102 | /*
103 | int main(void)
104 | {
105 | char v[] = "hello";
106 | char v1[] = "World";
107 |
108 | printf("%s ->%p\n", v, ft_memcpy(v, v1, 3));
109 |
110 | printf("%s ->%p\n", v, memcpy(v, v1, 3));
111 | }
112 | */
113 |
--------------------------------------------------------------------------------
/ft_strlcat.c:
--------------------------------------------------------------------------------
1 | /* ************************************************************************** */
2 | /* */
3 | /* ::: :::::::: */
4 | /* ft_strlcat.c :+: :+: :+: */
5 | /* +:+ +:+ +:+ */
6 | /* By: Oceano +#+ +:+ +#+ */
7 | /* +#+#+#+#+#+ +#+ */
8 | /* Created: 2023/01/23 10:23:42 by Oceano #+# #+# */
9 | /* Updated: 2023/02/01 10:13:28 by Oceano ### ########.fr */
10 | /* */
11 | /* ************************************************************************** */
12 |
13 | /*
14 | * The function concatenates a copy of the null-terminated string src
15 | * to the end of the null-terminated string dest,
16 | * ensuring that the resulting string in dest is always null-terminated.
17 |
18 | *The function takes three parameters:
19 |
20 | ~dest: A pointer to the destination buffer.
21 | ~src: A pointer to the source string that will be concatenated
22 | to the end of the destination buffer.
23 | ~dstsize: The maximum size of the destination buffer,
24 | including the null-terminator.
25 | *
26 | * The function copies the source string
27 | * to the end of the destination string,
28 | * and ensures that the destination string
29 | * 🚨is not exceeded the maximum size provided.🚨
30 | *
31 | * If the destination string exceeds the maximum size,
32 | * 🚨the function does not add anything to the destination string.🚨
33 | *
34 | * RETURN
35 | * the initial length of dst plus the length of src.
36 | * If the return value is >= dstsize,
37 | * the output string has been truncated.
38 | *
39 | * if (dest_len > dstsize)
40 | * the function returns the sum of the length of the source string (src_len)
41 | * and the size of the destination buffer (dstsize).
42 | * This value represents the total length of the destination string
43 | * that would have been produced if there had been enough space in the buffer.
44 | */
45 |
46 | #include "libft.h"
47 | //#include
48 | //#include
49 | //#include
50 |
51 | size_t ft_strlcat(char *dst, const char *src, size_t dstsize)
52 | {
53 | size_t dest_len;
54 | size_t src_len;
55 |
56 | src_len = ft_strlen(src);
57 | if (NULL == dst && 0 == dstsize)
58 | return (src_len);
59 | dest_len = ft_strlen(dst);
60 | if (dest_len >= dstsize)
61 | return (src_len + dstsize);
62 | else
63 | dstsize -= dest_len;
64 | ft_strlcpy(dst + dest_len, src, dstsize);
65 | return (dest_len + src_len);
66 | }
67 |
68 | /*
69 | int main()
70 | {
71 | int dest_len;
72 | dest_len = 15;
73 | char dest[dest_len];
74 | int ret;
75 |
76 |
77 |
78 | for (int i = 0; i < 15; ++i)dest[i] = 0;
79 | memset(dest, 'r', 6);
80 | ret = ft_strlcat(dest, "lorem ipsum dolor sit amet", 15);
81 | printf("%s->%d\n", dest, ret);
82 | for (int i = 0; i < 15; ++i)dest[i] = 0;
83 | memset(dest, 'r', 6);
84 | ret = strlcat(dest, "lorem ipsum dolor sit amet", 15);
85 | printf("%s->%d\n", dest, ret);
86 |
87 |
88 | for (int i = 0; i < 15; ++i)dest[i] = 0;
89 | memset(dest, 'r', 6);
90 | ret = ft_strlcat(dest, "lorem ipsum dolor sit amet", 2);
91 | printf("%s->%d\n", dest, ret);
92 | for (int i = 0; i < 15; ++i)dest[i] = 0;
93 | memset(dest, 'r', 6);
94 | ret = strlcat(dest, "lorem ipsum dolor sit amet", 2);
95 | printf("%s->%d\n", dest, ret);
96 |
97 | for (int i = 0; i < 15; ++i)dest[i] = 0;
98 | memset(dest, 'r', 6);
99 | ft_strlcat(dest, "lorem ipsum dolor sit amet", 0);
100 | printf("%s\n", dest);
101 | for (int i = 0; i < 15; ++i)dest[i] = 0;
102 | memset(dest, 'r', 6);
103 | strlcat(dest, "lorem ipsum dolor sit amet", 0);
104 |
105 |
106 | }
107 | */
108 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | # **************************************************************************** #
2 | # #
3 | # ::: :::::::: #
4 | # Makefile :+: :+: :+: #
5 | # +:+ +:+ +:+ #
6 | # By: Oceano +#+ +:+ +#+ #
7 | # +#+#+#+#+#+ +#+ #
8 | # Created: 2023/01/27 20:30:16 by Oceano #+# #+# #
9 | # Updated: 2023/01/31 17:55:23 by Oceano ### ########.fr #
10 | # #
11 | # **************************************************************************** #
12 |
13 | # "YOUR MAKEFILE MUST NOT RELINK"
14 | # When a Makefile is being used to build a software project,
15 | # relinking is the process of linking object files together to create an executable.
16 | # The statement "your Makefile must not relink" means that the Makefile should not
17 | # perform the linking step again if the object files have not been modified since
18 | # the last time the executable was built. This is an important concept in Makefile
19 | # because it can save a lot of time when building large projects.
20 | # When make runs, it checks the timestamps of the dependencies and only rebuilds
21 | # the target if the dependencies are newer. If the dependencies of the executable
22 | # (object files) have not been modified, the executable does not need to be rebuilt.
23 | # In this way the makefile is efficient and don't need to relink the files that haven't
24 | # been modified, so the build process will be faster and the software will be ready to use faster.
25 |
26 | #Your Makefile must at least contain the rules $(NAME), all, clean, fclean and re.
27 |
28 | #To turn in bonuses to your project, you must include a rule bonus to your Makefile,
29 | #which will add all the various headers, librairies or functions that are forbidden on
30 | #the main part of the project. Bonuses must be in a different file _bonus.{c/h} if
31 | #the subject does not specify anything else. Mandatory and bonus part evaluation
32 | #is done separately
33 |
34 | #VARIBLES
35 |
36 | NAME = libft.a
37 | SRCS = ft_atoi.c \
38 | ft_bzero.c \
39 | ft_calloc.c \
40 | ft_isalnum.c \
41 | ft_isalpha.c \
42 | ft_isascii.c \
43 | ft_isdigit.c \
44 | ft_isprint.c \
45 | ft_itoa.c \
46 | ft_memccpy.c \
47 | ft_memchr.c \
48 | ft_memcmp.c \
49 | ft_memcpy.c \
50 | ft_memmove.c \
51 | ft_memset.c \
52 | ft_putchar_fd.c \
53 | ft_putendl_fd.c \
54 | ft_putnbr_fd.c \
55 | ft_putstr_fd.c \
56 | ft_split.c \
57 | ft_strchr.c \
58 | ft_strdup.c \
59 | ft_striteri.c \
60 | ft_strjoin.c \
61 | ft_strlcat.c \
62 | ft_strlcpy.c \
63 | ft_strlen.c \
64 | ft_strmapi.c \
65 | ft_strncmp.c \
66 | ft_strnstr.c \
67 | ft_strrchr.c \
68 | ft_strtrim.c \
69 | ft_substr.c \
70 | ft_tolower.c \
71 | ft_toupper.c
72 |
73 | SRCS_B = ft_lstadd_back.c \
74 | ft_lstadd_front.c \
75 | ft_lstclear.c \
76 | ft_lstdelone.c \
77 | ft_lstiter.c \
78 | ft_lstlast.c \
79 | ft_lstmap.c \
80 | ft_lstnew.c \
81 | ft_lstsize.c
82 |
83 | OBJS = $(SRCS:.c=.o)
84 | OBJS_B = $(SRCS_B:.c=.o)
85 | RM = rm -f
86 | LIBC = ar -rcs
87 | FLAGS = -Wall -Wextra -Werror
88 | INCS = .
89 |
90 | #"all files with the extension .c, build a file with the same name but with the extension .o".
91 | #$< is a special variable in makefiles that refers to the first prerequisite of the rule.
92 | #In this case, the prerequisite is the .c file that the rule is trying to build the .o file from.
93 | #It is not mentioned as prerequisites in the rule itself, but it is implied that the .c file
94 | #is the prerequisite for the .o file because of the pattern matching with the rule .c.o.
95 | #So when we run make command, make will look for the corresponding .c file to
96 | #the given .o file, and the $< will be replaced by the name of the .c file.
97 | #.c.o : is an example of an implicit rule in make.
98 | #In make, implicit rules are predefined rules that are used when no explicit rule
99 | #for a target can be found. They are used to build a target from a source file of a different type.
100 | #This particular rule .c.o : is an implicit rule that tells make how to build a .o file from a .c file, if no explicit rule is provided.
101 | #It is a pattern rule, that specifies how to create the target file (here .o) from the source file(here .c) by matching the pattern.
102 | #IT IS LIKE WRITING
103 | #%.o: %.c
104 | # ${CC} ${FLAGS} -c $< -o $@ -I${INCS}
105 |
106 | .c.o :
107 | ${CC} ${FLAGS} -c $< -o ${<:.c=.o} -I${INCS}
108 |
109 | $(NAME): ${OBJS}
110 | ${LIBC} $(NAME) $(OBJS)
111 |
112 | all: $(NAME)
113 |
114 | bonus: $(NAME) $(OBJS_B)
115 | ${LIBC} $(NAME) $(OBJS_B)
116 |
117 | fclean: clean
118 | $(RM) $(NAME) $(bonus)
119 |
120 | clean:
121 | $(RM) -f $(OBJS) $(OBJS_B)
122 |
123 | re: fclean all
124 |
125 | .PHONY: all bonus clean fclean re .c.o
126 |
127 | #The first line, NAME = libft.a, sets a variable called "NAME" to be the string "libft.a".
128 | # This variable is used later in the Makefile to specify the
129 | # name of the library file that will be created.
130 |
131 | #The next line, SRCS = ft_atoi.c \, sets a variable called "SRCS" to be a list of C files
132 | #that will be used to create the library.
133 | #The list includes files such as "ft_atoi.c", "ft_bzero.c", and so on.
134 | #Each file is separated by a backslash \ so that the list can continue on the next line.
135 |
136 | #The following line SRCS_B.., sets a variable called "SRCS_B" to be a list
137 | #of C files that will be used to create the bonus version of the library.
138 | #The list includes files such as "ft_lstadd_back.c", "ft_lstadd_front.c", and so on.
139 |
140 | #The next line OBJS = $(SRCS:.c=.o), sets a variable called "OBJS"
141 | #to be the list of object files that will be created from the C file
142 | #listed in the "SRCS" variable.
143 | #SUBSTITUTION REFERENCE
144 | # SYNTAX
145 | # ~$(VARIABLE_NAME:OLD_STRING=NEW_STRING)
146 | #The $(SRCS:.c=.o) is a shorthand way of saying
147 | # ~"replace the '.c' extension of the files in the 'SRCS' variable with '.o' ".
148 | #TL;DR
149 | #$(SRCS:.c=.o) is a makefile variable substitution.
150 |
151 | #The following line OBJS_B = $(SRCS_B:.c=.o), sets a variable called "OBJS_B"
152 | #to be the list of object files that will be created from the C files listed
153 | #in the "SRCS_B" variable.
154 | #The $(SRCS_B:.c=.o) is a shorthand way of saying
155 | # ~"replace the '.c' extension of the files in the 'SRCS_B' variable with '.o' ".
156 |
157 | #The next line RM = rm -f, sets a variable called "RM" to the string "rm -f".
158 | #This variable is used later in the Makefile as a shorthand way to delete files.
159 | #The -f flag is used to ignore nonexistent files and never prompt the user.
160 |
161 | #The following line LIBC = ar -rcs, sets a variable called "LIBC" to the string "ar -rcs".
162 | #This variable is used later in the Makefile as a shorthand way to create the library file.
163 | #The ar command is used to create, modify, and extract from archives.
164 | #TL;DR
165 | #ar rcs library.a file1.o file2.o
166 | #This command creates a new archive library called "library.a"
167 | #and adds the object files "file1.o" and "file2.o" to it. The options "r", "c", and "s"
168 | #are usedto insert new files, create a new archive, and write an index, respectively.
169 |
170 | #The next line FLAGS = -Wall -Wextra -Werror, sets a variable called "FLAGS"
171 | #to the string "-Wall -Wextra -Werror".
172 | #These flags are used when compiling the C files into object files.
173 |
174 | #The following line INCS = . sets a variable called "INCS" to the string ".".
175 | #This variable is used to specify the include path when compiling the C files into object files.
176 |
--------------------------------------------------------------------------------