├── Makefile ├── README.md ├── ft_flags.c ├── ft_flags_utils.c ├── ft_nbr_len.c ├── ft_print_char.c ├── ft_print_flag.c ├── ft_print_hex.c ├── ft_print_int.c ├── ft_print_ptr.c ├── ft_print_str.c ├── ft_print_unsigned.c ├── ft_printf.c ├── ft_printf.h ├── ft_printf_itoa.c ├── ft_printf_utoa.c ├── ft_printf_xtoa.c ├── libft ├── .gitignore ├── Makefile ├── ft_atoi.c ├── ft_bzero.c ├── ft_calloc.c ├── ft_isalnum.c ├── ft_isalpha.c ├── ft_isascii.c ├── ft_isdigit.c ├── ft_isprint.c ├── ft_itoa.c ├── ft_lstadd_back.c ├── ft_lstadd_front.c ├── ft_lstclear.c ├── ft_lstdelone.c ├── ft_lstiter.c ├── ft_lstlast.c ├── ft_lstmap.c ├── ft_lstnew.c ├── ft_lstsize.c ├── ft_memchr.c ├── ft_memcmp.c ├── ft_memcpy.c ├── ft_memmove.c ├── ft_memset.c ├── ft_putchar_fd.c ├── ft_putendl_fd.c ├── ft_putnbr_fd.c ├── ft_putstr_fd.c ├── ft_split.c ├── ft_strchr.c ├── ft_strdup.c ├── ft_striteri.c ├── ft_strjoin.c ├── ft_strlcat.c ├── ft_strlcpy.c ├── ft_strlen.c ├── ft_strmapi.c ├── ft_strncmp.c ├── ft_strnstr.c ├── ft_strrchr.c ├── ft_strtrim.c ├── ft_substr.c ├── ft_tolower.c ├── ft_toupper.c └── libft.h └── testing ├── Makefile ├── main.c └── test /Makefile: -------------------------------------------------------------------------------- 1 | NAME = libftprintf.a 2 | 3 | CC = gcc 4 | CFLAGS = -Wall -Wextra -Werror 5 | AR = ar 6 | ARFLAGS = rcs 7 | RM = rm -rf 8 | 9 | SRC = ft_printf ft_print_char ft_print_str ft_print_hex ft_print_int ft_print_ptr ft_print_unsigned ft_nbr_len ft_flags ft_flags_utils ft_print_flag ft_printf_itoa ft_printf_utoa ft_printf_xtoa 10 | SRCS = $(addsuffix .c, $(SRC)) 11 | 12 | OBJ_DIR = obj 13 | OBJS = $(SRCS:%.c=$(OBJ_DIR)/%.o) 14 | 15 | LIBFT_PATH = ./libft 16 | LIBFT = $(LIBFT_PATH)/libft.a 17 | 18 | $(OBJ_DIR)/%.o: %.c 19 | $(CC) $(CFLAGS) -c $< -o $@ 20 | 21 | all: $(NAME) 22 | 23 | bonus: all 24 | 25 | $(NAME): $(LIBFT) $(OBJ_DIR) $(OBJS) 26 | cp $(LIBFT) $(NAME) 27 | $(AR) $(ARFLAGS) $(NAME) $(OBJS) 28 | 29 | $(LIBFT): 30 | make -C $(LIBFT_PATH) all 31 | 32 | $(OBJ_DIR): 33 | mkdir -p $(OBJ_DIR) 34 | 35 | clean: 36 | make -C $(LIBFT_PATH) clean 37 | $(RM) $(OBJ_DIR) 38 | 39 | fclean: clean 40 | make -C $(LIBFT_PATH) fclean 41 | $(RM) $(NAME) 42 | 43 | re: fclean all 44 | 45 | .PHONY: all bonus clean fclean re libft -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ft_printf 2 | 3 |

4 | ft_printf 42 project badge 5 |

6 | 7 | For the ft_printf project of the 42 school cursus, we must recreate the famous C library printf function. This project teaches us about variadic arguments as well as structures if we plan to implement printf's extra flags. 8 | 9 | > This project has been archived in the state it was in at the time of evaluation. 10 | 11 | - Supported conversions: %, c, s, p, i, d, u, x, X 12 | - Supported flags: # + (space) 13 | - Supported options: - 0 . * width 14 | 15 | ## Status 16 | Finished: 2022-02-07. Grade: 125/100. 17 | 18 | ## Usage 19 | 20 | ``make`` or ``make bonus`` to compile. 21 | 22 | ### Basic Usage 23 | For example, let's create a ``main.c`` file. 24 | 25 | ```c 26 | // Include the header 27 | #include "ft_printf.h" 28 | 29 | int main(void) 30 | { 31 | // Call the function 32 | ft_printf("Testing ft_printf!"); 33 | return (0); 34 | } 35 | ``` 36 | 37 | Compile the ``main.c`` file with the ft_printf library and run the program: 38 | ```bash 39 | gcc main.c libftprintf.a && ./a.out 40 | ``` 41 | Output should be: 42 | ``` 43 | Testing ft_printf! 44 | ``` 45 | 46 | ### Advanced Usage: Format Specifiers 47 | 48 | This ft_printf function supports several format specifiers, described below. 49 | These are optional, can be used in any combination, and are implemented as: 50 | %[flags][width][.precision]specifier 51 | 52 | The table below lists supported format specifiers: 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 |

Flags

FlagDescription
-Left justify the result within the field. By default it is right justified.
+Forces to precede the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a sign.
(space)If there is no sign, a space is attached to the beginning of the result.
#Used with x or X specifiers the value is preceded with 0x or 0X respectively for values different than zero.
0Leading zeros are used to pad the numbers instead of space.

Width

ValueDescription
(number)Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.
*The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

Precision

ValueDescription
.(number)For integer specifiers (d, i, u, x, X) − precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0. For s − this is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered. For c type − it has no effect. When no precision is specified, the default is 1. If the period is specified without an explicit value for precision, 0 is assumed.
.(*)The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

Specifiers

Format SpecifierDescription
%% followed by another % character writes % to the screen.
cwrites a single character.
swrites a character string.
pwrites an implementation-defined character sequence defining a pointer address.
d or iwrites a signed integer to decimal representation.
uwrites an unsigned integer to decimal representation.
x or Xwrites an unsigned integer to hexadecimal representation.
168 | 169 | For example, let's ask to print the integer 42 with a width of 20, and then to print it again but with a width of 8, the 0 flag and the + flag: 170 | ```c 171 | #include "ft_printf" 172 | 173 | int main(void) 174 | { 175 | ft_printf("Number [%20i]\n", 42); 176 | ft_printf("Number [%+0*i]\n", 8, 42); 177 | return (0); 178 | } 179 | ``` 180 | Outputs: 181 | ``` 182 | Number [ 42] 183 | Number [+0000042] 184 | ``` 185 | 186 | --- 187 | Made by mcombeau: mcombeau@student.42.fr | LinkedIn: [mcombeau](https://www.linkedin.com/in/mia-combeau-86653420b/) | Website: [codequoi.com](https://www.codequoi.com) 188 | -------------------------------------------------------------------------------- /ft_flags.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_flags.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/12/16 15:57:31 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/17 16:21:21 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | t_flags ft_flags_init(void) 16 | { 17 | t_flags flags; 18 | 19 | flags.spec = 0; 20 | flags.width = 0; 21 | flags.left = 0; 22 | flags.zero = 0; 23 | flags.star = 0; 24 | flags.precision = -1; 25 | flags.hash = 0; 26 | flags.space = 0; 27 | flags.plus = 0; 28 | return (flags); 29 | } 30 | 31 | t_flags ft_flag_left(t_flags flags) 32 | { 33 | flags.left = 1; 34 | flags.zero = 0; 35 | return (flags); 36 | } 37 | 38 | t_flags ft_flag_digit(char c, t_flags flags) 39 | { 40 | if (flags.star == 1) 41 | flags.width = 0; 42 | flags.width = (flags.width * 10) + (c - '0'); 43 | return (flags); 44 | } 45 | 46 | t_flags ft_flag_width(va_list args, t_flags flags) 47 | { 48 | flags.star = 1; 49 | flags.width = va_arg(args, int); 50 | if (flags.width < 0) 51 | { 52 | flags.left = 1; 53 | flags.width *= -1; 54 | } 55 | return (flags); 56 | } 57 | 58 | int ft_flag_precision(const char *str, int pos, va_list args, t_flags *flags) 59 | { 60 | int i; 61 | 62 | i = pos + 1; 63 | if (str[i] == '*') 64 | { 65 | flags->precision = va_arg(args, int); 66 | return (i++); 67 | } 68 | flags->precision = 0; 69 | while (ft_isdigit(str[i])) 70 | { 71 | flags->precision = (flags->precision * 10) + (str[i] - '0'); 72 | i++; 73 | } 74 | return (i); 75 | } 76 | -------------------------------------------------------------------------------- /ft_flags_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_flags_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/12/16 16:44:50 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/17 16:21:36 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | int ft_isflag(int c) 16 | { 17 | return (ft_istype(c) || ft_isdigit(c) || ft_isspec(c)); 18 | } 19 | 20 | int ft_isspec(int c) 21 | { 22 | if (c == '-' || c == '0' || c == '.' || c == '*' 23 | || c == '#' || c == ' ' || c == '+') 24 | return (1); 25 | return (0); 26 | } 27 | 28 | int ft_istype(int c) 29 | { 30 | if (c == 'c' || c == 's' || c == 'd' || c == 'i' || c == 'u' 31 | || c == 'x' || c == 'X' || c == 'p' || c == '%') 32 | return (1); 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /ft_nbr_len.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_nbr_len.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/12/11 16:41:32 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/11 16:42:38 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | int ft_ptr_len(unsigned long int n) 16 | { 17 | int len; 18 | 19 | len = 0; 20 | if (n == 0) 21 | return (1); 22 | while (n >= 1) 23 | { 24 | len++; 25 | n /= 16; 26 | } 27 | return (len); 28 | } 29 | 30 | int ft_unint_len(unsigned int n) 31 | { 32 | int len; 33 | 34 | len = 0; 35 | if (n == 0) 36 | return (1); 37 | while (n >= 1) 38 | { 39 | len++; 40 | n /= 10; 41 | } 42 | return (len); 43 | } 44 | 45 | int ft_hex_len(unsigned int n) 46 | { 47 | int len; 48 | 49 | len = 0; 50 | if (n == 0) 51 | return (1); 52 | while (n >= 1) 53 | { 54 | len++; 55 | n /= 16; 56 | } 57 | return (len); 58 | } 59 | -------------------------------------------------------------------------------- /ft_print_char.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_print_char.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/12/11 16:01:33 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/17 14:26:33 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | int ft_print_c(char c) 16 | { 17 | write(1, &c, 1); 18 | return (1); 19 | } 20 | 21 | int ft_print_char(char c, t_flags flags) 22 | { 23 | int count; 24 | 25 | count = 0; 26 | if (flags.left == 1) 27 | count += ft_print_c(c); 28 | count += ft_pad_width(flags.width, 1, flags.zero); 29 | if (flags.left == 0) 30 | count += ft_print_c(c); 31 | return (count); 32 | } 33 | -------------------------------------------------------------------------------- /ft_print_flag.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_print_flag.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/12/16 18:11:01 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/17 13:49:39 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | int ft_pad_width(int total_width, int size, int zero) 16 | { 17 | int count; 18 | 19 | count = 0; 20 | while (total_width - size > 0) 21 | { 22 | if (zero) 23 | count += ft_print_c('0'); 24 | else 25 | count += ft_print_c(' '); 26 | total_width--; 27 | } 28 | return (count); 29 | } 30 | -------------------------------------------------------------------------------- /ft_print_hex.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_print_hex.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/12/11 15:30:24 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/17 15:27:40 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | int ft_print_x_prefix(int is_upper) 16 | { 17 | if (is_upper == 1) 18 | ft_print_s("0X"); 19 | else 20 | ft_print_s("0x"); 21 | return (2); 22 | } 23 | 24 | int ft_print_x(char *nbstr, int n, int is_upper, t_flags flags) 25 | { 26 | int count; 27 | 28 | count = 0; 29 | if (flags.zero == 0 && flags.hash == 1 && n != 0) 30 | count += ft_print_x_prefix(is_upper); 31 | if (flags.precision >= 0) 32 | count += ft_pad_width(flags.precision - 1, 33 | ft_strlen(nbstr) - 1, 1); 34 | count += ft_print_s(nbstr); 35 | return (count); 36 | } 37 | 38 | int ft_print_hexadec(char *nbstr, int n, int is_upper, t_flags flags) 39 | { 40 | int count; 41 | 42 | count = 0; 43 | if (flags.zero == 1 && flags.hash == 1 && n != 0) 44 | count += ft_print_x_prefix(is_upper); 45 | if (flags.left == 1) 46 | count += ft_print_x(nbstr, n, is_upper, flags); 47 | if (flags.precision >= 0 && (size_t)flags.precision < ft_strlen(nbstr)) 48 | flags.precision = ft_strlen(nbstr); 49 | if (flags.precision >= 0) 50 | { 51 | flags.width -= flags.precision; 52 | count += ft_pad_width(flags.width, 0, 0); 53 | } 54 | else 55 | count += ft_pad_width(flags.width, 56 | ft_strlen(nbstr) + (flags.hash * 2), flags.zero); 57 | if (flags.left == 0) 58 | count += ft_print_x(nbstr, n, is_upper, flags); 59 | return (count); 60 | } 61 | 62 | int ft_print_hex(unsigned int n, int is_upper, t_flags flags) 63 | { 64 | char *nbstr; 65 | int count; 66 | 67 | count = 0; 68 | if (flags.precision == 0 && n == 0) 69 | { 70 | count += ft_pad_width(flags.width, 0, 0); 71 | return (count); 72 | } 73 | nbstr = ft_printf_xtoa(n, is_upper); 74 | if (!nbstr) 75 | return (0); 76 | count += ft_print_hexadec(nbstr, n, is_upper, flags); 77 | free(nbstr); 78 | return (count); 79 | } 80 | -------------------------------------------------------------------------------- /ft_print_int.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_print_int.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/12/11 14:46:32 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/17 17:27:06 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | int ft_print_i(char *nbstr, int n, t_flags flags) 16 | { 17 | int count; 18 | 19 | count = 0; 20 | if (n < 0) 21 | { 22 | if (flags.zero == 0 || flags.precision >= 0) 23 | count += ft_print_c('-'); 24 | } 25 | else if (flags.plus == 1 && flags.zero == 0) 26 | count += ft_print_c('+'); 27 | else if (flags.space == 1 && flags.zero == 0) 28 | count += ft_print_c(' '); 29 | if (flags.precision >= 0) 30 | count += ft_pad_width(flags.precision - 1, 31 | ft_strlen(nbstr) - 1, 1); 32 | count += ft_print_s(nbstr); 33 | return (count); 34 | } 35 | 36 | int ft_print_sign_pre(int n, t_flags *flags) 37 | { 38 | int count; 39 | 40 | count = 0; 41 | if (n < 0 && flags->precision == -1) 42 | { 43 | count += ft_print_c('-'); 44 | flags->width--; 45 | } 46 | else if (flags->plus == 1) 47 | count += ft_print_c('+'); 48 | else if (flags->space == 1) 49 | { 50 | count += ft_print_c(' '); 51 | flags->width--; 52 | } 53 | return (count); 54 | } 55 | 56 | int ft_print_integer(char *nbstr, int n, t_flags flags) 57 | { 58 | int count; 59 | 60 | count = 0; 61 | if (flags.zero == 1) 62 | count += ft_print_sign_pre(n, &flags); 63 | if (flags.left == 1) 64 | count += ft_print_i(nbstr, n, flags); 65 | if (flags.precision >= 0 && (size_t)flags.precision < ft_strlen(nbstr)) 66 | flags.precision = ft_strlen(nbstr); 67 | if (flags.precision >= 0) 68 | { 69 | flags.width -= flags.precision; 70 | if (n < 0 && flags.left == 0) 71 | flags.width -= 1; 72 | count += ft_pad_width(flags.width, 0, 0); 73 | } 74 | else 75 | count += ft_pad_width(flags.width - flags.plus - flags.space, 76 | ft_strlen(nbstr), flags.zero); 77 | if (flags.left == 0) 78 | count += ft_print_i(nbstr, n, flags); 79 | return (count); 80 | } 81 | 82 | int ft_print_int(int n, t_flags flags) 83 | { 84 | char *nbstr; 85 | long nb; 86 | int count; 87 | 88 | nb = n; 89 | count = 0; 90 | if (nb < 0) 91 | { 92 | nb *= -1; 93 | if (flags.zero == 0) 94 | flags.width--; 95 | } 96 | if (flags.precision == 0 && n == 0) 97 | { 98 | count += ft_pad_width(flags.width, 0, 0); 99 | return (count); 100 | } 101 | nbstr = ft_printf_itoa(nb); 102 | if (!nbstr) 103 | return (0); 104 | count += ft_print_integer(nbstr, n, flags); 105 | free(nbstr); 106 | return (count); 107 | } 108 | -------------------------------------------------------------------------------- /ft_print_ptr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_print_ptr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/12/11 15:49:21 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/13 12:13:11 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | void ft_print_adr(unsigned long int n) 16 | { 17 | if (n >= 16) 18 | { 19 | ft_print_adr(n / 16); 20 | ft_print_adr(n % 16); 21 | } 22 | else 23 | { 24 | if (n < 10) 25 | ft_print_c(n + '0'); 26 | else if (n >= 10) 27 | ft_print_c((n - 10) + 'a'); 28 | } 29 | } 30 | 31 | int ft_print_p(unsigned long int n) 32 | { 33 | int count; 34 | 35 | count = 0; 36 | if (n == 0) 37 | { 38 | count += ft_print_s(PTRNULL); 39 | return (count); 40 | } 41 | count += ft_print_s("0x"); 42 | ft_print_adr(n); 43 | count += ft_ptr_len(n); 44 | return (count); 45 | } 46 | 47 | int ft_print_ptr(unsigned long int n, t_flags flags) 48 | { 49 | int count; 50 | 51 | count = 0; 52 | if (n == 0) 53 | flags.width -= ft_strlen(PTRNULL) - 1; 54 | else 55 | flags.width -= 2; 56 | if (flags.left == 1) 57 | count += ft_print_p(n); 58 | count += ft_pad_width(flags.width, ft_ptr_len(n), 0); 59 | if (flags.left == 0) 60 | count += ft_print_p(n); 61 | return (count); 62 | } 63 | -------------------------------------------------------------------------------- /ft_print_str.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_print_str.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/12/11 16:01:33 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/17 14:26:33 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | static int ft_print_string(const char *str, t_flags flags) 16 | { 17 | int count; 18 | 19 | count = 0; 20 | if (flags.precision >= 0) 21 | { 22 | count += ft_pad_width(flags.precision, ft_strlen(str), 0); 23 | count += ft_print_s_pre(str, flags.precision); 24 | } 25 | else 26 | count += ft_print_s_pre(str, ft_strlen(str)); 27 | return (count); 28 | } 29 | 30 | #if defined(__linux__) || defined(__gnu_linux__) 31 | 32 | int ft_print_str(const char *str, t_flags flags) 33 | { 34 | int count; 35 | 36 | count = 0; 37 | if (str == NULL && flags.precision >= 0 && flags.precision < 6) 38 | { 39 | count += ft_pad_width(flags.width, 0, 0); 40 | return (count); 41 | } 42 | if (str == NULL) 43 | str = "(null)"; 44 | if (flags.precision >= 0 && (size_t)flags.precision > ft_strlen(str)) 45 | flags.precision = ft_strlen(str); 46 | if (flags.left == 1) 47 | count += ft_print_string(str, flags); 48 | if (flags.precision >= 0) 49 | count += ft_pad_width(flags.width, flags.precision, 0); 50 | else 51 | count += ft_pad_width(flags.width, ft_strlen(str), 0); 52 | if (flags.left == 0) 53 | count += ft_print_string(str, flags); 54 | return (count); 55 | } 56 | 57 | #else 58 | 59 | int ft_print_str(const char *str, t_flags flags) 60 | { 61 | int count; 62 | 63 | count = 0; 64 | if (str == NULL) 65 | str = "(null)"; 66 | if (flags.precision >= 0 && (size_t)flags.precision > ft_strlen(str)) 67 | flags.precision = ft_strlen(str); 68 | if (flags.left == 1) 69 | count += ft_print_string(str, flags); 70 | if (flags.precision >= 0) 71 | count += ft_pad_width(flags.width, flags.precision, 0); 72 | else 73 | count += ft_pad_width(flags.width, ft_strlen(str), 0); 74 | if (flags.left == 0) 75 | count += ft_print_string(str, flags); 76 | return (count); 77 | } 78 | #endif 79 | 80 | int ft_print_s_pre(const char *str, int precision) 81 | { 82 | int count; 83 | 84 | count = 0; 85 | while (str[count] && count < precision) 86 | write(1, &str[count++], 1); 87 | return (count); 88 | } 89 | 90 | int ft_print_s(const char *str) 91 | { 92 | int len; 93 | 94 | if (str == NULL) 95 | { 96 | write(1, "(null)", 6); 97 | return (6); 98 | } 99 | len = 0; 100 | while (str[len]) 101 | len++; 102 | write(1, str, len); 103 | return (len); 104 | } 105 | -------------------------------------------------------------------------------- /ft_print_unsigned.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_print_unsigned.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/12/17 15:15:24 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/17 15:42:11 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | int ft_print_u(char *nbstr, t_flags flags) 16 | { 17 | int count; 18 | 19 | count = 0; 20 | if (flags.precision >= 0) 21 | count += ft_pad_width(flags.precision - 1, 22 | ft_strlen(nbstr) - 1, 1); 23 | count += ft_print_s(nbstr); 24 | return (count); 25 | } 26 | 27 | int ft_print_unint(char *nbstr, t_flags flags) 28 | { 29 | int count; 30 | 31 | count = 0; 32 | if (flags.left == 1) 33 | count += ft_print_u(nbstr, flags); 34 | if (flags.precision >= 0 && (size_t)flags.precision < ft_strlen(nbstr)) 35 | flags.precision = ft_strlen(nbstr); 36 | if (flags.precision >= 0) 37 | { 38 | flags.width -= flags.precision; 39 | count += ft_pad_width(flags.width, 0, 0); 40 | } 41 | else 42 | count += ft_pad_width(flags.width, ft_strlen(nbstr), flags.zero); 43 | if (flags.left == 0) 44 | count += ft_print_u(nbstr, flags); 45 | return (count); 46 | } 47 | 48 | int ft_print_unsigned(unsigned n, t_flags flags) 49 | { 50 | char *nbstr; 51 | int count; 52 | 53 | count = 0; 54 | if (flags.precision == 0 && n == 0) 55 | { 56 | count += ft_pad_width(flags.width, 0, 0); 57 | return (count); 58 | } 59 | nbstr = ft_printf_utoa(n); 60 | if (!nbstr) 61 | return (0); 62 | count += ft_print_unint(nbstr, flags); 63 | free(nbstr); 64 | return (count); 65 | } 66 | -------------------------------------------------------------------------------- /ft_printf.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_printf.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/12/10 13:21:32 by mcombeau #+# #+# */ 9 | /* Updated: 2022/01/27 17:31:12 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | int ft_print_arg(char type, va_list args, t_flags flags) 16 | { 17 | int count; 18 | 19 | count = 0; 20 | if (type == '%') 21 | count += ft_print_char('%', flags); 22 | else if (type == 'c') 23 | count += ft_print_char(va_arg(args, int), flags); 24 | else if (type == 's') 25 | count += ft_print_str(va_arg(args, const char *), flags); 26 | else if (type == 'd' || type == 'i') 27 | count += ft_print_int(va_arg(args, int), flags); 28 | else if (type == 'x') 29 | count += ft_print_hex(va_arg(args, unsigned int), 0, flags); 30 | else if (type == 'X') 31 | count += ft_print_hex(va_arg(args, unsigned int), 1, flags); 32 | else if (type == 'u') 33 | count += ft_print_unsigned(va_arg(args, unsigned int), flags); 34 | else if (type == 'p') 35 | count += ft_print_ptr((unsigned long int)va_arg(args, void *), flags); 36 | return (count); 37 | } 38 | 39 | int ft_parse_flags(const char *str, int i, va_list args, t_flags *flags) 40 | { 41 | while (str[++i] && ft_isflag(str[i])) 42 | { 43 | if (str[i] == '-') 44 | *flags = ft_flag_left(*flags); 45 | if (str[i] == '#') 46 | flags->hash = 1; 47 | if (str[i] == ' ') 48 | flags->space = 1; 49 | if (str[i] == '+') 50 | flags->plus = 1; 51 | if (str[i] == '0' && flags->left == 0 && flags->width == 0) 52 | flags->zero = 1; 53 | if (str[i] == '.') 54 | i = ft_flag_precision(str, i, args, flags); 55 | if (str[i] == '*') 56 | *flags = ft_flag_width(args, *flags); 57 | if (ft_isdigit(str[i])) 58 | *flags = ft_flag_digit(str[i], *flags); 59 | if (ft_istype(str[i])) 60 | { 61 | flags->spec = str[i]; 62 | break ; 63 | } 64 | } 65 | return (i); 66 | } 67 | 68 | #if defined(__linux__) || defined(__gnu_linux__) 69 | 70 | int ft_parse(char *str, va_list args) 71 | { 72 | int i; 73 | int x; 74 | int count; 75 | t_flags flags; 76 | 77 | i = -1; 78 | count = 0; 79 | while (str[++i]) 80 | { 81 | flags = ft_flags_init(); 82 | if (str[i] == '%' && str[i + 1] != '\0') 83 | { 84 | x = ft_parse_flags(str, i, args, &flags); 85 | if (flags.spec > 0) 86 | i = x; 87 | if (str[i] != '\0' && flags.spec > 0 && ft_istype(str[i])) 88 | count += ft_print_arg(str[i], args, flags); 89 | else if (str[i] != '\0') 90 | count += ft_print_c(str[i]); 91 | } 92 | else 93 | count += ft_print_c(str[i]); 94 | } 95 | return (count); 96 | } 97 | 98 | #else 99 | 100 | int ft_parse(char *str, va_list args) 101 | { 102 | int i; 103 | int count; 104 | t_flags flags; 105 | 106 | i = -1; 107 | count = 0; 108 | while (str[++i]) 109 | { 110 | flags = ft_flags_init(); 111 | if (str[i] == '%' && str[i + 1] != '\0') 112 | { 113 | i = ft_parse_flags(str, i, args, &flags); 114 | if (str[i] != '\0' && flags.spec > 0 && ft_istype(str[i])) 115 | count += ft_print_arg(str[i], args, flags); 116 | else if (str[i] != '\0') 117 | count += ft_print_char(str[i], flags); 118 | } 119 | else 120 | count += ft_print_c(str[i]); 121 | } 122 | return (count); 123 | } 124 | 125 | #endif 126 | 127 | int ft_printf(const char *format, ...) 128 | { 129 | va_list args; 130 | int count; 131 | char *str; 132 | 133 | if (!format || *format == '\0') 134 | return (0); 135 | str = ft_strdup(format); 136 | if (!str || *str == '\0') 137 | return (0); 138 | count = 0; 139 | va_start(args, format); 140 | count = ft_parse(str, args); 141 | va_end(args); 142 | free(str); 143 | return (count); 144 | } 145 | -------------------------------------------------------------------------------- /ft_printf.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_printf.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/12/10 13:25:09 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/17 16:21:09 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef FT_PRINTF_H 14 | # define FT_PRINTF_H 15 | 16 | # include 17 | # include 18 | # include 19 | # include 20 | # include "libft/libft.h" 21 | 22 | # if defined (__linux__) 23 | # define PTRNULL "(nil)" 24 | # elif defined (__APPLE__) 25 | # define PTRNULL "0x0" 26 | # endif 27 | 28 | /* ---------- FLAGS --------------------- */ 29 | typedef struct s_flags 30 | { 31 | int spec; 32 | int width; 33 | int left; 34 | int zero; 35 | int star; 36 | int precision; 37 | int hash; 38 | int space; 39 | int plus; 40 | } t_flags; 41 | 42 | t_flags ft_flags_init(void); 43 | t_flags ft_flag_left(t_flags flags); 44 | t_flags ft_flag_digit(char c, t_flags flags); 45 | t_flags ft_flag_width(va_list args, t_flags flags); 46 | int ft_flag_precision(const char *str, int pos, 47 | va_list args, t_flags *flags); 48 | 49 | /* ---------- PRINTF -------------------- */ 50 | int ft_printf(const char *format, ...); 51 | int ft_print_arg(char type, va_list args, t_flags flags); 52 | 53 | /* ---------- PRINT SPECIFIERS ---------- */ 54 | // c 55 | int ft_print_char(char c, t_flags flags); 56 | int ft_print_c(char c); 57 | // s 58 | int ft_print_str(const char *str, t_flags flags); 59 | int ft_print_s(const char *str); 60 | int ft_print_s_pre(const char *str, int precision); 61 | int ft_print_sign_pre(int n, t_flags *flags); 62 | // i, d 63 | int ft_print_int(int n, t_flags flags); 64 | int ft_print_integer(char *nbstr, int n, t_flags flags); 65 | int ft_print_i(char *nbstr, int n, t_flags flags); 66 | // u 67 | int ft_print_unsigned(unsigned int n, t_flags flags); 68 | int ft_print_u(char *nbstr, t_flags flags); 69 | int ft_print_unint(char *nbstr, t_flags flags); 70 | // x, X 71 | int ft_print_hex(unsigned int n, int is_upper, t_flags flags); 72 | int ft_print_x(char *nbstr, int n, int is_upper, t_flags flags); 73 | int ft_print_hexadec(char *nbstr, int n, int is_upper, t_flags flags); 74 | // p 75 | int ft_print_ptr(unsigned long int n, t_flags flags); 76 | int ft_print_p(unsigned long int n); 77 | void ft_print_adr(unsigned long int n); 78 | 79 | /* ---------- HELPER FUNCTIONS ---------- */ 80 | char *ft_printf_itoa(long nb); 81 | char *ft_printf_utoa(unsigned int nb); 82 | char *ft_printf_xtoa(unsigned long int nb, int is_upper); 83 | int ft_unint_len(unsigned int n); 84 | int ft_hex_len(unsigned int n); 85 | int ft_ptr_len(unsigned long int n); 86 | int ft_istype(int c); 87 | int ft_isspec(int c); 88 | int ft_isflag(int c); 89 | 90 | /* ---------- FLAG FUNCTIONS ------------ */ 91 | int ft_pad_width(int total_width, int size, int zero); 92 | 93 | #endif 94 | -------------------------------------------------------------------------------- /ft_printf_itoa.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_printf_itoa.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/01/17 18:02:06 by mcombeau #+# #+# */ 9 | /* Updated: 2022/01/17 18:02:15 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | static size_t ft_itoa_len(long num) 16 | { 17 | size_t len; 18 | 19 | len = 0; 20 | if (num == 0) 21 | return (1); 22 | if (num < 0) 23 | { 24 | len++; 25 | num = -num; 26 | } 27 | while (num >= 1) 28 | { 29 | len++; 30 | num /= 10; 31 | } 32 | return (len); 33 | } 34 | 35 | static char *ft_num_to_str(long num, char *str, size_t len) 36 | { 37 | str = ft_calloc(len + 1, sizeof(char)); 38 | if (str == NULL) 39 | return (NULL); 40 | if (num < 0) 41 | { 42 | str[0] = '-'; 43 | num = -num; 44 | } 45 | len--; 46 | while (len) 47 | { 48 | str[len] = (num % 10) + '0'; 49 | num /= 10; 50 | len--; 51 | } 52 | if (str[0] != '-') 53 | str[0] = (num % 10) + '0'; 54 | return (str); 55 | } 56 | 57 | char *ft_printf_itoa(long num) 58 | { 59 | size_t len; 60 | char *str; 61 | 62 | len = ft_itoa_len(num); 63 | str = 0; 64 | str = ft_num_to_str(num, str, len); 65 | if (!str) 66 | return (NULL); 67 | return (str); 68 | } 69 | -------------------------------------------------------------------------------- /ft_printf_utoa.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_printf_utoa.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/01/17 18:02:22 by mcombeau #+# #+# */ 9 | /* Updated: 2022/01/17 18:02:28 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | static size_t ft_utoa_len(long num) 16 | { 17 | size_t len; 18 | 19 | len = 0; 20 | if (num == 0) 21 | return (1); 22 | while (num >= 1) 23 | { 24 | len++; 25 | num /= 10; 26 | } 27 | return (len); 28 | } 29 | 30 | static char *ft_u_to_str(unsigned int num, char *str, size_t len) 31 | { 32 | str = ft_calloc(len + 1, sizeof(char)); 33 | if (str == NULL) 34 | return (NULL); 35 | len--; 36 | while (len) 37 | { 38 | str[len] = (num % 10) + '0'; 39 | num /= 10; 40 | len--; 41 | } 42 | str[0] = (num % 10) + '0'; 43 | return (str); 44 | } 45 | 46 | char *ft_printf_utoa(unsigned int num) 47 | { 48 | size_t len; 49 | char *str; 50 | 51 | len = ft_utoa_len(num); 52 | str = 0; 53 | str = ft_u_to_str(num, str, len); 54 | if (!str) 55 | return (NULL); 56 | return (str); 57 | } 58 | -------------------------------------------------------------------------------- /ft_printf_xtoa.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_printf_xtoa.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/01/17 18:02:35 by mcombeau #+# #+# */ 9 | /* Updated: 2022/01/17 18:02:42 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | static size_t ft_xtoa_len(long num) 16 | { 17 | size_t len; 18 | 19 | len = 0; 20 | if (num == 0) 21 | return (1); 22 | while (num >= 1) 23 | { 24 | len++; 25 | num /= 16; 26 | } 27 | return (len); 28 | } 29 | 30 | static char *ft_hex_to_str(unsigned long int num, char *str, size_t len) 31 | { 32 | int mod; 33 | 34 | str = ft_calloc(len + 1, sizeof(char)); 35 | if (str == NULL) 36 | return (NULL); 37 | len--; 38 | while (len != (size_t)-1) 39 | { 40 | mod = num % 16; 41 | if (mod < 10) 42 | str[len] = mod + '0'; 43 | else if (mod >= 10) 44 | str[len] = (mod - 10) + 'a'; 45 | num = num / 16; 46 | len--; 47 | } 48 | return (str); 49 | } 50 | 51 | char *ft_printf_xtoa(unsigned long int num, int is_upper) 52 | { 53 | size_t len; 54 | char *str; 55 | int i; 56 | 57 | len = ft_xtoa_len(num); 58 | str = 0; 59 | str = ft_hex_to_str(num, str, len); 60 | if (!str) 61 | return (NULL); 62 | if (is_upper == 1) 63 | { 64 | i = 0; 65 | while (str[i]) 66 | { 67 | if (str[i] >= 'a' && str[i] <= 'f') 68 | str[i] -= 32; 69 | i++; 70 | } 71 | } 72 | return (str); 73 | } 74 | -------------------------------------------------------------------------------- /libft/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | -------------------------------------------------------------------------------- /libft/Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: mcombeau +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2021/11/22 13:48:30 by mcombeau #+# #+# # 9 | # Updated: 2021/12/02 13:26:19 by mcombeau ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | NAME = libft.a 14 | CC = gcc 15 | CFLAGS = -Wall -Werror -Wextra -g3 16 | AR = ar rcs 17 | SRC = ft_isalpha \ 18 | ft_isdigit \ 19 | ft_isalnum \ 20 | ft_isascii \ 21 | ft_isprint \ 22 | ft_strlen \ 23 | ft_memset \ 24 | ft_bzero \ 25 | ft_memcpy \ 26 | ft_memmove \ 27 | ft_strlcpy \ 28 | ft_strlcat \ 29 | ft_toupper \ 30 | ft_tolower \ 31 | ft_strchr \ 32 | ft_strrchr \ 33 | ft_strncmp \ 34 | ft_memchr \ 35 | ft_memcmp \ 36 | ft_strnstr \ 37 | ft_atoi \ 38 | ft_calloc \ 39 | ft_strdup \ 40 | ft_substr \ 41 | ft_strjoin \ 42 | ft_strtrim \ 43 | ft_split \ 44 | ft_itoa \ 45 | ft_strmapi \ 46 | ft_striteri \ 47 | ft_putchar_fd \ 48 | ft_putstr_fd \ 49 | ft_putendl_fd \ 50 | ft_putnbr_fd 51 | BONUS_SRC = ft_lstnew \ 52 | ft_lstadd_front \ 53 | ft_lstsize \ 54 | ft_lstlast \ 55 | ft_lstadd_back \ 56 | ft_lstdelone \ 57 | ft_lstclear \ 58 | ft_lstiter \ 59 | ft_lstmap 60 | 61 | SRCS = $(addsuffix .c, $(SRC)) 62 | OBJS = $(addsuffix .o, $(SRC)) 63 | BONUS_SRCS = $(addsuffix .c, $(BONUS_SRC)) 64 | BONUS_OBJS = $(addsuffix .o, $(BONUS_SRC)) 65 | 66 | .c.o: $(SRCS) $(BONUS_SRCS) 67 | $(CC) $(CFLAGS) -c -o $@ $< 68 | 69 | $(NAME): $(OBJS) 70 | $(AR) $@ $^ 71 | 72 | bonus: $(OBJS) $(BONUS_OBJS) 73 | $(AR) $(NAME) $^ 74 | 75 | all: $(NAME) 76 | 77 | clean: 78 | rm -f *.o 79 | 80 | fclean: clean 81 | rm -f $(NAME) 82 | 83 | re: clean all 84 | 85 | .PHONY: all clean fclean re bonus 86 | -------------------------------------------------------------------------------- /libft/ft_atoi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_atoi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/24 18:06:58 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/02 16:48:58 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_atoi converts a string into an int. 18 | 19 | RETURN VALUE : 20 | The converted int. 21 | */ 22 | 23 | int ft_atoi(const char *str) 24 | { 25 | int num; 26 | int isneg; 27 | int i; 28 | 29 | num = 0; 30 | isneg = 1; 31 | i = 0; 32 | while (str[i] && (str[i] == ' ' || str[i] == '\t' 33 | || str[i] == '\n' || str[i] == '\r' 34 | || str[i] == '\v' || str[i] == '\f')) 35 | i++; 36 | if (str[i] == '+') 37 | i++; 38 | else if (str[i] == '-') 39 | { 40 | isneg *= -1; 41 | i++; 42 | } 43 | while (ft_isdigit(str[i])) 44 | { 45 | num = (num * 10) + (str[i] - '0'); 46 | i++; 47 | } 48 | return (num * isneg); 49 | } 50 | -------------------------------------------------------------------------------- /libft/ft_bzero.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_bzero.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/23 14:22:49 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/02 14:21:37 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_bzero erases data in the n bytes of memory starting 18 | at location s by writing '\0's. 19 | 20 | RETURN VALUE : 21 | None. 22 | */ 23 | 24 | void ft_bzero(void *s, size_t n) 25 | { 26 | unsigned char *p; 27 | 28 | p = (unsigned char *)s; 29 | while (n != 0) 30 | { 31 | *p = '\0'; 32 | p++; 33 | n--; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /libft/ft_calloc.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_calloc.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/26 15:28:22 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/02 16:49:19 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_calloc allocates memory for an array of count elements 18 | of size bytes each and sets the memory to zero. 19 | 20 | RETURN VALUE : 21 | The pointer to the allocated memory. NULL if the memory allocation fails. 22 | */ 23 | 24 | void *ft_calloc(size_t count, size_t size) 25 | { 26 | void *r; 27 | 28 | r = malloc(count * size); 29 | if (!r) 30 | return (NULL); 31 | ft_bzero(r, size * count); 32 | return (r); 33 | } 34 | -------------------------------------------------------------------------------- /libft/ft_isalnum.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalnum.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/22 13:52:40 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/02 16:49:56 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_isalnum checks whether the value of c is alphanumeric. 18 | 19 | RETURN VALUE : 20 | Non-zero if c is alphanumeric, zero if not. 21 | */ 22 | 23 | int ft_isalnum(int c) 24 | { 25 | if (ft_isalpha(c) || ft_isdigit(c)) 26 | return (c); 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /libft/ft_isalpha.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalpha.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/22 13:50:28 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/02 14:36:38 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | DESCRIPTION : 15 | The function ft_isalpha checks whether c is alphabetic or not. 16 | 17 | RETURN VALUE : 18 | Non-zero if c is alphabetic, zero if not. 19 | */ 20 | 21 | int ft_isalpha(int c) 22 | { 23 | if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) 24 | return (c); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /libft/ft_isascii.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isascii.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/22 13:48:51 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/02 14:38:16 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | DESCRIPTION : 15 | The function ft_isascii checks whether c is an ascii character or not. 16 | 17 | RESULT VALUE : 18 | Non-zero if c is ascii, zero if not. 19 | */ 20 | 21 | int ft_isascii(int c) 22 | { 23 | if (c == 0) 24 | return (1); 25 | if (c > 0 && c <= 127) 26 | return (c); 27 | return (0); 28 | } 29 | -------------------------------------------------------------------------------- /libft/ft_isdigit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isdigit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/22 13:53:06 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/02 14:40:52 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | DESCRIPTION : 15 | The function ft_isdigit checks whether c is a digit character or not. 16 | 17 | RETURN VALUE: 18 | Non-zero if c is a digit, zero if not. 19 | */ 20 | 21 | int ft_isdigit(int c) 22 | { 23 | if (c >= '0' && c <= '9') 24 | return (c); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /libft/ft_isprint.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isprint.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/22 13:50:49 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/02 14:42:17 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | DESCRIPTION : 15 | The function ft_isprint checks whether c is a printable character or not. 16 | 17 | RETURN VALUE : 18 | Non-zero if c is printable, zero if not. 19 | */ 20 | 21 | int ft_isprint(int c) 22 | { 23 | if (c >= ' ' && c <= '~') 24 | return (c); 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /libft/ft_itoa.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_itoa.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/27 18:04:16 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/08 12:12:23 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_itoa converts the integer n into a string of characters. 18 | 19 | RESULT VALUE : 20 | The string of the converted integer. 21 | */ 22 | 23 | static size_t ft_itoa_len(long num) 24 | { 25 | size_t len; 26 | 27 | len = 0; 28 | if (num == 0) 29 | return (1); 30 | if (num < 0) 31 | { 32 | len++; 33 | num = -num; 34 | } 35 | while (num >= 1) 36 | { 37 | len++; 38 | num /= 10; 39 | } 40 | return (len); 41 | } 42 | 43 | static char *ft_num_to_str(long num, char *str, size_t len) 44 | { 45 | str = ft_calloc(len + 1, sizeof(char)); 46 | if (str == NULL) 47 | return (NULL); 48 | if (num < 0) 49 | { 50 | str[0] = '-'; 51 | num = -num; 52 | } 53 | len--; 54 | while (len) 55 | { 56 | str[len] = (num % 10) + '0'; 57 | num /= 10; 58 | len--; 59 | } 60 | if (str[0] != '-') 61 | str[0] = (num % 10) + '0'; 62 | return (str); 63 | } 64 | 65 | char *ft_itoa(int n) 66 | { 67 | long num; 68 | size_t len; 69 | char *str; 70 | 71 | num = n; 72 | len = ft_itoa_len(num); 73 | str = 0; 74 | str = ft_num_to_str(num, str, len); 75 | if (!str) 76 | return (NULL); 77 | return (str); 78 | } 79 | -------------------------------------------------------------------------------- /libft/ft_lstadd_back.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstadd_back.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/12/01 19:56:07 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/08 12:22:04 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_lstadd_back adds a new node to the back of a list: 18 | [.]->[.]->[.]->[NEW]->[NULL] 19 | 20 | RETURN VALUE : 21 | None. 22 | */ 23 | 24 | void ft_lstadd_back(t_list **alst, t_list *new) 25 | { 26 | t_list *tmp; 27 | 28 | if (!new) 29 | return ; 30 | if (!*alst) 31 | { 32 | *alst = new; 33 | return ; 34 | } 35 | tmp = ft_lstlast(*alst); 36 | tmp->next = new; 37 | } 38 | -------------------------------------------------------------------------------- /libft/ft_lstadd_front.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstadd_front.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/30 22:10:05 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/07 12:13:58 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_lstadd_front adds a new node to the front of a list: 18 | [NEW]->[.]->[.]->[.]->[NULL] 19 | 20 | RETURN VALUE : 21 | None. 22 | */ 23 | 24 | void ft_lstadd_front(t_list **alst, t_list *new) 25 | { 26 | if (!new) 27 | return ; 28 | if (!*alst) 29 | { 30 | *alst = new; 31 | return ; 32 | } 33 | new->next = *alst; 34 | *alst = new; 35 | } 36 | -------------------------------------------------------------------------------- /libft/ft_lstclear.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstclear.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/12/01 20:11:58 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/05 13:09:17 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_lstclear deletes each node of a list with the function 18 | passed as parameter. It also frees the memory of each node and finally 19 | sets the list pointer to NULL. 20 | 21 | RETURN VALUE : 22 | None. 23 | */ 24 | 25 | void ft_lstclear(t_list **lst, void (*del)(void *)) 26 | { 27 | t_list *tmp; 28 | 29 | if (!lst) 30 | return ; 31 | while (*lst) 32 | { 33 | tmp = (*lst)->next; 34 | ft_lstdelone(*lst, del); 35 | *lst = tmp; 36 | } 37 | *lst = NULL; 38 | } 39 | -------------------------------------------------------------------------------- /libft/ft_lstdelone.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstdelone.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/12/01 20:05:57 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/05 13:10:41 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_lstdelone deletes the content of a list node with the 18 | function passed as parameter before freeing the memory of the node. 19 | 20 | RETURN VALUE : 21 | None. 22 | */ 23 | 24 | void ft_lstdelone(t_list *lst, void (*del)(void *)) 25 | { 26 | if (!lst) 27 | return ; 28 | if (del) 29 | (del)(lst->content); 30 | free(lst); 31 | } 32 | -------------------------------------------------------------------------------- /libft/ft_lstiter.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstiter.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/12/01 20:26:30 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/02 16:50:47 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_lstiter applies the function f passed as parameter 18 | to the content of each node of a given list. 19 | 20 | RETURN VALUE : 21 | None. 22 | */ 23 | 24 | void ft_lstiter(t_list *lst, void (*f)(void *)) 25 | { 26 | if (!f || !lst) 27 | return ; 28 | while (lst) 29 | { 30 | f(lst->content); 31 | lst = lst->next; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /libft/ft_lstlast.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstlast.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/12/01 19:48:52 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/02 16:51:11 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_lstlast finds the last node in a given list. 18 | 19 | RETURN VALUE : 20 | The last node of a list. 21 | [.]->[.]->[.]->[LAST]->[NULL] 22 | */ 23 | 24 | t_list *ft_lstlast(t_list *lst) 25 | { 26 | if (!lst) 27 | return (NULL); 28 | while (lst != NULL && lst->next != NULL) 29 | lst = lst->next; 30 | return (lst); 31 | } 32 | -------------------------------------------------------------------------------- /libft/ft_lstmap.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstmap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/12/01 20:34:19 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/04 13:13:40 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_lstmap creates a new list from a given list by 18 | applying the function passed as parameter to the original list. If 19 | the memory allocation fails for any node in the new list, the new list 20 | will be deleted with the function passed as parameter and its memory 21 | will be freed. 22 | 23 | RETURN VALUE : 24 | The new list containing the new values if a functon was provided. 25 | A new copy of the list if no function was provided. 26 | NULL if the memory allocation failed. 27 | */ 28 | 29 | t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) 30 | { 31 | t_list *newlst; 32 | t_list *node; 33 | 34 | if (!lst) 35 | return (NULL); 36 | newlst = NULL; 37 | node = NULL; 38 | while (lst) 39 | { 40 | if (!f) 41 | node = ft_lstnew(lst->content); 42 | else 43 | node = ft_lstnew(f(lst->content)); 44 | if (!node) 45 | { 46 | ft_lstclear(&newlst, del); 47 | return (NULL); 48 | } 49 | ft_lstadd_back(&newlst, node); 50 | lst = lst->next; 51 | } 52 | return (newlst); 53 | } 54 | -------------------------------------------------------------------------------- /libft/ft_lstnew.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstnew.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/30 17:28:58 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/02 15:13:17 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_lstnew allocates memory for a new list node and 18 | initializes its content to the value passed as parameter, before 19 | setting its next node to NULL. 20 | 21 | RESULT VALUE : 22 | The new list ode. 23 | */ 24 | 25 | t_list *ft_lstnew(void *content) 26 | { 27 | t_list *list; 28 | 29 | list = malloc(sizeof(t_list)); 30 | if (!list) 31 | return (NULL); 32 | list->content = content; 33 | list->next = NULL; 34 | return (list); 35 | } 36 | -------------------------------------------------------------------------------- /libft/ft_lstsize.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstsize.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/12/01 19:44:21 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/02 15:15:06 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_lstsize measures the size of a given list by counting 18 | the number of nodes in it. 19 | 20 | RETURN VALUE : 21 | The integer number of nodes in the given list. 22 | */ 23 | 24 | int ft_lstsize(t_list *lst) 25 | { 26 | int i; 27 | 28 | i = 0; 29 | while (lst) 30 | { 31 | lst = lst->next; 32 | i++; 33 | } 34 | return (i); 35 | } 36 | -------------------------------------------------------------------------------- /libft/ft_memchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/25 21:57:14 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/03 16:31:15 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_memchr searches n bytes of the memory area pointed to 18 | by s for the first occurence of c. Both c and the bytes of s are 19 | interpreted as unsigned char. 20 | 21 | RETURN VALUE: 22 | A pointer to the matching byte. NULL if the character does not occur 23 | in the given memory area. 24 | */ 25 | 26 | void *ft_memchr(const void *s, int c, size_t n) 27 | { 28 | size_t i; 29 | unsigned char ch; 30 | const unsigned char *str; 31 | 32 | ch = c; 33 | str = (const unsigned char *)s; 34 | i = 0; 35 | while (i < n) 36 | { 37 | if (str[i] == ch) 38 | return ((void *)s + i); 39 | i++; 40 | } 41 | return (0); 42 | } 43 | -------------------------------------------------------------------------------- /libft/ft_memcmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/25 22:41:23 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/02 16:51:42 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_memcmp compares the first n bytes of the memory areas 18 | s1 and s2. The bytes are interpreted as unsigned char. 19 | 20 | RETURN VALUE : 21 | An integer less than, equal to, or greater than zero if the first 22 | n bytes of s1 is found to be less than, equal to, or greater than the 23 | first n bytes of s2. Zero if n is equal to zero. 24 | */ 25 | 26 | int ft_memcmp(const void *s1, const void *s2, size_t n) 27 | { 28 | const char *str1; 29 | const char *str2; 30 | size_t i; 31 | 32 | if (n == 0) 33 | return (0); 34 | str1 = (const char *)s1; 35 | str2 = (const char *)s2; 36 | i = 0; 37 | while ((i < n - 1) && str1[i] == str2[i]) 38 | i++; 39 | return ((unsigned char)str1[i] - (unsigned char)str2[i]); 40 | } 41 | -------------------------------------------------------------------------------- /libft/ft_memcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/23 15:02:13 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/02 15:26:40 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_memcpy copies n bytes from memory area src to memory 18 | area dst. 19 | Does not account for memory overlaps. Use ft_memmove if the memory areas 20 | overlap or might overlap. 21 | 22 | RETURN VALUE : 23 | A pointer to dst. NULL if src and dst are both NULL. 24 | */ 25 | 26 | void *ft_memcpy(void *dst, const void *src, size_t n) 27 | { 28 | char *dp; 29 | const char *sp; 30 | 31 | if (!dst && !src) 32 | return (0); 33 | if (n == 0 || (dst == src)) 34 | return (dst); 35 | dp = (char *)dst; 36 | sp = (const char *)src; 37 | while (n != 0) 38 | { 39 | if (*dp != *sp) 40 | *dp = *sp; 41 | dp++; 42 | sp++; 43 | n--; 44 | } 45 | return (dst); 46 | } 47 | -------------------------------------------------------------------------------- /libft/ft_memmove.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memmove.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/23 15:57:19 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/02 16:48:38 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_memmove copies n bytes from memory area src to memory 18 | area dst. The memory areas may overlap: if the dst pointer is found 19 | to be between the src pointer and the index n, copying will be done 20 | back to front to prevent data being modified before being copied. 21 | Otherwise it will be done front to back to preserve data. 22 | 23 | RETURN VALUE : 24 | A pointer to dst. 25 | */ 26 | 27 | void *ft_memmove(void *dst, const void *src, size_t len) 28 | { 29 | char *dp; 30 | const char *sp; 31 | 32 | if (src == dst) 33 | return (dst); 34 | dp = (char *)dst; 35 | sp = (const char *)src; 36 | if (sp < dp && sp + len > dp) 37 | while (len--) 38 | *(dp + len) = *(sp + len); 39 | else 40 | { 41 | while (len--) 42 | { 43 | *dp = *sp; 44 | sp++; 45 | dp++; 46 | } 47 | } 48 | return (dst); 49 | } 50 | -------------------------------------------------------------------------------- /libft/ft_memset.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memset.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/22 13:48:14 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/03 12:05:11 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_memset fills the first len bytes of the memory area 18 | pointed to by b with the byte c. Both b and c are interpreted as 19 | unsigned char. 20 | 21 | RETURN VALUE : 22 | A pointer to memory area s. 23 | */ 24 | 25 | void *ft_memset(void *b, int c, size_t len) 26 | { 27 | unsigned char *p; 28 | unsigned char ch; 29 | 30 | p = (unsigned char *)b; 31 | ch = c; 32 | while (len--) 33 | { 34 | *p = ch; 35 | p++; 36 | } 37 | return (b); 38 | } 39 | -------------------------------------------------------------------------------- /libft/ft_putchar_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putchar_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/28 05:38:33 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/02 15:40:12 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_putchar_fd writes the given character to the given 18 | file descriptor. 19 | 20 | RETURN VALUE : 21 | None. 22 | */ 23 | 24 | void ft_putchar_fd(char c, int fd) 25 | { 26 | write(fd, &c, 1); 27 | } 28 | -------------------------------------------------------------------------------- /libft/ft_putendl_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putendl_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/28 06:13:20 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/02 15:41:38 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_putendl_fd writes the given string to the given 18 | file descriptor followed by a new line. 19 | 20 | RETURN VALUE : 21 | None. 22 | */ 23 | 24 | void ft_putendl_fd(char *s, int fd) 25 | { 26 | ft_putstr_fd(s, fd); 27 | ft_putchar_fd('\n', fd); 28 | } 29 | -------------------------------------------------------------------------------- /libft/ft_putnbr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putnbr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/28 06:22:02 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/02 15:44:08 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_putnbr_fd writes the given integer n on the given 18 | file descriptor by converting it into char. 19 | 20 | RETURN VALUE : 21 | None. 22 | */ 23 | 24 | void ft_putnbr_fd(int n, int fd) 25 | { 26 | long nbr; 27 | 28 | nbr = n; 29 | if (nbr < 0) 30 | { 31 | ft_putchar_fd('-', fd); 32 | nbr = -nbr; 33 | } 34 | if (nbr >= 10) 35 | { 36 | ft_putnbr_fd(nbr / 10, fd); 37 | ft_putchar_fd((nbr % 10) + '0', fd); 38 | } 39 | else 40 | ft_putchar_fd(nbr + '0', fd); 41 | } 42 | -------------------------------------------------------------------------------- /libft/ft_putstr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putstr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/28 05:42:28 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/03 16:23:10 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_putstr_fd writes the given string to the given 18 | file descriptor. 19 | 20 | RETURN VALUE : 21 | None. 22 | */ 23 | 24 | void ft_putstr_fd(char *s, int fd) 25 | { 26 | if (!s) 27 | return ; 28 | while (*s != '\0') 29 | { 30 | ft_putchar_fd(*s, fd); 31 | s++; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /libft/ft_split.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_split.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/27 17:56:03 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/08 12:14:01 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_split allocates and copies an array of strings by 18 | splitting the given string s using the given separator c. 19 | 20 | RETURN VALUE : 21 | An array of strings resulting from the split. NULL if the memory 22 | allocation fails. 23 | */ 24 | 25 | static int ft_count_words(const char *s, char c) 26 | { 27 | int words; 28 | int i; 29 | 30 | words = 0; 31 | i = 0; 32 | while (s[i]) 33 | { 34 | if (i == 0 && s[i] != c) 35 | words++; 36 | if (i > 0 && s[i] != c && s[i - 1] == c) 37 | words++; 38 | i++; 39 | } 40 | return (words); 41 | } 42 | 43 | static char **ft_malloc_strs(char **strs, const char *s, char c) 44 | { 45 | int count; 46 | int i; 47 | int x; 48 | 49 | count = 0; 50 | i = 0; 51 | x = 0; 52 | while (s[i]) 53 | { 54 | if (s[i] != c) 55 | count++; 56 | if ((s[i] == c && i > 0 && s[i - 1] != c) 57 | || (s[i] != c && s[i + 1] == '\0')) 58 | { 59 | strs[x] = malloc(sizeof(char) * (count + 1)); 60 | if (!strs[x]) 61 | return (NULL); 62 | count = 0; 63 | x++; 64 | } 65 | i++; 66 | } 67 | return (strs); 68 | } 69 | 70 | static char **ft_cpy_strs(char **strs, const char *s, char c) 71 | { 72 | int i; 73 | int x; 74 | int y; 75 | 76 | i = 0; 77 | x = 0; 78 | y = 0; 79 | while (s[i]) 80 | { 81 | if (s[i] != c) 82 | strs[x][y++] = s[i]; 83 | if (s[i] != c && s[i + 1] == '\0') 84 | strs[x][y] = '\0'; 85 | if (s[i] == c && i > 0 && s[i - 1] != c) 86 | { 87 | strs[x][y] = '\0'; 88 | x++; 89 | y = 0; 90 | } 91 | i++; 92 | } 93 | return (strs); 94 | } 95 | 96 | static char **ft_merror(char **strs) 97 | { 98 | int i; 99 | 100 | i = 0; 101 | while (strs[i]) 102 | { 103 | free(strs[i]); 104 | strs[i] = NULL; 105 | i++; 106 | } 107 | free(strs); 108 | return (NULL); 109 | } 110 | 111 | char **ft_split(char const *s, char c) 112 | { 113 | char **strs; 114 | int wordcount; 115 | 116 | if (!s) 117 | { 118 | strs = malloc(sizeof(char) * 1); 119 | if (!strs) 120 | return (NULL); 121 | *strs = NULL; 122 | return (strs); 123 | } 124 | wordcount = ft_count_words(s, c); 125 | strs = malloc(sizeof(*strs) * (wordcount + 1)); 126 | if (!strs) 127 | return (NULL); 128 | if (ft_malloc_strs(strs, s, c)) 129 | { 130 | ft_cpy_strs(strs, s, c); 131 | strs[wordcount] = NULL; 132 | } 133 | else 134 | strs = ft_merror(strs); 135 | return (strs); 136 | } 137 | -------------------------------------------------------------------------------- /libft/ft_strchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/22 13:53:33 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/05 15:35:48 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_strchr finds the first occurence of character c in 18 | string str. 19 | 20 | RETURN VALUE : 21 | A pointer to the first occurence of c in str. 22 | NULL if c is not found. 23 | */ 24 | 25 | char *ft_strchr(const char *str, int c) 26 | { 27 | int i; 28 | unsigned char ch; 29 | 30 | i = 0; 31 | ch = c; 32 | if (ch == '\0') 33 | { 34 | i = ft_strlen(str); 35 | return ((char *)str + i++); 36 | } 37 | while (str[i]) 38 | { 39 | if (str[i] == ch) 40 | return ((char *)str + i); 41 | i++; 42 | } 43 | return (NULL); 44 | } 45 | -------------------------------------------------------------------------------- /libft/ft_strdup.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strdup.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/26 16:03:27 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/02 15:58:22 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_strdup duplicates the given string s1 by allocating 18 | memory and performing a copy of the given string. 19 | 20 | RETURN VALUE : 21 | A pointer to the new string. NULL if the memory allocation fails. 22 | */ 23 | 24 | char *ft_strdup(const char *s1) 25 | { 26 | char *s2; 27 | size_t len; 28 | 29 | len = ft_strlen(s1) + 1; 30 | s2 = malloc(len * sizeof(char)); 31 | if (!s2) 32 | return (NULL); 33 | ft_strlcpy(s2, s1, len); 34 | return (s2); 35 | } 36 | -------------------------------------------------------------------------------- /libft/ft_striteri.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_striteri.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/28 05:10:58 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/02 16:00:25 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_striteri applies the given function f to each 18 | character in the given string s. 19 | 20 | RETURN VALUE : 21 | None. 22 | */ 23 | 24 | void ft_striteri(char *s, void (*f)(unsigned int, char*)) 25 | { 26 | int i; 27 | 28 | if (!s || !f) 29 | return ; 30 | i = 0; 31 | while (s[i]) 32 | { 33 | (*f)(i, &s[i]); 34 | i++; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /libft/ft_strjoin.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strjoin.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/26 18:18:15 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/06 15:09:40 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_strjoin concatenates the given strings s1 and s2 18 | and allocates sufficient memory for the newly created string. 19 | 20 | RETURN VALUE : 21 | A pointer to the new concatenated string. 22 | NULL if the memory allocation fails. 23 | */ 24 | 25 | char *ft_strjoin(char const *s1, char const *s2) 26 | { 27 | char *s; 28 | size_t len; 29 | int i; 30 | 31 | len = ft_strlen(s1) + ft_strlen(s2); 32 | s = ft_calloc(len + 1, sizeof(char)); 33 | if (!s) 34 | return (NULL); 35 | len = 0; 36 | while (s1[len]) 37 | { 38 | s[len] = s1[len]; 39 | len++; 40 | } 41 | i = 0; 42 | while (s2[i]) 43 | { 44 | s[len + i] = s2[i]; 45 | i++; 46 | } 47 | return (s); 48 | } 49 | -------------------------------------------------------------------------------- /libft/ft_strlcat.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlcat.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/24 15:14:19 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/02 16:13:28 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_strlcat appends the given string src to the end of 18 | dst. It will append at most dstsize - ft_strlen(dst) - 1 and 19 | nul-terminate the result. 20 | 21 | Note : space for the terminating \0 character must be included in dstsize. 22 | 23 | RETURN VALUE : 24 | The total length of the string that it tried to create : the initial 25 | length of dst + the length of src, with the goal to facilitate 26 | truncaction detection. 27 | */ 28 | 29 | size_t ft_strlcat(char *dst, const char *src, size_t dstsize) 30 | { 31 | size_t i; 32 | size_t j; 33 | size_t d_size; 34 | size_t s_size; 35 | 36 | d_size = ft_strlen(dst); 37 | s_size = ft_strlen(src); 38 | if (dstsize <= d_size) 39 | return (dstsize + s_size); 40 | i = d_size; 41 | j = 0; 42 | while ((i + j) < (dstsize - 1) && src[j] != '\0') 43 | { 44 | dst[i + j] = src[j]; 45 | j++; 46 | } 47 | dst[i + j] = '\0'; 48 | return (d_size + s_size); 49 | } 50 | -------------------------------------------------------------------------------- /libft/ft_strlcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/24 14:16:24 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/03 16:32:30 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_strlcpy copies up to size - 1 characters from the given 18 | string src to the given string dst, nul-terminating the result. 19 | 20 | Note : space for the terminating \0 character must be included in dstsize. 21 | 22 | RETURN VALUE : 23 | The total length of the string that it tried to create : the length of 24 | src, with the goal to facilitate truncaction detection. 25 | */ 26 | 27 | size_t ft_strlcpy(char *dst, const char *src, size_t dstsize) 28 | { 29 | size_t i; 30 | size_t srclen; 31 | 32 | srclen = ft_strlen(src); 33 | if (dstsize == 0) 34 | return (srclen); 35 | i = 0; 36 | while (i < (dstsize - 1) && src[i] != '\0') 37 | { 38 | dst[i] = src[i]; 39 | i++; 40 | } 41 | dst[i] = '\0'; 42 | return (srclen); 43 | } 44 | -------------------------------------------------------------------------------- /libft/ft_strlen.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlen.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/22 13:51:11 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/02 16:17:56 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_strlen measures the length of the given string str, 18 | excluding the terminating \0 character. 19 | 20 | RETURN VALUE : 21 | The number of bytes in the string str. 22 | */ 23 | 24 | size_t ft_strlen(const char *str) 25 | { 26 | size_t i; 27 | 28 | i = 0; 29 | while (str[i] != '\0') 30 | i++; 31 | return (i); 32 | } 33 | -------------------------------------------------------------------------------- /libft/ft_strmapi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strmapi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/28 04:35:39 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/02 16:21:12 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESRIPTION : 17 | The function ft_strmapi applies the given function f to each character 18 | in the given string s and allocates sufficient memory to store the 19 | resulting new string. 20 | 21 | RETURN VALUE : 22 | A pointer to the newly created string. NULL if the memory allocation 23 | fails. 24 | */ 25 | 26 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) 27 | { 28 | char *str; 29 | unsigned int i; 30 | 31 | if (!s || (!s && !f)) 32 | return (ft_strdup("")); 33 | else if (!f) 34 | return (ft_strdup(s)); 35 | str = ft_strdup(s); 36 | if (!str) 37 | return (NULL); 38 | i = 0; 39 | while (s[i]) 40 | { 41 | str[i] = (*f)(i, s[i]); 42 | i++; 43 | } 44 | return (str); 45 | } 46 | -------------------------------------------------------------------------------- /libft/ft_strncmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strncmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/24 17:09:14 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/06 15:15:37 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_strncmp compares the first n bytes of the given strings 18 | s1 and s2. 19 | 20 | RETURN VALUE : 21 | An integer less than, equal to, or greater than zero if one of the first 22 | n bytes of s1 is found to be less than, to match, or to be greater than 23 | s2. 24 | */ 25 | 26 | int ft_strncmp(const char *s1, const char *s2, size_t n) 27 | { 28 | size_t i; 29 | 30 | i = 0; 31 | if (n == 0) 32 | return (0); 33 | while ((s1[i] != '\0' && s2[i] != '\0') 34 | && (i < n - 1) && s1[i] == s2[i]) 35 | i++; 36 | return ((unsigned char)s1[i] - (unsigned char)s2[i]); 37 | } 38 | -------------------------------------------------------------------------------- /libft/ft_strnstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/25 23:07:33 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/03 16:33:24 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_strnstr searches the first n bytes of the given string 18 | s1 for the first occurence of the full string s2. 19 | Characters that appear after \0 are not searched. 20 | 21 | RETURN VALUE : 22 | A pointer to the first character of the first occurrence of s2. 23 | A pointer to s1 if s2 is empty. 24 | NULL if s2 occurs nowhere in s1. 25 | */ 26 | 27 | char *ft_strnstr(const char *s1, const char *s2, size_t n) 28 | { 29 | size_t s2len; 30 | size_t i; 31 | size_t j; 32 | 33 | s2len = ft_strlen(s2); 34 | if (s1 == s2 || s2len == 0) 35 | return ((char *)s1); 36 | i = 0; 37 | while (i < n && s1[i] != '\0') 38 | { 39 | j = 0; 40 | while (s1[i + j] != '\0' && s2[j] != '\0' 41 | && (i + j) < n && s1[i + j] == s2[j]) 42 | { 43 | j++; 44 | if ((j == n && j == s2len) || j == s2len) 45 | return ((char *)(s1 + i)); 46 | } 47 | i++; 48 | } 49 | return (0); 50 | } 51 | -------------------------------------------------------------------------------- /libft/ft_strrchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strrchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/24 15:50:46 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/05 15:37:01 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_strrchr finds the last occurrence of character c in 18 | string str. 19 | 20 | RETURN VALUE : 21 | A pointer to the last occurrence of c in str. 22 | NULL if c is not found. 23 | */ 24 | 25 | char *ft_strrchr(const char *str, int c) 26 | { 27 | char *p; 28 | unsigned char ch; 29 | size_t offset; 30 | 31 | ch = c; 32 | offset = ft_strlen(str); 33 | p = (char *)str + offset; 34 | if (ch == '\0') 35 | return (p++); 36 | while (p >= str) 37 | { 38 | if (*p == ch) 39 | return (p); 40 | p--; 41 | } 42 | p = NULL; 43 | return (p); 44 | } 45 | -------------------------------------------------------------------------------- /libft/ft_strtrim.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strtrim.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/27 16:51:42 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/03 16:21:52 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_strtrim removes any characters of the given set from 18 | the beginning and end of the given string s1, and allocates sufficient 19 | memory to store the trimmed copy of the string. 20 | 21 | RETURN VALUE : 22 | A pointer to the trimmed copy of the string. 23 | NULL if the memory allocation fails. 24 | */ 25 | 26 | static int is_set(char c, char const *set) 27 | { 28 | int i; 29 | 30 | i = 0; 31 | while (set[i]) 32 | { 33 | if (set[i] == c) 34 | return (1); 35 | i++; 36 | } 37 | return (0); 38 | } 39 | 40 | char *ft_strtrim(char const *s1, char const *set) 41 | { 42 | size_t start; 43 | size_t end; 44 | 45 | if (!s1) 46 | return (ft_strdup("")); 47 | if (!set) 48 | return (ft_strdup(s1)); 49 | start = 0; 50 | end = ft_strlen(s1); 51 | while (is_set(s1[start], set)) 52 | start++; 53 | if (start == end) 54 | return (ft_strdup("")); 55 | while (is_set(s1[end - 1], set)) 56 | end--; 57 | return (ft_substr(s1, start, end - start)); 58 | } 59 | -------------------------------------------------------------------------------- /libft/ft_substr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_substr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/26 16:50:44 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/02 16:53:34 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_substr extracts a substring from the given string by 18 | allocating sufficient memory for the new string starting at index start 19 | and ending at len characters. 20 | 21 | RETURN VALUE : 22 | A pointer to the new string. 23 | NULL if the memory allocation fails. 24 | */ 25 | 26 | char *ft_substr(char const *s, unsigned int start, size_t len) 27 | { 28 | char *res; 29 | char *src; 30 | size_t reslen; 31 | 32 | if (!s) 33 | return (NULL); 34 | if (ft_strlen(s) < (size_t)start) 35 | return (ft_strdup("")); 36 | src = (char *)s + start; 37 | if (ft_strlen(src) < len) 38 | reslen = ft_strlen(src) + 1; 39 | else 40 | reslen = len + 1; 41 | res = malloc(reslen * sizeof(char)); 42 | if (!res) 43 | return (NULL); 44 | ft_strlcpy(res, src, reslen); 45 | return (res); 46 | } 47 | -------------------------------------------------------------------------------- /libft/ft_tolower.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_tolower.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/22 13:52:04 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/06 15:42:20 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_tolower converts a given uppercase letter c to its 18 | lowercase equivalent. 19 | 20 | RETURN VALUE : 21 | The lowercase equivalent letter. 22 | The original character c if c is not an uppercase letter. 23 | */ 24 | 25 | int ft_tolower(int c) 26 | { 27 | if (c >= 'A' && c <= 'Z') 28 | c += 32; 29 | return (c); 30 | } 31 | -------------------------------------------------------------------------------- /libft/ft_toupper.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_toupper.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/22 13:53:56 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/06 15:41:08 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | /* 16 | DESCRIPTION : 17 | The function ft_toupper converts a given lowercase letter c to its 18 | uppercase equivalent. 19 | 20 | RETURN VALUE : 21 | The uppercase equivalent letter. 22 | The original character c if c is not a lowercase letter. 23 | */ 24 | 25 | int ft_toupper(int c) 26 | { 27 | if (c >= 'a' && c <= 'z') 28 | c -= 32; 29 | return (c); 30 | } 31 | -------------------------------------------------------------------------------- /libft/libft.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* libft.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: mcombeau +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2021/11/22 13:54:20 by mcombeau #+# #+# */ 9 | /* Updated: 2021/12/04 13:57:17 by mcombeau ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef LIBFT_H 14 | # define LIBFT_H 15 | 16 | # include 17 | # include 18 | # include 19 | 20 | /* --------------- LISTS --------------- */ 21 | typedef struct s_list 22 | { 23 | void *content; 24 | struct s_list *next; 25 | } t_list; 26 | 27 | t_list *ft_lstnew(void *content); 28 | void ft_lstadd_front(t_list **alst, t_list *new); 29 | int ft_lstsize(t_list *lst); 30 | t_list *ft_lstlast(t_list *lst); 31 | void ft_lstadd_back(t_list **alst, t_list *new); 32 | void ft_lstdelone(t_list *lst, void (*del)(void *)); 33 | void ft_lstclear(t_list **lst, void (*del)(void *)); 34 | void ft_lstiter(t_list *lst, void (*f)(void *)); 35 | t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); 36 | 37 | /* --------------- CHARS --------------- */ 38 | int ft_isalpha(int c); 39 | int ft_isdigit(int c); 40 | int ft_isalnum(int c); 41 | int ft_isascii(int c); 42 | int ft_isprint(int c); 43 | int ft_toupper(int c); 44 | int ft_tolower(int c); 45 | 46 | /* --------------- STRINGS --------------- */ 47 | size_t ft_strlen(const char *str); 48 | char *ft_strchr(const char *str, int c); 49 | char *ft_strrchr(const char *str, int c); 50 | size_t ft_strlcpy(char *dst, const char *src, size_t dstsize); 51 | size_t ft_strlcat(char *dst, const char *src, size_t dstsize); 52 | int ft_strncmp(const char *s1, const char *s2, size_t n); 53 | char *ft_strnstr(const char *s1, const char *s2, size_t n); 54 | char *ft_strdup(const char *s1); 55 | char *ft_substr(char const *s, unsigned int start, size_t len); 56 | char *ft_strjoin(char const *s1, char const *s2); 57 | char *ft_strtrim(char const *s1, char const *set); 58 | char **ft_split(char const *s, char c); 59 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); 60 | void ft_striteri(char *s, void (*f)(unsigned int, char*)); 61 | 62 | /* --------------- FILE DESCRIPTORS --------------- */ 63 | void ft_putchar_fd(char c, int fd); 64 | void ft_putstr_fd(char *s, int fd); 65 | void ft_putendl_fd(char *s, int fd); 66 | void ft_putnbr_fd(int n, int fd); 67 | 68 | /* --------------- MEMORY --------------- */ 69 | void *ft_memset(void *b, int c, size_t len); 70 | void ft_bzero(void *s, size_t n); 71 | void *ft_memcpy(void *dst, const void *src, size_t n); 72 | void *ft_memmove(void *dst, const void *src, size_t len); 73 | void *ft_memchr(const void *s, int c, size_t n); 74 | int ft_memcmp(const void *s1, const void *s2, size_t n); 75 | void *ft_calloc(size_t count, size_t size); 76 | 77 | /* --------------- NUMBERS --------------- */ 78 | int ft_atoi(const char *str); 79 | char *ft_itoa(int n); 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /testing/Makefile: -------------------------------------------------------------------------------- 1 | NAME = test 2 | 3 | all: $(NAME) 4 | 5 | $(NAME): 6 | $(MAKE) -C .. all 7 | cp ../libftprintf.a $(NAME) 8 | gcc main.c $(NAME) 9 | 10 | clean: 11 | $(MAKE) -C .. clean 12 | 13 | fclean: clean 14 | rm -rf a.out $(NAME) 15 | 16 | re: fclean 17 | $(MAKE) all -------------------------------------------------------------------------------- /testing/main.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include "../ft_printf.h" 5 | 6 | void test_simple(void) 7 | { 8 | int x; 9 | int y; 10 | x = 0; 11 | y = 0; 12 | printf("\n----------- TEST: simple string -----------\n\n"); 13 | printf("Testing: (\"\\tHello!\\n\")\n"); 14 | x += printf(" Or\t:\tHello!\n"); 15 | y += ft_printf(" Ft\t:\tHello!\n"); 16 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 17 | } 18 | 19 | void test_percent(void) 20 | { 21 | int x; 22 | int y; 23 | x = 0; 24 | y = 0; 25 | printf("\n----------- TEST: percent -----------\n\n"); 26 | printf("Testing: (\"\\t%%%%\\n\")\n"); 27 | x += printf(" Or\t:\t%%\n"); 28 | y += ft_printf(" Ft\t:\t%%\n"); 29 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 30 | 31 | } 32 | 33 | void test_c(void) 34 | { 35 | int x, y; 36 | x = 0; 37 | y = 0; 38 | printf("\n----------- TEST: %%c -----------\n\n"); 39 | printf("Testing: (\"\\t%%c\\n\", 'a')\n"); 40 | x += printf(" Or\t:\t%c\n", 'a'); 41 | y += ft_printf(" Ft\t:\t%c\n", 'a'); 42 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 43 | x = 0; 44 | y = 0; 45 | printf("Testing: (\"\\tThe character %%c is visible.\\n\", 'x')\n"); 46 | x += printf(" Or\t:\tThe character %c is visible.\n", 'x'); 47 | y += ft_printf(" Ft\t:\tThe character %c is visible.\n", 'x'); 48 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 49 | } 50 | 51 | void test_s(void) 52 | { 53 | int x, y; 54 | x = 0; 55 | y = 0; 56 | printf("\n----------- TEST: %%s -----------\n\n"); 57 | printf("Testing: (\"\\t%%s\\n\", \"coucou, ca va?\")\n"); 58 | x += printf(" Or\t:\t%s\n", "coucou, ca va?"); 59 | y += ft_printf(" Ft\t:\t%s\n", "coucou, ca va?"); 60 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 61 | x = 0; 62 | y = 0; 63 | printf("Testing: (\"\\tThe string \\\"%%s\\\" is visible.\\n\", \"TRALALA\")\n"); 64 | x += printf(" Or\t:\tThe string \"%s\" is visible.\n", "TRALALA"); 65 | y += ft_printf(" Ft\t:\tThe string \"%s\" is visible.\n", "TRALALA"); 66 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 67 | x = 0; 68 | y = 0; 69 | char *s = NULL; 70 | if (s == NULL) 71 | write(1, "(null)\n", 7); 72 | printf("Testing: (\"\\tNULL %%s NULL.\\n\", NULL)\n"); 73 | x += printf(" Or\t:\tNULL %s NULL.\n", s); 74 | y += ft_printf(" Ft\t:\tNULL %s NULL.\n", s); 75 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 76 | } 77 | 78 | void test_d(void) 79 | { 80 | int x, y; 81 | x = 0; 82 | y = 0; 83 | printf("\n----------- TEST: %%d -----------\n\n"); 84 | printf("Testing: (\"\\t%%d\\n\", 42)\n"); 85 | x += printf(" Or\t:\t%d\n", 42); 86 | y += ft_printf(" Ft\t:\t%d\n", 42); 87 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 88 | x = 0; 89 | y = 0; 90 | printf("Testing: (\"\\tThe number %%d is visible.\\n\", 5671)\n"); 91 | x += printf(" Or\t:\tThe number %d is visible.\n", 5671); 92 | y += ft_printf(" Ft\t:\tThe number %d is visible.\n", 5671); 93 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 94 | x = 0; 95 | y = 0; 96 | printf("Testing: (\"\\t%%d\\n\", INT_MAX)\n"); 97 | x += printf(" Or\t:\t%d\n", INT_MAX); 98 | y += ft_printf(" Ft\t:\t%d\n", INT_MAX); 99 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 100 | x = 0; 101 | y = 0; 102 | printf("Testing: (\"\\t%%d\\n\", INT_MIN)\n"); 103 | x += printf(" Or\t:\t%d\n", INT_MIN); 104 | y += ft_printf(" Ft\t:\t%d\n", INT_MIN); 105 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 106 | x = 0; 107 | y = 0; 108 | printf("Testing: (\"\\t%%i\\n\", INT_MIN - INT_MIN)\n"); 109 | x += printf(" Or\t:\t%i\n", INT_MIN - INT_MIN); 110 | y += ft_printf(" Ft\t:\t%i\n", INT_MIN - INT_MIN); 111 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 112 | } 113 | 114 | void test_i(void) 115 | { 116 | int x, y; 117 | printf("\n----------- TEST: %%i -----------\n\n"); 118 | x = 0; 119 | y = 0; 120 | printf("Testing: (\"\\t%%i\\n\", 4422)\n"); 121 | x += printf(" Or\t:\t%i\n", 4422); 122 | y += ft_printf(" Ft\t:\t%i\n", 4422); 123 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 124 | x = 0; 125 | y = 0; 126 | printf("Testing: (\"\\tThe number %%i is visible.\\n\", 0)\n"); 127 | x += printf(" Or\t:\tThe number %i is visible.\n", 0); 128 | y += ft_printf(" Ft\t:\tThe number %i is visible.\n", 0); 129 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 130 | x = 0; 131 | y = 0; 132 | printf("Testing: (\"\\t%%i\\n\", INT_MAX)\n"); 133 | x += printf(" Or\t:\t%i\n", INT_MAX); 134 | y += ft_printf(" Ft\t:\t%i\n", INT_MAX); 135 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 136 | x = 0; 137 | y = 0; 138 | printf("Testing: (\"\\t%%i\\n\", INT_MIN)\n"); 139 | x += printf(" Or\t:\t%i\n", INT_MIN); 140 | y += ft_printf(" Ft\t:\t%i\n", INT_MIN); 141 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 142 | x = 0; 143 | y = 0; 144 | printf("Testing: (\"\\t%%i\\n\", INT_MIN + INT_MAX)\n"); 145 | x += printf(" Or\t:\t%i\n", INT_MIN + INT_MAX); 146 | y += ft_printf(" Ft\t:\t%i\n", INT_MIN + INT_MAX); 147 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 148 | } 149 | 150 | void test_x(void) 151 | { 152 | int x, y; 153 | x = 0; 154 | y = 0; 155 | printf("\n----------- TEST: %%x -----------\n\n"); 156 | printf("Testing: (\"\\t%%x\\n\", 42)\n"); 157 | x += printf(" Or\t:\t%x\n", 42); 158 | y += ft_printf(" Ft\t:\t%x\n", 42); 159 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 160 | x = 0; 161 | y = 0; 162 | printf("Testing: (\"\\tThe hex number %%x is visible.\\n\", 5671)\n"); 163 | x += printf(" Or\t:\tThe hex number %x is visible.\n", 5671); 164 | y += ft_printf(" Ft\t:\tThe hex number %x is visible.\n", 5671); 165 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 166 | x = 0; 167 | y = 0; 168 | printf("Testing: (\"\\t%%x\\n\", INT_MAX)\n"); 169 | x += printf(" Or\t:\t%x\n", INT_MAX); 170 | y += ft_printf(" Ft\t:\t%x\n", INT_MAX); 171 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 172 | x = 0; 173 | y = 0; 174 | printf("Testing: (\"\\t%%x\\n\", INT_MIN)\n"); 175 | x += printf(" Or\t:\t%x\n", INT_MIN); 176 | y += ft_printf(" Ft\t:\t%x\n", INT_MIN); 177 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 178 | x = 0; 179 | y = 0; 180 | printf("Testing: (\"\\t%%x\\n\", -42)\n"); 181 | x += printf(" Or\t:\t%x\n", -42); 182 | y += ft_printf(" Ft\t:\t%x\n", -42); 183 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 184 | x = 0; 185 | y = 0; 186 | printf("Testing: (\"\\t%%x\\n\", UINT_MAX)\n"); 187 | x += printf(" Or\t:\t%x\n", UINT_MAX); 188 | y += ft_printf(" Ft\t:\t%x\n", UINT_MAX); 189 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 190 | } 191 | 192 | void test_X(void) 193 | { 194 | int x, y; 195 | x = 0; 196 | y = 0; 197 | printf("\n----------- TEST: %%X -----------\n\n"); 198 | printf("Testing: (\"\\t%%X\\n\", 42)\n"); 199 | x += printf(" Or\t:\t%X\n", 42); 200 | y += ft_printf(" Ft\t:\t%X\n", 42); 201 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 202 | x = 0; 203 | y = 0; 204 | printf("Testing: (\"\\tThe hex number %%X is visible.\\n\", 5671)\n"); 205 | x += printf(" Or\t:\tThe hex number %X is visible.\n", 5671); 206 | y += ft_printf(" Ft\t:\tThe hex number %X is visible.\n", 5671); 207 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 208 | x = 0; 209 | y = 0; 210 | printf("Testing: (\"\\t%%X\\n\", INT_MAX)\n"); 211 | x += printf(" Or\t:\t%X\n", INT_MAX); 212 | y += ft_printf(" Ft\t:\t%X\n", INT_MAX); 213 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 214 | x = 0; 215 | y = 0; 216 | printf("Testing: (\"\\t%%X\\n\", INT_MIN)\n"); 217 | x += printf(" Or\t:\t%X\n", INT_MIN); 218 | y += ft_printf(" Ft\t:\t%X\n", INT_MIN); 219 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 220 | x = 0; 221 | y = 0; 222 | printf("Testing: (\"\\t%%X\\n\", -42)\n"); 223 | x += printf(" Or\t:\t%X\n", -42); 224 | y += ft_printf(" Ft\t:\t%X\n", -42); 225 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 226 | x = 0; 227 | y = 0; 228 | printf("Testing: (\"\\t%%X\\n\", UINT_MAX)\n"); 229 | x += printf(" Or\t:\t%X\n", UINT_MAX); 230 | y += ft_printf(" Ft\t:\t%X\n", UINT_MAX); 231 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 232 | } 233 | 234 | void test_u(void) 235 | { 236 | int x, y; 237 | x = 0; 238 | y = 0; 239 | printf("\n----------- TEST: %%u -----------\n\n"); 240 | printf("Testing: (\"\\t%%u\\n\", 42)\n"); 241 | x += printf(" Or\t:\t%u\n", 42); 242 | y += ft_printf(" Ft\t:\t%u\n", 42); 243 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 244 | x = 0; 245 | y = 0; 246 | printf("Testing: (\"\\tThe number %%u is visible.\\n\", 5671)\n"); 247 | x += printf(" Or\t:\tThe number %u is visible.\n", 5671); 248 | y += ft_printf(" Ft\t:\tThe number %u is visible.\n", 5671); 249 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 250 | x = 0; 251 | y = 0; 252 | printf("Testing: (\"\\t%%u\\n\", INT_MAX)\n"); 253 | x += printf(" Or\t:\t%u\n", INT_MAX); 254 | y += ft_printf(" Ft\t:\t%u\n", INT_MAX); 255 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 256 | x = 0; 257 | y = 0; 258 | printf("Testing: (\"\\t%%u\\n\", INT_MIN)\n"); 259 | x += printf(" Or\t:\t%u\n", INT_MIN); 260 | y += ft_printf(" Ft\t:\t%u\n", INT_MIN); 261 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 262 | x = 0; 263 | y = 0; 264 | printf("Testing: (\"\\t%%u\\n\", -42)\n"); 265 | x += printf(" Or\t:\t%u\n", -42); 266 | y += ft_printf(" Ft\t:\t%u\n", -42); 267 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 268 | x = 0; 269 | y = 0; 270 | printf("Testing: (\"\\t%%u\\n\", UINT_MAX)\n"); 271 | x += printf(" Or\t:\t%u\n", UINT_MAX); 272 | y += ft_printf(" Ft\t:\t%u\n", UINT_MAX); 273 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 274 | } 275 | 276 | void test_p(void) 277 | { 278 | int x, y; 279 | printf("\n----------- TEST: %%p -----------\n\n"); 280 | x = 0; 281 | y = 0; 282 | int n; 283 | char c; 284 | void *p; 285 | 286 | n = 50; 287 | c = 'a'; 288 | p = NULL; 289 | printf("Testing: (\"\\t%%p\\n\", (void *)&n)\n"); 290 | x += printf(" Or\t:\t%p\n", (void *)&n); 291 | y += ft_printf(" Ft\t:\t%p\n", (void *)&n); 292 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 293 | x = 0; 294 | y = 0; 295 | printf("Testing: (\"\\t%%p\\n\", (void *)&c)\n"); 296 | x += printf(" Or\t:\t%p\n", (void *)&c); 297 | y += ft_printf(" Ft\t:\t%p\n", (void *)&c); 298 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 299 | x = 0; 300 | y = 0; 301 | printf("Testing: (\"\\t%%p\\n\", p)\n"); 302 | x += printf(" Or\t:\t%p\n", p); 303 | y += ft_printf(" Ft\t:\t%p\n", p); 304 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 305 | } 306 | 307 | void test_all(void) 308 | { 309 | int x, y; 310 | x = 0; 311 | y = 0; 312 | printf("\n----------- TEST: ALL -----------\n\n"); 313 | printf("Testing: (\"\\tThe int is %%i or %%d, the char is %%c, the string is \\\"%%s\\\", the hex is %%x or %%X, the unsigned is %%u and the percent is %%%%.\\n\", 42, -42, 's', \"Hello\", 42, 42, 242)\n"); 314 | x += printf(" Or\t:\tThe int is %i or %d, the char is %c, the string is \"%s\", the hex is %x or %X, the unsigned is %u and the percent is %%.\n", 42, -42, 's', "Hello", 42, 42, 242); 315 | y += ft_printf(" Or\t:\tThe int is %i or %d, the char is %c, the string is \"%s\", the hex is %x or %X, the unsigned is %u and the percent is %%.\n", 42, -42, 's', "Hello", 42, 42, 242); 316 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 317 | } 318 | 319 | void test_bonus_char(void) 320 | { 321 | int x, y; 322 | x = 0; 323 | y = 0; 324 | printf("\n----------- TEST: WIDTH CHAR -----------\n\n"); 325 | x = printf(" Or:\tChar [%20c]\n", 'x'); 326 | y = ft_printf(" Ft:\tChar [%20c]\n", 'x'); 327 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 328 | 329 | x = printf(" Or:\tChar [%-20c]\n", 'x'); 330 | y = ft_printf(" Ft:\tChar [%-20c]\n", 'x'); 331 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 332 | 333 | x = printf(" Or:\tChar [%*c]\n", 8, 'x'); 334 | y = ft_printf(" Ft:\tChar [%*c]\n", 8, 'x'); 335 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 336 | 337 | y = ft_printf(" Ft:\tChar [%0*c]\n", 8, 'x'); 338 | y = ft_printf(" Ft:\tChar [%-0*c]\n", 8, 'x'); 339 | 340 | /* printf("\n----------- TEST: WIDTH %% -----------\n\n"); 341 | x = printf(" Or:\tChar [%20%]\n"); 342 | y = ft_printf(" Ft:\tChar [%20%]\n"); 343 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 344 | 345 | x = printf(" Or:\tChar [%0*%]\n", 8); 346 | y = ft_printf(" Ft:\tChar [%0*%]\n", 8); 347 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 348 | */} 349 | 350 | void test_bonus_str(void) 351 | { 352 | int x, y; 353 | 354 | printf("\n----------- TEST: WIDTH STR -----------\n\n"); 355 | x = printf(" Or:\tString [%-25s]\n", "Hello!"); 356 | y = ft_printf(" Ft:\tString [%-25s]\n", "Hello!"); 357 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 358 | 359 | x = printf(" Or:\tString [%*s]\n", 20, "Hello!"); 360 | y = ft_printf(" Ft:\tString [%*s]\n", 20, "Hello!"); 361 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 362 | 363 | x = printf(" Or:\tString [%1s]\n", "Hello World!"); 364 | y = ft_printf(" Ft:\tString [%1s]\n", "Hello World!"); 365 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 366 | } 367 | 368 | void test_bonus_hex(void) 369 | { 370 | int x, y; 371 | 372 | printf("\n----------- TEST: SHARP HEX -----------\n\n"); 373 | x = printf(" Or:\tNumber [%#x]\n", 0); 374 | y = ft_printf(" Ft:\tNumber [%#x]\n", 0); 375 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 376 | //x = printf(" Or:\tNumber [%#x]\n", LONG_MIN); 377 | y = ft_printf(" Ft:\tNumber [%#x]\n", LONG_MIN); 378 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 379 | x = printf(" Or:\tNumber [%#x]\n", 42); 380 | y = ft_printf(" Ft:\tNumber [%#x]\n", 42); 381 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 382 | x = printf(" Or:\tNumber [%#X]\n", 42); 383 | y = ft_printf(" Ft:\tNumber [%#X]\n", 42); 384 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 385 | 386 | printf("\n----------- TEST: WIDTH HEX -----------\n\n"); 387 | x = printf(" Or:\tNumber [%*x]\n", 10, 42); 388 | y = ft_printf(" Ft:\tNumber [%*x]\n",10, 42); 389 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 390 | x = printf(" Or:\tNumber [%20X]\n", 42); 391 | y = ft_printf(" Ft:\tNumber [%20X]\n", 42); 392 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 393 | x = printf(" Or:\tNumber [%0*X]\n", 20, 42); 394 | y = ft_printf(" Ft:\tNumber [%0*X]\n", 20, 42); 395 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 396 | 397 | printf("\n----------- TEST: WIDTH SHARP HEX -----------\n\n"); 398 | x = printf(" Or:\tNumber [%#*x]\n", 10, 42); 399 | y = ft_printf(" Ft:\tNumber [%#*x]\n",10, 42); 400 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 401 | x = printf(" Or:\tNumber [%#0*x]\n", 10, 42); 402 | y = ft_printf(" Ft:\tNumber [%#0*x]\n",10, 42); 403 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 404 | } 405 | 406 | void test_bonus_unsigned(void) 407 | { 408 | int x, y; 409 | 410 | printf("\n----------- TEST: WIDTH UNSIGNED -----------\n\n"); 411 | x = printf(" Or:\tNumber [%20u]\n", 42); 412 | y = ft_printf(" Ft:\tNumber [%20u]\n", 42); 413 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 414 | x = printf(" Or:\tNumber [%-15u]\n", 42); 415 | y = ft_printf(" Ft:\tNumber [%-15u]\n", 42); 416 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 417 | x = printf(" Or:\tNumber [%0*u]\n", 10, 42); 418 | y = ft_printf(" Ft:\tNumber [%0*u]\n", 10, 42); 419 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 420 | } 421 | 422 | void test_bonus_int(void) 423 | { 424 | int x, y; 425 | 426 | printf("\n----------- TEST: WIDTH INT -----------\n\n"); 427 | x = printf(" Or:\tNumber [%20i]\n", 42); 428 | y = ft_printf(" Ft:\tNumber [%20i]\n", 42); 429 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 430 | x = printf(" Or:\tNumber [%0*i]\n", 8, 42); 431 | y = ft_printf(" Ft:\tNumber [%0*i]\n", 8, 42); 432 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 433 | x = printf(" Or:\tNumber [%0*i]\n", 8, -42); 434 | y = ft_printf(" Ft:\tNumber [%0*i]\n", 8, -42); 435 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 436 | x = printf(" Or:\tNumber [%*i]\n", 8, -42); 437 | y = ft_printf(" Ft:\tNumber [%*i]\n", 8, -42); 438 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 439 | 440 | printf("\n----------- TEST: ZERO INT -----------\n\n"); 441 | x = printf(" Or:\tNumber [%011i]\n", (int)LONG_MAX); 442 | y = ft_printf(" Ft:\tNumber [%011i]\n", LONG_MAX); 443 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 444 | x = printf(" Or:\tNumber [%010d]\n", INT_MIN); 445 | y = ft_printf(" Ft:\tNumber [%010d]\n", INT_MIN); 446 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 447 | 448 | // 47 TEST(22, print(" %010d ", INT_MIN)); 449 | 450 | 451 | printf("\n----------- TEST: SPACE INT -----------\n\n"); 452 | x = printf(" Or:\tNumber [% 0*i]\n", 8, 42); 453 | y = ft_printf(" Ft:\tNumber [% 0*i]\n", 8, 42); 454 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 455 | 456 | x = printf(" Or:\tNumber [% 0*i]\n", 8, -42); 457 | y = ft_printf(" Ft:\tNumber [% 0*i]\n", 8, -42); 458 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 459 | 460 | x = printf(" Or:\tNumber [%- *i]\n", 8, 42); 461 | y = ft_printf(" Ft:\tNumber [%- *i]\n", 8, 42); 462 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 463 | 464 | printf("\n----------- TEST: PLUS INT -----------\n\n"); 465 | x = printf(" Or:\tNumber [%+0*i]\n", 8, 42); 466 | y = ft_printf(" Ft:\tNumber [%+0*i]\n", 8, 42); 467 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 468 | x = printf(" Or:\tNumber [%+*i]\n", 8, 42); 469 | y = ft_printf(" Ft:\tNumber [%+*i]\n", 8, 42); 470 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 471 | } 472 | 473 | void test_bonus_ptr(void) 474 | { 475 | int x, y; 476 | 477 | printf("\n----------- TEST: LEFT JUSTIFY PTR -----------\n\n"); 478 | x = printf(" Or:\tPointer [%-15p]\n", &x); 479 | y = ft_printf(" Ft:\tPointer [%-15p]\n", &x); 480 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 481 | x = printf(" Or:\tPointer [%-20p]\n", &y); 482 | y = ft_printf(" Ft:\tPointer [%-20p]\n", &y); 483 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 484 | x = printf(" Or:\tPointer [%-15p]\n", (void *)0); 485 | y = ft_printf(" Ft:\tPointer [%-15p]\n", (void *)0); 486 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 487 | } 488 | 489 | void test_bonus_precision(void) 490 | { 491 | int x, y; 492 | 493 | printf("\n----------- TEST: PRECISION STRING -----------\n\n"); 494 | x = printf(" Or:\tString, no prec: [%s]\n", "0123456789"); 495 | y = ft_printf(" Ft:\tString, no prec: [%s]\n", "0123456789"); 496 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 497 | x = printf(" Or:\tString, prec 6: [%.6s]\n", "0123456789"); 498 | y = ft_printf(" Ft:\tString, prec 6: [%.6s]\n", "0123456789"); 499 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 500 | x = printf(" Or:\tString, prec 15: [%.15s]\n", "0123456789"); 501 | y = ft_printf(" Ft:\tString, prec 15: [%.15s]\n", "0123456789"); 502 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 503 | x = printf(" Or:\tString, prec 0: [%.0s]\n", "0123456789"); 504 | y = ft_printf(" Ft:\tString, prec 0: [%.0s]\n", "0123456789"); 505 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 506 | 507 | printf("\n----------- TEST: PRECISION INT -----------\n\n"); 508 | x = printf(" Or:\tInt, no prec: [%d]\n", 42424242); 509 | y = ft_printf(" Ft:\tInt, no prec: [%d]\n", 42424242); 510 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 511 | x = printf(" Or:\tInt, prec 2: [%.2d]\n", 42424242); 512 | y = ft_printf(" Ft:\tInt, prec 2: [%.2d]\n", 42424242); 513 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 514 | x = printf(" Or:\tInt, prec 42: [%.42d]\n", 42424242); 515 | y = ft_printf(" Ft:\tInt, prec 42: [%.42d]\n", 42424242); 516 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 517 | } 518 | 519 | void test_extra() 520 | { 521 | char *null_str = NULL; 522 | int x; 523 | int y; 524 | 525 | printf("\n----------- TEST: NULL STRING PRECISION THING -----------\n\n"); 526 | x = printf(" Or:\t.0s [%.0s]\n", null_str); 527 | y = ft_printf(" Ft:\t.0s [%.0s]\n", null_str); 528 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 529 | x = printf(" Or:\t.1s [%.1s]\n", null_str); 530 | y = ft_printf(" Ft:\t.1s [%.1s]\n", null_str); 531 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 532 | x = printf(" Or:\t.2s [%.2s]\n", null_str); 533 | y = ft_printf(" Ft:\t.2s [%.2s]\n", null_str); 534 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 535 | x = printf(" Or:\t.3s [%.3s]\n", null_str); 536 | y = ft_printf(" Ft:\t.3s [%.3s]\n", null_str); 537 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 538 | x = printf(" Or:\t.4s [%.4s]\n", null_str); 539 | y = ft_printf(" Ft:\t.4s [%.4s]\n", null_str); 540 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 541 | x = printf(" Or:\t.5s [%.5s]\n", null_str); 542 | y = ft_printf(" Ft:\t.5s [%.5s]\n", null_str); 543 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 544 | x = printf(" Or:\t.6s [%.6s]\n", null_str); 545 | y = ft_printf(" Ft:\t.6s [%.6s]\n", null_str); 546 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 547 | x = printf(" Or:\t.6s [%.7s]\n", null_str); 548 | y = ft_printf(" Ft:\t.6s [%.7s]\n", null_str); 549 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 550 | } 551 | 552 | void test_invalid_spec(void) 553 | { 554 | int x, y; 555 | 556 | x = 0; 557 | y = 0; 558 | printf("\n----------- TEST: INVALID TYPE -----------\n\n"); 559 | 560 | x = printf(" Or\t:\tInvalid: [%%w] = [%w]\n"); 561 | y = ft_printf(" Ft\t:\tInvalid: [%%w] = [%w]\n"); 562 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 563 | 564 | x = printf(" Or\t:\tInvalid: [%%0] = [%0]\n"); 565 | y = ft_printf(" Ft\t:\tInvalid: [%%0] = [%0]\n"); 566 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 567 | 568 | x = printf(" Or\t:\tInvalid: [%%14k] = [%14k]\n"); 569 | y = ft_printf(" Ft\t:\tInvalid: [%%14k] = [%14k]\n"); 570 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 571 | 572 | x = printf(" Or\t:\tInvalid: [%%-14k] = [%-14k]\n"); 573 | y = ft_printf(" Ft\t:\tInvalid: [%%-14k] = [%-14k]\n"); 574 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 575 | 576 | x = printf(" Or\t:\tInvalid: [%%wawa] = [%wawa]\n"); 577 | y = ft_printf(" Ft\t:\tInvalid: [%%wawa] = [%wawa]\n"); 578 | printf("Printf = %d, ft_printf = %d\n\n", x, y); 579 | } 580 | 581 | int main(void) 582 | { 583 | printf("\n\n========== MANDATORY TESTS ========\n\n"); 584 | test_simple(); 585 | test_percent(); 586 | test_c(); 587 | test_s(); 588 | test_d(); 589 | test_i(); 590 | test_x(); 591 | test_X(); 592 | test_u(); 593 | test_p(); 594 | test_all(); 595 | 596 | printf("\n\n========== BONUS TESTS ============\n\n"); 597 | test_bonus_char(); 598 | test_bonus_str(); 599 | test_bonus_hex(); 600 | test_bonus_unsigned(); 601 | test_bonus_int(); 602 | test_bonus_ptr(); 603 | test_bonus_precision(); 604 | test_extra(); 605 | test_invalid_spec(); 606 | 607 | return (0); 608 | } 609 | -------------------------------------------------------------------------------- /testing/test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcombeau/ft_printf/5f315d7ed96c02ed3fe48b39e642d87b6fca2739/testing/test --------------------------------------------------------------------------------