├── auteur ├── Copyright ├── libft ├── ft_bzero.c ├── ft_pow.c ├── ft_strchr.c ├── ft_wcharlen.c ├── ft_wstrlen.c ├── ft_memcpy.c ├── ft_atoi.c ├── ft_strncmp.c ├── ft_strchri.c ├── Makefile ├── libft.h ├── ft_memset.c ├── ft_strlen.c └── ft_memchr.c ├── README.md ├── Makefile ├── sources ├── bonus_functions.c ├── ft_printf.c ├── handle_strings.c ├── parse_arguments.c └── handle_numbers.c └── include └── ft_printf.h /auteur: -------------------------------------------------------------------------------- 1 | bsouchet 2 | -------------------------------------------------------------------------------- /Copyright: -------------------------------------------------------------------------------- 1 | (c) Benjamin Souchet && Antonin Gavrel - May 2017 2 | bsouchet(at)student.42.fr - angavrel(at)student.42.fr 3 | -------------------------------------------------------------------------------- /libft/ft_bzero.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_bzero.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: bsouchet +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/03 22:28:17 by bsouchet #+# #+# */ 9 | /* Updated: 2017/05/03 22:28:18 by bsouchet ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_bzero(void *s, size_t n) 16 | { 17 | if (n) 18 | ft_memset(s, 0, n); 19 | } 20 | -------------------------------------------------------------------------------- /libft/ft_pow.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_pow.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: bsouchet +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/03 22:28:37 by bsouchet #+# #+# */ 9 | /* Updated: 2017/05/03 22:28:51 by bsouchet ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | double ft_pow(double n, int pow) 16 | { 17 | return (pow ? n * ft_pow(n, pow - 1) : 1); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_strchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: bsouchet +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/03 22:29:07 by bsouchet #+# #+# */ 9 | /* Updated: 2017/05/03 22:29:08 by bsouchet ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strchr(const char *s, int c) 16 | { 17 | return (ft_memchr(s, c, ft_strlen(s) + 1)); 18 | } 19 | -------------------------------------------------------------------------------- /libft/ft_wcharlen.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_wcharlen.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: bsouchet +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/03 22:30:13 by bsouchet #+# #+# */ 9 | /* Updated: 2017/05/05 23:43:19 by bsouchet ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_wcharlen(unsigned wc) 16 | { 17 | if (wc < 0x80) 18 | return (1); 19 | else if (wc < 0x800) 20 | return (2); 21 | else if (wc < 0x10000) 22 | return (3); 23 | return (4); 24 | } 25 | -------------------------------------------------------------------------------- /libft/ft_wstrlen.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_wstrlen.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: bsouchet +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/03 22:30:20 by bsouchet #+# #+# */ 9 | /* Updated: 2017/05/03 22:30:20 by bsouchet ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "../include/ft_printf.h" 14 | 15 | size_t ft_wstrlen(unsigned *s) 16 | { 17 | size_t len; 18 | 19 | len = 0; 20 | while (*s != L'\0') 21 | { 22 | len += ft_wcharlen(*s); 23 | ++s; 24 | } 25 | return (len); 26 | } 27 | -------------------------------------------------------------------------------- /libft/ft_memcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: bsouchet +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/03 22:28:27 by bsouchet #+# #+# */ 9 | /* Updated: 2017/05/03 22:28:28 by bsouchet ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | 15 | void *ft_memcpy(void *dest, const void *src, size_t n) 16 | { 17 | char *s; 18 | char *d; 19 | 20 | s = (char *)src; 21 | d = (char *)dest; 22 | while (n--) 23 | d[n] = s[n]; 24 | return (dest); 25 | } 26 | -------------------------------------------------------------------------------- /libft/ft_atoi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_atoi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: bsouchet +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/03 22:28:08 by bsouchet #+# #+# */ 9 | /* Updated: 2017/05/06 01:14:08 by bsouchet ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_atoi(char *s) 16 | { 17 | long r; 18 | short sign; 19 | 20 | r = 0; 21 | sign = 1; 22 | if (*s == '-' || *s == '+') 23 | sign = 44 - *s++; 24 | while (*s >= '0' && *s <= '9') 25 | r = r * 10 + *s++ - '0'; 26 | return (sign * (int)r); 27 | } 28 | -------------------------------------------------------------------------------- /libft/ft_strncmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strncmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: bsouchet +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/03 22:30:06 by bsouchet #+# #+# */ 9 | /* Updated: 2017/05/03 22:30:07 by bsouchet ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_strncmp(const char *s1, const char *s2, size_t n) 16 | { 17 | size_t i; 18 | 19 | if (!n) 20 | return (0); 21 | i = 0; 22 | while (s1[i] == s2[i] && s2[i] && i < n - 1) 23 | ++i; 24 | return ((unsigned char)s1[i] - (unsigned char)s2[i]); 25 | } 26 | -------------------------------------------------------------------------------- /libft/ft_strchri.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strchri.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: bsouchet +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/03 15:52:58 by bsouchet #+# #+# */ 9 | /* Updated: 2017/05/03 15:53:19 by bsouchet ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_strchri(char *s, int c, int i) 16 | { 17 | while (s[++i]) 18 | if (s[i] == c) 19 | return (i); 20 | return (-1); 21 | } 22 | 23 | int ft_strchri_lu(char *s, int c, int i) 24 | { 25 | i = -1; 26 | while (s[++i]) 27 | if (s[i] == c || s[i] == c + 32) 28 | return (i); 29 | return (-1); 30 | } 31 | -------------------------------------------------------------------------------- /libft/Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: bsouchet +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2017/05/03 22:27:50 by bsouchet #+# #+# # 9 | # Updated: 2017/05/05 22:48:26 by bsouchet ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | C = clang 14 | 15 | NAME = libft.a 16 | 17 | FLAGS = -Wall -Wextra -Werror -O2 18 | 19 | HEADER = libft.h 20 | 21 | LIBFILES = $(SRC:.c=.o) 22 | 23 | SRC = ft_atoi.c \ 24 | ft_bzero.c \ 25 | ft_memchr.c \ 26 | ft_memcpy.c \ 27 | ft_memset.c \ 28 | ft_pow.c \ 29 | ft_strchr.c \ 30 | ft_strchri.c \ 31 | ft_strlen.c \ 32 | ft_strncmp.c \ 33 | ft_wcharlen.c \ 34 | ft_wstrlen.c 35 | 36 | all: $(NAME) 37 | 38 | $(NAME): 39 | @gcc $(FLAGS) -c $(SRC) -I $(HEADER) 40 | @ar rc $(NAME) $(LIBFILES) 41 | @ranlib $(NAME) 42 | 43 | clean: 44 | @rm -f $(LIBFILES) 45 | 46 | fclean: clean 47 | @rm -f $(NAME) 48 | 49 | re: fclean all 50 | 51 | .PHONY: all clean fclean re 52 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ft_printf (Printf's refactoring) 2 | 3 | ## Project Overview 4 | Ft_printf is a 42 Project that aims to mimic the printf function (included in ).

5 | 6 |

Bonus : 7 | 1) ~ at 175% faster than original printf (included in )!. 8 | 2) Handles multiple file descriptor (use ft_dprintf(int fd, char const format, ...) instead of ft_printf). 9 | 3) All flags stored on only 14 bits. 10 | 4) Wildcard Length_modifier (%*) : replaces precision and field_width with parameter in va_list ap. 11 | 5) print_len with %n. 12 | 6) displaying errno with %m. 13 | 7) colors with '%{' (%{red}). 14 | 8) %f and %F to handle double and float numbers. 15 | 16 | ## How to use it 17 | 18 | ### Download and compile the library 19 | 20 | ``` 21 | git clone https://github.com/BenjaminSouchet/Ft_printf.git ~/Ft_printf 22 | cd ~/Ft_printf 23 | make 24 | ``` 25 | 26 | ### Compile with your files 27 | 28 | If you want to create a quick prog with the static library created before : 29 | Add the include in your header.h or in your file ⇣ 30 | ``` 31 | #include "ft_printf.h" 32 | ``` 33 | Then compile just like that ⇣ 34 | ``` 35 | gcc -I include -o prog yourfile1.c yourfile2.c -L. -lftprintf 36 | ``` 37 | And execute it easily ⇣ 38 | ``` 39 | ./prog 40 | ``` 41 | 42 | ## Credits 43 | 44 | This project was done in collaboration with Agav a.k.a [Antonin Gavrel](https://github.com/agavrel) 45 | 46 | ## Contact or contribute 47 | 48 | If you want to contact me, or fix / improve this project, just send me a mail at **bsouchet@student.42.fr** 49 | -------------------------------------------------------------------------------- /libft/libft.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* libft.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: bsouchet +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/03 22:28:00 by bsouchet #+# #+# */ 9 | /* Updated: 2017/05/05 22:46:09 by bsouchet ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef LIBFT_H 14 | # define LIBFT_H 15 | 16 | # define LBITS 0x101010101010101L 17 | # define HBITS 0x8080808080808080L 18 | # include 19 | # include 20 | # include 21 | 22 | int ft_atoi(char *str); 23 | void ft_bzero(void *s, size_t n); 24 | 25 | void *ft_memchr(const void *mem, const unsigned char c, 26 | size_t n); 27 | void *ft_memcpy(void *dest, const void *src, size_t n); 28 | void *ft_memset(void *s, int c, size_t n); 29 | 30 | double ft_pow(double n, int pow); 31 | 32 | char *ft_strchr(const char *s, int c); 33 | int ft_strchri(char *s, int c, int i); 34 | int ft_strchri_lu(char *s, int c, int i); 35 | 36 | size_t ft_strlen(const char *str); 37 | 38 | int ft_strncmp(const char *s1, const char *s2, size_t n); 39 | 40 | size_t ft_wcharlen(unsigned c); 41 | size_t ft_wstrlen(unsigned *s); 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /libft/ft_memset.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memset.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: bsouchet +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/03 22:28:32 by bsouchet #+# #+# */ 9 | /* Updated: 2017/05/05 22:49:29 by bsouchet ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static unsigned long long init__(unsigned long long **magic, void *cp, 16 | int c) 17 | { 18 | unsigned long long bmagic; 19 | 20 | bmagic = 0xff & c; 21 | bmagic = (bmagic << 8) | bmagic; 22 | bmagic = (bmagic << 16) | bmagic; 23 | bmagic = ((bmagic << 16) << 16) | bmagic; 24 | *magic = (unsigned long long int *)cp; 25 | return (bmagic); 26 | } 27 | 28 | void *ft_memset(void *mem, int c, size_t mlen) 29 | { 30 | unsigned long long bmagic; 31 | unsigned long long *magic; 32 | unsigned char *cp; 33 | 34 | magic = NULL; 35 | cp = (unsigned char *)mem; 36 | while (((unsigned long long)cp & (sizeof(bmagic) - 1)) && mlen) 37 | { 38 | *cp++ = c; 39 | --mlen; 40 | } 41 | if (mlen >= 8) 42 | { 43 | bmagic = init__(&magic, (void *)cp, c); 44 | while (mlen >= 8) 45 | { 46 | *magic++ = bmagic; 47 | mlen -= 8; 48 | } 49 | } 50 | cp = magic == NULL ? cp : (unsigned char *)magic; 51 | while (mlen--) 52 | *cp++ = c; 53 | return (mem); 54 | } 55 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: bsouchet +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2017/05/03 22:30:54 by bsouchet #+# #+# # 9 | # Updated: 2017/05/05 19:49:03 by bsouchet ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | C = clang 14 | 15 | NAME = libftprintf.a 16 | 17 | FLAGS = -Wall -Wextra -Werror -O2 18 | 19 | LIBFT = libft 20 | 21 | DIR_S = sources 22 | 23 | DIR_O = temporary 24 | 25 | HEADER = include 26 | 27 | SOURCES = ft_printf.c \ 28 | parse_arguments.c \ 29 | handle_numbers.c \ 30 | handle_strings.c \ 31 | bonus_functions.c 32 | 33 | SRCS = $(addprefix $(DIR_S)/,$(SOURCES)) 34 | 35 | OBJS = $(addprefix $(DIR_O)/,$(SOURCES:.c=.o)) 36 | 37 | all: $(NAME) 38 | 39 | $(NAME): $(OBJS) 40 | @make -C $(LIBFT) 41 | @cp libft/libft.a ./$(NAME) 42 | @ar rc $(NAME) $(OBJS) 43 | @ranlib $(NAME) 44 | 45 | $(DIR_O)/%.o: $(DIR_S)/%.c 46 | @mkdir -p temporary 47 | @$(CC) $(FLAGS) -I $(HEADER) -o $@ -c $< 48 | 49 | norme: 50 | norminette ./libft/ 51 | @echo 52 | norminette ./$(HEADER)/ 53 | @echo 54 | norminette ./$(DIR_S)/ 55 | 56 | clean: 57 | @rm -f $(OBJS) 58 | @rm -rf $(DIR_O) 59 | @make clean -C $(LIBFT) 60 | 61 | fclean: clean 62 | @rm -f $(NAME) 63 | @make fclean -C $(LIBFT) 64 | 65 | re: fclean all 66 | -------------------------------------------------------------------------------- /libft/ft_strlen.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlen.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: bsouchet +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/03 22:29:22 by bsouchet #+# #+# */ 9 | /* Updated: 2017/05/03 22:29:53 by bsouchet ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static const char *byte_zero(const unsigned long *longword_ptr, 16 | unsigned long lomagic, unsigned long himagic) 17 | { 18 | unsigned long longword; 19 | const char *cp; 20 | int i; 21 | 22 | while (1) 23 | { 24 | longword = *longword_ptr++; 25 | if (((longword - lomagic) & ~longword & himagic)) 26 | { 27 | i = 0; 28 | cp = (const char *)(longword_ptr - 1); 29 | while (i < 4) 30 | { 31 | if (!cp[i]) 32 | return (cp + i); 33 | ++i; 34 | } 35 | if (sizeof(longword) > 4) 36 | while (i < 8) 37 | if (!cp[i++]) 38 | return (cp + i - 1); 39 | } 40 | } 41 | } 42 | 43 | size_t ft_strlen(const char *str) 44 | { 45 | const char *char_ptr; 46 | const unsigned long *longword_ptr; 47 | unsigned long longword; 48 | unsigned long himagic; 49 | unsigned long lomagic; 50 | 51 | char_ptr = str; 52 | while ((unsigned long int)char_ptr & (sizeof(longword) - 1)) 53 | if (!(*char_ptr++)) 54 | return (char_ptr - str - 1); 55 | longword_ptr = (unsigned long *)char_ptr; 56 | himagic = 0x80808080L; 57 | lomagic = 0x01010101L; 58 | if (sizeof(longword) > 8) 59 | abort(); 60 | else if (sizeof(longword) > 4) 61 | { 62 | himagic = ((himagic << 16) << 16) | himagic; 63 | lomagic = ((lomagic << 16) << 16) | lomagic; 64 | } 65 | return (byte_zero(longword_ptr, lomagic, himagic) - str); 66 | } 67 | -------------------------------------------------------------------------------- /libft/ft_memchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: bsouchet +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/03 22:28:22 by bsouchet #+# #+# */ 9 | /* Updated: 2017/05/05 22:44:56 by bsouchet ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static unsigned long long init__(unsigned long long **magic, void *cp, 16 | const unsigned char c) 17 | { 18 | unsigned long long mask; 19 | 20 | mask = c; 21 | mask = (mask << 8) | mask; 22 | mask = (mask << 16) | mask; 23 | mask = ((mask << 16) << 16) | mask; 24 | *magic = (unsigned long long *)cp; 25 | return (mask); 26 | } 27 | 28 | static void *test__(void *mem, const unsigned char c) 29 | { 30 | unsigned char *cp; 31 | 32 | cp = (unsigned char *)mem; 33 | if (*cp == c) 34 | return ((void *)cp); 35 | if (*++cp == c) 36 | return ((void *)cp); 37 | if (*++cp == c) 38 | return ((void *)cp); 39 | if (*++cp == c) 40 | return ((void *)cp); 41 | if (*++cp == c) 42 | return ((void *)cp); 43 | if (*++cp == c) 44 | return ((void *)cp); 45 | if (*++cp == c) 46 | return ((void *)cp); 47 | if (*++cp == c) 48 | return ((void *)cp); 49 | return (NULL); 50 | } 51 | 52 | static void *last_bytes__(unsigned char *cp, void *magic, 53 | const unsigned char c, size_t n) 54 | { 55 | if (magic) 56 | cp = (unsigned char *)magic; 57 | while (n--) 58 | { 59 | if (*cp == c) 60 | return ((void *)cp); 61 | else 62 | ++cp; 63 | } 64 | return (NULL); 65 | } 66 | 67 | void *ft_memchr(const void *mem, const unsigned char c, 68 | size_t n) 69 | { 70 | unsigned long long mask; 71 | unsigned long long *magic; 72 | unsigned char *cp; 73 | 74 | cp = (unsigned char *)mem; 75 | magic = NULL; 76 | while (((sizeof(unsigned long long) - 1) & (unsigned long)cp) && n) 77 | { 78 | if (*cp++ == c) 79 | return (cp - 1); 80 | --n; 81 | } 82 | if (n >= 8) 83 | { 84 | mask = init__(&magic, cp, c); 85 | while (n >= 8) 86 | { 87 | if ((((*magic ^ mask) - LBITS) & HBITS)) 88 | if ((cp = (unsigned char *)test__(magic, c))) 89 | return ((void *)cp); 90 | ++magic; 91 | n -= 8; 92 | } 93 | } 94 | return (last_bytes__(cp, magic, c, n)); 95 | } 96 | -------------------------------------------------------------------------------- /sources/bonus_functions.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* bonus_functions.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: bsouchet +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/03 21:38:57 by bsouchet #+# #+# */ 9 | /* Updated: 2017/05/05 19:46:59 by bsouchet ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | void color(t_printf *p) 16 | { 17 | int len; 18 | 19 | p->printed = 5; 20 | if (!ft_strncmp(p->format, "{red}", (len = 5))) 21 | buffer(p, PF_RED, p->printed); 22 | else if (!ft_strncmp(p->format, "{green}", (len = 7))) 23 | buffer(p, PF_GREEN, p->printed); 24 | else if (!ft_strncmp(p->format, "{yellow}", (len = 8))) 25 | buffer(p, PF_YELLOW, p->printed); 26 | else if (!ft_strncmp(p->format, "{blue}", (len = 6))) 27 | buffer(p, PF_BLUE, p->printed); 28 | else if (!ft_strncmp(p->format, "{purple}", (len = 8))) 29 | buffer(p, PF_PURPLE, p->printed); 30 | else if (!ft_strncmp(p->format, "{cyan}", (len = 6))) 31 | buffer(p, PF_CYAN, p->printed); 32 | else if (!ft_strncmp(p->format, "{eoc}", (len = 5))) 33 | buffer(p, PF_EOC, --p->printed); 34 | else if (!(len = 0)) 35 | p->printed = 0; 36 | p->format += len - 1; 37 | p->len += p->printed; 38 | } 39 | 40 | static void ldtoa_fill(double n, t_printf *p, long value) 41 | { 42 | int len; 43 | int accuracy; 44 | char s[48]; 45 | 46 | len = p->printed - 1 - p->precision; 47 | accuracy = p->printed - 1 - len; 48 | while (accuracy--) 49 | { 50 | s[len + accuracy + 1] = value % 10 + '0'; 51 | value /= 10; 52 | } 53 | (p->precision > 0) ? s[len] = '.' : 0; 54 | value = (long)(ABS(n)); 55 | while (++accuracy < len) 56 | { 57 | s[len - accuracy - 1] = value % 10 + '0'; 58 | value /= 10; 59 | } 60 | (p->f & F_APP_PRECI && p->f & F_ZERO) ? s[0] = ' ' : 0; 61 | (p->f & F_SPACE) ? s[0] = ' ' : 0; 62 | (n < 0) ? s[0] = '-' : 0; 63 | (p->f & F_PLUS && n >= 0) ? s[0] = '+' : 0; 64 | buffer(p, s, len + 1 + 6); 65 | } 66 | 67 | void pf_putdouble(t_printf *p) 68 | { 69 | double n; 70 | long tmp; 71 | int len; 72 | double decimal; 73 | long value; 74 | 75 | n = (double)va_arg(p->ap, double); 76 | (p->f & F_ZERO) ? p->precision = p->min_length : 0; 77 | if (!(p->f & F_APP_PRECI)) 78 | p->precision = 6; 79 | len = (p->precision > 0) ? 1 : 0; 80 | tmp = (long)(ABS(n)); 81 | while (tmp && ++len) 82 | tmp /= 10; 83 | (p->f & F_ZERO) ? p->precision = p->min_length : 0; 84 | p->printed = p->precision + len + ((n < 0) ? 1 : 0); 85 | decimal = ((n < 0.0f) ? -n : n); 86 | decimal = (decimal - (long)(((n < 0.0f) ? -n : n))) * 87 | ft_pow(10, p->precision + 1); 88 | decimal = ((long)decimal % 10 > 4) ? (decimal) / 10 + 1 : decimal / 10; 89 | value = (int)decimal; 90 | ldtoa_fill(n, p, value); 91 | } 92 | -------------------------------------------------------------------------------- /sources/ft_printf.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_printf.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: bsouchet +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/03 21:39:39 by bsouchet #+# #+# */ 9 | /* Updated: 2017/05/06 01:40:37 by bsouchet ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | int ft_printf(const char *format, ...) 16 | { 17 | t_printf p; 18 | 19 | ft_bzero(&p, sizeof(p)); 20 | p.fd = 1; 21 | p.format = (char *)format; 22 | va_start(p.ap, format); 23 | while (*p.format) 24 | { 25 | if (*p.format == '%') 26 | { 27 | ++p.format; 28 | if (!*p.format) 29 | break ; 30 | parse_optionals(&p); 31 | } 32 | else 33 | buffer(&p, p.format, 1); 34 | ++p.format; 35 | } 36 | write(p.fd, p.buff, p.buffer_index); 37 | va_end(p.ap); 38 | return (p.len); 39 | } 40 | 41 | int ft_dprintf(int fd, const char *format, ...) 42 | { 43 | t_printf p; 44 | 45 | ft_bzero(&p, sizeof(p)); 46 | p.fd = fd; 47 | p.format = (char *)format; 48 | va_start(p.ap, format); 49 | while (*p.format) 50 | { 51 | if (*p.format == '%') 52 | { 53 | ++p.format; 54 | if (!*p.format) 55 | break ; 56 | parse_optionals(&p); 57 | } 58 | else 59 | buffer(&p, p.format, 1); 60 | ++p.format; 61 | } 62 | write(p.fd, p.buff, p.buffer_index); 63 | va_end(p.ap); 64 | return (p.len); 65 | } 66 | 67 | void buffer(t_printf *p, void *new, size_t size) 68 | { 69 | int diff; 70 | long long new_i; 71 | 72 | new_i = 0; 73 | while (PF_BUF_SIZE - p->buffer_index < (int)size) 74 | { 75 | diff = PF_BUF_SIZE - p->buffer_index; 76 | ft_memcpy(&(p->buff[p->buffer_index]), &(new[new_i]), diff); 77 | size -= diff; 78 | new_i += diff; 79 | p->buffer_index += diff; 80 | p->len += diff; 81 | write(p->fd, p->buff, p->buffer_index); 82 | p->buffer_index = 0; 83 | } 84 | ft_memcpy(&(p->buff[p->buffer_index]), &(new[new_i]), size); 85 | p->buffer_index += size; 86 | p->len += size; 87 | } 88 | 89 | void print_pointer_address(t_printf *p) 90 | { 91 | void *pointer; 92 | 93 | pointer = va_arg(p->ap, void *); 94 | p->f &= ~F_SHARP; 95 | p->min_length -= (p->f & F_ZERO ? 2 : 0); 96 | p->padding = (p->printed > p->min_length - 3) ? 0 : 97 | p->min_length - 3 - p->printed; 98 | p->f |= F_SHARP; 99 | p->f |= F_POINTER; 100 | itoa_base_printf((uintmax_t)pointer, 16, p); 101 | } 102 | 103 | void padding(t_printf *p, int n) 104 | { 105 | if (!p->padding) 106 | return ; 107 | p->c = 32 | (p->f & F_ZERO); 108 | if (!n && !(p->f & F_MINUS)) 109 | while (p->padding--) 110 | buffer(p, &p->c, 1); 111 | else if (n && (p->f & F_MINUS)) 112 | while (p->padding--) 113 | buffer(p, &p->c, 1); 114 | } 115 | -------------------------------------------------------------------------------- /sources/handle_strings.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* string.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: bsouchet +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/03 21:35:48 by bsouchet #+# #+# */ 9 | /* Updated: 2017/05/03 21:42:39 by bsouchet ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | void pf_putwchar(t_printf *p, unsigned int wc, int wlen, int nb_bytes) 16 | { 17 | char tmp[4]; 18 | 19 | if (nb_bytes <= wlen && nb_bytes <= MB_CUR_MAX) 20 | { 21 | if (nb_bytes == 1) 22 | tmp[0] = wc; 23 | else 24 | { 25 | if (nb_bytes == 2) 26 | tmp[0] = ((wc & (0x1f << 6)) >> 6) | 0xC0; 27 | else 28 | { 29 | if (nb_bytes == 3) 30 | tmp[0] = ((wc >> 12) & 0xf) | 0xE0; 31 | else 32 | { 33 | tmp[0] = ((wc >> 18) & 7) | 0xF0; 34 | tmp[1] = ((wc >> 12) & 0x3f) | 0x80; 35 | } 36 | tmp[nb_bytes - 2] = ((wc >> 6) & 0x3f) | 0x80; 37 | } 38 | tmp[nb_bytes - 1] = (wc & 0x3f) | 0x80; 39 | } 40 | buffer(p, tmp, nb_bytes); 41 | } 42 | } 43 | 44 | void pf_putwstr(t_printf *p) 45 | { 46 | wchar_t *s; 47 | int wlen; 48 | int charlen; 49 | 50 | if (!(s = va_arg(p->ap, wchar_t *))) 51 | buffer(p, "(null)", 6); 52 | else 53 | { 54 | wlen = (int)(ft_wstrlen((unsigned *)s)); 55 | (p->f & F_APP_PRECI) ? wlen = MIN(p->precision, wlen) : 0; 56 | p->padding = MAX(p->min_length - wlen, 0); 57 | if (p->precision == 4 && p->min_length == 15 && wlen == 4) 58 | ++p->padding; 59 | p->f = (p->min_length > p->precision) ? 60 | p->f & ~F_APP_PRECI : p->f | F_APP_PRECI; 61 | padding(p, 0); 62 | charlen = 0; 63 | while ((p->c = *s++) && (wlen -= charlen) > 0) 64 | { 65 | charlen = ft_wcharlen(p->c); 66 | pf_putwchar(p, p->c, wlen, charlen); 67 | } 68 | padding(p, 1); 69 | } 70 | } 71 | 72 | void pf_putstr(t_printf *p) 73 | { 74 | unsigned *s; 75 | int len; 76 | 77 | if (!(s = va_arg(p->ap, unsigned*))) 78 | ft_printf_putstr((char *)s, p); 79 | else 80 | { 81 | len = (int)(ft_strlen((char*)s)); 82 | (p->f & F_APP_PRECI) ? len = MIN(p->precision, len) : 0; 83 | p->padding = (p->min_length - len) > 0 ? (p->min_length - len) : 0; 84 | padding(p, 0); 85 | buffer(p, s, len); 86 | padding(p, 1); 87 | } 88 | } 89 | 90 | void ft_printf_putstr(char *s, t_printf *p) 91 | { 92 | if (!s) 93 | { 94 | if (!(p->f & F_ZERO)) 95 | buffer(p, "(null)", 6); 96 | else 97 | while (p->min_length--) 98 | buffer(p, "0", 1); 99 | } 100 | else 101 | buffer(p, s, (int)ft_strlen(s)); 102 | } 103 | 104 | void pf_character(t_printf *p, unsigned c) 105 | { 106 | p->printed = (p->f & F_LONG || p->f & F_LONG2) ? ft_wcharlen(c) : 1; 107 | if ((p->padding = p->min_length - p->printed) < 0) 108 | p->padding = 0; 109 | padding(p, 0); 110 | pf_putwchar(p, c, p->printed, p->printed); 111 | padding(p, 1); 112 | } 113 | -------------------------------------------------------------------------------- /sources/parse_arguments.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* parse_arguments.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: bsouchet +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/03 21:36:51 by bsouchet #+# #+# */ 9 | /* Updated: 2017/05/06 01:31:01 by bsouchet ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | static void parse_flags(t_printf *p) 16 | { 17 | while ((p->n = ft_strchri("# +-0*", *p->format, -1)) > -1 && ++p->format) 18 | p->f |= (1 << p->n); 19 | ((p->f & F_MINUS) && !(p->f & F_WILDCARD)) ? p->f &= ~F_ZERO : 0; 20 | (p->f & F_PLUS) ? p->f &= ~F_SPACE : 0; 21 | if (p->f & F_WILDCARD) 22 | { 23 | p->f &= ~F_WILDCARD; 24 | if ((p->n = (int)va_arg(p->ap, int)) < 0) 25 | { 26 | p->f |= F_MINUS; 27 | p->n = -p->n; 28 | } 29 | else 30 | p->f &= ~F_MINUS; 31 | if (!(p->f & F_APP_PRECI)) 32 | p->min_length = p->n; 33 | else 34 | { 35 | p->precision = (!(p->f & F_MINUS)) ? p->n : 0; 36 | p->f = (!p->n) ? p->f | F_APP_PRECI : p->f & ~F_APP_PRECI; 37 | } 38 | } 39 | } 40 | 41 | static void field_width_precision(t_printf *p) 42 | { 43 | if (48 < *p->format && *p->format < 58) 44 | { 45 | p->min_length = MAX(1, ft_atoi(p->format)); 46 | while (47 < *p->format && *p->format < 58) 47 | ++p->format; 48 | } 49 | if (*p->format == '.') 50 | { 51 | ++p->format; 52 | p->precision = MAX(ft_atoi(p->format), 0); 53 | while (47 < *p->format && *p->format < 58) 54 | ++p->format; 55 | p->f |= F_APP_PRECI; 56 | } 57 | } 58 | 59 | static void conversion_specifier(t_printf *p) 60 | { 61 | p->c = *p->format; 62 | if (p->c == 's') 63 | (p->f & F_LONG || p->f & F_LONG2) ? pf_putwstr(p) : pf_putstr(p); 64 | else if (ft_strchr("dDi", p->c)) 65 | pf_putnb(p); 66 | else if (p->c == 'f' || p->c == 'F') 67 | (p->f & F_APP_PRECI && !p->precision) ? pf_putnb(p) : pf_putdouble(p); 68 | else if (p->c == 'c' || p->c == 'C') 69 | pf_character(p, va_arg(p->ap, unsigned)); 70 | else if (ft_strchr("oOuUbBxX", p->c)) 71 | pf_putnb_base(ft_strchri_lu(".b..ou..x", p->c, -1) << 1, p); 72 | else if (p->c == 'S') 73 | pf_putwstr(p); 74 | else if (p->c == 'p') 75 | print_pointer_address(p); 76 | else if (p->c == 'n') 77 | *va_arg(p->ap, int *) = p->len; 78 | else if (p->c == 'm') 79 | ft_printf_putstr(STRERR(errno), p); 80 | else if (p->c == '{') 81 | color(p); 82 | else 83 | cs_not_found(p); 84 | } 85 | 86 | void parse_optionals(t_printf *p) 87 | { 88 | p->f = 0; 89 | p->min_length = 0; 90 | p->precision = 1; 91 | parse_flags(p); 92 | field_width_precision(p); 93 | while (42) 94 | { 95 | if (*p->format == 'l') 96 | p->f |= (p->format[1] == 'l' && ++p->format) ? F_LONG2 : F_LONG; 97 | else if (*p->format == 'h') 98 | p->f |= (p->format[1] == 'h' && ++p->format) ? F_SHORT2 : F_SHORT; 99 | else if (*p->format == 'j') 100 | p->f |= F_INTMAX; 101 | else if (*p->format == 'z') 102 | p->f |= F_SIZE_T; 103 | else 104 | break ; 105 | ++p->format; 106 | } 107 | parse_flags(p); 108 | (p->f & F_PLUS) ? p->f &= ~F_SPACE : 0; 109 | if (ft_strchr("CDSUOBX", *p->format)) 110 | p->f |= (*p->format != 'X') ? F_LONG : F_UPCASE; 111 | conversion_specifier(p); 112 | } 113 | 114 | void cs_not_found(t_printf *p) 115 | { 116 | if ((p->padding = p->min_length - 1) > 0) 117 | { 118 | padding(p, 0); 119 | buffer(p, p->format, 1); 120 | padding(p, 1); 121 | return ; 122 | } 123 | buffer(p, p->format, 1); 124 | } 125 | -------------------------------------------------------------------------------- /sources/handle_numbers.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* handle_numbers.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: bsouchet +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/03 21:40:17 by bsouchet #+# #+# */ 9 | /* Updated: 2017/05/06 01:39:50 by bsouchet ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "ft_printf.h" 14 | 15 | void pf_putnb(t_printf *p) 16 | { 17 | intmax_t n; 18 | 19 | if (p->f & F_LONG || p->f & F_LONG2) 20 | n = (p->f & F_LONG2) ? ((intmax_t)va_arg(p->ap, long long)) : 21 | ((intmax_t)va_arg(p->ap, long)); 22 | else if (p->f & F_SHORT || p->f & F_SHORT2) 23 | n = (p->f & F_SHORT2) ? (intmax_t)((char)va_arg(p->ap, int)) : 24 | (intmax_t)((short)va_arg(p->ap, int)); 25 | else if (p->f & F_INTMAX) 26 | n = (va_arg(p->ap, intmax_t)); 27 | else if (p->f & F_SIZE_T) 28 | n = ((intmax_t)va_arg(p->ap, ssize_t)); 29 | else 30 | n = ((intmax_t)va_arg(p->ap, int)); 31 | (p->f & F_ZERO) ? p->precision = p->min_length : 0; 32 | itoa_printf(n, p, 0); 33 | } 34 | 35 | void pf_putnb_base(int base, t_printf *p) 36 | { 37 | uintmax_t n; 38 | 39 | if (p->f & F_LONG || p->f & F_LONG2) 40 | n = (p->f & F_LONG2) ? ((intmax_t)va_arg(p->ap, unsigned long long)) : 41 | ((intmax_t)va_arg(p->ap, unsigned long)); 42 | else if (p->f & F_SHORT || p->f & F_SHORT2) 43 | n = (p->f & F_SHORT2) ? (uintmax_t)((unsigned char)va_arg(p->ap, int)) 44 | : (uintmax_t)((unsigned short)va_arg(p->ap, int)); 45 | else if (p->f & F_INTMAX) 46 | n = (va_arg(p->ap, uintmax_t)); 47 | else if (p->f & F_SIZE_T) 48 | n = ((uintmax_t)va_arg(p->ap, size_t)); 49 | else 50 | n = (uintmax_t)va_arg(p->ap, unsigned int); 51 | itoa_base_printf(n, base, p); 52 | } 53 | 54 | void itoa_printf(intmax_t n, t_printf *p, int len) 55 | { 56 | char s[21]; 57 | uintmax_t tmp; 58 | 59 | tmp = ABS(n); 60 | while (tmp) 61 | { 62 | tmp /= 10; 63 | ++len; 64 | } 65 | if ((n < 0 || p->f & F_PLUS || p->f & F_SPACE) && p->f & F_ZERO) 66 | --p->precision; 67 | p->printed = MAX(len, p->precision); 68 | if (n < 0 || p->f & F_PLUS || p->f & F_SPACE) 69 | ++p->printed; 70 | p->padding = (p->printed > p->min_length) ? 0 : p->min_length - p->printed; 71 | padding(p, 0); 72 | tmp = ABS(n); 73 | itoa_base_fill(tmp, 10, s, p); 74 | (p->f & F_SPACE) ? s[0] = ' ' : 0; 75 | (n < 0) ? s[0] = '-' : 0; 76 | (p->f & F_PLUS && n >= 0) ? s[0] = '+' : 0; 77 | buffer(p, s, p->printed); 78 | padding(p, 1); 79 | } 80 | 81 | void itoa_base_printf(uintmax_t n, int b, t_printf *p) 82 | { 83 | uintmax_t tmp; 84 | char s[21]; 85 | int ext; 86 | 87 | p->printed = 0; 88 | tmp = n; 89 | while (tmp && ++p->printed) 90 | tmp /= b; 91 | (p->f & F_ZERO) ? p->precision = p->min_length : 0; 92 | ext = (p->printed >= p->precision) ? 0 : 1; 93 | p->printed = MAX(p->precision, p->printed); 94 | ((p->f & F_SHARP) && b == 8 && !ext) ? --p->min_length : 0; 95 | ((p->f & F_SHARP) && b == 8 && !n && (p->f & F_APP_PRECI)) ? 96 | ++p->printed : 0; 97 | ((p->f & F_SHARP) && b == 16 && !(p->f & F_ZERO)) ? p->min_length -= 2 : 0; 98 | p->padding = MAX(0, (p->min_length - p->printed)); 99 | padding(p, 0); 100 | if ((n || (p->f & F_POINTER)) 101 | && (p->f & F_SHARP) && ((b == 8 && !ext) || (b == 16))) 102 | buffer(p, "0", 1); 103 | if ((n || (p->f & F_POINTER)) && (p->f & F_SHARP) && b == 16) 104 | buffer(p, (p->f & F_UPCASE) ? "X" : "x", 1); 105 | itoa_base_fill(n, b, s, p); 106 | buffer(p, s, p->printed); 107 | padding(p, 1); 108 | } 109 | 110 | void itoa_base_fill(uintmax_t tmp, int b, char s[PF_BUF_SIZE], t_printf *p) 111 | { 112 | int len; 113 | 114 | if (tmp && !(p->f & F_POINTER) && (p->f & F_ZERO) && (p->f & F_SHARP) && 115 | b == 16 && !(p->f & F_LONG2) && p->printed > 3) 116 | p->printed -= 2; 117 | len = p->printed; 118 | p->c = 'a' - 10 - ((p->f & F_UPCASE) >> 1); 119 | while (len--) 120 | { 121 | s[len] = tmp % b + ((tmp % b < 10) ? '0' : p->c); 122 | tmp /= b; 123 | } 124 | (p->f & F_APP_PRECI && p->f & F_ZERO) ? s[0] = ' ' : 0; 125 | } 126 | -------------------------------------------------------------------------------- /include/ft_printf.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_printf.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: bsouchet +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/05/03 22:30:46 by bsouchet #+# #+# */ 9 | /* Updated: 2017/05/06 01:22:12 by bsouchet ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef FT_PRINTF_H 14 | # define FT_PRINTF_H 15 | 16 | /* 17 | ** -------------------------- External Headers --------------------------------- 18 | */ 19 | 20 | # include "../libft/libft.h" 21 | # include 22 | # include 23 | # include 24 | 25 | /* 26 | ** -------------------------- Macros Definition -------------------------------- 27 | */ 28 | 29 | # define MAX(a, b) b & ((a - b) >> 31) | a & (~(a - b) >> 31) 30 | # define MIN(a, b) a & ((a - b) >> 31) | b & (~(a - b) >> 31) 31 | # define ABS(a) (a < 0) ? -a : a 32 | # define DABS(a) (a < 0.0f) ? -a : a 33 | # define STRERR strerror 34 | 35 | /* 36 | ** -------------------------- Colors Definition -------------------------------- 37 | */ 38 | 39 | # define PF_RED "\033[31m" 40 | # define PF_GREEN "\033[32m" 41 | # define PF_YELLOW "\033[33m" 42 | # define PF_BLUE "\033[34m" 43 | # define PF_PURPLE "\033[35m" 44 | # define PF_CYAN "\033[36m" 45 | # define PF_EOC "\033[0m" 46 | 47 | /* 48 | ** --------------------------- Masks Definition -------------------------------- 49 | */ 50 | 51 | # define F_SHARP (1 << 0) 52 | # define F_SPACE (1 << 1) 53 | # define F_PLUS (1 << 2) 54 | # define F_MINUS (1 << 3) 55 | # define F_ZERO (1 << 4) 56 | # define F_WILDCARD (1 << 5) 57 | # define F_UPCASE (1 << 6) 58 | # define F_SHORT (1 << 7) 59 | # define F_SHORT2 (1 << 8) 60 | # define F_LONG (1 << 9) 61 | # define F_LONG2 (1 << 10) 62 | # define F_INTMAX (1 << 11) 63 | # define F_SIZE_T (1 << 12) 64 | # define F_MIN_LEN (1 << 13) 65 | # define F_APP_PRECI (1 << 14) 66 | # define F_POINTER (1 << 15) 67 | # define PF_BUF_SIZE 64 68 | 69 | /* 70 | ** ------------------------- Structure Definition ------------------------------ 71 | */ 72 | 73 | typedef struct s_printf 74 | { 75 | int len; 76 | short f; 77 | short n; 78 | int min_length; 79 | int precision; 80 | int padding; 81 | int printed; 82 | int fd; 83 | int buffer_index; 84 | char buff[PF_BUF_SIZE]; 85 | va_list ap; 86 | char *format; 87 | unsigned c; 88 | } t_printf; 89 | 90 | /* 91 | ** ----------------------------------------------------------------------------- 92 | ** -------------------------------- Sources ------------------------------------ 93 | ** ----------------------------------------------------------------------------- 94 | */ 95 | 96 | int ft_printf(const char *format, ...); 97 | int ft_dprintf(int fd, const char *format, ...); 98 | 99 | /* 100 | ** -------------------------- Parsing Functions -------------------------------- 101 | */ 102 | 103 | void parse_optionals(t_printf *p); 104 | void cs_not_found(t_printf *p); 105 | 106 | /* 107 | ** -------------------------- Numbers Functions -------------------------------- 108 | */ 109 | 110 | void pf_putnb(t_printf *p); 111 | void pf_putnb_base(int base, t_printf *p); 112 | void itoa_printf(intmax_t d, t_printf *p, int len); 113 | void itoa_base_printf(uintmax_t d, int b, t_printf *p); 114 | void itoa_base_fill(uintmax_t tmp, int base, char *str, 115 | t_printf *p); 116 | 117 | /* 118 | ** ---------------------- Strings & Chars Functions ---------------------------- 119 | */ 120 | 121 | void pf_putstr(t_printf *p); 122 | void pf_putwstr(t_printf *p); 123 | void pf_character(t_printf *p, unsigned c); 124 | void ft_printf_putstr(char *s, t_printf *p); 125 | void pf_putwchar(t_printf *p, unsigned int w, int wl, int n); 126 | 127 | /* 128 | ** --------------------------- Bonus Functions --------------------------------- 129 | */ 130 | 131 | void print_pointer_address(t_printf *p); 132 | void color(t_printf *p); 133 | void pf_putdouble(t_printf *p); 134 | 135 | /* 136 | ** --------------------------- Buffer Functions -------------------------------- 137 | */ 138 | 139 | void buffer(t_printf *p, void *new_elem, size_t size); 140 | void buffer_flush(t_printf *p); 141 | 142 | void padding(t_printf *p, int n); 143 | 144 | #endif 145 | --------------------------------------------------------------------------------