Skip to content

Latest commit

History

20 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Libft

This is my own implementation of some of the most used and basic funcations in C

Including recreating more than 22 functions from libc standard library and a couple from other libraries in C, 44 functions in total to be used in my future project to get a better understanding of how they fundamentally work.

The first 22 function:

• isalpha • isdigit • isalnum • isascii • isprint • toupper • tolower • strchr • strrchr • strncmp • strlen • memset • bzero • memcpy • memmove • strlcpy • strlcat • memchr • memcmp • strnstr • atoi • calloc • strdup

The rest of the functions:

Function nameft_substr
Prototypechar *ft_substr(char const *s, unsigned int start, size_t len);
Parameterss: The string from which to create the substring.
start: The start index of the substring in the string ’s’.
len: The maximum length of the substring.
Return valueThe substring.
NULL if the allocation fails.
External functsmalloc
DescriptionAllocates (with malloc(3)) and returns a substring\from the string ’s’.
The substring begins at index ’start’ and is of maximum size ’len’.
Function nameft_strjoin
Prototypechar *ft_strjoin(char const *s1, char const *s2);
Parameterss1: The prefix string.
s2: The suffix string.
Return valueThe new string.
NULL if the allocation fails.
External functsmalloc
DescriptionAllocates (with malloc(3)) and returns a new string,
which is the result of the concatenation of ’s1’ and ’s2’.
Function nameft_strtrim
Prototypechar *ft_strtrim(char const *s1, char const *set);
Parameterss1: The string to be trimmed.
set: The reference set of characters to trim.
Return valueThe trimmed string.
NULL if the allocation fails.
External functsmalloc
DescriptionAllocates (with malloc(3)) and returns a copy of
’s1’ with the characters specified in ’set’ removed
from the beginning and the end of the string.
Function nameft_split
Prototypechar **ft_split(char const *s, char c);
Parameterss: The string to be split.
c: The delimiter character.
Return valueThe array of new strings resulting from the split.
NULL if the allocation fails.
External functsmalloc, free
DescriptionAllocates (with malloc(3)) and returns an array
of strings obtained by splitting ’s’ using the
character ’c’ as a delimiter. The array must end
with a NULL pointer.
Function nameft_itoa
Prototypeachar *ft_itoa(int n);
Parametersn: the integer to convert.
Return valueThe string representing the integer.
NULL if the allocation fails.
External functsmalloc
DescriptionAllocates (with malloc(3)) and returns a string
representing the integer received as an argument.
Negative numbers must be handled.
Function nameft_strmapi
Prototypechar *ft_strmapi(char const *s, char (*f)(unsigned int, char));
Parameterss: The string on which to iterate.
f: The function to apply to each character.
Return valueThe string created from the successive applications of ’f’.
Returns NULL if the allocation fails.
External functsmalloc
DescriptionApplies the function ’f’ to each character of the
string ’s’, and passing its index as first argument
to create a new string (with malloc(3)) resulting
from successive applications of ’f’.
Function nameft_striteri
Prototypevoid ft_striteri(char *s, void (f)(unsigned int,char));
Parameterss: The string on which to iterate.
f: The function to apply to each character.
Return valueNone
External functsNone
DescriptionApplies the function ’f’ on each character of
the string passed as argument, passing its index
as first argument. Each character is passed by
address to ’f’ to be modified if necessary.
Function nameft_putchar_fd
Prototypevoid ft_putchar_fd(char c, int fd);
Parametersc: The character to output.
fd: The file descriptor on which to write.
Return valueNone
External functswrite
DescriptionOutputs the character ’c’ to the given file descriptor.
Function nameft_putstr_fd
Prototypevoid ft_putstr_fd(char *s, int fd);
Parameterss: The string to output.
fd: The file descriptor on which to write.
Return valueNone
External functsswrite
DescriptionOutputs the string ’s’ to the given file descriptor.
Function nameft_putendl_fd
Prototypevoid ft_putendl_fd(char *s, int fd);
Parameterss: The string to output.
fd: The file descriptor on which to write.
Return valueNone
External functswrite
DescriptionOutputs the string ’s’ to the given file descriptor
followed by a newline.
Function nameft_putnbr_fd
Prototypevoid ft_putnbr_fd(int n, int fd);
Parametersn: The integer to output.
fd: The file descriptor on which to write.
Return valueNone
External functswrite
DescriptionOutputs the integer ’n’ to the given file descriptor.

The functions that get complied when you use the bonus rule in the Makefile

Function nameft_lstnew
Prototypet_list *ft_lstnew(void *content);
Parameterscontent: The content to create the node with.
Return valueThe new node
External functsmalloc
DescriptionAllocates (with malloc(3)) and returns a new node.
The member variable ’content’ is initialized with
the value of the parameter ’content’. The variable
’next’ is initialized to NULL.
Function nameft_lstadd_front
Prototypevoid ft_lstadd_front(t_list **lst, t_list *new);
Parameterslst: The address of a pointer to the first link of a list.
new: The address of a pointer to the node to be added to the list.
Return valueNone
External functsNone
DescriptionAdds the node ’new’ at the beginning of the list.
Function nameft_lstsize
Prototypeint ft_lstsize(t_list *lst);
Parameterslst: The beginning of the list.
Return valueThe length of the list
External functsNone
DescriptionCounts the number of nodes in a list.
Function nameft_lstlast
Prototypet_list *ft_lstlast(t_list *lst);
Parameterslst: The beginning of the list.
Return valueLast node of the list
External functsNone
DescriptionReturns the last node of the list.
Function nameft_lstadd_back
Prototypevoid ft_lstadd_back(t_list **lst, t_list *new);
Parameterslst: The address of a pointer to the first link of a list.
new: The address of a pointer to the node to be
added to the list.
Return valueNone
External functsNone
DescriptionAdds the node ’new’ at the end of the list.
Function nameft_lstdelone
Prototypevoid ft_lstdelone(t_list *lst, void (del)(void));
Parameterslst: The node to free.
del: The address of the function used to delete
the content.
Return valueNone
External functsfree
DescriptionTakes as a parameter a node and frees the memory of
the node’s content using the function ’del’ given
as a parameter and free the node. The memory of
’next’ must not be freed.
Function nameft_lstclear
Prototypevoid ft_lstclear(t_list **lst, void (del)(void));
Parameterslst: The address of a pointer to a node.
del: The address of the function used to delete
the content of the node.
Return valueNone
External functsfree
DescriptionDeletes and frees the given node and every
successor of that node, using the function ’del’
and free(3).
Finally, the pointer to the list must be set to
NULL.
Function nameft_lstiter
Prototypevoid ft_lstiter(t_list *lst, void (*f)(void *));
Parameterslst: The address of a pointer to a node.
f: The address of the function used to iterate on
the list.
Return valueNone
External functsNone
DescriptionIterates the list ’lst’ and applies the function
’f’ on the content of each node.
Function nameft_lstmap
Prototypet_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
Parameterslst: The address of a pointer to a node.
f: The address of the function used to iterate on
the list.
del: The address of the function used to delete
the content of a node if needed.
Return valueThe new list.
NULL if the allocation fails.
External functsmalloc, free
DescriptionIterates the list ’lst’ and applies the function
’f’ on the content of each node. Creates a new
list resulting of the successive applications of
the function ’f’. The ’del’ function is used to
delete the content of a node if needed.

How to install:

clone the repository in the desired directory.

git clone https://github.com/SolarisCode/Libft.git

Then use make inside the pervious directory either with all or bonus rule if you want to use the bonus functions.

make all

or

make bonus

The pervious command will result in the static library file libft.a and you can include it with the header file libft.h while you complie your own programs like below:

gcc main.c -L"the directory that has the library" -I"the directory that has the include file" -lft

Feel free to contact me if you have a better way to implement any of the above functions :)

About

This is a library of my own implementation of some of the most used and basic funcations in C library libc, with some needed functions for my future projects.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Contributors

Languages