tgrekov-libft
HIVE libft Oct 2023
Loading...
Searching...
No Matches
libft.h File Reference

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tgrekov <tgrekov@student.hive.fi> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/23 14:26:57 by tgrekov #+# #+# */
/* Updated: 2023/12/12 09:35:47 by tgrekov ### ########.fr */
/* */
/* ************************************************************************** */

Definition in file libft.h.

#include <stdlib.h>
#include <unistd.h>
Include dependency graph for libft.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  s_list
 Linked list node. More...
 

Macros

#define FT_LONG_MAX   9223372036854775807L
 Not allowed to include limits.h.
 

Typedefs

typedef struct s_list t_list
 Linked list node.
 

Functions

int ft_atoi (const char *str)
 Converts ASCII string str to integer representation.
 
void ft_bzero (void *s, size_t n)
 Write n zeroes to byte string at pointer s.
 
void * ft_calloc (size_t count, size_t size)
 Allocates count * size bytes with malloc and returns a pointer to the result.
 
int ft_isalnum (int c)
 Is c an alphanumeric character.
 
int ft_isalpha (int c)
 Is c an alphabetical character.
 
int ft_isascii (int c)
 Is c in the ASCII character range.
 
int ft_isdigit (int c)
 Is c a numeric character.
 
int ft_isprint (int c)
 Is c a printable character.
 
char * ft_itoa (int n)
 Converts integer n to an allocated ASCII string.
 
void ft_lstiter (t_list *lst, void(*f)(void *))
 Iterates over a list and applies the function f over each node.
 
void ft_lstadd_back (t_list **lst, t_list *new)
 Adds node at pointer new to the end of the list starting at pointer pointed to by lst.
 
void ft_lstadd_front (t_list **lst, t_list *new)
 Insert node new into the start of the list pointed to by the pointer at lst.
 
void ft_lstclear (t_list **lst, void(*del)(void *))
 Delete every node in a list along with it's content.
 
void ft_lstdelone (t_list *lst, void(*del)(void *))
 Delete a node and it's content.
 
t_listft_lstlast (t_list *lst)
 Get the last node in a list.
 
t_listft_lstmap (t_list *lst, void *(*f)(void *), void(*del)(void *))
 Iterate over a list and create a new one from the results of applying f to the content of each node.
 
t_listft_lstnew (void *content)
 Allocates a new node, set it's content, and return it's address.
 
int ft_lstsize (t_list *lst)
 Counts the number of nodes in a list.
 
void * ft_memchr (const void *s, int c, size_t n)
 Finds first occurence of c in byte string at s within n bytes.
 
int ft_memcmp (const void *s1, const void *s2, size_t n)
 Compares n bytes of byte string s1 against byte string s2.
 
void * ft_memcpy (void *dst, const void *src, size_t n)
 Copies n bytes from byte string src to byte string dst.
 
void * ft_memmove (void *dst, const void *src, size_t len)
 Copies len bytes from byte string src to byte string dst non-destructively.
 
void * ft_memset (void *b, int c, size_t len)
 Fills len bytes of byte string b with value c.
 
void ft_putchar_fd (char c, int fd)
 Write character c to file descriptor fd.
 
void ft_putendl_fd (char *s, int fd)
 Write string at pointer s followed by a newline to file descriptor fd.
 
void ft_putnbr_fd (int n, int fd)
 Converts integer n to it's ASCII representation and writes it to file descriptor fd.
 
void ft_putstr_fd (char *s, int fd)
 Write string at pointer s to file descriptor fd.
 
char ** ft_split (char const *s, char c)
 Split string s by characer c into allocated array of strings, with NULL as the last element.
 
char * ft_strchr (const char *s, int c)
 Finds first occurence of character c in string s.
 
char * ft_strdup (const char *s1)
 Allocates memory for, copies to, and returns a duplicate of s1.
 
void ft_striteri (char *s, void(*f)(unsigned int, char *))
 Applies function f to each character of string s.
 
char * ft_strjoin (char const *s1, char const *s2)
 Allocates enough space for and appends string s2 to string s1 and returns the new string.
 
size_t ft_strlcat (char *dst, const char *src, size_t dstsize)
 Appends at most dstsize - ft_strlen(dst) - 1 characters of string src to the end of string dst.
 
size_t ft_strlcpy (char *dst, const char *src, size_t dstsize)
 Copy at most dstsize - 1 characters from string src into dst.
 
size_t ft_strlen (const char *str)
 Get length of str.
 
char * ft_strmapi (char const *s, char(*f)(unsigned int, char))
 Allocates and creates a new string from the output of function f processing each character of string s.
 
int ft_strncmp (const char *s1, const char *s2, size_t n)
 Compare up to n characters of string s1 with string s2.
 
char * ft_strnstr (const char *haystack, const char *needle, size_t len)
 Finds first occurence of needle in string haystack.
 
char * ft_strrchr (const char *s, int c)
 Finds last occurence of c in string at s.
 
char * ft_strtrim (char const *s1, char const *set)
 Allocates and returns a copy of s1 with any successive characters found in string set removed from the start and end.
 
char * ft_substr (char const *s, unsigned int start, size_t len)
 Allocates and returns a substring of s, starting at s + start, and ending no later than s + start + len.
 
int ft_tolower (int c)
 Convert uppercase letter to corresponding lowercase letter.
 
int ft_toupper (int c)
 Convert lowercase letter to corresponding uppercase letter.
 

Macro Definition Documentation

◆ FT_LONG_MAX

#define FT_LONG_MAX   9223372036854775807L

Not allowed to include limits.h.

Definition at line 29 of file libft.h.

Function Documentation

◆ ft_atoi()

int ft_atoi ( const char *  str)

Converts ASCII string str to integer representation.

Converts the initial numerical portion of the string at pointer str to int representation.

Numerical portion may be preceded by a singular optional + or - sign.

Parameters
[in]strNull-terminated string containing integer
Return values
intResult of conversion, unless the value (handled as a long during conversion), would overflow or underflow, in which case it returns FT_LONG_MAX or FT_LONG_MAX - 1, respectively.

Definition at line 42 of file ft_atoi.c.

References ft_isdigit(), and FT_LONG_MAX.

Here is the call graph for this function:

◆ ft_bzero()

void ft_bzero ( void *  s,
size_t  n 
)

Write n zeroes to byte string at pointer s.

Parameters
[in]sPointer to start of byte string
[in]nNumber of zeroes to write

Definition at line 28 of file ft_bzero.c.

Here is the caller graph for this function:

◆ ft_calloc()

void * ft_calloc ( size_t  count,
size_t  size 
)

Allocates count * size bytes with malloc and returns a pointer to the result.

Parameters
[in]countNumber of objects to allocate for
[in]sizeSize of each object
Return values
void*Freeable pointer to allocated memory after it is filled with zeroes
If either count or size are 0, returns ft_calloc(1, 1).
If the multiplication of count and size overflows, returns NULL.

Definition at line 34 of file ft_calloc.c.

References ft_bzero(), and ft_calloc().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ft_isalnum()

int ft_isalnum ( int  c)

Is c an alphanumeric character.

Parameters
[in]cCharacter to test
Return values
intft_isalpha(c) || ft_isdigit(c)

Definition at line 29 of file ft_isalnum.c.

References ft_isalpha(), and ft_isdigit().

Here is the call graph for this function:

◆ ft_isalpha()

int ft_isalpha ( int  c)

Is c an alphabetical character.

Parameters
[in]cCharacter to test
Return values
int1 if c is within either of the ranges A - Z or a - z, otherwise 0

Definition at line 27 of file ft_isalpha.c.

Here is the caller graph for this function:

◆ ft_isascii()

int ft_isascii ( int  c)

Is c in the ASCII character range.

Parameters
[in]cCharacter to test
Return values
int1 if c is within the range 0 - 127, otherwise 0

Definition at line 26 of file ft_isascii.c.

◆ ft_isdigit()

int ft_isdigit ( int  c)

Is c a numeric character.

Parameters
[in]cCharacter to test
Return values
int1 if c is within the range 0 - 9, otherwise 0

Definition at line 26 of file ft_isdigit.c.

Here is the caller graph for this function:

◆ ft_isprint()

int ft_isprint ( int  c)

Is c a printable character.

Parameters
[in]cCharacter to test
Return values
int1 if c is within the range ' ' - ~

Definition at line 26 of file ft_isprint.c.

◆ ft_itoa()

char * ft_itoa ( int  n)

Converts integer n to an allocated ASCII string.

Parameters
[in]nInteger to convert
Return values
char*Null-terminated string containing an ASCII representation of n, preceded by '-' if n is negative.
If allocation fails, returns NULL.

Definition at line 50 of file ft_itoa.c.

References ft_calloc().

Here is the call graph for this function:

◆ ft_lstadd_back()

void ft_lstadd_back ( t_list **  lst,
t_list new 
)

Adds node at pointer new to the end of the list starting at pointer pointed to by lst.

If lst is NULL, does nothing.
If pointer at lst is NULL, sets pointer at lst to new.
Otherwise sets next property of the last node in the list to new

Parameters
[in,out]lstPointer to pointer to first node in the list.
[in]newPointer to the new node to add

Definition at line 35 of file ft_lstadd_back_bonus.c.

References ft_lstlast(), and s_list::next.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ft_lstadd_front()

void ft_lstadd_front ( t_list **  lst,
t_list new 
)

Insert node new into the start of the list pointed to by the pointer at lst.

If lst or new are NULL, does nothing.
Sets next property of node at pointer new to address at pointer lst and sets pointer at lst to new

Parameters
[in,out]lstPointer to pointer to first node in the list.
[in]newPointer to the new node to add

Definition at line 34 of file ft_lstadd_front_bonus.c.

◆ ft_lstclear()

void ft_lstclear ( t_list **  lst,
void(*)(void *)  del 
)

Delete every node in a list along with it's content.

Parameters
[in,out]lstPointer to a pointer to the first node in the list
[in]delFunction used to properly handle the deletion of the node's content

Definition at line 31 of file ft_lstclear_bonus.c.

References ft_lstdelone(), and s_list::next.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ft_lstdelone()

void ft_lstdelone ( t_list lst,
void(*)(void *)  del 
)

Delete a node and it's content.

Parameters
[in,out]lstPointer to the first node in the list
[in]delFunction used to properly handle the deletion of the node's content

Definition at line 29 of file ft_lstdelone_bonus.c.

References s_list::content.

Here is the caller graph for this function:

◆ ft_lstiter()

void ft_lstiter ( t_list lst,
void(*)(void *)  f 
)

Iterates over a list and applies the function f over each node.

Parameters
[in]lstPointer to the first node in the list
[in]fFunction which accepts the content of a node

Definition at line 30 of file ft_lstiter_bonus.c.

References s_list::content, and s_list::next.

◆ ft_lstlast()

t_list * ft_lstlast ( t_list lst)

Get the last node in a list.

Parameters
[in]lstPointer to the first node in the list
Return values
t_list*Pointer to the last node in the list

Definition at line 28 of file ft_lstlast_bonus.c.

References s_list::next.

Here is the caller graph for this function:

◆ ft_lstmap()

t_list * ft_lstmap ( t_list lst,
void *(*)(void *)  f,
void(*)(void *)  del 
)

Iterate over a list and create a new one from the results of applying f to the content of each node.

Parameters
[in]lstPointer to the starting node
[in]fFunction which takes the content of the current node and returns the content for the new node
[in]delFunction used to properly handle deletion of each node's content in the new list, should allocation fail
Return values
t_list*Pointer to the first node in the new list

Definition at line 35 of file ft_lstmap_bonus.c.

References s_list::content, ft_lstadd_back(), ft_lstclear(), ft_lstnew(), and s_list::next.

Here is the call graph for this function:

◆ ft_lstnew()

t_list * ft_lstnew ( void *  content)

Allocates a new node, set it's content, and return it's address.

Parameters
[in]contentValue to set the content property to
Return values
t_list*Pointer to the allocated node

Definition at line 29 of file ft_lstnew_bonus.c.

References s_list::content, and s_list::next.

Here is the caller graph for this function:

◆ ft_lstsize()

int ft_lstsize ( t_list lst)

Counts the number of nodes in a list.

Parameters
[in]lstPointer to first node in the list
Return values
intLength of the list

Definition at line 28 of file ft_lstsize_bonus.c.

References s_list::next.

◆ ft_memchr()

void * ft_memchr ( const void *  s,
int  c,
size_t  n 
)

Finds first occurence of c in byte string at s within n bytes.

Check is done as unsigned char

Parameters
[in]sByte string haystack to search
[in]cint needle to find
[in]nNumber of bytes to check
Return values
void*Pointer to first occurence of needle c in haystack s.
NULL if none is found within n bytes.

Definition at line 33 of file ft_memchr.c.

◆ ft_memcmp()

int ft_memcmp ( const void *  s1,
const void *  s2,
size_t  n 
)

Compares n bytes of byte string s1 against byte string s2.

Comparisons are done with unsigned char.

Parameters
[in]s1First byte string to compare
[in]s2Second byte string to compare
[in]nNumber of bytes to compare
Return values
intResult of subtracting the differing byte of s2 from that of s1.
If no difference was found in n bytes, returns 0.

Definition at line 34 of file ft_memcmp.c.

◆ ft_memcpy()

void * ft_memcpy ( void *  dst,
const void *  src,
size_t  n 
)

Copies n bytes from byte string src to byte string dst.

Copy is always performed from end to start.
If your dst starts at any point after your src, even if they overlap, this function is safe.
If your dst starts prior to your src and overlaps it, or there is a possibility of this happening, use ft_memmove.

Parameters
[in]dstPointer to destination byte string
[in]srcPointer to source byte string
[in]nNumber of bytes to copy
Return values
void*dst

Definition at line 36 of file ft_memcpy.c.

Here is the caller graph for this function:

◆ ft_memmove()

void * ft_memmove ( void *  dst,
const void *  src,
size_t  len 
)

Copies len bytes from byte string src to byte string dst non-destructively.

src and dst may overlap in any manner, the function will switch copy directions to ensure the data is copied non-destructively.

Parameters
[in]dstPointer to destination byte string
[in]srcPointer to source byte string
[in]lenNumber of bytes to copy
Return values
void*dst

Definition at line 34 of file ft_memmove.c.

◆ ft_memset()

void * ft_memset ( void *  b,
int  c,
size_t  len 
)

Fills len bytes of byte string b with value c.

Parameters
[in,out]bPointer to start of byte string
[in]cInteger representation of value to write (cast to unsigned char)
[in]lenNumber of bytes to write
Return values
void*b

Definition at line 31 of file ft_memset.c.

◆ ft_putchar_fd()

void ft_putchar_fd ( char  c,
int  fd 
)

Write character c to file descriptor fd.

Parameters
[in]cCharacter to write
[in]fdFile descriptor to write to

Definition at line 28 of file ft_putchar_fd.c.

Here is the caller graph for this function:

◆ ft_putendl_fd()

void ft_putendl_fd ( char *  s,
int  fd 
)

Write string at pointer s followed by a newline to file descriptor fd.

Parameters
[in]sPointer to string to write
[in]fdFile descriptor to write to

Definition at line 29 of file ft_putendl_fd.c.

References ft_putchar_fd(), and ft_putstr_fd().

Here is the call graph for this function:

◆ ft_putnbr_fd()

void ft_putnbr_fd ( int  n,
int  fd 
)

Converts integer n to it's ASCII representation and writes it to file descriptor fd.

Parameters
[in]nInteger to convert
[in]fdFile descriptor to write to

Definition at line 29 of file ft_putnbr_fd.c.

References ft_putnbr_fd().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ft_putstr_fd()

void ft_putstr_fd ( char *  s,
int  fd 
)

Write string at pointer s to file descriptor fd.

Parameters
[in]sPointer to string to write
[in]fdFile descriptor to write to

Definition at line 28 of file ft_putstr_fd.c.

References ft_strlen().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ft_split()

char ** ft_split ( char const *  s,
char  c 
)

Split string s by characer c into allocated array of strings, with NULL as the last element.

Parameters
[in]sNull-terminated string to split
[in]cCharacter to split on
Return values
char**Allocated array of allocated string pointers, with NULL as the last element

Definition at line 72 of file ft_split.c.

References ft_calloc(), and ft_substr().

Here is the call graph for this function:

◆ ft_strchr()

char * ft_strchr ( const char *  s,
int  c 
)

Finds first occurence of character c in string s.

Check is done as char

Parameters
[in]sNull-terminated string haystack to search
[in]cint needle to find
Return values
char*Pointer to first occurence of needle c in haystack s.
NULL if none is found before the end of the string.

Definition at line 30 of file ft_strchr.c.

Here is the caller graph for this function:

◆ ft_strdup()

char * ft_strdup ( const char *  s1)

Allocates memory for, copies to, and returns a duplicate of s1.

Parameters
[in]s1Null-terminated string to copy
Return values
char*Null-terminated, duplicate string, result of malloc

Definition at line 28 of file ft_strdup.c.

References ft_memcpy(), and ft_strlen().

Here is the call graph for this function:

◆ ft_striteri()

void ft_striteri ( char *  s,
void(*)(unsigned int, char *)  f 
)

Applies function f to each character of string s.

This function iterates from end to start

Parameters
[in,out]sNull-terminated string to iterate over
[in]fPointer to a function where the first and second arguments are the index of the current character and a pointer to the current character

Definition at line 31 of file ft_striteri.c.

References ft_strlen().

Here is the call graph for this function:

◆ ft_strjoin()

char * ft_strjoin ( char const *  s1,
char const *  s2 
)

Allocates enough space for and appends string s2 to string s1 and returns the new string.

Parameters
[in]s1Null-terminated prefix string
[in]s2Null-terminated suffix string
Return values
char*Null-terminated combination of s1 and s2

Definition at line 30 of file ft_strjoin.c.

References ft_memcpy(), and ft_strlen().

Here is the call graph for this function:

◆ ft_strlcat()

size_t ft_strlcat ( char *  dst,
const char *  src,
size_t  dstsize 
)

Appends at most dstsize - ft_strlen(dst) - 1 characters of string src to the end of string dst.

Null-terminates dst if dstsize != 0 and ft_strlen(dst) < dstsize

Parameters
[in,out]dstNull-terminated string to append to
[in]srcNull terminated string to append from
[in]dstsizeMaximum size of the destination after appending
Return values
size_tLength of the string the function attempted to create

Definition at line 34 of file ft_strlcat.c.

References ft_strlen().

Here is the call graph for this function:

◆ ft_strlcpy()

size_t ft_strlcpy ( char *  dst,
const char *  src,
size_t  dstsize 
)

Copy at most dstsize - 1 characters from string src into dst.

Null-terminates dst if dstsize != 0

Parameters
[in,out]dstPointer to allocated memory of at least ft_strlen(src) or dstsize size, whichever is smaller
[in]srcOptionally null-terminated string string
[in]dstsizeNumber of characters to copy, should be no greater than the allocated space at dst
Return values
size_tLength of src

Definition at line 36 of file ft_strlcpy.c.

References ft_strlen().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ft_strlen()

size_t ft_strlen ( const char *  str)

Get length of str.

Parameters
[in]strString to get the length of
Return values
size_tLength of string starting at pointer str

Definition at line 28 of file ft_strlen.c.

Here is the caller graph for this function:

◆ ft_strmapi()

char * ft_strmapi ( char const *  s,
char(*)(unsigned int, char)  f 
)

Allocates and creates a new string from the output of function f processing each character of string s.

This function iterates from end to start

Parameters
[in]sNull-terminated string to iterate over
[in]fPointer to a function that returns each character of the new string individually, provided the index and value of each original character
Return values
char*Null-terminated string populated from successive applications of f. Result of malloc.

Definition at line 34 of file ft_strmapi.c.

References ft_calloc(), and ft_strlen().

Here is the call graph for this function:

◆ ft_strncmp()

int ft_strncmp ( const char *  s1,
const char *  s2,
size_t  n 
)

Compare up to n characters of string s1 with string s2.

Parameters
[in]s1Optionally null-terminated string
[in]s2Optionally null-terminated string
[in]nMaximum number of characters to compare
Return values
intResult of subtracting the differing character of s2 from that of s1.
If no difference was found in n characters, or before the strings were terminated, returns 0.

Definition at line 33 of file ft_strncmp.c.

◆ ft_strnstr()

char * ft_strnstr ( const char *  haystack,
const char *  needle,
size_t  len 
)

Finds first occurence of needle in string haystack.

Parameters
[in]haystackOptionally null-terminated string haystack to search
[in]needleNull-terminated string needle to find
[in]lenMaximum number of bytes to check
Return values
char*Pointer to the start of the first occurence of needle in haystack.
If not found within len characters or before termination of haystack, returns NULL.

Definition at line 33 of file ft_strnstr.c.

◆ ft_strrchr()

char * ft_strrchr ( const char *  s,
int  c 
)

Finds last occurence of c in string at s.

Check is done as char

Parameters
[in]sNull-terminated string haystack to search
[in]cint needle to find
Return values
char*Pointer to last occurence of needle c in haystack s.
NULL if none is found before the end of the string.

Definition at line 30 of file ft_strrchr.c.

◆ ft_strtrim()

char * ft_strtrim ( char const *  s1,
char const *  set 
)

Allocates and returns a copy of s1 with any successive characters found in string set removed from the start and end.

Parameters
[in]s1Null-terminated string to trim
[in]setNull-terminated string containing characters to trim off s1
Return values
char*Pointer to allocated trimmed string

Definition at line 31 of file ft_strtrim.c.

References ft_strchr(), ft_strlcpy(), and ft_strlen().

Here is the call graph for this function:

◆ ft_substr()

char * ft_substr ( char const *  s,
unsigned int  start,
size_t  len 
)

Allocates and returns a substring of s, starting at s + start, and ending no later than s + start + len.

Parameters
[in]sNull-terminated string to make a substring of
[in]startPosition from s at which to start
[in]lenMaximum length of the substring to make
Return values
char*Pointer to allocated null-terminated substring

Definition at line 32 of file ft_substr.c.

References ft_calloc(), ft_strlcpy(), and ft_strlen().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ft_tolower()

int ft_tolower ( int  c)

Convert uppercase letter to corresponding lowercase letter.

Parameters
[in]cCharacter to convert
Return values
intIf c is an uppercase letter character, returns the integer representation of the corresponding lowercase letter character, otherwise returns c.

Definition at line 28 of file ft_tolower.c.

◆ ft_toupper()

int ft_toupper ( int  c)

Convert lowercase letter to corresponding uppercase letter.

Parameters
[in]cCharacter to convert
Return values
intIf c is a lowercase letter character, returns the integer representation of the corresponding uppercase letter character, otherwise returns c.

Definition at line 28 of file ft_toupper.c.