diff options
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | .gitmodules | 4 | ||||
| -rw-r--r-- | Makefile | 46 | ||||
| -rw-r--r-- | ft_printf.c | 94 | ||||
| -rw-r--r-- | header.h | 97 | ||||
| m--------- | libft | 0 | ||||
| -rw-r--r-- | list.c | 56 | ||||
| -rw-r--r-- | main.c | 34 | ||||
| -rw-r--r-- | parse.c | 12 | ||||
| -rw-r--r-- | printer.c | 6 | ||||
| -rwxr-xr-x | test | bin | 0 -> 8472 bytes | |||
| -rw-r--r-- | utils.c | 131 |
12 files changed, 182 insertions, 299 deletions
@@ -2,3 +2,4 @@ a.out *ghc ft_printf +*.a diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..e379e18 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,4 @@ +[submodule "libft"] + path = libft + url = http://github.com/HappyTramp/libft + branch = rendu @@ -1,32 +1,50 @@ +LIBFT_ROOT = ./libft + CC = gcc -CCFLAGS = -Wall -Wextra #-Werror +CCFLAGS = -Wall -Wextra -Werror +LDFLAGS = -L. -lftprintf +INCFLAGS = -I$(LIBFT_ROOT) RM = rm -f +LIB = ar rcs +MAKE = make -j4 -NAME = ft_printf +NAME = libftprintf.a SRC = ft_printf.c utils.c printer.c parse.c list.c extract.c OBJ = $(SRC:.c=.o) INCLUDE = header.h -all: $(NAME) - -debug: CCFLAGS += -g -fsanitize=address -debug: re +all: libft_all $(NAME) $(NAME): $(OBJ) - $(CC) $(CCFLAGS) -o $@ $(OBJ) + cp $(LIBFT_ROOT)/libft.a $(NAME) + $(LIB) $(NAME) $(OBJ) %.o: %.c $(INCLUDE) - $(CC) $(CCFLAGS) -c -o $@ $< + $(CC) $(CCFLAGS) $(INCFLAGS) -c -o $@ $< -clean: - $(RM) $(OBJ) +bonus: all -fclean: clean +clean: libft_clean $(RM) $(OBJ) +fclean: libft_fclean clean + $(RM) $(NAME) + re: fclean all -test: fclean - make -C ../schooltest - ../schooltest/all_tests +test: all + $(CC) $(CCFLAGS) $(LDFLAGS) $(INCFLAGS) -o test main.c + + +libft_all: + $(MAKE) -C $(LIBFT_ROOT) all + +libft_bonus: + $(MAKE) -C $(LIBFT_ROOT) bonus + +libft_clean: + $(MAKE) -C $(LIBFT_ROOT) clean + +libft_fclean: + $(MAKE) -C $(LIBFT_ROOT) fclean diff --git a/ft_printf.c b/ft_printf.c index 96641b1..27b4734 100644 --- a/ft_printf.c +++ b/ft_printf.c @@ -6,23 +6,23 @@ int ft_printf(const char *format, ...) { int print_len; - va_list ap; - /* va_list ap_start; */ - /* int ap_index; */ - t_list *format_list; - char *str; - - if ((format_list = parse(format)) == NULL) - return (-1); - /* ap_index = 0; */ - va_start(ap, format); - - /* va_copy(ap_start, ap); */ - print_len = -1; - while (format[++print_len]) - { - if (format[print_len] == '%') - { + /* va_list ap; */ + /* #<{(| va_list ap_start; |)}># */ + /* #<{(| int ap_index; |)}># */ + /* t_list *format_list; */ + /* char *str; */ + /* */ + /* if ((format_list = parse(format)) == NULL) */ + /* return (-1); */ + /* #<{(| ap_index = 0; |)}># */ + /* va_start(ap, format); */ + /* */ + /* #<{(| va_copy(ap_start, ap); |)}># */ + /* print_len = -1; */ + /* while (format[++print_len]) */ + /* { */ + /* if (format[print_len] == '%') */ + /* { */ /* if (format_list->data->ap_index != -1) */ /* { */ /* ap_index = format_list->data->ap_index; */ @@ -33,51 +33,19 @@ int ft_printf(const char *format, ...) /* printf("\n%d\n", format_list->data->precision); */ /* printf("\n%d\n", format_list->data->precision_wildcard); */ /* printf("\n%d\n", format_list->data->min_field_width); */ - if ((str = convert_to_str(format_list->data, ap)) == NULL) - return (-1); - ft_putstr(str); - free(str); - - print_len += format_list->data->len; - list_pop_front(&format_list); - } - else - write(STDOUT_FILENO, format + print_len, 1); - } - va_end(ap); + /* if ((str = convert_to_str(format_list->content, ap)) == NULL) */ + /* return (-1); */ + /* ft_putstr(str); */ + /* free(str); */ + /* */ + /* print_len += format_list->content->len; */ + /* list_pop_front(&format_list); */ + /* } */ + /* else */ + /* write(STDOUT_FILENO, format + print_len, 1); */ + /* } */ + /* va_end(ap); */ + (void)format; + print_len = 0; return (print_len); } - -#ifndef TESTING - -int main() -{ - /* int test; */ - - /* ft_printf("char: %c\n", 'r'); */ - /* ft_printf("string: %s\n", "bonjour"); */ - /* ft_printf("pointer: %p\n", &test); */ - /* ft_printf("int: %d or %i\n", 45, 54); */ - /* ft_printf("uint: %u\n", 1 << 31); */ - /* ft_printf("hex lower: %x\n", 0xabcf012); */ - /* ft_printf("hex upper: %X\n", 0xabcf012); */ - /* ft_printf("percent: %%\n"); */ - - /* ft_printf("precision |%.9d|\n", 43); */ - /* ft_printf("string precision |%.9s|\n", "jesuisbonjourbonjour"); */ - /* ft_printf("min width |%9d|\n", 43); */ - /* ft_printf("zero padding |%09d|\n", 43); */ - /* ft_printf("left adjusted |%-9d|\n", 43); */ - /* ft_printf("string padding |%9s|\n", "bon"); */ - - ft_printf("width wildcard |%*d|\n", 5, 43); - ft_printf("precision wildcard |%.*d|\n", 5, 43); - ft_printf("precision/width wildcard |%*.*d|\n", 5, 3, 43); - ft_printf("left adjusted |%*d|\n", -5, 43); - - /* ft_printf("overwrite |%*3d|\n", 5, 43); */ - /* ft_printf("overwrite |%*-1d|\n", 0, 43); */ - return 0; -} - -#endif @@ -2,6 +2,7 @@ # define HEADER_H # include <stdarg.h> +# include "libft.h" # define TRUE 1 # define FALSE 0 @@ -13,20 +14,18 @@ # define CONVERSIONS_STR "cspdiuxX%" -typedef enum -{ - CONVERSION_CHAR = 0, - CONVERSION_STR, - CONVERSION_PTR, - CONVERSION_DECIMAL, - CONVERSION_INT, - CONVERSION_UINT, - CONVERSION_HEX_LOWER, - CONVERSION_HEX_UPPER, - CONVERSION_PERCENT -} t_conversion; - -typedef int t_bool; +# define CONVERSION_CHAR 'c' +# define CONVERSION_STR 's' +# define CONVERSION_PTR 'p' +# define CONVERSION_DECIMAL 'd' +# define CONVERSION_INT 'i' +# define CONVERSION_UINT 'u' +# define CONVERSION_HEX_LOWER 'x' +# define CONVERSION_HEX_UPPER 'X' +# define CONVERSION_PERCENT '%' + +typedef char t_conversion; +typedef int t_bool; typedef struct { @@ -40,78 +39,68 @@ typedef struct typedef struct { - int ap_index; - t_bool left_adjusted; - t_bool zero_padding; - t_maybe_wildcard precision; - t_maybe_wildcard min_width; - t_conversion conversion; - int len; -} t_pformat; - -typedef struct s_list + int ap_index; + t_bool left_adjusted; + t_bool zero_padding; + t_maybe_wildcard precision; + t_maybe_wildcard min_width; + t_conversion conversion; + int len; +} t_pformat; + +typedef struct s_pformat_list { - struct s_list *next; - t_pformat *data; -} t_list; + struct s_pformat_list *next; + t_pformat *content; +} t_pformat_list; /* ** ft_printf.c */ -int ft_printf(const char *format, ...); +int ft_printf(const char *format, ...); /* ** parse.c */ -t_list *parse(const char *format); -char *isolate_conversion(const char *conversion_start); -t_pformat *parse_reduced(char *fmt); +t_pformat_list *parse(const char *format); +char *isolate_conversion(const char *conversion_start); +t_pformat *parse_reduced(char *fmt); /* ** printer.c */ -void ft_putstr(char *str); -void handle_padding(t_pformat *pformat, char *str); -char *convert_to_str(t_pformat *pformat, va_list ap); -void handle_precision(t_pformat *pformat, char *str); +void handle_padding(t_pformat *pformat, char *str); +char *convert_to_str(t_pformat *pformat, va_list ap); +void handle_precision(t_pformat *pformat, char *str); /* ** utils.c */ -int ft_atoi(const char *str); -char *ft_strndup(const char *s1, int n); -char *ft_strrchr(const char *s, int c); int strrchr_index(const char *s, char c); -int ft_strlen(char *str); -t_bool ft_isdigit(char c); -int nbrlen_radix(long int nbr, int radix); -char *ft_itoa_base(long int n, char *base); -char *ft_strnew(int size); -char *ft_strdup(char *s); -char *ft_strcpy(char *dest, const char *src); /* ** extract.c */ -char *extract_ap_index(t_pformat *pformat, char *fmt); -char *extract_min_width(t_pformat *pformat, char *fmt); -char *extract_standalone_flags(t_pformat *pformat, char *fmt); -char *extract_precision(t_pformat *pformat, char *fmt); +// char *extract_ap_index(t_pformat *pformat, char *fmt); + +char *extract_min_width(t_pformat *pformat, char *fmt); +char *extract_standalone_flags(t_pformat *pformat, char *fmt); +char *extract_precision(t_pformat *pformat, char *fmt); /* ** list.c */ -t_list *list_new(t_pformat *data); -t_list *list_destroy(t_list *list); -void list_push_front(t_list **list, t_list *new_front); -void list_push_back(t_list **list, t_list *new_back); -void list_pop_front(t_list **list); +t_pformat_list *list_new(t_pformat *content); +void *list_destroy(t_pformat_list **lst); +void list_push_front(t_pformat_list **lst, t_pformat_list *new); +void list_push_back(t_pformat_list **lst, t_pformat_list *new); +void list_pop_front(t_pformat_list **lst); #endif diff --git a/libft b/libft new file mode 160000 +Subproject 70b928b3ee9a1de7769531edd82c0b43cf6d4d8 @@ -1,54 +1,60 @@ #include <stdlib.h> #include "header.h" -t_list *list_new(t_pformat *data) +t_pformat_list *list_new(t_pformat *content) { - t_list *list; + t_pformat_list *lst; - if ((list = (t_list*)malloc(sizeof(t_list))) == NULL) + if ((lst = (t_pformat_list*)malloc(sizeof(t_pformat_list))) == NULL) return NULL; - list->data = data; - list->next = NULL; - return (list); + lst->content = content; + lst->next = NULL; + return (lst); } -t_list *list_destroy(t_list *list) +void *list_destroy(t_pformat_list **lst) { - while (list != NULL) - list_pop_front(&list); + if (lst == NULL) + return (NULL); + while (*lst != NULL) + list_pop_front(lst); return (NULL); } -void list_push_front(t_list **list, t_list *new_front) +void list_push_front(t_pformat_list **lst, t_pformat_list *new) { - new_front->next = *list; - *list = new_front; + if (lst == NULL || new == NULL) + return ; + new->next = *lst; + *lst = new; } -void list_push_back(t_list **list, t_list *new_back) +void list_push_back(t_pformat_list **lst, t_pformat_list *new) { - t_list *cursor; + t_pformat_list *cursor; - if (*list == NULL) + if (lst == NULL || new == NULL) + return ; + if (*lst == NULL) { - *list = new_back; + *lst = new; return ; } - cursor = *list; + cursor = *lst; while (cursor->next != NULL) cursor = cursor->next; - cursor->next = new_back; + cursor->next = new; } -void list_pop_front(t_list **list) +void list_pop_front(t_pformat_list **lst) { - t_list *tmp; + t_pformat_list *tmp; - if (*list == NULL) + if (lst == NULL || *lst == NULL) return ; - tmp = (*list)->next; - free((*list)->data); - free(*list); - *list = tmp; + tmp = (*lst)->next; + free((*lst)->content); + free(*lst); + *lst = tmp; } @@ -0,0 +1,34 @@ +#include <stdio.h> +#include "libft.h" +#include "header.h" + +int main() +{ + int test; + + printf("bonjour"); + ft_printf("char: %c\n", 'r'); + ft_printf("string: %s\n", "bonjour"); + ft_printf("pointer: %p\n", &test); + ft_printf("int: %d or %i\n", 45, 54); + ft_printf("uint: %u\n", 1 << 31); + ft_printf("hex lower: %x\n", 0xabcf012); + ft_printf("hex upper: %X\n", 0xabcf012); + ft_printf("percent: %%\n"); + + ft_printf("precision |%.9d|\n", 43); + ft_printf("string precision |%.9s|\n", "jesuisbonjourbonjour"); + ft_printf("min width |%9d|\n", 43); + ft_printf("zero padding |%09d|\n", 43); + ft_printf("left adjusted |%-9d|\n", 43); + ft_printf("string padding |%9s|\n", "bon"); + + ft_printf("width wildcard |%*d|\n", 5, 43); + ft_printf("precision wildcard |%.*d|\n", 5, 43); + ft_printf("precision/width wildcard |%*.*d|\n", 5, 3, 43); + ft_printf("left adjusted |%*d|\n", -5, 43); + + /* ft_printf("overwrite |%*3d|\n", 5, 43); */ + /* ft_printf("overwrite |%*-1d|\n", 0, 43); */ + return 0; +} @@ -8,11 +8,11 @@ */ #include <stdio.h> -t_list *parse(const char *format) +t_pformat_list *parse(const char *format) { - t_list *format_list; - t_list *tmp; - t_pformat *parsed; + t_pformat_list *format_list; + t_pformat_list *tmp; + t_pformat *parsed; format_list = NULL; while (*format) @@ -24,9 +24,9 @@ t_list *parse(const char *format) } format++; if ((parsed = parse_reduced(isolate_conversion(format))) == NULL) - return (list_destroy(format_list)); + return (list_destroy(&format_list)); if ((tmp = list_new(parsed)) == NULL) - return (list_destroy(format_list)); + return (list_destroy(&format_list)); list_push_back(&format_list, tmp); } return (format_list); @@ -7,12 +7,6 @@ #define ITOA_HEX_LOWER(x) (ft_itoa_base(x, "0123456789abcdef")) #define ITOA_HEX_UPPER(x) (ft_itoa_base(x, "0123456789ABCDEF")) -void ft_putstr(char *str) -{ - while (*str) - write(STDOUT_FILENO, str++, 1); -} - char *convert_to_str(t_pformat *pformat, va_list ap) { char *str; Binary files differ@@ -4,76 +4,6 @@ #define MIN_INT (1 << 31) #define MAX_INT (~(1 << 31)) -static int precheck(const char **str) -{ - int is_negative; - - while (**str == ' ' || **str == '\t' || **str == '\n' - || **str == '\v' || **str == '\f' || **str == '\r') - (*str)++; - is_negative = 0; - if (**str == '-' || **str == '+') - { - if (**str == '-') - is_negative = 1; - (*str)++; - } - return (is_negative); -} - -int ft_atoi(const char *str) -{ - unsigned int nb; - int i; - int is_negative; - - is_negative = precheck(&str); - i = 0; - nb = 0; - while (str[i] >= '0' && str[i] <= '9') - { - if (!is_negative && nb > (unsigned int)MAX_INT) - return (-1); - else if (nb > (unsigned int)MIN_INT) - return (0); - nb *= 10; - nb += str[i] - '0'; - i++; - } - return ((int)(is_negative ? -nb : nb)); -} - -char *ft_strndup(const char *s1, int n) -{ - char *clone; - int i; - - if ((clone = (char*)malloc(sizeof(char) * (n + 1))) == NULL) - return (NULL); - i = 0; - while (i < n) - { - clone[i] = s1[i]; - i++; - } - clone[i] = '\0'; - return (clone); -} - -char *ft_strrchr(const char *s, int c) -{ - int i; - - i = ft_strlen((char*)s) - 1; - while (s[i] != (char)c) - { - if (i == 0) - return (NULL); - i--; - } - return ((char*)s + i); -} - int strrchr_index(const char *s, char c) { int i; @@ -89,21 +19,6 @@ int strrchr_index(const char *s, char c) } -int ft_strlen(char *str) -{ - int counter; - - counter = 0; - while (str[counter]) - counter++; - return (counter); -} - -t_bool ft_isdigit(char c) -{ - return (c >= '0' && c <= '9'); -} - int nbrlen_radix(long int nbr, int radix) { int counter; @@ -157,49 +72,3 @@ char *ft_itoa_base(long int n, char *base) } return (str); } - -char *ft_strnew(int size) -{ - char *str; - - if ((str = (char*)malloc(sizeof(char) * (size + 1))) == NULL) - return (NULL); - str[size] = '\0'; - while (size-- > 0) - str[size] = '\0'; - return (str); -} - -char *ft_strdup(char *s) -{ - char *clone; - size_t i; - size_t len; - - len = ft_strlen(s); - if ((clone = (char*)malloc(sizeof(char) * (len + 1))) == NULL) - return (NULL); - i = 0; - while (i < len) - { - clone[i] = s[i]; - i++; - } - clone[i] = '\0'; - return (clone); -} - -char *ft_strcpy(char *dest, const char *src) -{ - int i; - - i = 0; - while (src[i]) - { - dest[i] = src[i]; - i++; - } - dest[i] = '\0'; - return (dest); -} - |
