Giter Site home page Giter Site logo

libft's Introduction

Libft - @42sp

ℹ️ A C static library with recoded standard functions

125/100

Index

What is Libft?

The first project at 42 programming school. It consists of a static library with recoded standard functions in C that runs and compiles the code using a Makefile. The library can be used as a support in future school's projects, since it's not allowed to use the original functions. Once you finish the project, you can (and should) include more functions to Libft than previously asked. For example, now the projects get_next_line and ft_printf are part of my library.

The project is divided in three parts - Part I, Part II and Bonus.

Part I.

In this first part, you must re-code a set of the libc functions, as defined in their man. Your functions will need to present the same prototype and behaviors as the originals. Your functions’ names must be prefixed by “ft_”. For instance strlen becomes ft_strlen.

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

Part II.

In this second part, you must code a set of functions that are either not included in the libc, or included in a different form. Some of these functions can be useful to write Part 1’s functions.

• substr • strjoin • strtrim • split • itoa • strmapi • striteri • putchar_fd • putstr_fd • putendl_fd • putnbr_fd

Bonus.

In this Bonus part, you must code a set of functions that are useful to manipulate lists.

• lstnew • lstadd_front • lstsize • lstlast • lstadd_back • lstdelone • lstclear • lstiter • lstmap

Requirements

⚠️ The project must be written in accordance with the Norm

The Norm is a programming standard that defines a set of rules to follow when writing code at 42. It applies to all C projects within the Common Core by default, and to any project where it's specified. These are some of them:
Each function must be maximum 25 lines, not counting the function's own curly brackets.

Each line must be at most 80 columns wide, comments included.

A function can take 4 named parameters maximum.

You can't declare more than 5 variables per function.

You're not allowed to use: for , do...while , switch , case ,  goto  ,
ternary operators such as `?' and VLAs - Variable Length Arrays.

The norminette (as we call the norm at 42) is in python and open source.

Its repository is available at https://github.com/42School/norminette.

In addition to the Norm, the subject have other requirements, which are:

It is forbidden to declare global variables.

Using the command libtool to create your library is forbidden.

Your Makefile must compile with the flags -Wall, -Wextra and -Werror.

Functions

Those are the my Libft's functions today - some of them are not part of the subject - Once we're allowed to keep expanding our library to help us in other projects at 42.

Functions from <ctype.h>

ft_isalpha - checks for an alphabetic character.

ft_isdigit - checks for a digit (0 through 9).

ft_isalnum - checks for an alphanumeric character.

ft_isascii - checks whether c fits into the ASCII character set.

ft_isprint - checks for any printable character.

ft_toupper - convert char to uppercase.

ft_tolower - convert char to lowercase.

Functions from <string.h>

ft_memset - fill memory with a constant byte.

ft_strlen - calculate the length of a string.

ft_bzero - zero a byte string.

ft_memcpy - copy memory area.

ft_memmove - copy memory area.

ft_strlcpy - copy string to an specific size.

ft_strlcat - concatenate string to an specific size.

ft_strchr - locate character in string.

ft_strrchr - locate character in string.

ft_strncmp - compare two strings.

ft_memchr - scan memory for a character.

ft_memcmp - compare memory areas.

ft_strnstr - locate a substring in a string.

ft_strdup - creates a dupplicate for the string passed as parameter.

Functions from <stdlib.h>

ft_atoi - convert a string to an integer.

ft_calloc - allocates memory and sets its bytes' values to 0.

Non-standard functions

ft_substr - returns a substring from a string.

ft_strjoin - concatenates two strings.

ft_strtrim - trims the beginning and end of string with specific set of chars.

ft_split - splits a string using a char as parameter.

ft_itoa - converts a number into a string.

ft_strmapi - applies a function to each character of a string.

ft_striteri - applies a function to each character of a string.

ft_putchar_fd - output a char to a file descriptor.

ft_putstr_fd - output a string to a file descriptor.

ft_putendl_fd - output a string to a file descriptor, followed by a new line.

ft_putnbr_fd - output a number to a file descriptor.

Linked list functions

ft_lstnew - creates a new list element.

ft_lstadd_front - adds an element at the beginning of a list.

ft_lstsize - counts the number of elements in a list.

ft_lstlast - returns the last element of the list.

ft_lstadd_back - adds an element at the end of a list.

ft_lstclear - deletes and free list.

ft_lstiter - applies a function to each element of a list.

ft_lstmap - applies a function to each element of a list.

get_next_line (2nd Project at 42)

get_next_line - reads any valid file line by line.

Functions from "ft_printf.h" (3rd Project at 42)

ft_printf - output a formatted string in stdout.

ft_argument_c - output a char in stdout.

ft_arguments_d_i - output an integer number in stdout.

ft_argument_p - output a memory address(a hexadecimal) in stdout.

ft_argument_percent - output an percent sign in stdout.

ft_argument_s - output a string in stdout.

ft_argument_u - output an unsigned integer in stdout.

ft_arguments_x - output a hexadecimal number in stdout.

ft_decimal_length - returns the length of a decimal number.

ft_free_ptr - free memory for a pointer and set its value to null.

ft_decimal_converter_to_hex - converts a decimal number to hexadecimal.

ft_hex_length - returns the length of a hexadecimal number.

ft_print_reversed_str - print a string backwards.

Useful Functions

ft_count_occurrences - returns the number of occurrences of a char in a string.

ft_free_str_array - free memory from an array of strings.

Makefile

ℹ️ A handy automation tool to Run and compile your programs more efficiently.

A Makefile defines set of tasks to be executed, in shell script. These tasks are writed on a target in this format:

target: prerequisites
<TAB> recipe

such as:

fclean:	clean
	@echo "$(NAME): $(RED)$(NAME) was deleted$(RESET)"
	${REMOVE} ${NAME}
	@echo

The recipe(the commands @echo and so forth) for the target fclean will only be executed when the target clean (the prerequisite) be executed. a target works without prerequisites too:

clean:
	@echo "\n$(NAME): $(RED)object files were deleted$(RESET)"
	${REMOVE} ${OBJS} ${BONUS_OBJS}
	@echo

As you could see, there are a few variables inside the recipe. The variables can be assigned just as follow:

GREEN		= \033[0;32m
RED		= \033[0;31m
RESET		= \033[0m
CC		= clang
FLAGS 		= -Wall -Werror -Wextra

To use the variable value, just use the $ sign with parenthesis:

@echo "$(NAME): $(RED)$(NAME) was deleted$(RESET)"

Using variables makes your Makefile more readable and easily modifiable. Try it :)

It is not necessary for the target to be a file. It could be just a name for the recipe, just as above. We call these phony targets.

But if you have a file with the exact name of your phony target inside of your repo, things can get a little weird. To protected your Makefile from this, just use phony and the name of all your phony targets used:

.PHONY:		all clean fclean re bonus

Here at 42 school, the subject says that

Your Makefile must at least contain the rules $(NAME), all, clean, fclean and re.

The rules are the target, just name some of them as $(NAME), all, clean, fclean and re.

The rule $(NAME), in this case, should create the $(NAME) static library.

all is used to make the principal goal of your Makefile: create the $(NAME) static library.

clean removes the objects created to make the library.

fclean removes the objects created to make the library and the $(NAME).

re just removes the objects created to make the library and the $(NAME), to then recompile everything.

You can run a rule on your Makefile this way:

make $(target_name)

such as:

make clean

In the case of the target all, just type make

make

Colorful Makefile

Choose your color, add it as a variable and use in your Makefile:

BLACK		="\[\033[0;30m\]"        
RED		="\[\033[0;31m\]"       
GREEN		="\[\033[0;32m\]"      
YELLOW		="\[\033[0;33m\]"       
BLUE		="\[\033[0;34m\]"        
PURPLE		="\[\033[0;35m\]"     
CYAN		="\[\033[0;36m\]"         
WHITE		="\[\033[0;37m\]"    
RESET		="\033[0m"

You could use it this way:

@echo "$(NAME): $(RED)$(NAME) was deleted$(RESET)"

So $(NAME) was deleted will be printed in red.

Cool, right?

Remember to reset the color when you're done, otherwise your terminal will keep with the last used colour.

How does it work?

The functioning of the library can be explained just by breaking down the Makefile. The library functions are all coded in .c files. These files are compiled into objects (.o) to be later inserted in the library, we do this just by compiling with the -c and -o flags.

  clang -c example1.c -o example1.o
  clang -c example2.c -o example2.o
  clang -c example3.c -o example3.o
  clang -c example4.c -o example4.o

And then using ar rcs command to create the library with all the objects.

  ar rcs libft.a example1.o example2.o example3.o example4.o

With the files transformed into objects, we don't need to compile all the code again in case there is any change, only the file that was changed would be recompiled.

The Makefile is used to automate the process as there are at least 40 files to be compiled into objects and then linked into the library.

How do I use the library?

It aims to create a library called libft.a from the source files.

To create this library, clone the project:

git clone https://github.com/augustobecker/libft libft

Enter the repository:

cd libft

Run Make (to run the Makefile that will compile the source code and create the library):

make

You should see a libft.a file and some object files (.o).

Now to clean up (removing the .o files), call make clean:

make clean

Now you just have to add this header at your .c files and use the Libft functions:

#include "libft.h"

If you try to compile your .c files with clang using clang example.c you will get an undefined symbol error for Libft functions.

You have to tell the file where your library resides:

clang example.c libft.a

That's it. Now run it using ./a.out

How do I test it?

To test the code we're going to be using @jgambard's Libft Tester. There are some good others but I'll only be covering this one.

To test the code in this repo Clone this repo and cd into it:

git clone https://github.com/augustobecker/libft libft
 
cd libft/	

Now, clone @jgambard's Libft Tester

git clone https://github.com/Tripouille/libftTester libftTester

Go into the test folder and run the mandatory test and then the bonus test:

cd libftTester
make m
make b

If you did everything correctly you should see something like this: image

42 Cursus

42 is a global education initiative that proposes a new way of learning technology: no teachers, no classrooms, students learning from their fellow students (peer to peer learning), with a methodology that develops both computing and life skills. Not to mention that it's completely free of charge and open to all with no prerequisites.

Admissions at 42 are unlike other colleges. We use a merit-based admission process. The final step of the admission is the Piscine - This is part of the admissions process and requires 4 weeks of intensive and immersive coding. No prior coding experience is necessary at all.

You can check more about the admission process on the 42sp website: https://www.42sp.org.br or on my github repository: 42 Piscine

To see other projects developed by me at 42, click here: 42 Cursus

Author

Augusto Becker | acesar-l | 🇧🇷👨‍🚀

👋 How to reach me:

libft's People

Contributors

augustobecker avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

ellewolfgher

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.