Skip to content

NHariman/libft

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

64 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Codam Coding College Project: Libft

A recreation of some functions in the stdlib C library along with extra functions created by NHariman for their time at Codam Coding College.

Using the library

Clone the project:
git clone [repo_link] [folder]

Create the library by entering in the terminal/iterm:
make

To compile the library with the linked list functions use:
make bonus

The library will be created under the name libft.a. And can be compiled with C mains and other projects using:
gcc -Wall -Werror -Wextra main.c -L. -lft OR gcc -Wall -Werror -Wextra main.c libft.a

Summary

This project is a recreation of the stdlib C library along with some extra functions for string and array manipulation, linked list usage and fd reading and writing.

Overview of the functions in this library

Char content checkers

Function Name Description
int ft_isspecial(int c) returns 1 if c is a special character such as !@#$%^ etc.
int ft_isalnum(int c) returns 1 if c is a number or a character
int ft_isalpha(int c) returns a 1 if c is a letter
int ft_isascii(int c) returns a 1 if c is an ASCII value
int ft_isdigit(int c) returns a 1 if c is a number
int ft_isprint(int c) returns a 1 if c is a printable ASCII value
int ft_tolower(int c) converts an uppercase letter to a lowercase letter
int ft_istoupper(int c) converts a lowercase letter to an uppercase letter

Malloc functions

Function Name Description
void ft_bzero(void *s, size_t n) Fills a given void address of length n with 0s
void *ft_calloc(size_t count, size_t size) Creates a space in the memory of count length and size size

file descriptor functions

Function Name Description
void ft_putchar_fd(char c, int fd) Writes a character c to the file descriptor specified in fd
void ft_putstr_fd(char *s, int fd) Writes a char string to the file descriptor specified in fd
void ft_putendl_fd(char *s, int fd) writes a char string to the file descriptor specified in fd,
with a newline at the end.
void ft_putnbr_fd(int n, int fd) prints an int number onto the file descriptor specified in fd
int get_next_line(int fd, char **line) reads from file descriptor in fd and put its contents until a newline (\n)

Memory altering functions, stdlib mirrors

Function Name Description
void *ft_memset(void *b, int c, size_t len) function fills the first n bytes of the memory area pointed to by s with the constant byte c
void *ft_memcpy(void *dst, const void *src, size_t n) function copies n bytes from memory area src to memory area dest. The memory areas must not overlap.
void *ft_memccpy(void *dst, const void *src, int c, size_t n) function copies no more than n bytes from memory area src to memory area dest, stopping when the character c is found.
If the memory areas overlap, the results are undefined.
void *ft_memmove(void *dst, const void *src, size_t len) function copies n bytes from memory area src to memory area dest.
The memory areas may overlap: copying takes place as though the bytes in src are first copied into a temporary array that does not overlap src or dest, and the bytes are then copied from the temporary array to dest.
void *ft_memchr(const void *s, int c, size_t n) function scans the initial n bytes of the memory area
pointed to by s for the first instance of c. Both c and the bytes of the memory area pointed to by s are interpreted as unsigned char.
int ft_memcmp(const void *s1, const void *s2, size_t n) function compares the first n bytes (each interpreted as
unsigned char) of the memory areas s1 and s2.

String functions (part 1, compare/find/length)

Function Name Description
long ft_iswhitespaces(const char *str) Skips whitespaces such as spaces, tabs, etc. and reports back the amount of space it skipped.
int ft_strncmp(const char *s1, const char *s2, size_t n) function compares the two strings s1 and s2, except it compares only the first (at most) n bytes of s1 and s2.
int ft_strcasecmp(char *s1, char *s2) function compares the two strings s1 and s2, regardless of case.
int ft_strcmp(const char *s1, const char *s2) function compares the two strings s1 and s2.
char *ft_strchr(const char *s, int c) function returns a pointer to the first occurrence of the character c in the string s.
char *ft_strrchr(const char *s, int c) function returns a pointer to the last occurrence of the character c in the string s.
char *ft_strnstr(const char *haystack, const char *needle, size_t len) function locates the first occurrence of the null-ternated string little in the string big, where not more than len characters are searched. Characters that appear after a `\0' character are not searched.
size_t ft_strlen(const char *s) Counts length of string

String functions (part 2, manipulators)

Function Name Description
char *ft_strdup(const char *s1) function allocates sufficient memory for a copy of the string str, does the copy, and returns a pointer to it. The pointer may subsequently be used as an argument to the function free(3)
size_t ft_strlcpy(char *dst, const char *src, size_t dstsize) copies up to dstsize - 1 characters from the string src to dst, NUL-terminating the result if dstsize is not 0.
size_t ft_strlcat(char *dst, const char *src, size_t dstsize) appends string src to the end of dst. It will append at most dstsize - strlen(dst) - 1 characters. It will then NUL-terminate, unless dstsize is 0 or the original dst string was longer than dstsize (in practice this should not happen as it means that either dstsize is incorrect or that dst is not a proper string).
char *ft_substr(char const *s, unsigned int start, size_t len) 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_split(char const *s, char c) Allocates 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.
char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) pplies the function ’f’ to each character of the string ’s’ to create a new string resulting from successive applications of ’f’.
char *ft_strtrim(char const *s1, char const *set) Allocates and returns a copy of ’s1’ with the characters specified in ’set’ removed from the beginning and the end of the string.
char *ft_strtrimfree(char *s1, char const *set) Allocates and returns a copy of ’s1’ with the characters specified in ’set’ removed from the beginning and the end of the string. The s1 string is also freed.

String functions (part 3, joiners)

Function Name Description
char *ft_strjoin(char const *s1, char const *s2) Allocates and returns a new string, which is the result of the concatenation of ’s1’ and ’s2’.
char *gnl_strjoin(char *s1, char *s2) Allocates and returns a new string, which is the result of the concatenation of ’s1’ and ’s2’. And frees s1.
char *ft_strjointwo(char *s1, char *s2) Allocates and returns a new string, which is the result of the concatenation of ’s1’ and ’s2’. And frees s2.
char *ft_charjoin(char *str, char c) Allocates and returns a new string, which is the result of the concatenation of ’s1’ and char ’c’.
char *ft_make_single_char_str(char c) Allocates and returns a new string, which consists of a single character c.

Array functions

Function Name Description
int ft_arrlen(char **arr) Find the length of an array that is terminated with a NULL.
char **ft_empty_array(char *cmd) Create an array with a single string in it.
char **ft_arrdup(char **arr) Copies an array that is terminated with a NULL.
char **ft_add_arr_front(char **arr, char *input) Adds a new entry to the front of an existing array, terminated with NULL.
char **ft_add_arr_back(char **arr, char *input) Adds a new entry to the back of an existing array, terminated with NULL.
void ft_free_array(char **arr, int len) Frees an allocated array of length len and NULL terminated.
char **ft_empty_array(char *cmd) Create an array with a single string in it, terminated with NULL
char *ft_arr_to_str_nl(char **arr) Takes a NULL terminated char array and creates a string out of each entry, separated by newlines.
char *ft_arr_to_str_sp(char **arr) Takes a NULL terminated char array and creates a string out of each entry, separated by spaces.
char *ft_arr_to_str(char **arr) Takes a NULL terminated char array and creates a string out of each entry, separated by nothing.

Linked list functions

Function Name Description
t_list *ft_lstnew(void *content) Allocates with malloc and returns a new element. The variable ’content’ is initialized with the value of the parameter ’content’. The variable ’next’ is initialized to NULL.
void ft_lstadd_front(t_list **lst, t_list *new) Adds the element ’new’ at the beginning of the list.
int ft_lstsize(t_list *lst) Counts the number of elements in a list
t_list *ft_lstlast(t_list *lst) Returns the last element of the list.
void ft_lstadd_back(t_list **lst, t_list *new) Adds the element ’new’ at the end of the list.
void ft_lstdelone(t_list *lst, void (*del)(void *)) 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_lstclear(t_list **lst, void (*del)(void *)) 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_lstiter(t_list *lst, void (*f)(void *)) Iterates the list ’lst’ and applies the function ’f’ to the content of each element.
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) 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.

printf's putchar and putstr.

Function Name Description
void pft_putchar_fd(char c, int fd, int *count) outputs a particular char to file descriptor fd and counts one to the return of printf.
void pft_putstr_fd(char *s, int fd, int *count) outputs a particular string to file descriptor fd and counts the len of the printed string to the return of printf.

Printfs (printf/printf_err/dprintf)

Function Name Description
int ft_printf(const char *format, ...) The functions in the printf() family produce output according to a format as described below. The function printf() write output to stdout, the standard output stream
int ft_dprintf(int fd, const char *format, ...) The functions in the printf() family produce output according to a format as described below. The function dprintf() write output to a particular fd.
int ft_printf_err(const char *format, ...) The functions in the printf() family produce output according to a format as described below. The function dprintf() write output to the standard errror (stderr).

Misc functions

Function Name Description
ft_atoi(const char *str) Converts an array of numbers into a valid integer.
ft_itoa(int n) Converts an integer into an array of numbers

About

Codam Coding College: libft project, with extra features.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published