libft 109 commits

readme

183 lines 8.4 kB

This project has been created as part of the 42 curriculum by mleitao.

Description

Libft is a custom C library that consists of reimplementations of standard C library functions along with additional utility functions for string, memory, data and file manipulation, as well as linked list operations.

The library is compiled into a static library archive (libft.a) and can be easily used in various C projects.

Instructions

Requirements

  • A C compiler, ideally clang.
  • make.

Makefile

CommandDescription
make, make all, make libft.aBuild the libft.a static library
make cleanRemoves object files from the working directory
make fcleanRemove the object files and the library
make reRebuild the library (equivalent to make fclean followed by make)

Usage

  1. Clone the repository:

    git clone ssh://git@codeberg.org/6times7/libft.git
    
  2. Build the library:

    make -C libft
    
  3. Include the header in your C file:

    #include "libft.h"
    
  4. Compile your program with the library:

    cc main.c -Lpath/to/library -lft
    

    Or explicitly:

    cc main.c path/to/libft.a
    

Example Usage

  1. Suppose you have a simple program main.c:

    #include "libft.h"
    #include <stdlib.h>
    
    int	main(void)
    {
        char	*s;
    
        s = ft_strdup("bleeeegh");
        if (!s)
     	   return (1);
        ft_putendl_fd(s, 1);
        free(s);
        return (0);
    }
    
  2. Compile it with the library:

    cc main.c -Lpath/to/library -lft -o yippie
    

    Or explicitly:

    cc main.c path/to/libft.a
    
  3. Run it:

    ./yippie
    

    Expected output:

    bleeeegh
    

Library Functions

Part I – Libc Functions


NamePrototypeDescription
ft_isalphaint ft_isalpha(int c);Checks if c is an alphabetic character
ft_isdigitint ft_isdigit(int c);Checks if c is a digit character
ft_isalnumint ft_isalnum(int c);Checks if c is an alphanumeric character
ft_isasciiint ft_isascii(int c);Checks if c is in ascii table
ft_isprintint ft_isprint(int c);Checks if c is a printable character
ft_strlensize_t ft_strlen(const char *s);Gets length of s
ft_memsetvoid *ft_memset(void *s, int c, size_t n);Fills s with n bytes c
ft_bzerovoid ft_bzero(void *s, size_t n);Fills s with n bytes '\0'
ft_memcpyvoid *ft_memcpy(void *dest, const void *src, size_t n);Copies n bytes from non-overlapping memory areas src to dest
ft_memmovevoid *ft_memmove(void *dest, const void *src, size_t n);Copies n bytes from src to a temporary buffer and from that buffer to dest
ft_strlcpysize_t ft_strlcpy(char *dst, const char *src, size_t size);Copies size - 1 characters from src into dst
ft_strlcatsize_t ft_strlcat(char *dst, const char *src, size_t size);Appends size - strlen(dst) - 1 bytes from src to dst
ft_toupperint ft_toupper(int c);Converts lowercase letter c to its uppercase equivalent
ft_tolowerint ft_tolower(int c);Converts uppercase letter c to its lowercase equivalent
ft_strchrchar *ft_strchr(const char *s, int c)Gets first occurence of c in s
ft_strrchrchar *ft_strrchr(const char *s, int c)Gets last occurence of c in s
ft_strncmpint ft_strncmp(const char *s1, const char *s2, size_t n)Compares the first n bytes of s1 and s2
ft_memchrvoid *ft_memchr(const void *s, int c, size_t n)Scans n bytes of s for first instance of c
ft_memcmpint ft_memcmp(const void *s1, const void *s2, size_t n)Compares the first n bytes of s1 and s2
ft_strnstrchar *ft_strnstr(const char *big, const char *little, size_t len)Locates little in big where no more than len characters are searched
ft_atoiint ft_atoi(const char *nptr)Converts nptr to int
ft_callocvoid *ft_calloc(size_t nmemb, size_t size);Allocates nmemb * size and initializes it to 0
ft_strdupchar *ft_strdup(const char *s);Duplicates s into heap

Part II – Additional Functions


NamePrototypeDescription
ft_substrchar *ft_substr(char const *s, unsigned int start, size_t len);Gets substring from s starting at index start and has maximum length len
ft_strjoinchar *ft_strjoin(char const *s1, char const *s2);Concatenates s1 and s2 into a new string
ft_strtrimchar *ft_strtrim(char const *s1, char const *set);Removes set from beginning and end of s1
ft_splitchar **ft_split(char const *s, char c);Gets array of new strings that result from splitting s using c
ft_itoachar *ft_itoa(int n);converts n to string
ft_strmapichar *ft_strmapi(char const *s, char (*f)(unsigned int, char));Gets a new string resulting from the application of f in every character of s
ft_striterivoid ft_striteri(char *s, void (*f)(unsigned int, char*));Applies f to every character in s
ft_putchar_fdvoid ft_putchar_fd(char c, int fd);Outputs c to file descriptor fd
ft_putstr_fdvoid ft_putstr_fd(char *s, int fd);Outputs s to file descriptor fd
ft_putendl_fdvoid ft_putendl_fd(char *s, int fd);Outputs s followed by \n to file descriptor fd
ft_putnbr_fdvoid ft_putnbr_fd(int n, int fd);Outputs n to file descriptor fd

Part III – Linked Lists


NamePrototypeDescription
ft_lstnewt_list *ft_lstnew(void *content);Creates new node with content content and next NULL
ft_lstadd_frontvoid ft_lstadd_front(t_list **lst, t_list *new);Adds node new at the beginning of lst
ft_lstsizeint ft_lstsize(t_list *lst);Counts number of nodes in lst
ft_lstlastt_list *ft_lstlast(t_list *lst);Gets last node of lst
ft_lstadd_backvoid ft_lstadd_back(t_list **lst, t_list *new);Adds node new at the end of lst
ft_lstdelonevoid ft_lstdelone(t_list *lst, void (*del)(void*));Frees the node lst and its content, latter with del
ft_lstclearvoid ft_lstclear(t_list **lst, void (*del)(void*));Deletes all the nodes starting at the one pointed to by lst and frees their contents
ft_lstitervoid ft_lstiter(t_list *lst, void (*f)(void *));Applies f iteractively to the list lst
ft_lstmapt_list *ft_lstmap(t_list *lst, void *(*f)(void *),void (*del)(void *));Gets new list resulting of the application of f to list lst iteractively

Resources

References

  • man pages
  • Peer discussions

LLM Usage

LLMs were used for:

  • General guidance during project development, primarily to make code more idiomatic
  • Parts of this README

Tests

This project was tested against libftTester. Kudos to @Tripouille for providing this test suite.

  1*This project has been created as part of the 42 curriculum by mleitao.*
  2
  3<h1 id="description">Description</h1>
  4
  5**Libft** is a custom C library that consists of reimplementations of standard C library functions along with additional utility functions for string, memory, data and file manipulation, as well as linked list operations.
  6
  7The library is compiled into a static library archive (`libft.a`) and can be easily used in various C projects.
  8
  9<h1 id="instructions">Instructions</h1>
 10
 11<h3 id="requirements">Requirements</h3>
 12
 13- A C compiler, ideally `clang`.
 14- `make`.
 15
 16<h3 id="makefile">Makefile</h3>
 17
 18Command | Description
 19--|--
 20`make`, `make all`, `make libft.a` | Build the `libft.a` static library
 21`make clean` | Removes object files from the working directory
 22`make fclean` | Remove the object files and the library
 23`make re` | Rebuild the library (equivalent to `make fclean` followed by `make`)
 24
 25<h3 id="usage">Usage</h3>
 26   
 271. Clone the repository:
 28   ```bash
 29   git clone ssh://git@codeberg.org/6times7/libft.git
 30   ```
 31
 322. Build the library:
 33   ```bash
 34   make -C libft
 35   ```
 36
 373. Include the header in your C file:
 38   ```c
 39   #include "libft.h"
 40   ```
 41
 424. Compile your program with the library:
 43   ```bash
 44   cc main.c -Lpath/to/library -lft
 45   ```
 46
 47   Or explicitly:
 48   ```bash
 49   cc main.c path/to/libft.a
 50   ```
 51
 52<h3 id="example-usage">Example Usage</h3>
 53
 541. Suppose you have a simple program `main.c`:
 55   ```c
 56   #include "libft.h"
 57   #include <stdlib.h>
 58
 59   int	main(void)
 60   {
 61	   char	*s;
 62
 63	   s = ft_strdup("bleeeegh");
 64	   if (!s)
 65		   return (1);
 66	   ft_putendl_fd(s, 1);
 67	   free(s);
 68	   return (0);
 69   }
 70   ```
 71
 722. Compile it with the library:
 73   ```bash
 74   cc main.c -Lpath/to/library -lft -o yippie
 75   ```
 76
 77   Or explicitly:
 78   ```bash
 79   cc main.c path/to/libft.a
 80   ```
 81
 823. Run it:
 83   ```bash
 84   ./yippie
 85   ```
 86
 87   Expected output:
 88   ```
 89   bleeeegh
 90   ```
 91
 92<h1 id="functions">Library Functions</h1>
 93
 94<details open>
 95  <summary><h3 id="part1">Part I – Libc Functions</h3></summary>
 96  
 97  <br/>
 98  
 99  Name | Prototype | Description
100  --|--|--
101  [ft_isalpha](./ft_isalpha.c) | `int  ft_isalpha(int c);` | Checks if `c` is an alphabetic character
102  [ft_isdigit](./ft_isdigit.c) | `int  ft_isdigit(int c);` | Checks if `c` is a digit character
103  [ft_isalnum](./ft_isalnum.c) | `int  ft_isalnum(int c);` | Checks if `c` is an alphanumeric character
104  [ft_isascii](./ft_isascii.c) | `int  ft_isascii(int c);` | Checks if `c` is in ascii table
105  [ft_isprint](./ft_isprint.c) | `int  ft_isprint(int c);` | Checks if `c` is a printable character
106  [ft_strlen](./ft_strlen.c) | `size_t  ft_strlen(const char *s);` | Gets length of `s`
107  [ft_memset](./ft_memset.c) | `void  *ft_memset(void *s, int c, size_t n);` | Fills `s` with `n` bytes `c`
108  [ft_bzero](./ft_bzero.c) | `void  ft_bzero(void *s, size_t n);` | Fills `s` with `n` bytes `'\0'`
109  [ft_memcpy](./ft_memcpy.c) | `void  *ft_memcpy(void *dest, const void *src, size_t n);` | Copies `n` bytes from non-overlapping memory areas `src` to `dest`
110  [ft_memmove](./ft_memmove.c) | `void  *ft_memmove(void *dest, const void *src, size_t n);` | Copies `n` bytes from `src` to a temporary buffer and from that buffer to `dest`
111  [ft_strlcpy](./ft_strlcpy.c) | `size_t  ft_strlcpy(char *dst, const char *src, size_t size);` | Copies `size - 1` characters from `src` into `dst`
112  [ft_strlcat](./ft_strlcat.c) | `size_t  ft_strlcat(char *dst, const char *src, size_t size);` | Appends `size - strlen(dst) - 1` bytes from `src` to `dst`
113  [ft_toupper](./ft_toupper.c) | `int  ft_toupper(int c);` | Converts lowercase letter `c` to its uppercase equivalent
114  [ft_tolower](./ft_tolower.c) | `int  ft_tolower(int c);` | Converts uppercase letter `c` to its lowercase equivalent
115  [ft_strchr](./ft_strchr.c) | `char  *ft_strchr(const char *s, int c)` | Gets first occurence of `c` in `s`
116  [ft_strrchr](./ft_strrchr.c) | `char  *ft_strrchr(const char *s, int c)` | Gets last occurence of `c` in `s`
117  [ft_strncmp](./ft_strncmp.c) | `int  ft_strncmp(const char *s1, const char *s2, size_t n)` | Compares the first `n` bytes of `s1` and `s2`
118  [ft_memchr](./ft_memchr.c) | `void  *ft_memchr(const void *s, int c, size_t n)` | Scans `n` bytes of `s` for first instance of `c`
119  [ft_memcmp](./ft_memcmp.c) | `int  ft_memcmp(const void *s1, const void *s2, size_t n)` | Compares the first `n` bytes of `s1` and `s2`
120  [ft_strnstr](./ft_strnstr.c) | `char  *ft_strnstr(const char *big, const char *little, size_t len)` | Locates `little` in `big` where no more than `len` characters are searched
121  [ft_atoi](./ft_atoi.c) | `int  ft_atoi(const char *nptr)` | Converts `nptr` to int
122  [ft_calloc](./ft_calloc.c) | `void  *ft_calloc(size_t nmemb, size_t size);` | Allocates `nmemb * size` and initializes it to `0`
123  [ft_strdup](./ft_strdup.c) | `char *ft_strdup(const char *s);` | Duplicates `s` into heap
124</details>
125
126<br/>
127
128<details open>
129  <summary><h3 id="part2">Part II – Additional Functions</h3></summary>
130
131  <br/>
132
133  Name | Prototype | Description
134  --|--|--
135  [ft_substr](./ft_substr.c) | `char  *ft_substr(char const *s, unsigned int start, size_t len);` | Gets substring from `s` starting at index `start` and has maximum length `len`
136  [ft_strjoin](./ft_strjoin.c) | `char  *ft_strjoin(char const *s1, char const *s2);` | Concatenates `s1` and `s2` into a new string
137  [ft_strtrim](./ft_strtrim.c) | `char  *ft_strtrim(char const *s1, char const *set);` | Removes `set` from beginning and end of `s1`
138  [ft_split](./ft_split.c) | `char  **ft_split(char const *s, char c);` | Gets array of new strings that result from splitting `s` using `c`
139  [ft_itoa](./ft_itoa.c) | `char  *ft_itoa(int n);` | converts `n` to string
140  [ft_strmapi](./ft_strmapi.c) | `char  *ft_strmapi(char const *s, char (*f)(unsigned int, char));` | Gets a new string resulting from the application of `f` in every character of `s`
141  [ft_striteri](./ft_striteri.c) | `void  ft_striteri(char *s, void (*f)(unsigned int, char*));` | Applies `f` to every character in `s`
142  [ft_putchar_fd](./ft_putchar_fd.c) | `void  ft_putchar_fd(char c, int fd);` | Outputs `c` to file descriptor `fd`
143  [ft_putstr_fd](./ft_putstr_fd.c) | `void  ft_putstr_fd(char *s, int fd);` | Outputs `s` to file descriptor `fd`
144  [ft_putendl_fd](./ft_putendl_fd.c) | `void  ft_putendl_fd(char *s, int fd);` | Outputs `s` followed by `\n` to file descriptor `fd`
145  [ft_putnbr_fd](./ft_putnbr_fd.c) | `void  ft_putnbr_fd(int n, int fd);` | Outputs `n` to file descriptor `fd`
146</details>
147
148<br/>
149
150<details open>
151  <summary><h3 id="part3">Part III – Linked Lists</h3></summary>
152
153  <br/>
154
155  Name | Prototype | Description
156  --|--|--
157  [ft_lstnew](./ft_lstnew.c) | `t_list  *ft_lstnew(void *content);` | Creates new node with content `content` and next `NULL`
158  [ft_lstadd_front](./ft_lstadd_front.c) | `void  ft_lstadd_front(t_list **lst, t_list *new);` | Adds node `new` at the beginning of `lst`
159  [ft_lstsize](./ft_lstsize.c) | `int  ft_lstsize(t_list *lst);` | Counts number of nodes in `lst`
160  [ft_lstlast](./ft_lstlast.c) | `t_list  *ft_lstlast(t_list *lst);` | Gets last node of `lst`
161  [ft_lstadd_back](./ft_lstadd_back.c) | `void  ft_lstadd_back(t_list **lst, t_list *new);` | Adds node `new` at the end of `lst`
162  [ft_lstdelone](./ft_lstdelone.c) | `void  ft_lstdelone(t_list *lst, void (*del)(void*));` | Frees the node `lst` and its content, latter with `del`
163  [ft_lstclear](./ft_lstclear.c) | `void  ft_lstclear(t_list **lst, void (*del)(void*));` | Deletes all the nodes starting at the one pointed to by `lst` and frees their contents
164  [ft_lstiter](./ft_lstiter.c) | `void  ft_lstiter(t_list *lst, void (*f)(void *));` | Applies `f` iteractively to the list `lst`
165  [ft_lstmap](/ft_lstmap.c) | `t_list  *ft_lstmap(t_list *lst, void *(*f)(void *),void (*del)(void *));` | Gets new list resulting of the application of `f` to list `lst` iteractively
166</details>
167
168<h1 id="resources">Resources</h1>
169
170<h3 id="references">References</h3>
171
172- `man` pages
173- Peer discussions
174
175<h3 id="llm_usage">LLM Usage</h3>
176
177LLMs were used for:
178- General guidance during project development, primarily to make code more idiomatic
179- Parts of this README
180
181<h3 id="tests">Tests</h3>
182
183This project was tested against [libftTester](https://github.com/Tripouille/libftTester). Kudos to [@Tripouille](https://github.com/Tripouille) for providing this test suite.