LIBFT LIBRARY

A library used in c projects for the 42 foundation. Note that not all functions are fully done yet and they passed moulinette but aren't perfect. Changes are still to come!

I'm currently working on the syntax highlighting

ft_isalpha

Function that checks if a ascii character is part of the alphabet or not. Returns a 1 or 0 for use like a boolean

int	ft_isalpha(int i)
{
		if ((i >= 65 && i <= 90) || (i >= 97 && i <= 122))
			return (1);
		return (0);
}
ft_digit

Function that checks if a ascii character is a digit. Returns a 1 or 0 for use like a boolean

int	ft_isdigit(int i)
{
		if ('0' <= i && i <= '9')
			return (1);
		return (0);
}
ft_isalnum

Function that checks if a ascii character is a digit or in the alphabet. Returns a 1 or 0 for use like a boolean

int	ft_isalnum(int i)
{
		if (('0' <= i && i <= '9') || ('A' <= i && i <= 'Z')
					|| ('a' <= i && i <= 'z'))
			return (1);
		return (0);
}
ft_isascii

Function that checks if a character is part of ascii. Returns a 1 or 0 for use like a boolean

int	ft_isascii(int i)
{
		if (i >= 0 && i <= 127)
			return (1);
		return (0);
}
ft_isprint

Function that checks if the character is printable. Returns a 1 or 0 for use like a boolean

int	ft_isprint(int i)
{
		if (i >= 32 && i <= 126)
			return (1);
		return (0);
}
ft_strlen

Function that will get you the length of a string

size_t	ft_strlen(const char *str)
{
		int		i;
		i = 0
		while (str[i])
			i++;
		return (i);
}
ft_memset

Function that writes len bytes of value c (converted to an unsigned char) to the string b

void	*ft_memset(void *b, int c, size_t len)
{
		char	*placeholder;
		placeholder = (char *)b;
		while (len > 0)
		{
			placeholder[len - 1] = (unsigned char)c;
			len--;
		}
		return (b);
}
ft_bzero

Function that writes n zereod bytes to the string s. If n is zero then ft_bzero() does nothing

void	ft_bzero(void *s, size_t n)
{
		ft_memset(s, 0, n);
}
ft_memcpy

Function that copies n bytes from memory area src to memory area dst. If dst and src overlap, behavior is undefined. Applications in which dst and src might overlap should use memmove() instead.

void	*ft_memcpy(void *dest, const void *src, size_t n)
{
		char	*d;
		char	*s;
		size_t	i;
		d = (char *)dest;
		s = (char *)src;
		if (dest == NULL && src == NULL)
			return (NULL);
		i = 0;
		while (i < n)
		{
			d[i] = s[i];
			i++;
		}
		return (dest);
}
ft_memmove

Function that copies len bytes from string src to string dst. The two strings may overlap; the copy is always done in a non-destructive manner.

void	*ft_memmove(void *dst, const void *src, size_t n)
{
		size_t	len
		if (dst == NULL && src == NULL)
			return (NULL);
		if (dst < src)
			len = n;
			while (len > 0)
			{
				len--;
				((unsigned char *)dst)[len]
						= ((unsigned char *)src)[len];
			}
		}
		else
			{
				len = 0;
				while (len < n)
				{
					((unsigned char *)dst)[len]
							= ((unsigned char *)src)[len];
					len++;
				}
			}
		return (dst);
}
ft_strlcpy

Function that copies up to size - 1 characters from the NUL-terminated string src to dst, NULL-terminating the result.

size_t	ft_strlcpy(char *dst, const char *src, size_t size)
{
		size_t	i;
		i = 0;
		if (size == 0)
			return (ft_strlen(src));
		while (src[i] && i < size - 1)
		{
			dst[i] = src[i];
			i++;
		}
		if (i < size)
			dst[i] = '\0';
		while (src[i] != '\0')
			i++;
		return (i);
}
ft_strlcat

Function that appends the NUL-terminated string src to the end of dst. It will append at most size - strlen(dst) - 1 bytes, NUL-terminating the result.

size_t	ft_strlcat(char *dst, const char *src, size_t size)
{
		size_t	i;
		size_t	j;
		i = 0;
		j = 0;
		while (dst[i] && i < size)
			i++;
		while (src[j] && i + j + 1 < size)
		{
			dst[i + j] = src[j];
			j++;
		}
		if (i < size)
			dst[i + j] = '\0';
		return (i + ft_strlen(src));
}
ft_toupper

Function that converts a lower-case letter to the corresponding upper-case letter. The argument must be representable as an unsigned char or the value of EOF.

int	ft_toupper(int c)
{
	if (c >= 'a' && c <= 'z')
		return (c - 32);
	return (c);
}
ft_tolower

Function that converts an upper-case letter to the corresponding lower-case letter. The argument must be representable as an unsigned char or the value of EOF.

int	ft_tolower(int c)
{
	if (c >= 'A' && c <= 'Z')
		return (c + 32);
	return (c);
}
ft_strchr

Function that locates the first occurrence of c (converted to a char) in the string pointed to by s. The terminating null character is considered to be part of the string; therefore if c is '\0', the functions locate the terminating '\0'.

char	*ft_strchr(const char *s, int c)
{
		int	i;
		i = 0;
		while (s[i])
		{
			if (s[i] == (char)c)
				return ((char *)&s[i]);
			i++;
		}
		if (s[i] == (char)c)
			return ((char *)&s[i]);
		return (NULL);
}
ft_strrchr

Function that locates the last occurrence of c (converted to a char) in the string pointed to by s. The terminating null character is considered to be part of the string; therefore if c is '\0', the functions locate the terminating '\0'.

char	*ft_strrchr(const char *s, int c)
{
		int	i;
		char *t;
		i = 0;
		t = NULL;
		while (s[i])
		{
			if (s[i] == (char)c)
				t = (char *)&s[i];
			i++;
		}
		if (s[i] == (char)c)
			return ((char *)&s[i]);
		return (t);
}
ft_strncmp

Function that lexicographically compare the null-terminated strings s1 and s2. The function compares not more than n characters. Because ft_strncmp() is designed for comparing strings rather than binary data, characters that appear after a '\0' character are not compared.

int	ft_strncmp(const char *s1, const char *s2, size_t n)
{
		while(*s1 && *s1 == *s2 && n > 0)
		{
			s1++;
			s2++;
			n--;
		}
		if (n == 0)
			return (0);
		return (*(unsigned char *)s1 - *(unsigned char *)s2);
}
ft_memchr

Function that locates the first occurrence of c (converted to an unsigned char) in string s. Returns a pointer to the byte located, or NULL if no such byte exists within n bytes.

void	*ft_memchr(const void *s, int c, size_t n)
{
		size_t	i;
		char	*string;
		i = 0;
		string = (void *)s;
		while (i < n)
		{
			if (string[i] == (char)c)
				return (&string[i]);
			i++;
		}
		return (NULL);
}
ft_memcmp

Function that compares byte string s1 against byte string s2. Both strings are assumed to be n bytes long.

int	ft_memcmp(const void *s1, const void *s2, size_t n)
{
		unsigned char	*str1;
		unsigned char	*str2;
		str1 = (unsigned char *)s1;
		str2 = (unsigned char *)s2;
		while (n--)
		{
			if (*str1++ != *str2++)
				return (*--str1 - *--str2);
		}
		return (0);
}
ft_strnstr

Function that locates the first occurrence of the null-terminated string needle in the string haystack, where not more than len characters are searched. Characters that appear after a `\0' character are not searched.

char	*ft_strnstr(const char *haystack,
						const char *needle, size_t len)
{
	unsigned long	i;
	int				j;
	j = 0;
	i = 0;
	if (!*needle)
		return ((char *)haystack);
	while (haystack[i])
	{
		j = 0;
		while (haystack[i] == needle[j] &&
							 haystack[i] && i < len)
		{
			i++;
			j++;
		}
		if (!needle[j])
			return ((char *)&haystack[i - j]);
		i = (i - j) + 1;
	}
	return (NULL);
}
ft_atoi

Function that converts the initial portion of the string pointed to by str to int representation.

int	ft_atoi(const char *str)
{
	char	sign;
	int		i;
	int		result;
	i = 0;
	result = 0;
	sign = 1;
	while (str[i] && ((str[i] >= 9 &&
					 str[i] <= 13) || str[i] == 32))
		i++;
	if (str[i] == '+' || str[i] == '-')
	{
		if (str[i] == '-')
		{
			sign = -1;
		}
		i++;
	}
	while ((str[i] >= '0' && str[i] <= '9') && str[i])
	{
		result = result * 10 + (str[i] - 48);
		i++;
	}
	return (result * sign);
}
ft_calloc

Function that allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the allocated memory. The memory is set to zero.

void	*ft_calloc(size_t nmemb, size_t size)
{
	void	*ptr;
	ptr = malloc(nmemb * size);
	if (ptr == NULL)
		return (NULL);
	ft_bzero(ptr, nmemb * size);
	return (ptr);
}
ft_strdup

Function that allocates sufficient memory for a copy of the string s1, does the copy, and returns a pointer to it. The pointer may subsequently be used as an argument to the function free(3). If insufficient memory is available, NULL is returned and errno is set to ENOMEM.

char	*ft_strdup(const char *s1)
{
	char	*ptr;
	int		i;
	i = 0;
	ptr = malloc(ft_strlen(s1) + 1);
	if (ptr == NULL)
		return (NULL);
	while (s1[i])
	{
		ptr[i] = s1[i];
		i++;
	}
	ptr[i] = '\0';
	return (ptr);
}
ft_substr

Function that allocates (with malloc(3)) and returns a substring from the string ’s’. The substring begins at index ’start’ and is of maximum size ’len’.

char	*ft_substr(char const *s, unsigned int start,
			size_t len)
{
	char	*ptr;
	size_t	i;
	i = 0;
	if (s == NULL)
		return (NULL);
	if (start > ft_strlen(s))
		return (ft_strdup(""));
	ptr = malloc(len + 1);
	if (ptr == NULL)
		return (NULL);
	while (s[start] && i < len)
	{
		ptr[i] = s[start];
		i++;
		start++;
	}
	ptr[i] = '\0';
	return (ptr);
}
ft_strjoin

Function that allocates (with malloc(3)) and returns a new string, which is the result of the concatenation of ’s1’ and ’s2’.

char	*ft_strjoin(char const *s1, char const *s2)
{
	char	*ptr;
	size_t	i;
	size_t	j;
	i = 0;
	j = 0;
	if (s1 == NULL || s2 == NULL)
		return (NULL);
	ptr = malloc(ft_strlen(s1) + ft_strlen(s2) + 1);
	if (ptr == NULL)
		return (NULL);
	while (s1[i])
	{
		ptr[i] = s1[i];
		i++;
	}
	while (s2[j])
	{
		ptr[i + j] = s2[j];
		j++;
	}
	ptr[i + j] = '\0';
	return (ptr);
}
ft_strtrim

Function that allocates (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.

char	*ft_strtrim(char const *s1, char const *set)
{
	char	*ptr;
	size_t	i;
	size_t	j;
	size_t	k;
	i = 0;
	j = 0;
	k = 0;
	if (s1 == NULL || set == NULL)
		return (NULL);
	while (s1[i] && ft_strchr(set, s1[i]))
		i++;
	j = ft_strlen(s1) - 1;
	while (j > i && ft_strchr(set, s1[j]))
		j--;
	ptr = malloc(j - i + 2);
	if (ptr == NULL)
		return (NULL);
	while (i <= j)
		ptr[k++] = s1[i++];
	ptr[k] = '\0';
	return (ptr);
}
ft_split

Function that allocates (with malloc(3)) and returns an array of strings obtained by splitting ’s’ using the character ’c’ as a delimiter. The array must be ended by a NULL pointer.

static int	ft_count_words(char const *s, char c)
{
	int	i;
	int	count;
	i = 0;
	count = 0;
	while (s[i])
	{
		if (s[i] != c)
		{
			count++;
			while (s[i] != c && s[i])
				i++;
		}
		else
			i++;
	}
	return (count);
}
static int	ft_wordlen(char const *s, char c)
{
	int	i;
	i = 0;
	while (s[i] != c && s[i])
		i++;
	return (i);
}
char	**ft_split(char const *s, char c)
{
	char	**ptr;
	int		i;
	int		j;
	int		k;
	i = 0;
	j = 0;
	if (s == NULL)
		return (NULL);
	ptr = malloc(sizeof(char *) * (ft_count_words(s, c) + 1));
	if (ptr == NULL)
		return (NULL);
	while (s[i])
	{
		if (s[i] != c)
		{
			ptr[j] = malloc(ft_wordlen(&s[i], c) + 1);
			if (ptr[j] == NULL)
				return (NULL);
			k = 0;
			while (s[i] != c && s[i])
				ptr[j][k++] = s[i++];
			ptr[j++][k] = '\0';
		}
		else
			i++;
	}
	ptr[j] = NULL;
	return (ptr);
}
ft_itoa

Function that allocates (with malloc(3)) and returns a string representing the integer received as an argument. Negative numbers must be handled.

static int	ft_intlen(int n)
{
	int	i;
	i = 0;
	if (n < 0)
		i++;
	while (n / 10 != 0)
	{
		n = n / 10;
		i++;
	}
	return (i + 1);
}
char	*ft_itoa(int n)
{
	char	*ptr;
	int		i;
	long	nbr;
	i = ft_intlen(n);
	nbr = n;
	ptr = malloc(i + 1);
	if (ptr == NULL)
		return (NULL);
	ptr[i--] = '\0';
	if (nbr == 0)
		ptr[0] = '0';
	if (nbr < 0)
	{
		ptr[0] = '-';
		nbr = nbr * -1;
	}
	while (nbr > 0)
	{
		ptr[i--] = (nbr % 10) + 48;
		nbr = nbr / 10;
	}
	return (ptr);
}
ft_strmapi

Function that applies the function ’f’ to each character of the string ’s’ to create a new string (with malloc(3)) resulting from successive applications of ’f’.

char	*ft_strmapi(char const *s,
							char (*f)(unsigned int, char))
{
	char	*ptr;
	int		i;
	i = 0;
	if (s == NULL)
		return (NULL);
	ptr = malloc(ft_strlen(s) + 1);
	if (ptr == NULL)
		return (NULL);
	while (s[i])
	{
		ptr[i] = f(i, s[i]);
		i++;
	}
	ptr[i] = '\0';
	return (ptr);
}
ft_striteri

Function that applies the function ’f’ to each character of the string ’s’ passed as argument, and passing its index as first argument. Each character is passed by address to ’f’ to be modified if necessary.

void	ft_striteri(char *s, void (*f)(unsigned int, char *))
{
	unsigned int	i;
	i = 0;
	if (s == NULL || f == NULL)
		return ;
	while (s[i])
	{
		f(i, &s[i]);
		i++;
	}
}
ft_putchar_fd

Function that outputs the character ’c’ to the given file descriptor.

void	ft_putchar_fd(char c, int fd)
{
	write(fd, &c, 1);
}
ft_putstr_fd

Function that outputs the string ’s’ to the given file descriptor.

void	ft_putstr_fd(char *s, int fd)
{
	if (s == NULL)
		return ;
	write(fd, s, ft_strlen(s));
}
ft_putendl_fd

Function that outputs the string ’s’ to the given file descriptor, followed by a newline.

void	ft_putendl_fd(char *s, int fd)
{
	if (s == NULL)
		return ;
	write(fd, s, ft_strlen(s));
	write(fd, "\n", 1);
}
ft_putnbr_fd

Function that outputs the integer ’n’ to the given file descriptor.

void	ft_putnbr_fd(int n, int fd)
{
	long	nbr;
	nbr = n;
	if (nbr < 0)
	{
		ft_putchar_fd('-', fd);
		nbr = nbr * -1;
	}
	if (nbr > 9)
	{
		ft_putnbr_fd(nbr / 10, fd);
		ft_putnbr_fd(nbr % 10, fd);
	}
	else
		ft_putchar_fd(nbr + 48, fd);
}
ft_lstnew

Function that allocates (with malloc(3)) and returns a new element. The variable ’content’ is initialized with the value of the parameter ’content’. The variable ’next’ is initialized to NULL.

t_list	*ft_lstnew(void *content)
{
	t_list	*ptr;
	ptr = malloc(sizeof(t_list));
	if (ptr == NULL)
		return (NULL);
	ptr->content = content;
	ptr->next = NULL;
	return (ptr);
}
ft_lstadd_front

Function that adds the element ’new’ at the beginning of the list.

void	ft_lstadd_front(t_list **alst, t_list *new)
{
	if (alst == NULL || new == NULL)
		return ;
	new->next = *alst;
	*alst = new;
}
ft_lstsize

Function that counts the number of elements in a list.

int	ft_lstsize(t_list *lst)
{
	int		i;
	i = 0;
	while (lst != NULL)
	{
		lst = lst->next;
		i++;
	}
	return (i);
}
ft_lstlast

Function that returns the last element of the list.

t_list	*ft_lstlast(t_list *lst)
{
	if (lst == NULL)
		return (NULL);
	while (lst->next != NULL)
		lst = lst->next;
	return (lst);
}
ft_lstadd_back

Function that adds the element ’new’ at the end of the list.

void	ft_lstadd_back(t_list **alst, t_list *new)
{
	t_list	*ptr;
	if (alst == NULL || new == NULL)
		return ;
	ptr = *alst;
	if (ptr == NULL)
		*alst = new;
	else
	{
		ptr = ft_lstlast(ptr);
		ptr->next = new;
	}
}
ft_lstdelone

Function that takes as a parameter an element and frees the memory of the element’s content using the function ’del’ given as a parameter and free the element. The memory of ’next’ must not be freed.

void	ft_lstdelone(t_list *lst, void (*del)(void *))
{
	if (lst == NULL || del == NULL)
		return ;
	del(lst->content);
	free(lst);
}
ft_lstclear

Function that deletes and frees the given element and every successor of that element, using the function ’del’ and free(3). Finally, the pointer to the list must be set to NULL.

void	ft_lstclear(t_list **lst, void (*del)(void *))
{
	t_list	*ptr;
	if (lst == NULL || del == NULL)
		return ;
	while (*lst != NULL)
	{
		ptr = (*lst)->next;
		ft_lstdelone(*lst, del);
		*lst = ptr;
	}
}
ft_lstiter

Function that iterates the list ’lst’ and applies the function ’f’ to the content of each element.

void	ft_lstiter(t_list *lst, void (*f)(void *))
{
	if (lst == NULL || f == NULL)
		return ;
	while (lst != NULL)
	{
		f(lst->content);
		lst = lst->next;
	}
}
ft_lstmap

Function that iterates the list ’lst’ and applies the function ’f’ to the content of each element. Creates a new list resulting of the successive applications of the function ’f’. The ’del’ function is used to delete the content of an element if needed.

t_list	*ft_lstmap(t_list *lst, void *(*f)(void *),
			void (*del)(void *))
{
	t_list	*ptr;
	t_list	*new;
	if (lst == NULL || f == NULL)
		return (NULL);
	new = NULL;
	while (lst != NULL)
	{
		ptr = ft_lstnew(f(lst->content));
		if (ptr == NULL)
		{
			ft_lstclear(&new, del);
			return (NULL);
		}
		ft_lstadd_back(&new, ptr);
		lst = lst->next;
	}
	return (new);
}