diff --git a/Makefile b/Makefile index 43403a4..e5fd67b 100644 --- a/Makefile +++ b/Makefile @@ -108,10 +108,12 @@ SRCS = ft_memset.c \ ft_concat_free.c \ \ ft_printf.c \ - ft_next_word.c \ - ft_convert.c \ - ft_flag_transform.c \ - ft_flag_transform_bonus.c \ + pf_next_word.c \ + pf_convert.c \ + pf_flag_transform.c \ + pf_flag_transform_bonus.c \ + pf_utils.c \ + \ ft_abs.c \ ft_greater.c \ ft_smaller.c \ @@ -121,25 +123,6 @@ SRCS = ft_memset.c \ \ ft_arrint.c - # deleted : \ - ft_lstnew.c \ - ft_lstadd_front.c \ - ft_lstsize.c \ - ft_lstlast.c \ - ft_lstadd_back.c \ - ft_lstdelone.c \ - ft_lstclear.c \ - ft_lstiter.c \ - ft_lstmap.c \ - \ - ft_putstr.c \ - ft_putchar.c \ - ft_putendl.c \ - ft_putnbr.c \ - ft_putnbrendl.c \ - ft_putnbrendl_fd.c \ - ft_putendl_fd.c \ - ODIR = ./builds OBJS = $(SRCS:%.c=$(ODIR)/%.o) diff --git a/includes/ft_printf.h b/includes/ft_printf.h index 1f0e1fa..d10acff 100644 --- a/includes/ft_printf.h +++ b/includes/ft_printf.h @@ -11,52 +11,15 @@ /* ************************************************************************** */ #ifndef FT_PRINTF_H -# define FT_PRINTF_H -# include // to use va_arg +#define FT_PRINTF_H +#include // to use va_arg /* ** ft_printf.c */ -char *specifier(char *s); -int ft_expand_star(int nbr, char **string); -int ft_put_word(char *s, char *type, int size); -char *convert_with_flags(char *s, va_list ap, char *type, int *size); -int ft_printf(char *string, ...); - -/* -** ft_next_word.c -*/ - -int width_precision(char *s); -int word_length(char *s); -char *next_word(char **s); - -/* -** ft_convert.c -*/ - -char *conv_i(char c, long int i); -char *conv_u(char c, unsigned long int i); -char *ft_convert(va_list ap, char *type, char **s); - -/* -** ft_flag_transform.c -*/ - -char *precision_int(char *print, int precision); -char *ft_precision(char *s, char *print, char *type); -char *width_flags(char *print, char *s, int width, int zero); -char *ft_width(char *s, char *print, int *size, char *type); -char *ft_flag_transform(char *s, char *print, char *type, int *size); - -/* -** ft_flag_transform_bonus.c -*/ - -char *ft_plus(char *s, char *print, char *type); -char *ft_sharp(char *s, char *print, char *type); -char *ft_sharp_again(char *s, char *print, char *type); -char *ft_space(char *s, char *print, char *type, int *size); +int ft_dprintf(int fd, const char *string, ...); +int ft_printf(const char *string, ...); +int ft_vdprintf(int fd, const char *string, va_list ap); #endif diff --git a/srcs/ft_printf_files/ft_flag_transform_bonus.c b/srcs/ft_printf_files/ft_flag_transform_bonus.c deleted file mode 100644 index f6d773a..0000000 --- a/srcs/ft_printf_files/ft_flag_transform_bonus.c +++ /dev/null @@ -1,75 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_flag_transform_bonus.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: hulamy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/03/12 22:30:50 by hulamy #+# #+# */ -/* Updated: 2020/03/12 22:30:57 by hulamy ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -char *ft_plus(char *s, char *print, char *type) -{ - if (!ft_strchrset(type, "di")) - return (print); - if (ft_strchr(s, '+') && !ft_strchr(print, '-')) - print = ft_concat_free(ft_strdup("+"), print); - return (print); -} - -char *ft_sharp(char *s, char *print, char *type) -{ - if (ft_strchr(s, '#')) - { - if (ft_strchr(type, 'x')) - print = ft_concat_free(ft_strdup("0x"), print); - else - print = ft_concat_free(ft_strdup("0X"), print); - } - return (print); -} - -char *ft_sharp_again(char *s, char *print, char *type) -{ - char *tmp; - - if (!ft_strchr(s, '#')) - return (print); - if (print[0] == '0' && print[1] == '0' && ft_strchrset(type, "xX")) - { - tmp = ft_strchrset("xX", print); - print[1] = tmp[0]; - tmp[0] = '0'; - } - return (print); -} - -char *ft_space(char *s, char *print, char *type, int *size) -{ - int i; - - i = 0; - if (print[0] == ' ' || !ft_strchr(s, ' ') || !ft_strchrset(type, "diuxX")) - return (print); - while (print[i] == ' ') - i++; - if (print[i] == '-' || print[i] == '+') - return (print); - if (ft_strchr(s, '.') || (i == 0 && print[i] != '0')) - { - print = ft_concat_free(ft_strdup(" "), print); - *size += 1; - } - else - print[i] = ' '; - if (ft_strchr(s, '-') && print[*size - 1] == ' ') - { - print[*size] = '\0'; - *size -= 1; - } - return (print); -} diff --git a/srcs/ft_printf_files/ft_printf.c b/srcs/ft_printf_files/ft_printf.c index d96f663..ec8d7c7 100644 --- a/srcs/ft_printf_files/ft_printf.c +++ b/srcs/ft_printf_files/ft_printf.c @@ -1,162 +1,81 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_printf.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: hulamy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/03/12 22:31:29 by hulamy #+# #+# */ -/* Updated: 2020/06/30 00:41:35 by hulamy ### ########.fr */ -/* */ -/* ************************************************************************** */ +/* ft_printf.c */ +#include "pf_private_header.h" #include "libft.h" -/* -** SPECIFIER : -** receive a word as a string, check if it start by '%', and return the -** specifier (diuxXspefgn) and the length (h hh l ll) -** -if s is a string, or is a single '%' -** return NULL (to print is as a string) -** -if s is a double '%%', remove one '%', and -** return NULL (to print is as a string) -** -then s is a conversion, go to the length and specifier -** -copy them in 'string' -** -and remove them from s -** -return the length and specifier in a string -*/ - -char *specifier(char *s) -{ - char *string; - int i; - - if (s[0] != '%' || s[1] == '\0') - return (NULL); - if (s[1] == '%') - { - s[1] = '\0'; - return (NULL); - } - i = 1; - while (ft_strchr("#0- +'0123456789.*", s[i]) != NULL) - i++; - string = ft_strdup(s + i); - while (s[i] != '\0') - { - s[i] = '\0'; - i++; - } - return (string); -} - -/* -** -receive 'i' the number in which '*' will expand -** -turn it into a string -** -calculate the total lentgh of the string '%...' for nbr replacing '*' -** -allocate a new string with this length -** -copy the original str with first '*' expanded into it's corresponding nbr -*/ - -int ft_expand_star(int nbr, char **string) -{ - char *s; - char *n; - int i; - int j; - int k; - - n = ft_itoa(nbr); - if (!(s = ft_memalloc(sizeof(char) * (ft_strlen(n) + ft_strlen(*string))))) - return (0); - i = -1; - j = 0; - k = 0; - while ((*string)[++i] != '\0') - { - s[j] = (*string)[i]; - if (s[j] == '*') - while (n[k] != '\0') - s[j++] = n[k++]; - else - j++; - } - free(n); - free(*string); - *string = s; - return (1); -} - -/* -** print the string -** because of lake of space, it also free 'type' -*/ - -int ft_put_word(char *s, char *type, int size) -{ - int i; - - i = 0; - while (i < size) - write(1, &(s[i++]), 1); - free(type); - free(s); - return (i); -} - -/* -** because of lake of space... -** -1 expand the specifier according to its type and its length -** and put in a string 'print' -** -2 transform 'print' according to the flags -*/ - -char *convert_with_flags(char *s, va_list ap, char *type, int *size) -{ - char *print; - - if (!(print = ft_convert(ap, type, &s))) - return (NULL); - if (!(print = ft_flag_transform(s, print, type, size))) - return (NULL); - free(s); - return (print); -} - /* ** -printf receive a string to print with a variadic number of arguments ** -it will go in a loop through each 'words' ** -a word is either a string containing no '%' or a conversion starting by '%' ** -if it's a string it's printed right away -** -if it's a conversion it will call convert_with_flags for some actions : +** -if it's a conversion it will call pf_convert_with_flags for some actions : ** -1 expand the specifier according to its type and its length ** and put in a string 'print' ** -2 transform 'print' according to the flags */ - -int ft_printf(char *string, ...) +int ft_printf(const char *string, ...) { - char *s; - char *type; - int length; - int size; - va_list ap; + va_list ap; + int ret; + + va_start(ap, string); + ret = ft_vdprintf(STDOUT_FILENO, string, ap); + va_end(ap); + return (ret); +} + +/* +** ft_dprintf : prints formatted output to a file descriptor +** @param fd : the file descriptor to write to (e.g., STDOUT_FILENO, STDERR_FILENO) +** @param string: the format string (see ft_printf for format specifiers) +** @param ... : variadic arguments for the format string +** @return : the number of characters printed, or -1 on error +** @note : this is the file descriptor equivalent of ft_printf. +** Use this when you need to print to a specific file descriptor, +** such as STDERR_FILENO or a custom file descriptor from open(). +*/ +int ft_dprintf(int fd, const char *string, ...) +{ + va_list ap; + int ret; + + va_start(ap, string); + ret = ft_vdprintf(fd, string, ap); + va_end(ap); + return (ret); +} + +/* +** ft_vdprintf : core implementation of formatted output to a file descriptor +** @param fd : the file descriptor to write to +** @param string : the format string (see ft_printf for format specifiers) +** @param ap : va_list containing the variadic arguments for the format string +** @return : the number of characters printed, or -1 on error +** @note : this is the va_list version of ft_dprintf. +** It processes the format string and variadic arguments, +** then writes the result to the file descriptor `fd`. +** This function is used internally by ft_dprintf and ft_printf. +*/ +int ft_vdprintf(int fd, const char *string, va_list ap) +{ + char *s; + char *type; + int length; + int size; length = 0; - va_start(ap, string); - while ((s = next_word(&string)) != NULL) + while ((s = pf_next_word(&string)) != NULL) { - if ((type = specifier(s)) == NULL) - length += ft_put_word(s, type, ft_strlen(s)); + if ((type = pf_specifier(s)) == NULL) + length += pf_put_word(fd, s, type, ft_strlen(s)); else { size = 0; - if (!(s = convert_with_flags(s, ap, type, &size))) + if (!(s = pf_convert_with_flags(s, ap, type, &size))) return (-1); - length += ft_put_word(s, type, size); + length += pf_put_word(fd, s, type, size); } } free(s); - va_end(ap); return (length); } diff --git a/srcs/ft_printf_files/ft_convert.c b/srcs/ft_printf_files/pf_convert.c similarity index 65% rename from srcs/ft_printf_files/ft_convert.c rename to srcs/ft_printf_files/pf_convert.c index 644a270..d899b7b 100644 --- a/srcs/ft_printf_files/ft_convert.c +++ b/srcs/ft_printf_files/pf_convert.c @@ -1,15 +1,6 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_convert.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: hulamy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/03/12 22:30:05 by hulamy #+# #+# */ -/* Updated: 2020/06/30 00:40:49 by hulamy ### ########.fr */ -/* */ -/* ************************************************************************** */ +/* pf_convert.c */ +#include "pf_private_header.h" #include "libft.h" /* @@ -36,10 +27,9 @@ ** -'h' and 'hh', are traited just like regular size because of ** default promotion, that promote smaller type than int into int */ - -char *conv_i(char c, long int i) +static char *pf_conv_i(char c, long int i) { - char *s; + char *s; if (c == 'c') { @@ -52,9 +42,9 @@ char *conv_i(char c, long int i) return (NULL); } -char *conv_u(char c, unsigned long int i) +static char *pf_conv_u(char c, unsigned long int i) { - char *s; + char *s; if (c == 's') return (i == 0 ? ft_strdup("(null)") : ft_strdup((char *)i)); @@ -68,6 +58,42 @@ char *conv_u(char c, unsigned long int i) return (NULL); } +/* +** -receive 'i' the number in which '*' will expand +** -turn it into a string +** -calculate the total lentgh of the string '%...' for nbr replacing '*' +** -allocate a new string with this length +** -copy the original str with first '*' expanded into it's corresponding nbr +*/ +static int pf_expand_star(int nbr, char **string) +{ + char *s; + char *n; + int i; + int j; + int k; + + n = ft_itoa(nbr); + if (!(s = ft_memalloc(sizeof(char) * (ft_strlen(n) + ft_strlen(*string))))) + return (0); + i = -1; + j = 0; + k = 0; + while ((*string)[++i] != '\0') + { + s[j] = (*string)[i]; + if (s[j] == '*') + while (n[k] != '\0') + s[j++] = n[k++]; + else + j++; + } + free(n); + free(*string); + *string = s; + return (1); +} + /* ** -first a loop to expand all the stars from width and .precision ** they always expand into int type @@ -81,22 +107,21 @@ char *conv_u(char c, unsigned long int i) ** (because it will never find a 'p' or a 's' if there are 'uxX' already) ** or for 'p' and again for 'p', or 's' twice similarly */ - -char *ft_convert(va_list ap, char *type, char **s) +char *pf_convert(va_list ap, char *type, char **s) { - char *tmp; + char *tmp; while (ft_strchr(*s, '*')) - if (!(ft_expand_star(va_arg(ap, int), s))) + if (!(pf_expand_star(va_arg(ap, int), s))) return (NULL); if ((tmp = ft_strchrset(type, "dic")) && ft_strchr(type, 'l')) - return (conv_i(tmp[0], va_arg(ap, long int))); + return (pf_conv_i(tmp[0], va_arg(ap, long int))); if ((tmp = ft_strchrset(type, "dic"))) - return (conv_i(tmp[0], va_arg(ap, int))); + return (pf_conv_i(tmp[0], va_arg(ap, int))); if ((tmp = ft_strchrset(type, "uxXps")) && ft_strchrset(type, "lps")) - return (conv_u(tmp[0], va_arg(ap, unsigned long int))); + return (pf_conv_u(tmp[0], va_arg(ap, unsigned long int))); if ((tmp = ft_strchrset(type, "uxX"))) - return (conv_u(tmp[0], va_arg(ap, unsigned int))); + return (pf_conv_u(tmp[0], va_arg(ap, unsigned int))); if (ft_strchr(type, '%')) return (ft_strdup("%")); if (ft_strchrset(type, "efgn")) diff --git a/srcs/ft_printf_files/ft_flag_transform.c b/srcs/ft_printf_files/pf_flag_transform.c similarity index 70% rename from srcs/ft_printf_files/ft_flag_transform.c rename to srcs/ft_printf_files/pf_flag_transform.c index 21a06fc..62cad97 100644 --- a/srcs/ft_printf_files/ft_flag_transform.c +++ b/srcs/ft_printf_files/pf_flag_transform.c @@ -1,26 +1,16 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_flag_transform.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: hulamy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/03/12 22:30:28 by hulamy #+# #+# */ -/* Updated: 2020/03/12 22:30:41 by hulamy ### ########.fr */ -/* */ -/* ************************************************************************** */ +/* pf_flag_transform.c */ +#include "pf_private_header.h" #include "libft.h" /* ** -function that modify the string 'print' according to the precision flag : ** if length(s) < precision, add x '0' bfr nbr, but after '-' if negative */ - -char *precision_int(char *print, int precision) +static char *pf_precision_int(char *print, int precision) { - int i; - char *tmp; + int i; + char *tmp; i = ft_strlen(print); if (print[0] == '-') @@ -60,18 +50,17 @@ char *precision_int(char *print, int precision) ** -0 if .precision is 0 && print is "0": print nothing ** -1 if type is s: if length(s) > precision, removes end of 'print' to print ** only x chars, with x = precision -** -2 if type is "diouxX": call fonction 'precision_int' that return : +** -2 if type is "diouxX": call fonction 'pf_precision_int' that return : ** if length(s) < precision, add x '0' bfr nbr, but after '-' if negative ** -3 if type is "aAeEfF": not covered ** -4 if type is "gG": not covered ** -5 else: error */ - -char *ft_precision(char *s, char *print, char *type) +static char *pf_precision(char *s, char *print, char *type) { - char *tmp; - int precision; - int i; + char *tmp; + int precision; + int i; if ((tmp = ft_strchr(s, '.'))) { @@ -90,7 +79,7 @@ char *ft_precision(char *s, char *print, char *type) print[i] = '\0'; } else if (ft_strchrset(type, "diouxX")) - print = precision_int(print, precision); + print = pf_precision_int(print, precision); } return (print); } @@ -100,12 +89,11 @@ char *ft_precision(char *s, char *print, char *type) ** -if flag '0' is present, put extra width as '0' to left of 'print' ** -else, put extra width as ' ' to left of 'print' */ - -char *width_flags(char *print, char *s, int width, int zero) +static char *pf_width_flags(char *print, char *s, int width, int zero) { - char *tmp; - char *minus; - int len; + char *tmp; + char *minus; + int len; len = ft_strlen(print) + zero; if (!(tmp = ft_strnew(width))) @@ -141,15 +129,14 @@ char *width_flags(char *print, char *s, int width, int zero) ** otherwise the int 'size' take the value returned by atoi ** in case print isn't changed, 'size' is the length of 'print' ** 3 then if the size of the width shield is bigger than the size of print -** (plus zero in case print is a (char)0), call 'width_flags' that will +** (plus zero in case print is a (char)0), call 'pf_width_flags' that will ** create a new char* to contain the string to print after transformation ** 4 otherwise 'size' is the length of print + zero */ - -char *ft_width(char *s, char *print, int *size, char *type) +static char *pf_width(char *s, char *print, int *size, char *type) { - char *tmp; - int zero; + char *tmp; + int zero; tmp = s; zero = 0; @@ -165,7 +152,7 @@ char *ft_width(char *s, char *print, int *size, char *type) *size = ft_atoi(tmp); tmp[0] = '\0'; if ((unsigned int)*size > ft_strlen(print) + zero) - print = width_flags(print, s, *size, zero); + print = pf_width_flags(print, s, *size, zero); else *size = ft_strlen(print) + zero; tmp[0] = '1'; @@ -181,19 +168,18 @@ char *ft_width(char *s, char *print, int *size, char *type) ** -then p ** -the case of 'p' is treated without any subtelness because i don't care */ - -char *ft_flag_transform(char *s, char *print, char *type, int *size) +char *pf_flag_transform(char *s, char *print, char *type, int *size) { - print = ft_precision(s, print, type); - print = ft_plus(s, print, type); - print = ft_sharp(s, print, type); + print = pf_precision(s, print, type); + print = pf_plus(s, print, type); + print = pf_sharp(s, print, type); if (ft_strchr(type, 'p')) { print = ft_concat_free(ft_strdup("0x"), print); *size += 2; } - print = ft_width(s, print, size, type); - print = ft_sharp_again(s, print, type); - print = ft_space(s, print, type, size); + print = pf_width(s, print, size, type); + print = pf_sharp_again(s, print, type); + print = pf_space(s, print, type, size); return (print); } diff --git a/srcs/ft_printf_files/pf_flag_transform_bonus.c b/srcs/ft_printf_files/pf_flag_transform_bonus.c new file mode 100644 index 0000000..d28e3d0 --- /dev/null +++ b/srcs/ft_printf_files/pf_flag_transform_bonus.c @@ -0,0 +1,66 @@ +/* pf_flag_transform_bonus.c */ + +#include "pf_private_header.h" +#include "libft.h" + +char *pf_plus(char *s, char *print, char *type) +{ + if (!ft_strchrset(type, "di")) + return (print); + if (ft_strchr(s, '+') && !ft_strchr(print, '-')) + print = ft_concat_free(ft_strdup("+"), print); + return (print); +} + +char *pf_sharp(char *s, char *print, char *type) +{ + if (ft_strchr(s, '#')) + { + if (ft_strchr(type, 'x')) + print = ft_concat_free(ft_strdup("0x"), print); + else + print = ft_concat_free(ft_strdup("0X"), print); + } + return (print); +} + +char *pf_sharp_again(char *s, char *print, char *type) +{ + char *tmp; + + if (!ft_strchr(s, '#')) + return (print); + if (print[0] == '0' && print[1] == '0' && ft_strchrset(type, "xX")) + { + tmp = ft_strchrset("xX", print); + print[1] = tmp[0]; + tmp[0] = '0'; + } + return (print); +} + +char *pf_space(char *s, char *print, char *type, int *size) +{ + int i; + + i = 0; + if (print[0] == ' ' || !ft_strchr(s, ' ') || !ft_strchrset(type, "diuxX")) + return (print); + while (print[i] == ' ') + i++; + if (print[i] == '-' || print[i] == '+') + return (print); + if (ft_strchr(s, '.') || (i == 0 && print[i] != '0')) + { + print = ft_concat_free(ft_strdup(" "), print); + *size += 1; + } + else + print[i] = ' '; + if (ft_strchr(s, '-') && print[*size - 1] == ' ') + { + print[*size] = '\0'; + *size -= 1; + } + return (print); +} diff --git a/srcs/ft_printf_files/ft_next_word.c b/srcs/ft_printf_files/pf_next_word.c similarity index 60% rename from srcs/ft_printf_files/ft_next_word.c rename to srcs/ft_printf_files/pf_next_word.c index 6980aac..c4bc173 100644 --- a/srcs/ft_printf_files/ft_next_word.c +++ b/srcs/ft_printf_files/pf_next_word.c @@ -1,15 +1,6 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* next_word.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: hulamy +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/10 13:58:30 by hulamy #+# #+# */ -/* Updated: 2020/02/26 18:24:04 by hulamy ### ########.fr */ -/* */ -/* ************************************************************************** */ +/* pf_next_word.c */ +#include "pf_private_header.h" #include "libft.h" /* @@ -19,9 +10,9 @@ ** for a '.' for the 'precision' flag */ -int width_precision(char *s) +static int width_precision(const char *s) { - int i; + int i; i = 0; if (ft_strchr("*", s[i]) != NULL) @@ -47,9 +38,9 @@ int width_precision(char *s) ** to save a line (3 with the brackets) */ -int word_length(char *s) +static int word_length(const char *s) { - int i; + int i; i = 1; if (s[0] == '\0') @@ -79,11 +70,11 @@ int word_length(char *s) ** but is treated as a '%' here */ -char *next_word(char **string) +char *pf_next_word(const char **string) { - char *s; - char *word; - int i; + const char *s; + char *word; + int i; s = *string; if (*s == '\0') diff --git a/srcs/ft_printf_files/pf_private_header.h b/srcs/ft_printf_files/pf_private_header.h new file mode 100644 index 0000000..df520f6 --- /dev/null +++ b/srcs/ft_printf_files/pf_private_header.h @@ -0,0 +1,42 @@ +/* pf_private_header.h */ + +#ifndef FT_PF_PRIVATE_H +#define FT_PF_PRIVATE_H +#include // to use va_arg + +/* +** pf_next_word.c +*/ + +char *pf_next_word(const char **s); + +/* +** pf_convert.c +*/ + +char *pf_convert(va_list ap, char *type, char **s); + +/* +** pf_flag_transform.c +*/ + +char *pf_flag_transform(char *s, char *print, char *type, int *size); + +/* +** pf_flag_transform_bonus.c +*/ + +char *pf_plus(char *s, char *print, char *type); +char *pf_sharp(char *s, char *print, char *type); +char *pf_sharp_again(char *s, char *print, char *type); +char *pf_space(char *s, char *print, char *type, int *size); + +/* +** pf_utils.c +*/ + +char *pf_specifier(char *s); +int pf_put_word(int fd, char *s, char *type, int size); +char *pf_convert_with_flags(char *s, va_list ap, char *type, int *size); + +#endif diff --git a/srcs/ft_printf_files/pf_utils.c b/srcs/ft_printf_files/pf_utils.c new file mode 100644 index 0000000..6469f23 --- /dev/null +++ b/srcs/ft_printf_files/pf_utils.c @@ -0,0 +1,75 @@ +/* pf_utils.c */ + +#include "pf_private_header.h" +#include "libft.h" + +/* +** SPECIFIER : +** receive a word as a string, check if it start by '%', and return the +** specifier (diuxXspefgn) and the length (h hh l ll) +** -if s is a string, or is a single '%' +** return NULL (to print is as a string) +** -if s is a double '%%', remove one '%', and +** return NULL (to print is as a string) +** -then s is a conversion, go to the length and specifier +** -copy them in 'string' +** -and remove them from s +** -return the length and specifier in a string +*/ +char *pf_specifier(char *s) +{ + char *string; + int i; + + if (s[0] != '%' || s[1] == '\0') + return (NULL); + if (s[1] == '%') + { + s[1] = '\0'; + return (NULL); + } + i = 1; + while (ft_strchr("#0- +'0123456789.*", s[i]) != NULL) + i++; + string = ft_strdup(s + i); + while (s[i] != '\0') + { + s[i] = '\0'; + i++; + } + return (string); +} + +/* +** print the string +** because of lake of space, it also free 'type' +*/ +int pf_put_word(int fd, char *s, char *type, int size) +{ + int i; + + i = 0; + while (i < size) + write(fd, &(s[i++]), 1); + free(type); + free(s); + return (i); +} + +/* +** because of lake of space... +** -1 expand the specifier according to its type and its length +** and put in a string 'print' +** -2 transform 'print' according to the flags +*/ +char *pf_convert_with_flags(char *s, va_list ap, char *type, int *size) +{ + char *print; + + if (!(print = pf_convert(ap, type, &s))) + return (NULL); + if (!(print = pf_flag_transform(s, print, type, size))) + return (NULL); + free(s); + return (print); +} \ No newline at end of file