├── .gitignore ├── Makefile ├── README.md ├── author ├── includes └── push_swap.h ├── libft ├── Makefile ├── author ├── includes │ ├── ft_printf.h │ ├── get_next_line.h │ └── libft.h ├── objs │ └── .DS_Store └── srcs │ ├── .DS_Store │ ├── create_b_c.c │ ├── create_b_s.c │ ├── create_binary.c │ ├── create_c.c │ ├── create_i_d.c │ ├── create_i_d_sup.c │ ├── create_n.c │ ├── create_nprntbl.c │ ├── create_o.c │ ├── create_s.c │ ├── create_u.c │ ├── create_x.c │ ├── create_x_sup.c │ ├── flags.c │ ├── ft_atoi.c │ ├── ft_bzero.c │ ├── ft_isalnum.c │ ├── ft_isalpha.c │ ├── ft_isascii.c │ ├── ft_isdigit.c │ ├── ft_isprint.c │ ├── ft_itoa.c │ ├── ft_itoa_base.c │ ├── ft_itoa_max.c │ ├── ft_lst_add_back.c │ ├── ft_lst_last.c │ ├── ft_lst_push_data_back.c │ ├── ft_lst_push_data_front.c │ ├── ft_lst_size.c │ ├── ft_lstadd.c │ ├── ft_lstdel.c │ ├── ft_lstdelone.c │ ├── ft_lstiter.c │ ├── ft_lstmap.c │ ├── ft_lstnew.c │ ├── ft_memalloc.c │ ├── ft_memccpy.c │ ├── ft_memchr.c │ ├── ft_memcmp.c │ ├── ft_memcpy.c │ ├── ft_memdel.c │ ├── ft_memmove.c │ ├── ft_memset.c │ ├── ft_printf.c │ ├── ft_putchar.c │ ├── ft_putchar_fd.c │ ├── ft_putendl.c │ ├── ft_putendl_fd.c │ ├── ft_putnbr.c │ ├── ft_putnbr_fd.c │ ├── ft_putstr.c │ ├── ft_putstr_fd.c │ ├── ft_strcat.c │ ├── ft_strchr.c │ ├── ft_strclr.c │ ├── ft_strcmp.c │ ├── ft_strcpy.c │ ├── ft_strdel.c │ ├── ft_strdup.c │ ├── ft_strequ.c │ ├── ft_striter.c │ ├── ft_striteri.c │ ├── ft_strjoin.c │ ├── ft_strlcat.c │ ├── ft_strlen.c │ ├── ft_strmap.c │ ├── ft_strmapi.c │ ├── ft_strncat.c │ ├── ft_strncmp.c │ ├── ft_strncpy.c │ ├── ft_strnequ.c │ ├── ft_strnew.c │ ├── ft_strnstr.c │ ├── ft_strrchr.c │ ├── ft_strsplit.c │ ├── ft_strstr.c │ ├── ft_strsub.c │ ├── ft_strtrim.c │ ├── ft_tolower.c │ ├── ft_toupper.c │ ├── get_next_line.c │ ├── mod_int_uox.c │ ├── modifiers.c │ ├── pre_res.c │ ├── print_in_color.c │ ├── result_creator.c │ ├── result_printer.c │ └── width_precision.c └── srcs ├── checker.c ├── comb.c ├── comb_2.c ├── commands.c ├── commands_2.c ├── divide_stacks.c ├── error_check.c ├── helpers.c ├── printer.c ├── printer_helper.c ├── push_back.c ├── push_swap.c ├── quick_sort.c ├── stack_creator.c ├── swap_elements.c └── swappers.c /.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 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: sshiling +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2017/10/02 13:01:36 by sshiling #+# #+# # 9 | # Updated: 2017/10/11 23:20:04 by sshiling ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | CHECK_NAME = checker 14 | PUSH_NAME = push_swap 15 | FLAGS = -Wall -Wextra -Werror 16 | INC = -Iincludes/ -I$(LIB_DIR)/includes 17 | 18 | CHECK_SRC_NAME = checker.c stack_creator.c quick_sort.c commands.c\ 19 | commands_2.c printer.c comb.c comb_2.c divide_stacks.c helpers.c\ 20 | push_back.c swap_elements.c swappers.c error_check.c printer_helper.c 21 | 22 | CHECK_OBJ_NAME = $(CHECK_SRC_NAME:.c=.o) 23 | CHECK_OBJ = $(addprefix $(OBJ_DIR),$(CHECK_OBJ_NAME)) 24 | 25 | PUSH_SRC_NAME = push_swap.c stack_creator.c quick_sort.c commands.c\ 26 | commands_2.c printer.c comb.c comb_2.c divide_stacks.c helpers.c\ 27 | push_back.c swap_elements.c swappers.c error_check.c printer_helper.c 28 | 29 | PUSH_OBJ_NAME = $(PUSH_SRC_NAME:.c=.o) 30 | PUSH_OBJ = $(addprefix $(OBJ_DIR),$(PUSH_OBJ_NAME)) 31 | 32 | LIB_DIR = libft/ 33 | SRC_DIR = srcs/ 34 | OBJ_DIR = objs/ 35 | 36 | all: $(CHECK_NAME) $(PUSH_NAME) 37 | 38 | $(CHECK_NAME): $(CHECK_OBJ) 39 | @make -C $(LIB_DIR) --silent 40 | @gcc -o $(CHECK_NAME) $(CHECK_OBJ) -L $(LIB_DIR) -lft 41 | @echo "##### checker compiling finished! #####" 42 | 43 | $(PUSH_NAME): $(PUSH_OBJ) 44 | @make -C $(LIB_DIR) --silent 45 | @gcc -o $(PUSH_NAME) $(PUSH_OBJ) -L $(LIB_DIR) -lft 46 | @echo "##### push_swap compiling finished! #####" 47 | 48 | $(OBJ_DIR)%.o: $(SRC_DIR)%.c 49 | @mkdir -p $(OBJ_DIR) 50 | @echo "##### Creating" [ $@ ] " #####" 51 | @gcc $(FLAGS) -o $@ -c $< $(INC) 52 | 53 | clean: 54 | @make -C $(LIB_DIR) clean --silent 55 | @rm -f $(CHECK_OBJ) $(PUSH_OBJ) 56 | @echo "##### Removed object files #####" 57 | 58 | fclean: clean 59 | @make -C $(LIB_DIR) fclean --silent 60 | @rm -f $(CHECK_NAME) $(PUSH_NAME) 61 | @echo "##### Removed binary files #####" 62 | 63 | re: fclean all 64 | 65 | .PHONY: all clean fclean re 66 | 67 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 42-push_swap 2 | ## Algorithmic project 3 | 4 | Push Swap is the third project of algorithm branch in the study program at School 42 (UNIT Factory).
5 | 6 | The main goal is to sort data on a stack, with a limited set of instructions, using 7 | the lowest possible number of actions. To succeed you’ll have to manipulate various 8 | types of algorithms and choose the one (of many) most appropriate solution for an 9 | optimized data sorting. 10 | 11 | All functions are created in accordance with Norm - the bunch of rules how code should be formatted. 12 | 13 | **!NOTE**
14 | Because of 42 School norm requirements:
15 | * All variables are declared and aligned at the top of each function
16 | * Each function can't have more then 25 lines of code
17 | * C++ style code commenting is forbidden
18 | * Project should be created just with allowed functions otherwise it's cheating.
19 | 20 | ### Game rules 21 | 22 | * The game is composed of 2 stacks named a and b.
23 | * To start with:
24 | ◦ a contains a random number of either positive or negative numbers without 25 | any duplicates.
26 | ◦ b is empty
27 | * The goal is to sort in ascending order numbers into stack a.
28 | * To do this you have the following operations at your disposal:
29 | 30 | | Command | Description | 31 | | ------------- | --------------------------------------------------------------------------------------| 32 | | sa | swap a - swap the first 2 elements at the top of stack a. Do nothing if there is only one or no elements). | 33 | | sb | swap b - swap the first 2 elements at the top of stack b. Do nothing if there is only one or no elements). | 34 | | ss | sa and sb at the same time. | 35 | | pa | push a - take the first element at the top of b and put it at the top of a. Do nothing if b is empty. | 36 | | pb | push b - take the first element at the top of a and put it at the top of b. Do nothing if a is empty. | 37 | | ra | rotate a - shift up all elements of stack a by 1. The first element becomes the last one. | 38 | | rb | rotate b - shift up all elements of stack b by 1. The first element becomes the last one. | 39 | | rr | ra and rb at the same time. | 40 | | rra | reverse rotate a - shift down all elements of stack a by 1. The flast element becomes the first one. | 41 | | rrb | reverse rotate b - shift down all elements of stack b by 1. The flast element becomes the first one. | 42 | | rrr | rra and rrb at the same time. | 43 | 44 | ### Sorting example 45 | 46 | ``` 47 | ------------------------------------------------------------------------------------------------------- 48 | Init a and b: 49 | 2 50 | 1 51 | 3 52 | 6 53 | 8 54 | 5 55 | _ _ 56 | a b 57 | ------------------------------------------------------------------------------------------------------- 58 | Exec sa: 59 | 1 60 | 2 61 | 3 62 | 6 63 | 8 64 | 5 65 | _ _ 66 | a b 67 | ------------------------------------------------------------------------------------------------------- 68 | Exec pb pb pb: 69 | 6 3 70 | 5 2 71 | 8 1 72 | _ _ 73 | a b 74 | ------------------------------------------------------------------------------------------------------- 75 | Exec ra rb (equiv. to rr): 76 | 5 2 77 | 8 1 78 | 6 3 79 | _ _ 80 | a b 81 | ------------------------------------------------------------------------------------------------------- 82 | Exec rra rrb (equiv. to rrr): 83 | 6 3 84 | 5 2 85 | 8 1 86 | _ _ 87 | a b 88 | ------------------------------------------------------------------------------------------------------- 89 | Exec sa: 90 | 5 3 91 | 6 2 92 | 8 1 93 | _ _ 94 | a b 95 | ------------------------------------------------------------------------------------------------------- 96 | Exec pa pa pa: 97 | 1 98 | 2 99 | 3 100 | 5 101 | 6 102 | 8 103 | _ _ 104 | a b 105 | ------------------------------------------------------------------------------------------------------- 106 | ``` 107 | This example sort integers from a in 12 instructions. 108 | 109 | ### The checker 110 | * You have to write a program named checker, which will get as an argument the 111 | stack a formatted as a list of integers. The first argument should be at the top of 112 | the stack (be careful about the order). If no argument is given checker stops and 113 | displays nothing. 114 | * Checker will then wait and read instructions on the standard input, each instruction 115 | will be followed by ’\n’. Once all the instructions have been read, checker will 116 | execute them on the stack received as an argument. 117 | * If after executing those instructions, stack a is actually sorted and b is empty, then 118 | checker must display "OK" followed by a ’\n’ on the standard output. In every 119 | other case, checker must display "KO" followed by a ’\n’ on the standard output. 120 | * In case of error, you must display Error followed by a ’\n’ on the standard error. 121 | Errors include for example: some arguments are not integers, some arguments are 122 | bigger than an integer, there are duplicates, an instruction don’t exist and/or is 123 | incorrectly formatted. 124 | 125 | #### Checker example 126 | ``` 127 | $>./checker 3 2 1 0 128 | rra 129 | pb 130 | sa 131 | rra 132 | pa 133 | OK 134 | $>./checker 3 2 1 0 135 | sa 136 | rra 137 | pb 138 | KO 139 | $>./checker 3 2 one 0 140 | Error 141 | ``` 142 | 143 | ### The “push_swap” program 144 | * You have to write a program named push_swap which will receive as an argument 145 | the stack a formatted as a list of integers. The first argument should be at the top 146 | of the stack (be careful about the order). 147 | * The program must display the smallest list of instructions possible to sort the stack 148 | a, the smallest number being at the top. 149 | * Instructions must be separaed by a ’\n’ and nothing else. 150 | * The goal is to sort the stack with the minimum possible number of operations. 151 | During defence we’ll compare the number of instructions your program found with 152 | a maximum number of operation tolerated. If your program either displays a list 153 | too big or if the list isn’t sorted properly, you’ll get no points. 154 | * In case of error, you must display Error followed by a ’\n’ on the standard error. 155 | Errors include for example: some arguments aren’t integers, some arguments are 156 | bigger than an integer, and/or there are duplicates. 157 | 158 | #### Pushswap Example 159 | ``` 160 | $>./push_swap 2 1 3 6 5 8 161 | sa 162 | pb 163 | pb 164 | pb 165 | sa 166 | pa 167 | pa 168 | pa 169 | $>./push_swap 0 one 2 3 170 | Error 171 | $> 172 | ``` 173 | 174 | ### Execution example 175 | ``` 176 | $>ARG="4 67 3 87 23"; ./push_swap $ARG | wc -l 177 | 6 178 | $>ARG="4 67 3 87 23"; ./push_swap $ARG | ./checker $ARG 179 | OK 180 | $> 181 | ``` 182 | 183 | #### More about School 42 you can find here: https://en.wikipedia.org/wiki/42_(school) 184 | -------------------------------------------------------------------------------- /author: -------------------------------------------------------------------------------- 1 | sshiling 2 | -------------------------------------------------------------------------------- /includes/push_swap.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* push_swap.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/01/29 13:34:36 by sshiling #+# #+# */ 9 | /* Updated: 2018/01/29 13:34:38 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef PUSH_SWAP_H 14 | # define PUSH_SWAP_H 15 | # include "libft.h" 16 | 17 | typedef struct s_stack 18 | { 19 | int num; 20 | int diff; 21 | struct s_stack *next; 22 | } t_stack; 23 | 24 | typedef struct s_com 25 | { 26 | int rotator; 27 | char *final; 28 | } t_com; 29 | 30 | t_stack *create_new_node(void); 31 | t_stack *copy_args_in_stack(int argc, char **argv); 32 | void ft_swap(int *a, int *b); 33 | int partition(int *stack1, int start, int end); 34 | void quick_sort(int *stack1, int start, int end); 35 | void print_final_result(char *origin); 36 | int sab(t_stack **head); 37 | int pab(t_stack **head_to, t_stack **head_from); 38 | int rab(t_stack **head); 39 | int rrab(t_stack **head); 40 | int ss(t_stack **stack1, t_stack **stack2); 41 | int rr(t_stack **stack1, t_stack **stack2); 42 | int rrr(t_stack **stack1, t_stack **stack2); 43 | int s_len(t_stack *stack); 44 | int *create_array_from_list(t_stack *stack, int len); 45 | int find_mid_value(t_stack *stack, int len); 46 | t_com *new_result(void); 47 | char *add_to_string(char *to, char *add); 48 | int div_stack_a(t_stack **stack1, t_stack **stack2, 49 | t_com **result, int len); 50 | int div_stack_b(t_stack **stack1, t_stack **stack2, 51 | t_com **result, int len); 52 | void comb_1(t_stack **stack, t_com **result); 53 | void comb_2(t_stack **stack, t_com **result); 54 | void comb_3(t_stack **stack, t_com **result); 55 | void comb_4(t_stack **stack, t_com **result); 56 | void comb_5(t_stack **stack, t_com **result); 57 | void comb_6(t_stack **stack, t_com **result); 58 | void comb_7(t_stack **stack, t_com **result); 59 | void comb_8(t_stack **stack, t_com **result); 60 | void comb_9(t_stack **stack, t_com **result); 61 | void swap_three_a(t_stack **stack, t_com **result); 62 | void swap_three_b(t_stack **stack, t_com **result); 63 | void swap_three(t_stack **stack, t_com **result); 64 | void swap_two(t_stack **stack, t_com **result); 65 | void push_back(t_stack **stack1, t_stack **stack2, 66 | t_com **result, int half); 67 | void rotate_back(t_stack **stack, t_com **result, int back); 68 | void swap_elements(t_stack **stack1, t_stack **stack2, 69 | t_com **result, int len); 70 | void stack_del(t_stack **stack); 71 | void doubles_checker(t_stack **stack); 72 | int check_int_overflow(char *str); 73 | int check_argv(char *str); 74 | int add_pa_pb(char **origin, char **res, int i); 75 | int add_ra_rra(char **origin, char **res, int i); 76 | int add_rb_rrb(char **origin, char **res, int i); 77 | int add_others(char **tmp, char **res, int i); 78 | 79 | #endif 80 | -------------------------------------------------------------------------------- /libft/Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: sshiling +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2017/11/02 12:39:17 by sshiling #+# #+# # 9 | # Updated: 2017/11/02 12:41:22 by sshiling ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | NAME = libft.a 14 | FLAGS = -Wall -Wextra -Werror -I./includes 15 | 16 | SRCS = ft_memset.c ft_bzero.c ft_memcpy.c ft_memccpy.c ft_memmove.c\ 17 | ft_memchr.c ft_memcmp.c ft_strlen.c ft_strdup.c ft_strcpy.c ft_strncpy.c\ 18 | ft_strcat.c ft_strncat.c ft_strlcat.c ft_strchr.c ft_strrchr.c ft_strstr.c\ 19 | ft_strnstr.c ft_strcmp.c ft_strncmp.c ft_atoi.c ft_isalpha.c ft_isdigit.c\ 20 | ft_isalnum.c ft_isascii.c ft_isprint.c ft_toupper.c ft_tolower.c ft_memalloc.c\ 21 | ft_memdel.c ft_strnew.c ft_strdel.c ft_strclr.c ft_striter.c ft_striteri.c\ 22 | ft_strmap.c ft_strmapi.c ft_strequ.c ft_strnequ.c ft_strsub.c ft_strjoin.c\ 23 | ft_strtrim.c ft_strsplit.c ft_itoa.c ft_putchar.c ft_putstr.c ft_putendl.c\ 24 | ft_putnbr.c ft_putchar_fd.c ft_putstr_fd.c ft_putendl_fd.c ft_putnbr_fd.c\ 25 | ft_lstnew.c ft_lstdelone.c ft_lstdel.c ft_lstadd.c ft_lstiter.c ft_lstmap.c\ 26 | ft_lst_add_back.c ft_lst_push_data_front.c ft_lst_push_data_back.c\ 27 | ft_lst_last.c ft_lst_size.c\ 28 | ft_printf.c pre_res.c result_creator.c create_c.c create_i_d.c create_s.c\ 29 | create_u.c ft_itoa_max.c create_o.c create_x.c\ 30 | ft_itoa_base.c create_b_c.c create_b_s.c create_n.c width_precision.c\ 31 | flags.c modifiers.c create_i_d_sup.c mod_int_uox.c create_x_sup.c\ 32 | result_printer.c create_binary.c create_nprntbl.c print_in_color.c\ 33 | get_next_line.c 34 | 35 | OBJ_NAME = $(SRCS:.c=.o) 36 | 37 | OBJ = $(addprefix $(OBJ_DIR),$(OBJ_NAME)) 38 | SRC_DIR = srcs/ 39 | OBJ_DIR = objs/ 40 | 41 | all: $(NAME) 42 | 43 | $(NAME): $(OBJ) 44 | @ar rc $(NAME) $(OBJ) 45 | @ranlib $(NAME) 46 | @echo "##### libft created #####" 47 | 48 | $(OBJ_DIR)%.o: $(SRC_DIR)%.c 49 | @mkdir -p $(OBJ_DIR) 50 | @gcc $(FLAGS) -o $@ -c $< $(INC) 51 | 52 | clean: 53 | @rm -f $(OBJ) 54 | @echo "##### Removed libft object files #####" 55 | 56 | fclean: clean 57 | @rm -f $(NAME) 58 | @echo "##### Removed libft.a #####" 59 | 60 | re: fclean all 61 | 62 | .PHONY: all clean fclean re 63 | -------------------------------------------------------------------------------- /libft/author: -------------------------------------------------------------------------------- 1 | sshiling 2 | -------------------------------------------------------------------------------- /libft/includes/ft_printf.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_printf.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/01/24 15:59:23 by sshiling #+# #+# */ 9 | /* Updated: 2018/01/24 15:59:26 by sshiling ### ########.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 21 | # define S1(x) (x == 'c' || x == 'C' || x == 's' || x == 'S' || x == 'p') 22 | # define S2(x) (x == 'd' || x == 'D' || x == 'i' || x == 'o' || x == 'O') 23 | # define S3(x) (x == 'u' || x == 'U' || x == 'x' || x == 'X' || x == 'y') 24 | # define S4(x) (x == 'n' || x == 'b' || x == 'r' || x == 'w' || x == 'm') 25 | # define SPECIFIER(x) (S1(x) || S2(x) || S3(x) || S4(x)) 26 | # define N1(a) (a == '1' || a == '2' || a == '3' || a == '4' || a == '5') 27 | # define N2(a) (a == '6' || a == '7' || a == '8' || a == '9' || a == '0') 28 | # define NUM(a) (N1(a) || N2(a)) 29 | # define A1(b) (b == '0' || b == '-' || b == '+' || b == ' ' || b == 'h') 30 | # define A2(b) (b == 'l' || b == 'j' || b == 'z' || (b >= '1' && b <= '9')) 31 | # define A3(b) (b == '.' || b == '#' || b == '*' || b == 't') 32 | # define ALL(b) (A1(b) || A2(b) || A3(b)) 33 | # define ON 1 34 | # define OFF 0 35 | # define ZERO tmp->spec[0] 36 | # define DOT tmp->spec[1] 37 | # define MINUS tmp->spec[2] 38 | # define PLUS tmp->spec[3] 39 | # define SHARP tmp->spec[4] 40 | # define SPACE tmp->spec[5] 41 | # define MOD_HH tmp->spec[6] 42 | # define MOD_H tmp->spec[7] 43 | # define MOD_LL tmp->spec[8] 44 | # define MOD_L tmp->spec[9] 45 | # define MOD_J tmp->spec[10] 46 | # define MOD_Z tmp->spec[11] 47 | # define LOWER tmp->spec[12] 48 | # define UPPER tmp->spec[13] 49 | # define WIDTH tmp->width 50 | # define PREC tmp->precision 51 | # define EOC "\e[0m" 52 | # define RED "\e[1;31m" 53 | # define GREEN "\e[1;32m" 54 | # define YELLOW "\e[1;33m" 55 | # define BLUE "\e[1;34m" 56 | # define MAGENTA "\e[1;35m" 57 | # define CYAN "\e[1;36m" 58 | # define WHITE "\e[1;37m" 59 | 60 | typedef struct s_res 61 | { 62 | int *spec; 63 | void *data; 64 | char c; 65 | int width; 66 | int precision; 67 | int i; 68 | void *result; 69 | int length; 70 | struct s_res *next; 71 | } t_res; 72 | int ft_printf(const char *format, ...); 73 | t_res *tmp_result(const char *format, t_res *tmp, size_t i, 74 | va_list list); 75 | t_res *new_res(void); 76 | char *add_one_char(char *tmp, char c); 77 | t_res *save_prcnt(t_res *tmp, int i); 78 | t_res *pre_res(t_res *tmp, const char *format, size_t i, 79 | va_list list); 80 | t_res *result_creator(va_list list, t_res *tmp); 81 | char *create_c(va_list list, t_res *tmp); 82 | char *create_b_d(va_list list); 83 | char *create_b_u(va_list list); 84 | char *create_i_d(va_list list, t_res *tmp); 85 | char *create_s(va_list list, t_res *tmp); 86 | char *create_u(va_list list, t_res *tmp); 87 | char *create_o(va_list list, t_res *tmp); 88 | char *ft_itoa_max(ssize_t nbr); 89 | char *ft_itoa_base(size_t nb, int base, t_res *tmp); 90 | char *create_x(va_list list, t_res *tmp); 91 | char *ft_itoa_u_max(size_t nbr); 92 | char *create_b_c(va_list list, t_res *tmp); 93 | char *create_b_s(va_list list, t_res *tmp); 94 | char *fill_mask(char *dst, char *src, int len); 95 | char *find_mask(int len); 96 | char make_num_from_bi(char *src, int len); 97 | int ft_pow(int nb, int power); 98 | size_t *create_n(va_list list, t_res *tmp); 99 | size_t find_dot(t_res *tmp, const char *format, size_t i); 100 | size_t wildcard_precision(t_res *tmp, va_list list, size_t i); 101 | size_t find_precision(t_res *tmp, const char *format, 102 | size_t i, va_list list); 103 | size_t wildcard_width(t_res *tmp, va_list list, size_t i); 104 | size_t find_width(t_res *tmp, const char *format, size_t i, 105 | va_list list); 106 | size_t find_minus(t_res *tmp, const char *format, size_t i); 107 | size_t find_plus(t_res *tmp, const char *format, size_t i); 108 | size_t find_sharp(t_res *tmp, const char *format, size_t i); 109 | size_t find_space(t_res *tmp, const char *format, size_t i); 110 | size_t find_zero(t_res *tmp, const char *format, size_t i); 111 | size_t find_h(t_res *tmp, const char *format, size_t i); 112 | size_t find_l(t_res *tmp, const char *format, size_t i); 113 | size_t find_j(t_res *tmp, const char *format, size_t i); 114 | size_t find_z(t_res *tmp, const char *format, size_t i); 115 | ssize_t mod_int_i_d(va_list list, t_res *tmp); 116 | char *find_prefix_i_d(int key, t_res *tmp); 117 | char *num_first_i_d(int len, char *src, t_res *tmp); 118 | char filler_finder(t_res *tmp); 119 | char *num_after_i_d(int len, char *src, t_res *tmp); 120 | size_t mod_int_uox(va_list list, t_res *tmp); 121 | char *create_precision_x(char *prefix, char *src, t_res *tmp); 122 | char *create_result_x(char *dst, t_res *tmp); 123 | char *fill_zero_x(char *prefix, char *src, char *dst, 124 | t_res *tmp); 125 | char *fill_minus_x(char *prefix, char *src, char *dst, 126 | t_res *tmp); 127 | char *fill_space_x(char *prefix, char *src, char *dst, 128 | t_res *tmp); 129 | int ft_putstr_new(char const *s); 130 | int ft_putstr_res(char *s, t_res *tmp); 131 | int result_printer(t_res *res); 132 | void t_res_del(t_res *src); 133 | char *create_binary(va_list list, t_res *tmp); 134 | char *create_nprntbl(va_list list, t_res *tmp); 135 | size_t find_t(t_res *tmp, const char *format, size_t i); 136 | char *color_setter(char *s); 137 | char *color_finder(char *s); 138 | char *create_final_color(char *finder, char *s); 139 | int ft_putstr_data(char *s); 140 | extern int g_fd; 141 | 142 | #endif 143 | -------------------------------------------------------------------------------- /libft/includes/get_next_line.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/12/07 14:06:33 by sshiling #+# #+# */ 9 | /* Updated: 2017/12/07 14:06:38 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef GET_NEXT_LINE_H 14 | # define GET_NEXT_LINE_H 15 | 16 | # define BUFF_SIZE 512 17 | # include 18 | # include 19 | 20 | typedef struct s_line 21 | { 22 | char *data; 23 | int fd; 24 | struct s_line *next; 25 | } t_line; 26 | int get_next_line(const int fd, char **line); 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /libft/includes/libft.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/01 13:34:45 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/04 12:17:18 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef LIBFT_H 14 | # define LIBFT_H 15 | # include "ft_printf.h" 16 | # include "get_next_line.h" 17 | # include 18 | # include 19 | # include 20 | 21 | typedef struct s_list 22 | { 23 | void *content; 24 | size_t content_size; 25 | struct s_list *next; 26 | } t_list; 27 | void *ft_memset(void *b, int c, size_t len); 28 | void ft_bzero(void *s, size_t n); 29 | void *ft_memcpy(void *dst, const void *src, size_t n); 30 | void *ft_memccpy(void *dst, const void *src, int c, size_t n); 31 | void *ft_memmove(void *dst, const void *src, size_t len); 32 | void *ft_memchr(const void *s, int c, size_t n); 33 | int ft_memcmp(const void *s1, const void *s2, size_t n); 34 | size_t ft_strlen(const char *s); 35 | char *ft_strdup(const char *s1); 36 | char *ft_strcpy(char *dst, const char *src); 37 | char *ft_strncpy(char *dst, const char *src, size_t len); 38 | char *ft_strcat(char *s1, const char *s2); 39 | char *ft_strncat(char *s1, const char *s2, size_t n); 40 | size_t ft_strlcat(char *dst, const char *src, size_t dstsize); 41 | char *ft_strchr(const char *s, int c); 42 | char *ft_strrchr(const char *s, int c); 43 | char *ft_strstr(const char *haystack, const char *needle); 44 | char *ft_strnstr(const char *haystack, 45 | const char *needle, size_t len); 46 | int ft_strcmp(const char *s1, const char *s2); 47 | int ft_strncmp(const char *s1, const char *s2, size_t n); 48 | int ft_atoi(const char *str); 49 | int ft_isalpha(int c); 50 | int ft_isdigit(int c); 51 | int ft_isalnum(int c); 52 | int ft_isascii(int c); 53 | int ft_isprint(int c); 54 | int ft_toupper(int c); 55 | int ft_tolower(int c); 56 | void *ft_memalloc(size_t size); 57 | void ft_memdel(void **ap); 58 | char *ft_strnew(size_t size); 59 | void ft_strdel(char **as); 60 | void ft_strclr(char *s); 61 | void ft_striter(char *s, void (*f)(char *)); 62 | void ft_striteri(char *s, void (*f)(unsigned int, char *)); 63 | char *ft_strmap(char const *s, char (*f)(char)); 64 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); 65 | int ft_strequ(char const *s1, char const *s2); 66 | int ft_strnequ(char const *s1, char const *s2, size_t n); 67 | char *ft_strsub(char const *s, unsigned int start, size_t len); 68 | char *ft_strjoin(char const *s1, char const *s2); 69 | char *ft_strtrim(char const *s); 70 | char **ft_strsplit(char const *s, char c); 71 | char *ft_itoa(int n); 72 | void ft_putchar(char c); 73 | void ft_putstr(char const *s); 74 | void ft_putendl(char const *s); 75 | void ft_putnbr(int n); 76 | void ft_putchar_fd(char c, int fd); 77 | void ft_putstr_fd(char const *s, int fd); 78 | void ft_putendl_fd(char const *s, int fd); 79 | void ft_putnbr_fd(int n, int fd); 80 | t_list *ft_lstnew(void const *content, size_t content_size); 81 | void ft_lstdelone(t_list **alst, void (*del)(void*, size_t)); 82 | void ft_lstdel(t_list **alst, void (*del)(void *, size_t)); 83 | void ft_lstadd(t_list **alst, t_list *new); 84 | void ft_lstiter(t_list *lst, void (*f)(t_list *elem)); 85 | t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)); 86 | void ft_lst_add_back(t_list **alst, t_list *new); 87 | void ft_lst_push_data_front(t_list **alst, 88 | void const *content, size_t content_size); 89 | void ft_lst_push_data_back(t_list **alst, 90 | void const *content, size_t content_size); 91 | t_list *ft_lst_last(t_list **alst); 92 | int ft_lst_size(t_list **alst); 93 | 94 | #endif 95 | -------------------------------------------------------------------------------- /libft/objs/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sshiling/42-push_swap/be233949e4c1bd5bd9a1727bcc8ff3d60eb9c8de/libft/objs/.DS_Store -------------------------------------------------------------------------------- /libft/srcs/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sshiling/42-push_swap/be233949e4c1bd5bd9a1727bcc8ff3d60eb9c8de/libft/srcs/.DS_Store -------------------------------------------------------------------------------- /libft/srcs/create_b_c.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* create_b_c.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/01/10 12:42:00 by sshiling #+# #+# */ 9 | /* Updated: 2018/01/10 12:42:03 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_pow(int nb, int power) 16 | { 17 | int rengers; 18 | 19 | rengers = nb; 20 | if (power < 0) 21 | rengers = 0; 22 | if (power > 0) 23 | { 24 | while (power > 1) 25 | { 26 | rengers = rengers * nb; 27 | power--; 28 | } 29 | } 30 | if (power == 0) 31 | rengers = 1; 32 | return (rengers); 33 | } 34 | 35 | char make_num_from_bi(char *src, int len) 36 | { 37 | int rem; 38 | int sum; 39 | int power; 40 | 41 | rem = 0; 42 | sum = 0; 43 | power = 0; 44 | len--; 45 | while (len >= 0) 46 | { 47 | rem = (src[len] - '0') % 10; 48 | len--; 49 | sum = sum + rem * ft_pow(2, power); 50 | power++; 51 | } 52 | return (sum); 53 | } 54 | 55 | char *find_mask(int len) 56 | { 57 | if (len <= 7) 58 | return ("0xxxxxxx"); 59 | if (len <= 11) 60 | return ("110xxxxx10xxxxxx"); 61 | if (len > 11 && len <= 16) 62 | return ("1110xxxx10xxxxxx10xxxxxx"); 63 | else 64 | return ("11110xxx10xxxxxx10xxxxxx10xxxxxx"); 65 | } 66 | 67 | char *fill_mask(char *dst, char *src, int len) 68 | { 69 | char *tmp; 70 | int i; 71 | 72 | dst = ft_strnew(4); 73 | tmp = ft_strdup(find_mask(len + 1)); 74 | i = ft_strlen(tmp) - 1; 75 | while (i >= 0) 76 | { 77 | if (tmp[i] == 'x' && len >= 0) 78 | { 79 | tmp[i] = src[len]; 80 | len--; 81 | } 82 | else if (tmp[i] == 'x' && len < 0) 83 | tmp[i] = '0'; 84 | i--; 85 | } 86 | while (++i < (int)ft_strlen(tmp) / 8) 87 | dst[i] = make_num_from_bi(&tmp[i * 8], 8); 88 | free(tmp); 89 | return (dst); 90 | } 91 | 92 | char *create_b_c(va_list list, t_res *tmp) 93 | { 94 | char *src; 95 | char *dst; 96 | 97 | dst = NULL; 98 | src = ft_itoa_base(va_arg(list, int), 2, tmp); 99 | if (src[0] == '0' && src[1] == '\0') 100 | { 101 | tmp->length = 1; 102 | free(src); 103 | return (ft_strdup("\0")); 104 | } 105 | dst = fill_mask(dst, src, (ft_strlen(src) - 1)); 106 | tmp->length = ft_strlen(dst); 107 | free(src); 108 | return (dst); 109 | } 110 | -------------------------------------------------------------------------------- /libft/srcs/create_b_s.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* create_b_s.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/01/10 19:34:57 by sshiling #+# #+# */ 9 | /* Updated: 2018/01/10 19:34:59 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static char filler_finder_s(t_res *tmp) 16 | { 17 | if (!MINUS && ZERO) 18 | return ('0'); 19 | return (' '); 20 | } 21 | 22 | static char *add_width(char *dst, t_res *tmp) 23 | { 24 | char filler; 25 | char *new; 26 | 27 | filler = filler_finder_s(tmp); 28 | if (WIDTH > (int)ft_strlen(dst)) 29 | { 30 | new = ft_strnew(WIDTH); 31 | ft_memset(new, filler, WIDTH); 32 | if (MINUS) 33 | ft_strncpy(new, dst, ft_strlen(dst)); 34 | else 35 | ft_strncpy(&new[WIDTH - ft_strlen(dst)], dst, ft_strlen(dst)); 36 | tmp->length = ft_strlen(new); 37 | free(dst); 38 | return (new); 39 | } 40 | return (dst); 41 | } 42 | 43 | static char *dst_creator(int *val, char *src, char *dst, t_res *tmp) 44 | { 45 | char *del; 46 | int i; 47 | 48 | i = 0; 49 | while (val[i]) 50 | { 51 | src = ft_itoa_base(val[i], 2, tmp); 52 | del = src; 53 | src = fill_mask(dst, src, (ft_strlen(src) - 1)); 54 | free(del); 55 | if (!DOT || (DOT && PREC >= (int)(ft_strlen(dst) + ft_strlen(src)))) 56 | { 57 | del = dst; 58 | dst = ft_strjoin(dst, src); 59 | free(del); 60 | free(src); 61 | } 62 | else 63 | { 64 | free(src); 65 | break ; 66 | } 67 | i++; 68 | } 69 | return (dst); 70 | } 71 | 72 | char *create_b_s(va_list list, t_res *tmp) 73 | { 74 | char *src; 75 | char *dst; 76 | int *val; 77 | 78 | src = NULL; 79 | dst = ft_strdup("\0"); 80 | if (!(val = va_arg(list, void*))) 81 | { 82 | free(dst); 83 | return (ft_strdup("(null)")); 84 | } 85 | dst = dst_creator(val, src, dst, tmp); 86 | dst = add_width(dst, tmp); 87 | tmp->length = ft_strlen(dst); 88 | return (dst); 89 | } 90 | -------------------------------------------------------------------------------- /libft/srcs/create_binary.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* create_binary.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/01/23 14:56:20 by sshiling #+# #+# */ 9 | /* Updated: 2018/01/23 14:56:21 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *create_binary(va_list list, t_res *tmp) 16 | { 17 | char *src; 18 | 19 | src = ft_itoa_base((int)(va_arg(list, ssize_t)), 2, tmp); 20 | if (src == NULL) 21 | src = ft_strdup("null"); 22 | return (src); 23 | } 24 | -------------------------------------------------------------------------------- /libft/srcs/create_c.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* create_c.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/12/22 15:50:38 by sshiling #+# #+# */ 9 | /* Updated: 2017/12/22 15:50:53 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char str_fill(t_res *tmp) 16 | { 17 | if (ZERO && !MINUS) 18 | return ('0'); 19 | return (' '); 20 | } 21 | 22 | char *create_c(va_list list, t_res *tmp) 23 | { 24 | char *str; 25 | int c; 26 | int i; 27 | 28 | tmp->length = !(WIDTH) ? 1 : WIDTH; 29 | str = ft_strnew(tmp->length); 30 | ft_memset(str, str_fill(tmp), tmp->length); 31 | c = !(SPECIFIER(tmp->c)) ? tmp->c : va_arg(list, int); 32 | i = (WIDTH && !(MINUS)) ? WIDTH - 1 : 0; 33 | str[i] = (char)c; 34 | return (str); 35 | } 36 | -------------------------------------------------------------------------------- /libft/srcs/create_i_d.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* create_i_d.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/01/16 11:46:30 by sshiling #+# #+# */ 9 | /* Updated: 2018/01/16 11:46:34 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static char *do_str_shorter(char *dst) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | while (dst[i]) 21 | i++; 22 | i--; 23 | if (!(NUM(dst[0])) && dst[i] == ' ') 24 | dst[i] = '\0'; 25 | return (dst); 26 | } 27 | 28 | static char *add_prefix(char *prefix, char *dst, t_res *tmp) 29 | { 30 | char *del; 31 | int i; 32 | 33 | i = 0; 34 | if (prefix) 35 | { 36 | if ((dst[i] == '0' && dst[ft_strlen(dst) - 1] == ' ') || 37 | (NUM(dst[i]) && dst[i] != '0') || PREC == 38 | (int)ft_strlen(dst) || (dst[i] == '0' && dst[i + 1] == '\0')) 39 | { 40 | del = dst; 41 | dst = ft_strjoin(prefix, dst); 42 | free(del); 43 | } 44 | else if (dst[i] == '0') 45 | ft_strncpy(&dst[i], prefix, 1); 46 | else 47 | { 48 | while (!NUM(dst[i])) 49 | i++; 50 | ft_strncpy(&dst[i - 1], prefix, 1); 51 | } 52 | } 53 | return (dst); 54 | } 55 | 56 | static char *null_exception_i_d(int len) 57 | { 58 | char *new; 59 | 60 | new = ft_strnew(len); 61 | ft_memset(new, ' ', len); 62 | return (new); 63 | } 64 | 65 | static char *exceptions_i_d(char *src, t_res *tmp) 66 | { 67 | if (DOT && !PREC && !WIDTH && src[0] == '0') 68 | { 69 | free(src); 70 | return (ft_strdup("")); 71 | } 72 | if (DOT && !PREC && src[0] == '0') 73 | { 74 | free(src); 75 | return (null_exception_i_d(WIDTH)); 76 | } 77 | return (NULL); 78 | } 79 | 80 | char *create_i_d(va_list list, t_res *tmp) 81 | { 82 | char *src; 83 | char *dst; 84 | char *del; 85 | int len; 86 | int key; 87 | 88 | src = ft_itoa_max(mod_int_i_d(list, tmp)); 89 | del = src; 90 | key = src[0] == '-' ? -1 : 1; 91 | src = src[0] == '-' ? &src[1] : src; 92 | len = WIDTH >= (int)ft_strlen(src) ? WIDTH : ft_strlen(src); 93 | len = (int)PREC >= len ? (int)PREC : len; 94 | if ((dst = exceptions_i_d(src, tmp)) != NULL) 95 | return (dst); 96 | if (MINUS) 97 | dst = num_first_i_d(len, src, tmp); 98 | else 99 | dst = num_after_i_d(len, src, tmp); 100 | free(del); 101 | dst = do_str_shorter(add_prefix(find_prefix_i_d(key, tmp), dst, tmp)); 102 | tmp->length = ft_strlen(dst); 103 | return (dst); 104 | } 105 | -------------------------------------------------------------------------------- /libft/srcs/create_i_d_sup.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* create_i_d_sup.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/01/22 18:23:04 by sshiling #+# #+# */ 9 | /* Updated: 2018/01/22 18:23:06 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | ssize_t mod_int_i_d(va_list list, t_res *tmp) 16 | { 17 | if (MOD_Z) 18 | return ((ssize_t)(va_arg(list, ssize_t))); 19 | else if (MOD_J) 20 | return ((intmax_t)(va_arg(list, ssize_t))); 21 | else if (MOD_LL) 22 | return ((long long int)(va_arg(list, ssize_t))); 23 | else if (MOD_L) 24 | return ((long int)(va_arg(list, ssize_t))); 25 | else if (MOD_H) 26 | return ((short int)(va_arg(list, ssize_t))); 27 | else if (MOD_HH) 28 | return ((signed char)(va_arg(list, ssize_t))); 29 | else 30 | return (int)(va_arg(list, ssize_t)); 31 | } 32 | 33 | char *find_prefix_i_d(int key, t_res *tmp) 34 | { 35 | if (key < 0) 36 | return ("-"); 37 | else if (PLUS) 38 | return ("+"); 39 | else if (SPACE) 40 | return (" "); 41 | return (NULL); 42 | } 43 | 44 | char *num_first_i_d(int len, char *src, t_res *tmp) 45 | { 46 | char *dst; 47 | int i; 48 | 49 | i = ft_strlen(src); 50 | dst = ft_strnew(len); 51 | ft_memset(dst, ' ', len); 52 | ft_memset(dst, '0', PREC); 53 | if ((int)PREC > i) 54 | ft_strncpy(&dst[PREC - i], src, i); 55 | else 56 | ft_strncpy(dst, src, i); 57 | return (dst); 58 | } 59 | 60 | char filler_finder(t_res *tmp) 61 | { 62 | if (!DOT && !MINUS && ZERO) 63 | return ('0'); 64 | return (' '); 65 | } 66 | 67 | char *num_after_i_d(int len, char *src, t_res *tmp) 68 | { 69 | char *dst; 70 | int i; 71 | 72 | i = ft_strlen(src); 73 | dst = ft_strnew(len); 74 | ft_memset(dst, filler_finder(tmp), len); 75 | ft_memset(&dst[len - PREC], '0', PREC); 76 | ft_strncpy(&dst[len - i], src, i); 77 | return (dst); 78 | } 79 | -------------------------------------------------------------------------------- /libft/srcs/create_n.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* create_n.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/01/22 15:49:23 by sshiling #+# #+# */ 9 | /* Updated: 2018/01/22 15:50:25 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t *create_n(va_list list, t_res *tmp) 16 | { 17 | if (MOD_Z) 18 | return (size_t*)(va_arg(list, size_t*)); 19 | else if (MOD_J) 20 | return (size_t*)(va_arg(list, intmax_t*)); 21 | else if (MOD_LL) 22 | return (size_t*)(va_arg(list, long long unsigned int*)); 23 | else if (MOD_L) 24 | return (size_t*)(va_arg(list, long unsigned int*)); 25 | else if (MOD_H) 26 | return (size_t*)(va_arg(list, short unsigned int*)); 27 | else if (MOD_HH) 28 | return (size_t*)(va_arg(list, unsigned char*)); 29 | else 30 | return (size_t*)(va_arg(list, unsigned int*)); 31 | } 32 | -------------------------------------------------------------------------------- /libft/srcs/create_nprntbl.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* create_nprntbl.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/01/23 15:14:16 by sshiling #+# #+# */ 9 | /* Updated: 2018/01/23 15:14:18 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *create_nprntbl(va_list list, t_res *tmp) 16 | { 17 | char *src; 18 | char *dst; 19 | int i; 20 | 21 | i = 0; 22 | src = va_arg(list, char*); 23 | if (src == NULL) 24 | { 25 | src = ft_strdup("null"); 26 | WIDTH = 4; 27 | return (src); 28 | } 29 | dst = ft_strnew(WIDTH); 30 | while (i < WIDTH) 31 | { 32 | dst[i] = src[i]; 33 | i++; 34 | } 35 | tmp->length = WIDTH; 36 | return (dst); 37 | } 38 | -------------------------------------------------------------------------------- /libft/srcs/create_o.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* create_o.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/12/25 19:09:20 by sshiling #+# #+# */ 9 | /* Updated: 2017/12/25 19:09:24 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static char *ft_create_mid_l(char *src, t_res *tmp, int len) 16 | { 17 | char *dst; 18 | 19 | dst = ft_strnew(len); 20 | ft_memset(dst, ' ', len); 21 | if (PREC > (int)ft_strlen(src)) 22 | { 23 | ft_memset(dst, '0', PREC); 24 | ft_strncpy(&dst[PREC - ft_strlen(src)], src, ft_strlen(src)); 25 | } 26 | else 27 | ft_strncpy(dst, src, ft_strlen(src)); 28 | return (dst); 29 | } 30 | 31 | static char *ft_create_mid_r(char *src, t_res *tmp, int len) 32 | { 33 | char filler; 34 | char *dst; 35 | 36 | filler = filler_finder(tmp); 37 | dst = ft_strnew(len); 38 | ft_memset(dst, filler, len); 39 | ft_memset(&dst[len - PREC], '0', PREC); 40 | ft_strncpy(&dst[len - ft_strlen(src)], src, ft_strlen(src)); 41 | return (dst); 42 | } 43 | 44 | static int find_symbol_o(char *dst) 45 | { 46 | int i; 47 | 48 | i = 0; 49 | while (dst[i]) 50 | i++; 51 | i--; 52 | while (!NUM(dst[i])) 53 | { 54 | if (dst[i] == ' ') 55 | return (1); 56 | i--; 57 | } 58 | return (0); 59 | } 60 | 61 | static char *upgrade_o(char *pre, char *dst, t_res *tmp) 62 | { 63 | int i; 64 | char *del; 65 | 66 | i = 0; 67 | if (SHARP && dst[0] == ' ') 68 | { 69 | while (dst[i] && dst[i] == ' ') 70 | i++; 71 | if (i != 0 && dst[i] != '0') 72 | dst[i - 1] = pre[0]; 73 | } 74 | else if (SHARP && dst[0] != ' ' && dst[0] != '0') 75 | { 76 | if (find_symbol_o(dst)) 77 | dst[ft_strlen(dst) - 1] = '\0'; 78 | del = dst; 79 | dst = ft_strjoin(pre, dst); 80 | free(del); 81 | } 82 | return (dst); 83 | } 84 | 85 | char *create_o(va_list list, t_res *tmp) 86 | { 87 | char *src; 88 | char *dst; 89 | char *pre; 90 | int len; 91 | 92 | src = ft_itoa_base(mod_int_uox(list, tmp), 8, tmp); 93 | if (DOT && !PREC && src[0] == '0') 94 | { 95 | free(src); 96 | src = ft_strdup(""); 97 | } 98 | len = WIDTH > (int)ft_strlen(src) ? WIDTH : ft_strlen(src); 99 | len = (int)PREC > len ? (int)PREC : len; 100 | pre = SHARP == ON ? "0" : ""; 101 | if (MINUS) 102 | dst = ft_create_mid_l(src, tmp, len); 103 | else 104 | dst = ft_create_mid_r(src, tmp, len); 105 | dst = upgrade_o(pre, dst, tmp); 106 | free(src); 107 | tmp->length = ft_strlen(dst); 108 | return (dst); 109 | } 110 | -------------------------------------------------------------------------------- /libft/srcs/create_s.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* create_s.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/12/28 13:21:07 by sshiling #+# #+# */ 9 | /* Updated: 2017/12/28 13:21:10 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static char filler_finder_s(t_res *tmp) 16 | { 17 | if (MINUS) 18 | return (' '); 19 | if (ZERO) 20 | return ('0'); 21 | return (' '); 22 | } 23 | 24 | static char *fill_minus(char *src, int len, t_res *tmp) 25 | { 26 | char *dst; 27 | char filler; 28 | int size; 29 | 30 | size = (int)WIDTH >= len ? (int)WIDTH : len; 31 | dst = ft_strnew(size); 32 | filler = filler_finder_s(tmp); 33 | dst = ft_strncpy(dst, src, len); 34 | ft_memset(&dst[len], filler, size - len); 35 | tmp->length = ft_strlen(dst); 36 | return (dst); 37 | } 38 | 39 | static char *fill_first(char *src, int len, t_res *tmp) 40 | { 41 | char *dst; 42 | char filler; 43 | int size; 44 | 45 | size = (int)WIDTH >= len ? (int)WIDTH : len; 46 | dst = ft_strnew(size); 47 | filler = filler_finder_s(tmp); 48 | ft_memset(dst, filler, size - len); 49 | ft_strncpy(&dst[size - len], src, len); 50 | tmp->length = ft_strlen(dst); 51 | return (dst); 52 | } 53 | 54 | char *change_case(char *src, t_res *tmp) 55 | { 56 | int i; 57 | 58 | i = 0; 59 | if (UPPER) 60 | { 61 | while (src[i]) 62 | { 63 | src[i] = ft_toupper(src[i]); 64 | i++; 65 | } 66 | } 67 | else 68 | { 69 | while (src[i]) 70 | { 71 | src[i] = ft_tolower(src[i]); 72 | i++; 73 | } 74 | } 75 | return (src); 76 | } 77 | 78 | char *create_s(va_list list, t_res *tmp) 79 | { 80 | char *src; 81 | int len; 82 | int fin_len; 83 | 84 | src = va_arg(list, char*); 85 | if (src == NULL) 86 | src = "(null)"; 87 | len = ft_strlen(src); 88 | fin_len = DOT == ON ? PREC : len; 89 | fin_len = fin_len >= len ? len : fin_len; 90 | if (MINUS) 91 | src = fill_minus(src, fin_len, tmp); 92 | else 93 | src = fill_first(src, fin_len, tmp); 94 | if (LOWER || UPPER) 95 | src = change_case(src, tmp); 96 | return (src); 97 | } 98 | -------------------------------------------------------------------------------- /libft/srcs/create_u.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* create_u.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/12/25 17:10:26 by sshiling #+# #+# */ 9 | /* Updated: 2017/12/25 17:10:29 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static char *ft_create_mid_l(char *src, t_res *tmp, int len) 16 | { 17 | char *dst; 18 | 19 | dst = ft_strnew(len); 20 | ft_memset(dst, ' ', len); 21 | if (PREC > (int)ft_strlen(src)) 22 | { 23 | ft_memset(dst, '0', PREC); 24 | ft_strncpy(&dst[PREC - ft_strlen(src)], src, ft_strlen(src)); 25 | } 26 | else 27 | ft_strncpy(dst, src, ft_strlen(src)); 28 | return (dst); 29 | } 30 | 31 | static char *ft_create_mid_r(char *src, t_res *tmp, int len) 32 | { 33 | char filler; 34 | char *dst; 35 | 36 | filler = filler_finder(tmp); 37 | dst = ft_strnew(len); 38 | ft_memset(dst, filler, len); 39 | ft_memset(&dst[len - PREC], '0', PREC); 40 | ft_strncpy(&dst[len - ft_strlen(src)], src, ft_strlen(src)); 41 | return (dst); 42 | } 43 | 44 | char *create_u(va_list list, t_res *tmp) 45 | { 46 | char *src; 47 | char *dst; 48 | int len; 49 | 50 | src = ft_itoa_base(mod_int_uox(list, tmp), 10, tmp); 51 | if (DOT && !PREC && src[0] == '0') 52 | { 53 | free(src); 54 | src = ft_strdup(""); 55 | } 56 | len = WIDTH > (int)ft_strlen(src) ? WIDTH : ft_strlen(src); 57 | len = (int)PREC > len ? (int)PREC : len; 58 | if (MINUS) 59 | dst = ft_create_mid_l(src, tmp, len); 60 | else 61 | dst = ft_create_mid_r(src, tmp, len); 62 | free(src); 63 | tmp->length = ft_strlen(dst); 64 | return (dst); 65 | } 66 | -------------------------------------------------------------------------------- /libft/srcs/create_x.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* create_x.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/12/25 19:26:23 by sshiling #+# #+# */ 9 | /* Updated: 2017/12/25 19:26:24 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *create_width(char *prefix, char *src, t_res *tmp) 16 | { 17 | char *dst; 18 | int prefix_len; 19 | int len; 20 | 21 | len = ft_strlen(src); 22 | prefix_len = !(prefix) ? 0 : 2; 23 | if (WIDTH > len + prefix_len) 24 | { 25 | dst = ft_strnew(WIDTH); 26 | if (filler_finder(tmp) == '0') 27 | fill_zero_x(prefix, src, dst, tmp); 28 | else if (MINUS) 29 | fill_minus_x(prefix, src, dst, tmp); 30 | else 31 | fill_space_x(prefix, src, dst, tmp); 32 | } 33 | else 34 | { 35 | if (prefix) 36 | dst = ft_strjoin(prefix, src); 37 | else 38 | dst = ft_strdup(src); 39 | } 40 | return (dst); 41 | } 42 | 43 | char *exceptions_x(char *src, t_res *tmp) 44 | { 45 | if ((tmp->c == 'x' || tmp->c == 'X') && src[0] == '0' && 46 | src[1] == '\0' && !WIDTH && DOT && !PREC) 47 | return (ft_strdup("")); 48 | if ((tmp->c == 'x' || tmp->c == 'X') && src[0] == '0' && 49 | src[1] == '\0' && !WIDTH && SHARP && !DOT) 50 | return (ft_strdup("0")); 51 | if (tmp->c == 'p' && src[0] == '0' && src[1] == '\0' && 52 | !WIDTH && DOT && !PREC) 53 | return (ft_strdup("0x")); 54 | if (tmp->c == 'p' && src[0] == '0' && src[1] == '\0' && 55 | !WIDTH && !DOT) 56 | return (ft_strdup("0x0")); 57 | return (NULL); 58 | } 59 | 60 | char *create_x(va_list list, t_res *tmp) 61 | { 62 | char *src; 63 | char *dst; 64 | char *prefix; 65 | 66 | src = ft_itoa_base(mod_int_uox(list, tmp), 16, tmp); 67 | if ((dst = exceptions_x(src, tmp)) != NULL) 68 | { 69 | free(src); 70 | return (dst); 71 | } 72 | if (DOT && !PREC && src[0] == '0' && src[1] == '\0') 73 | src[0] = ' '; 74 | prefix = tmp->c == 'X' ? "0X" : "0x"; 75 | prefix = !(SHARP) ? NULL : prefix; 76 | if (DOT) 77 | dst = create_precision_x(prefix, src, tmp); 78 | if (dst) 79 | dst = create_result_x(dst, tmp); 80 | else 81 | dst = create_width(prefix, src, tmp); 82 | tmp->length = ft_strlen(dst); 83 | free(src); 84 | return (dst); 85 | } 86 | -------------------------------------------------------------------------------- /libft/srcs/create_x_sup.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* create_x_sup.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/01/22 18:51:45 by sshiling #+# #+# */ 9 | /* Updated: 2018/01/22 18:51:47 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *create_precision_x(char *prefix, char *src, t_res *tmp) 16 | { 17 | char *dst; 18 | int prefix_len; 19 | int len; 20 | 21 | len = ft_strlen(src); 22 | prefix_len = !(prefix) ? 0 : 2; 23 | if (PREC > len) 24 | { 25 | dst = ft_strnew(PREC + prefix_len); 26 | ft_memset(dst, '0', PREC + prefix_len); 27 | if (prefix) 28 | ft_strncpy(dst, prefix, prefix_len); 29 | ft_strncpy(&dst[PREC + prefix_len - len], src, len); 30 | } 31 | else 32 | { 33 | if (prefix) 34 | dst = ft_strjoin(prefix, src); 35 | else 36 | dst = ft_strdup(src); 37 | } 38 | return (dst); 39 | } 40 | 41 | char *create_result_x(char *dst, t_res *tmp) 42 | { 43 | char *new; 44 | 45 | if (WIDTH <= (int)ft_strlen(dst)) 46 | return (dst); 47 | new = ft_strnew(WIDTH); 48 | if (MINUS) 49 | { 50 | ft_strncpy(new, dst, ft_strlen(dst)); 51 | ft_memset(&new[ft_strlen(dst)], ' ', WIDTH - ft_strlen(dst)); 52 | free(dst); 53 | } 54 | else 55 | { 56 | ft_strncpy(&new[WIDTH - ft_strlen(dst)], dst, ft_strlen(dst)); 57 | ft_memset(new, ' ', WIDTH - ft_strlen(dst)); 58 | free(dst); 59 | } 60 | return (new); 61 | } 62 | 63 | char *fill_zero_x(char *prefix, char *src, char *dst, t_res *tmp) 64 | { 65 | char filler; 66 | int prefix_len; 67 | int len; 68 | 69 | len = ft_strlen(src); 70 | filler = filler_finder(tmp); 71 | prefix_len = !(prefix) ? 0 : 2; 72 | if (prefix) 73 | ft_strncpy(dst, prefix, prefix_len); 74 | ft_strncpy(&dst[WIDTH - len], src, len); 75 | ft_memset(&dst[prefix_len], filler, WIDTH - (len + prefix_len)); 76 | return (dst); 77 | } 78 | 79 | char *fill_minus_x(char *prefix, char *src, char *dst, t_res *tmp) 80 | { 81 | char filler; 82 | int prefix_len; 83 | int len; 84 | 85 | len = ft_strlen(src); 86 | filler = filler_finder(tmp); 87 | prefix_len = !(prefix) ? 0 : 2; 88 | if (prefix) 89 | ft_strncpy(dst, prefix, prefix_len); 90 | ft_strncpy(&dst[prefix_len], src, len); 91 | ft_memset(&dst[prefix_len + len], filler, WIDTH - len - prefix_len); 92 | return (dst); 93 | } 94 | 95 | char *fill_space_x(char *prefix, char *src, char *dst, t_res *tmp) 96 | { 97 | char filler; 98 | int prefix_len; 99 | int len; 100 | 101 | len = ft_strlen(src); 102 | filler = filler_finder(tmp); 103 | prefix_len = !(prefix) ? 0 : 2; 104 | ft_memset(dst, filler, WIDTH - len - prefix_len); 105 | if (prefix) 106 | ft_strncpy(&dst[WIDTH - len - prefix_len], prefix, prefix_len); 107 | ft_strncpy(&dst[WIDTH - len], src, len); 108 | return (dst); 109 | } 110 | -------------------------------------------------------------------------------- /libft/srcs/flags.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* flags.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/01/22 18:06:56 by sshiling #+# #+# */ 9 | /* Updated: 2018/01/22 18:06:58 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t find_minus(t_res *tmp, const char *format, size_t i) 16 | { 17 | if (format[i] == '-') 18 | { 19 | tmp->spec[2] = ON; 20 | i++; 21 | } 22 | return (i); 23 | } 24 | 25 | size_t find_plus(t_res *tmp, const char *format, size_t i) 26 | { 27 | if (format[i] == '+') 28 | { 29 | tmp->spec[3] = ON; 30 | i++; 31 | } 32 | return (i); 33 | } 34 | 35 | size_t find_sharp(t_res *tmp, const char *format, size_t i) 36 | { 37 | if (format[i] == '#') 38 | { 39 | tmp->spec[4] = ON; 40 | i++; 41 | } 42 | return (i); 43 | } 44 | 45 | size_t find_space(t_res *tmp, const char *format, size_t i) 46 | { 47 | if (format[i] == ' ') 48 | { 49 | tmp->spec[5] = ON; 50 | i++; 51 | } 52 | return (i); 53 | } 54 | 55 | size_t find_zero(t_res *tmp, const char *format, size_t i) 56 | { 57 | if (format[0] == '0') 58 | { 59 | tmp->spec[0] = ON; 60 | i++; 61 | } 62 | else if (format[i] == '0' && format[i - 1] != '.') 63 | { 64 | tmp->spec[0] = ON; 65 | i++; 66 | } 67 | return (i); 68 | } 69 | -------------------------------------------------------------------------------- /libft/srcs/ft_atoi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_atoi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/10/25 16:24:48 by sshiling #+# #+# */ 9 | /* Updated: 2017/10/25 16:37:36 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_atoi(const char *str) 16 | { 17 | int i; 18 | long long int nb; 19 | int n; 20 | 21 | i = 0; 22 | n = 1; 23 | nb = 0; 24 | while ((str[i] >= 9 && str[i] <= 13) || str[i] == ' ') 25 | i++; 26 | if (str[i] == '-') 27 | n = -1; 28 | if (str[i] == '-' || str[i] == '+') 29 | i++; 30 | while (str[i] >= '0' && str[i] <= '9') 31 | { 32 | nb = nb * 10 + (str[i] - '0'); 33 | if (nb < 0 && n == -1 && nb != -2147483648) 34 | return (0); 35 | if (nb < 0 && n == 1) 36 | return (-1); 37 | i++; 38 | } 39 | return (nb * n); 40 | } 41 | -------------------------------------------------------------------------------- /libft/srcs/ft_bzero.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_bzero.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/01 13:10:19 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/02 16:34:53 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_bzero(void *s, size_t n) 16 | { 17 | ft_memset(s, '\0', n); 18 | } 19 | -------------------------------------------------------------------------------- /libft/srcs/ft_isalnum.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalnum.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/10/30 18:51:31 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/02 15:04:44 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isalnum(int c) 16 | { 17 | if (ft_isalpha(c) || ft_isdigit(c)) 18 | return (1); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /libft/srcs/ft_isalpha.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalpha.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/10/30 16:54:35 by sshiling #+# #+# */ 9 | /* Updated: 2017/10/30 17:32:54 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isalpha(int c) 16 | { 17 | if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122)) 18 | return (1); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /libft/srcs/ft_isascii.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isascii.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/10/31 17:23:01 by sshiling #+# #+# */ 9 | /* Updated: 2017/10/31 17:58:06 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isascii(int c) 16 | { 17 | if (c >= 0 && c <= 127) 18 | return (1); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /libft/srcs/ft_isdigit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isdigit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/10/30 17:59:42 by sshiling #+# #+# */ 9 | /* Updated: 2017/10/30 18:42:45 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isdigit(int c) 16 | { 17 | if (c >= 48 && c <= 57) 18 | return (1); 19 | return (0); 20 | } 21 | -------------------------------------------------------------------------------- /libft/srcs/ft_isprint.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isprint.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/10/31 18:01:40 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/01 11:29:01 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_isprint(int c) 16 | { 17 | unsigned char ch; 18 | 19 | ch = (unsigned char)c; 20 | if (c >= 32 && c <= 126) 21 | { 22 | if (ch >= 32 && ch <= 126) 23 | return (1); 24 | } 25 | return (0); 26 | } 27 | -------------------------------------------------------------------------------- /libft/srcs/ft_itoa.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_itoa.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/03 12:47:12 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/03 15:50:42 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static char *do_min_max(int n) 16 | { 17 | char *result; 18 | int i; 19 | 20 | i = 9; 21 | result = (char*)malloc(sizeof(char) * 12); 22 | if (result == NULL) 23 | return (NULL); 24 | result[11] = '\0'; 25 | result[10] = '8'; 26 | n = n / 10; 27 | n = -n; 28 | while (i > 0) 29 | { 30 | result[i] = n % 10 + '0'; 31 | n = n / 10; 32 | i--; 33 | } 34 | result[i] = '-'; 35 | return (result); 36 | } 37 | 38 | static char *do_negative(int n) 39 | { 40 | char *result; 41 | int nb; 42 | int i; 43 | 44 | n = -n; 45 | nb = n; 46 | i = 0; 47 | while (nb > 0) 48 | { 49 | nb = nb / 10; 50 | i++; 51 | } 52 | result = (char*)malloc(sizeof(char) * (i + 2)); 53 | if (result == NULL) 54 | return (NULL); 55 | result[i + 1] = '\0'; 56 | while (i > 0) 57 | { 58 | result[i] = n % 10 + '0'; 59 | n = n / 10; 60 | i--; 61 | } 62 | result[i] = '-'; 63 | return (result); 64 | } 65 | 66 | static char *do_positive(int n) 67 | { 68 | char *result; 69 | int nb; 70 | int i; 71 | 72 | nb = n; 73 | i = 0; 74 | while (nb > 0) 75 | { 76 | nb = nb / 10; 77 | i++; 78 | } 79 | result = (char*)malloc(sizeof(char) * (i + 1)); 80 | if (result == NULL) 81 | return (NULL); 82 | result[i] = '\0'; 83 | while (i > 0) 84 | { 85 | result[i - 1] = n % 10 + '0'; 86 | n = n / 10; 87 | i--; 88 | } 89 | return (result); 90 | } 91 | 92 | static char *do_null(void) 93 | { 94 | char *result; 95 | 96 | result = (char*)malloc(sizeof(char) * 2); 97 | if (result == NULL) 98 | return (NULL); 99 | result[0] = '0'; 100 | result[1] = '\0'; 101 | return (result); 102 | } 103 | 104 | char *ft_itoa(int n) 105 | { 106 | if (n == -2147483648) 107 | return (do_min_max(n)); 108 | if (n < 0) 109 | return (do_negative(n)); 110 | if (n > 0) 111 | return (do_positive(n)); 112 | return (do_null()); 113 | } 114 | -------------------------------------------------------------------------------- /libft/srcs/ft_itoa_base.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_itoa_base.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/12/25 19:14:10 by sshiling #+# #+# */ 9 | /* Updated: 2017/12/25 19:14:12 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_itoa_base(size_t nb, int base, t_res *tmp) 16 | { 17 | char *result; 18 | char mod; 19 | size_t n; 20 | int i; 21 | 22 | n = nb; 23 | i = nb == 0 ? 1 : 0; 24 | while (n > 0) 25 | { 26 | n = n / base; 27 | i++; 28 | } 29 | mod = tmp->c == 'X' ? 'A' : 'a'; 30 | result = ft_strnew(i); 31 | result[i] = '\0'; 32 | while (i > 0) 33 | { 34 | if (nb % base < 10) 35 | result[i - 1] = nb % base + '0'; 36 | else 37 | result[i - 1] = nb % base - 10 + mod; 38 | nb = nb / base; 39 | i--; 40 | } 41 | return (result); 42 | } 43 | -------------------------------------------------------------------------------- /libft/srcs/ft_itoa_max.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_itoa_max.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/12/25 15:58:18 by sshiling #+# #+# */ 9 | /* Updated: 2017/12/25 15:58:20 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_itoa_max(ssize_t nbr) 16 | { 17 | char *result; 18 | ssize_t tmp; 19 | int len; 20 | int k; 21 | 22 | if (nbr == 0) 23 | return (ft_strdup("0")); 24 | tmp = nbr; 25 | len = nbr < 0 ? 1 : 0; 26 | k = nbr < 0 ? -1 : 1; 27 | while (tmp != 0) 28 | { 29 | tmp /= 10; 30 | len++; 31 | } 32 | result = ft_strnew(len); 33 | if (k < 0) 34 | result[0] = '-'; 35 | while (len > 0 && result[len - 1] != '-') 36 | { 37 | result[len - 1] = nbr % 10 * k + '0'; 38 | nbr /= 10; 39 | len--; 40 | } 41 | return (result); 42 | } 43 | 44 | char *ft_itoa_u_max(size_t nbr) 45 | { 46 | char *result; 47 | size_t tmp; 48 | int len; 49 | 50 | if (nbr == 0) 51 | return (ft_strdup("0")); 52 | tmp = nbr; 53 | len = 0; 54 | while (tmp != 0) 55 | { 56 | tmp /= 10; 57 | len++; 58 | } 59 | result = ft_strnew(len); 60 | while (len > 0 && result[len - 1] != '-') 61 | { 62 | result[len - 1] = nbr % 10 + '0'; 63 | nbr /= 10; 64 | len--; 65 | } 66 | return (result); 67 | } 68 | -------------------------------------------------------------------------------- /libft/srcs/ft_lst_add_back.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lst_add_back.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/09 12:14:04 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/09 12:14:08 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lst_add_back(t_list **alst, t_list *new) 16 | { 17 | t_list *tmp; 18 | 19 | tmp = *alst; 20 | if (!new) 21 | return ; 22 | if (*alst != NULL) 23 | { 24 | while (tmp->next != NULL) 25 | tmp = tmp->next; 26 | tmp->next = new; 27 | } 28 | else 29 | *alst = new; 30 | } 31 | -------------------------------------------------------------------------------- /libft/srcs/ft_lst_last.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lst_last.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/09 12:15:23 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/09 12:15:26 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lst_last(t_list **alst) 16 | { 17 | t_list *tmp; 18 | 19 | if (!(*alst)) 20 | return (NULL); 21 | tmp = *alst; 22 | if (tmp != NULL) 23 | { 24 | while (tmp->next != NULL) 25 | tmp = tmp->next; 26 | return (tmp); 27 | } 28 | else 29 | return (NULL); 30 | } 31 | -------------------------------------------------------------------------------- /libft/srcs/ft_lst_push_data_back.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lst_push_data_back.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/09 12:14:43 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/09 12:14:46 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lst_push_data_back(t_list **alst, 16 | void const *content, size_t content_size) 17 | { 18 | t_list *new; 19 | 20 | new = ft_lstnew(content, content_size); 21 | if (!new) 22 | return ; 23 | ft_lst_add_back(alst, new); 24 | } 25 | -------------------------------------------------------------------------------- /libft/srcs/ft_lst_push_data_front.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lst_push_data_front.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/09 12:14:29 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/09 12:14:33 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lst_push_data_front(t_list **alst, 16 | void const *content, size_t content_size) 17 | { 18 | t_list *new; 19 | 20 | if (!(*alst)) 21 | return ; 22 | new = ft_lstnew(content, content_size); 23 | if (!new) 24 | return ; 25 | ft_lstadd(alst, new); 26 | } 27 | -------------------------------------------------------------------------------- /libft/srcs/ft_lst_size.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lst_size.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/09 12:15:01 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/09 12:15:04 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_lst_size(t_list **alst) 16 | { 17 | int n; 18 | t_list *list; 19 | 20 | if (!(*alst)) 21 | return (0); 22 | n = 0; 23 | list = *alst; 24 | while (list) 25 | { 26 | list = list->next; 27 | n++; 28 | } 29 | return (n); 30 | } 31 | -------------------------------------------------------------------------------- /libft/srcs/ft_lstadd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstadd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/04 13:39:15 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/04 13:39:53 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstadd(t_list **alst, t_list *new) 16 | { 17 | if (!new) 18 | return ; 19 | new->next = *alst; 20 | *alst = new; 21 | } 22 | -------------------------------------------------------------------------------- /libft/srcs/ft_lstdel.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstdel.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/04 13:17:49 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/04 14:27:56 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstdel(t_list **alst, void (*del)(void *, size_t)) 16 | { 17 | t_list *next; 18 | 19 | if (*alst != NULL) 20 | { 21 | while (*alst != NULL) 22 | { 23 | next = (*alst)->next; 24 | del((*alst)->content, (*alst)->content_size); 25 | free(*alst); 26 | *alst = next; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /libft/srcs/ft_lstdelone.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstdelone.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/04 13:05:01 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/04 14:31:59 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstdelone(t_list **alst, void (*del)(void*, size_t)) 16 | { 17 | if (*alst != NULL) 18 | { 19 | del((*alst)->content, (*alst)->content_size); 20 | free(*alst); 21 | *alst = NULL; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /libft/srcs/ft_lstiter.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstiter.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/04 13:44:29 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/04 13:45:35 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_lstiter(t_list *lst, void (*f)(t_list *elem)) 16 | { 17 | while (lst) 18 | { 19 | f(lst); 20 | lst = lst->next; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /libft/srcs/ft_lstmap.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstmap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/04 13:49:18 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/04 13:51:28 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)) 16 | { 17 | t_list *tmp_lst; 18 | t_list *result; 19 | t_list *tmp; 20 | 21 | if (lst != NULL) 22 | { 23 | tmp_lst = lst; 24 | result = f(ft_lstnew(lst->content, lst->content_size)); 25 | tmp_lst = tmp_lst->next; 26 | tmp = result; 27 | while (tmp_lst != NULL) 28 | { 29 | tmp->next = f(ft_lstnew(tmp_lst->content, tmp_lst->content_size)); 30 | tmp = tmp->next; 31 | tmp_lst = tmp_lst->next; 32 | } 33 | return (result); 34 | } 35 | return (NULL); 36 | } 37 | -------------------------------------------------------------------------------- /libft/srcs/ft_lstnew.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstnew.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/04 12:48:28 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/04 14:24:21 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_list *ft_lstnew(void const *content, size_t content_size) 16 | { 17 | t_list *new_list; 18 | 19 | new_list = (t_list*)malloc(sizeof(t_list)); 20 | if (new_list == NULL) 21 | return (NULL); 22 | if (content == 0) 23 | { 24 | new_list->content = NULL; 25 | new_list->content_size = 0; 26 | } 27 | else 28 | { 29 | new_list->content = malloc(content_size); 30 | ft_memcpy(new_list->content, content, content_size); 31 | new_list->content_size = content_size; 32 | } 33 | new_list->next = NULL; 34 | return (new_list); 35 | } 36 | -------------------------------------------------------------------------------- /libft/srcs/ft_memalloc.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memalloc.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/02 15:27:40 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/04 15:08:12 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memalloc(size_t size) 16 | { 17 | void *result; 18 | 19 | result = malloc(sizeof(size_t) * (size)); 20 | if (result == NULL) 21 | return (NULL); 22 | ft_memset(result, 0, (size)); 23 | return (result); 24 | } 25 | -------------------------------------------------------------------------------- /libft/srcs/ft_memccpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memccpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/01 14:47:15 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/02 14:29:44 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memccpy(void *dst, const void *src, int c, size_t n) 16 | { 17 | size_t i; 18 | unsigned char *s1; 19 | unsigned char *s2; 20 | unsigned char ch; 21 | 22 | i = 0; 23 | s1 = (unsigned char*)dst; 24 | s2 = (unsigned char*)src; 25 | ch = (unsigned char)c; 26 | while (i < n) 27 | { 28 | s1[i] = s2[i]; 29 | dst++; 30 | if (s2[i] == ch) 31 | return (dst); 32 | i++; 33 | } 34 | return (0); 35 | } 36 | -------------------------------------------------------------------------------- /libft/srcs/ft_memchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/01 16:49:42 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/01 17:37:51 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memchr(const void *s, int c, size_t n) 16 | { 17 | size_t i; 18 | unsigned char ch; 19 | unsigned char *result; 20 | 21 | i = 0; 22 | ch = (unsigned char)c; 23 | result = (unsigned char*)s; 24 | while (i < n) 25 | { 26 | if (result[i] == ch) 27 | return (&result[i]); 28 | i++; 29 | } 30 | result = NULL; 31 | return (result); 32 | } 33 | -------------------------------------------------------------------------------- /libft/srcs/ft_memcmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/01 17:43:10 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/04 15:48:10 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_memcmp(const void *s1, const void *s2, size_t n) 16 | { 17 | unsigned char *z1; 18 | unsigned char *z2; 19 | 20 | z1 = (unsigned char*)s1; 21 | z2 = (unsigned char*)s2; 22 | while (n > 0) 23 | { 24 | if (*z1 != *z2) 25 | return (*z1 - *z2); 26 | z1++; 27 | z2++; 28 | n--; 29 | } 30 | return (0); 31 | } 32 | -------------------------------------------------------------------------------- /libft/srcs/ft_memcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/01 13:55:52 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/01 14:41:29 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memcpy(void *dst, const void *src, size_t n) 16 | { 17 | size_t i; 18 | unsigned char *s1; 19 | unsigned char *s2; 20 | 21 | i = 0; 22 | s1 = (unsigned char*)dst; 23 | s2 = (unsigned char*)src; 24 | while (i < n) 25 | { 26 | s1[i] = s2[i]; 27 | i++; 28 | } 29 | return (dst); 30 | } 31 | -------------------------------------------------------------------------------- /libft/srcs/ft_memdel.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memdel.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/02 15:44:40 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/02 21:35:42 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_memdel(void **ap) 16 | { 17 | if (ap == NULL) 18 | return ; 19 | free(*ap); 20 | *ap = NULL; 21 | } 22 | -------------------------------------------------------------------------------- /libft/srcs/ft_memmove.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memmove.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/01 16:17:06 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/01 16:47:02 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memmove(void *dst, const void *src, size_t len) 16 | { 17 | unsigned char *s1; 18 | unsigned char *s2; 19 | 20 | s1 = (unsigned char*)dst; 21 | s2 = (unsigned char*)src; 22 | if ((s1 - s2) < 0) 23 | { 24 | while (len > 0) 25 | { 26 | *s1++ = *s2++; 27 | len--; 28 | } 29 | } 30 | else 31 | { 32 | while (len > 0) 33 | { 34 | s1[len - 1] = s2[len - 1]; 35 | len--; 36 | } 37 | } 38 | return (dst); 39 | } 40 | -------------------------------------------------------------------------------- /libft/srcs/ft_memset.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memset.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/01 11:55:05 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/01 12:55:57 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void *ft_memset(void *b, int c, size_t len) 16 | { 17 | unsigned char *p; 18 | 19 | p = b; 20 | while (len > 0) 21 | { 22 | *p = c; 23 | p++; 24 | len--; 25 | } 26 | return (b); 27 | } 28 | -------------------------------------------------------------------------------- /libft/srcs/ft_printf.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_printf.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/12/13 18:03:27 by sshiling #+# #+# */ 9 | /* Updated: 2017/12/20 14:45:23 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int g_fd = 1; 16 | 17 | t_res *new_res(void) 18 | { 19 | t_res *res; 20 | int i; 21 | 22 | i = 0; 23 | res = (t_res*)malloc(sizeof(t_res)); 24 | if (res == NULL) 25 | return (NULL); 26 | res->spec = (int*)malloc(sizeof(int) * 14); 27 | while (i < 14) 28 | { 29 | res->spec[i] = OFF; 30 | i++; 31 | } 32 | res->data = ft_strnew(sizeof(char)); 33 | res->c = '\0'; 34 | res->width = 0; 35 | res->precision = 0; 36 | res->i = 0; 37 | res->result = NULL; 38 | res->length = 0; 39 | res->next = NULL; 40 | return (res); 41 | } 42 | 43 | char *add_one_char(char *tmp, char c) 44 | { 45 | char *str; 46 | 47 | str = ft_strnew(ft_strlen(tmp) + 1); 48 | ft_strncpy(str, tmp, ft_strlen(tmp)); 49 | str[ft_strlen(tmp)] = c; 50 | free(tmp); 51 | return (str); 52 | } 53 | 54 | t_res *save_prcnt(t_res *tmp, int i) 55 | { 56 | char *del; 57 | 58 | del = tmp->data; 59 | tmp->data = ft_strnew(sizeof(char)); 60 | ft_memset(tmp->data, '%', 1); 61 | tmp->next = new_res(); 62 | i += 1; 63 | tmp = tmp->next; 64 | tmp->i = i; 65 | free(del); 66 | return (tmp); 67 | } 68 | 69 | t_res *tmp_result(const char *format, t_res *tmp, size_t i, va_list list) 70 | { 71 | if (format[i] == '%' && format[i + 1] == '%') 72 | tmp = save_prcnt(tmp, i + 1); 73 | else if (format[i] == '%' && format[i + 1] != '%') 74 | { 75 | tmp = pre_res(tmp, format, i + 1, list); 76 | i = tmp->i; 77 | tmp->next = new_res(); 78 | tmp = tmp->next; 79 | tmp->i = i; 80 | } 81 | else 82 | { 83 | while (format[i] != '%' && format[i] != '\0') 84 | { 85 | tmp->data = add_one_char(tmp->data, format[i]); 86 | i++; 87 | } 88 | tmp->i = i; 89 | tmp->next = new_res(); 90 | tmp = tmp->next; 91 | tmp->i = i; 92 | } 93 | return (tmp); 94 | } 95 | 96 | int ft_printf(const char *format, ...) 97 | { 98 | va_list list; 99 | t_res *res; 100 | t_res *tmp; 101 | size_t i; 102 | 103 | i = 0; 104 | res = new_res(); 105 | tmp = res; 106 | va_start(list, format); 107 | while (i < ft_strlen(format)) 108 | { 109 | tmp = tmp_result(format, tmp, i, list); 110 | i = tmp->i; 111 | } 112 | res = result_creator(list, res); 113 | va_end(list); 114 | i = result_printer(res); 115 | t_res_del(res); 116 | return (i); 117 | } 118 | -------------------------------------------------------------------------------- /libft/srcs/ft_putchar.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putchar.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/03 16:06:27 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/03 16:32:32 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putchar(char c) 16 | { 17 | write(1, &c, 1); 18 | } 19 | -------------------------------------------------------------------------------- /libft/srcs/ft_putchar_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putchar_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/03 16:51:45 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/03 16:51:50 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putchar_fd(char c, int fd) 16 | { 17 | write(fd, &c, 1); 18 | } 19 | -------------------------------------------------------------------------------- /libft/srcs/ft_putendl.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putendl.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/03 16:29:50 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/03 16:32:05 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putendl(char const *s) 16 | { 17 | if (s == NULL) 18 | return ; 19 | ft_putstr(s); 20 | ft_putchar('\n'); 21 | } 22 | -------------------------------------------------------------------------------- /libft/srcs/ft_putendl_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putendl_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/03 16:52:18 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/03 16:52:21 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putendl_fd(char const *s, int fd) 16 | { 17 | if (s == NULL) 18 | return ; 19 | ft_putstr_fd(s, fd); 20 | ft_putchar_fd('\n', fd); 21 | } 22 | -------------------------------------------------------------------------------- /libft/srcs/ft_putnbr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putnbr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/03 16:40:42 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/03 17:18:27 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putnbr(int n) 16 | { 17 | if (n == -2147483648) 18 | { 19 | ft_putnbr(n / 10); 20 | ft_putchar('8'); 21 | return ; 22 | } 23 | if (n < 0) 24 | { 25 | ft_putchar('-'); 26 | n = -n; 27 | } 28 | if (n > 9) 29 | { 30 | ft_putnbr(n / 10); 31 | ft_putnbr(n % 10); 32 | } 33 | else 34 | ft_putchar(n + '0'); 35 | } 36 | -------------------------------------------------------------------------------- /libft/srcs/ft_putnbr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putnbr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/03 16:52:48 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/03 17:32:09 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putnbr_fd(int n, int fd) 16 | { 17 | if (n == -2147483648) 18 | { 19 | ft_putnbr_fd((n / 10), fd); 20 | ft_putchar_fd('8', fd); 21 | return ; 22 | } 23 | if (n < 0) 24 | { 25 | ft_putchar_fd('-', fd); 26 | n = -n; 27 | } 28 | if (n > 9) 29 | { 30 | ft_putnbr_fd((n / 10), fd); 31 | ft_putnbr_fd((n % 10), fd); 32 | } 33 | else 34 | ft_putchar_fd((n + '0'), fd); 35 | } 36 | -------------------------------------------------------------------------------- /libft/srcs/ft_putstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/03 16:12:54 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/04 11:51:34 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putstr(char const *s) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | if (s == NULL) 21 | return ; 22 | while (s[i] != '\0') 23 | { 24 | ft_putchar(s[i]); 25 | i++; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /libft/srcs/ft_putstr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putstr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/03 16:52:05 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/03 16:52:08 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_putstr_fd(char const *s, int fd) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | if (s == NULL) 21 | return ; 22 | while (s[i] != '\0') 23 | { 24 | write(fd, &s[i], 1); 25 | i++; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /libft/srcs/ft_strcat.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strcat.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/01 17:59:55 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/01 18:30:37 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strcat(char *s1, const char *s2) 16 | { 17 | int i; 18 | int j; 19 | int k; 20 | unsigned char *z1; 21 | unsigned char *z2; 22 | 23 | i = 0; 24 | j = 0; 25 | k = 0; 26 | z1 = (unsigned char*)s1; 27 | z2 = (unsigned char*)s2; 28 | while (z1[i] != '\0') 29 | i++; 30 | while (z2[j] != '\0') 31 | j++; 32 | k = i + j; 33 | j = 0; 34 | while (i < k) 35 | { 36 | z1[i] = z2[j]; 37 | j++; 38 | i++; 39 | } 40 | z1[i] = '\0'; 41 | return (s1); 42 | } 43 | -------------------------------------------------------------------------------- /libft/srcs/ft_strchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/01 20:08:29 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/01 20:20:31 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strchr(const char *s, int c) 16 | { 17 | char *str; 18 | int i; 19 | 20 | str = (char*)s; 21 | i = 0; 22 | if ((char)c == '\0') 23 | { 24 | i = ft_strlen(str); 25 | return (&str[i]); 26 | } 27 | while (str[i] != '\0') 28 | { 29 | if (str[i] == (char)c) 30 | return (&str[i]); 31 | i++; 32 | } 33 | return (NULL); 34 | } 35 | -------------------------------------------------------------------------------- /libft/srcs/ft_strclr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strclr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/02 16:51:21 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/02 16:54:50 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_strclr(char *s) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | if (s == NULL) 21 | return ; 22 | while (s[i] != '\0') 23 | i++; 24 | ft_bzero(s, i); 25 | } 26 | -------------------------------------------------------------------------------- /libft/srcs/ft_strcmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strcmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/10/30 16:06:54 by sshiling #+# #+# */ 9 | /* Updated: 2017/10/30 16:15:42 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_strcmp(const char *s1, const char *s2) 16 | { 17 | unsigned char *z1; 18 | unsigned char *z2; 19 | 20 | z1 = (unsigned char*)s1; 21 | z2 = (unsigned char*)s2; 22 | if (*z1 == '\0') 23 | return (-(*z2)); 24 | if (*z2 == '\0') 25 | return (*z1); 26 | while (*z1 != '\0' || *z2 != '\0') 27 | { 28 | if (*z1 != *z2) 29 | return (*z1 - *z2); 30 | z1++; 31 | z2++; 32 | } 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /libft/srcs/ft_strcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/10/25 13:06:20 by sshiling #+# #+# */ 9 | /* Updated: 2017/10/25 16:04:20 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strcpy(char *dst, const char *src) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | while (src[i] != '\0') 21 | { 22 | dst[i] = src[i]; 23 | i++; 24 | } 25 | dst[i] = '\0'; 26 | return (dst); 27 | } 28 | -------------------------------------------------------------------------------- /libft/srcs/ft_strdel.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strdel.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/02 16:42:18 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/02 21:35:59 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_strdel(char **as) 16 | { 17 | if (as == NULL) 18 | return ; 19 | free(*as); 20 | *as = NULL; 21 | } 22 | -------------------------------------------------------------------------------- /libft/srcs/ft_strdup.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strdup.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/10/25 16:44:50 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/02 19:57:19 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strdup(const char *s1) 16 | { 17 | int i; 18 | int len; 19 | char *dst; 20 | 21 | i = 0; 22 | len = 0; 23 | while (s1[len] != '\0') 24 | len++; 25 | dst = (char*)malloc(sizeof(*s1) * (len + 1)); 26 | if (dst == NULL) 27 | return (NULL); 28 | while (i < len) 29 | { 30 | dst[i] = s1[i]; 31 | i++; 32 | } 33 | dst[i] = '\0'; 34 | return (dst); 35 | } 36 | -------------------------------------------------------------------------------- /libft/srcs/ft_strequ.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strequ.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/02 20:21:50 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/02 20:30:40 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_strequ(char const *s1, char const *s2) 16 | { 17 | if (s1 == NULL || s2 == NULL) 18 | return (0); 19 | if (ft_strlen(s1) != ft_strlen(s2)) 20 | return (0); 21 | else if (ft_strcmp(s1, s2) != 0) 22 | return (0); 23 | else 24 | return (1); 25 | } 26 | -------------------------------------------------------------------------------- /libft/srcs/ft_striter.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_striter.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/02 16:56:01 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/02 19:35:17 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_striter(char *s, void (*f)(char *)) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | if (!s || !f) 21 | return ; 22 | while (s[i] != '\0') 23 | { 24 | f(&s[i]); 25 | i++; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /libft/srcs/ft_striteri.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_striteri.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/02 19:44:03 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/02 19:49:13 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | void ft_striteri(char *s, void (*f)(unsigned int, char *)) 16 | { 17 | unsigned int i; 18 | 19 | if (!s || !f) 20 | return ; 21 | i = 0; 22 | while (s[i] != '\0') 23 | { 24 | f(i, &s[i]); 25 | i++; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /libft/srcs/ft_strjoin.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strjoin.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/02 21:00:49 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/02 21:17:05 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static char *mk_result1(char const *s1, char *result) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | while (s1[i] != '\0') 21 | { 22 | result[i] = s1[i]; 23 | i++; 24 | } 25 | return (result); 26 | } 27 | 28 | static char *mk_result2(char const *s2, char *result, int i) 29 | { 30 | int j; 31 | 32 | j = 0; 33 | while (s2[j] != '\0') 34 | { 35 | result[i] = s2[j]; 36 | i++; 37 | j++; 38 | } 39 | result[i] = '\0'; 40 | return (result); 41 | } 42 | 43 | char *ft_strjoin(char const *s1, char const *s2) 44 | { 45 | int i; 46 | int j; 47 | char *result; 48 | 49 | if (s1 == NULL || s2 == NULL) 50 | return (NULL); 51 | i = ft_strlen(s1); 52 | j = ft_strlen(s2); 53 | result = (char*)malloc(sizeof(char) * (i + j + 1)); 54 | if (result == NULL) 55 | return (NULL); 56 | result = mk_result1(s1, result); 57 | result = mk_result2(s2, result, i); 58 | return (result); 59 | } 60 | -------------------------------------------------------------------------------- /libft/srcs/ft_strlcat.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlcat.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/01 18:50:55 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/01 20:02:24 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_strlcat(char *dst, const char *src, size_t dstsize) 16 | { 17 | size_t dst_len; 18 | size_t src_len; 19 | 20 | dst_len = ft_strlen(dst); 21 | src_len = ft_strlen(src); 22 | if (dstsize <= dst_len) 23 | return (src_len + dstsize); 24 | else 25 | ft_strncat(dst, src, dstsize - dst_len - 1); 26 | return (src_len + dst_len); 27 | } 28 | -------------------------------------------------------------------------------- /libft/srcs/ft_strlen.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlen.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/10/25 12:57:56 by sshiling #+# #+# */ 9 | /* Updated: 2017/10/25 16:04:50 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t ft_strlen(const char *s) 16 | { 17 | size_t i; 18 | 19 | i = 0; 20 | while (s[i] != '\0') 21 | i++; 22 | return (i); 23 | } 24 | -------------------------------------------------------------------------------- /libft/srcs/ft_strmap.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strmap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/02 19:58:03 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/02 20:11:40 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strmap(char const *s, char (*f)(char)) 16 | { 17 | int i; 18 | char *dst; 19 | 20 | i = 0; 21 | if (s == NULL) 22 | return (NULL); 23 | while (s[i] != '\0') 24 | i++; 25 | dst = (char*)malloc(sizeof(char) * (i + 1)); 26 | if (dst == NULL) 27 | return (NULL); 28 | i = 0; 29 | while (s[i] != '\0') 30 | { 31 | dst[i] = f(s[i]); 32 | i++; 33 | } 34 | dst[i] = '\0'; 35 | return (dst); 36 | } 37 | -------------------------------------------------------------------------------- /libft/srcs/ft_strmapi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strmapi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/02 20:14:00 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/02 20:17:40 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) 16 | { 17 | int i; 18 | char *dst; 19 | 20 | i = 0; 21 | if (s == NULL) 22 | return (NULL); 23 | while (s[i] != '\0') 24 | i++; 25 | dst = (char*)malloc(sizeof(char) * (i + 1)); 26 | if (dst == NULL) 27 | return (NULL); 28 | i = 0; 29 | while (s[i] != '\0') 30 | { 31 | dst[i] = f(i, s[i]); 32 | i++; 33 | } 34 | dst[i] = '\0'; 35 | return (dst); 36 | } 37 | -------------------------------------------------------------------------------- /libft/srcs/ft_strncat.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strncat.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/01 18:32:48 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/01 18:44:15 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strncat(char *s1, const char *s2, size_t n) 16 | { 17 | size_t i; 18 | size_t j; 19 | char *z1; 20 | char *z2; 21 | 22 | i = 0; 23 | j = 0; 24 | z1 = (char*)s1; 25 | z2 = (char*)s2; 26 | while (z1[i] != '\0') 27 | i++; 28 | if (n > ft_strlen(z2)) 29 | n = ft_strlen(z2); 30 | while (n > 0) 31 | { 32 | z1[i] = z2[j]; 33 | i++; 34 | j++; 35 | n--; 36 | } 37 | z1[i] = '\0'; 38 | return (s1); 39 | } 40 | -------------------------------------------------------------------------------- /libft/srcs/ft_strncmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strncmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/10/30 16:18:18 by sshiling #+# #+# */ 9 | /* Updated: 2017/10/30 16:27:12 by sshiling ### ########.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 | unsigned char *z1; 19 | unsigned char *z2; 20 | 21 | z1 = (unsigned char*)s1; 22 | z2 = (unsigned char*)s2; 23 | i = 0; 24 | if (n == 0) 25 | return (0); 26 | if (*z1 == '\0') 27 | return (-(*z2)); 28 | if (*z2 == '\0') 29 | return (*z1); 30 | while (z1[i] != '\0' || z2[i] != '\0') 31 | { 32 | if (i == n - 1 || z1[i] != z2[i]) 33 | return (z1[i] - z2[i]); 34 | i++; 35 | } 36 | return (z1[i] - z2[i]); 37 | } 38 | -------------------------------------------------------------------------------- /libft/srcs/ft_strncpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strncpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/10/25 14:31:16 by sshiling #+# #+# */ 9 | /* Updated: 2017/10/25 16:08:12 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strncpy(char *dst, const char *src, size_t len) 16 | { 17 | size_t i; 18 | 19 | i = 0; 20 | while (i < len && src[i] != '\0') 21 | { 22 | dst[i] = src[i]; 23 | i++; 24 | } 25 | while (i != len) 26 | { 27 | dst[i] = '\0'; 28 | i++; 29 | } 30 | return (dst); 31 | } 32 | -------------------------------------------------------------------------------- /libft/srcs/ft_strnequ.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnequ.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/02 20:31:29 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/02 20:41:32 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_strnequ(char const *s1, char const *s2, size_t n) 16 | { 17 | if (s1 == NULL || s2 == NULL) 18 | return (0); 19 | if (ft_strncmp(s1, s2, n) != 0) 20 | return (0); 21 | return (1); 22 | } 23 | -------------------------------------------------------------------------------- /libft/srcs/ft_strnew.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnew.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/02 15:49:18 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/02 16:49:47 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strnew(size_t size) 16 | { 17 | char *string; 18 | 19 | string = (char*)malloc(sizeof(char) * (size + 1)); 20 | if (string == NULL) 21 | return (NULL); 22 | ft_bzero(string, (size + 1)); 23 | return (string); 24 | } 25 | -------------------------------------------------------------------------------- /libft/srcs/ft_strnstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/10/30 15:44:45 by sshiling #+# #+# */ 9 | /* Updated: 2017/10/30 16:04:43 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strnstr(const char *haystack, const char *needle, size_t len) 16 | { 17 | size_t i; 18 | size_t j; 19 | char *result; 20 | 21 | result = (char*)haystack; 22 | j = 0; 23 | if (ft_strlen(needle) == 0) 24 | return (result); 25 | while (result[j] != '\0' && len >= ft_strlen(needle)) 26 | { 27 | i = 0; 28 | while (result[j + i] == needle[i] && 29 | result[j + i] != '\0' && needle[i] != '\0') 30 | i++; 31 | if (needle[i] == '\0') 32 | return (&result[j]); 33 | j++; 34 | len--; 35 | } 36 | return (NULL); 37 | } 38 | -------------------------------------------------------------------------------- /libft/srcs/ft_strrchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strrchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/01 20:21:46 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/01 20:32:16 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strrchr(const char *s, int c) 16 | { 17 | char *str; 18 | char ch; 19 | int i; 20 | 21 | str = (char*)s; 22 | ch = (char)c; 23 | i = ft_strlen(str); 24 | while (i >= 0) 25 | { 26 | if (str[i] == c) 27 | return (&str[i]); 28 | i--; 29 | } 30 | str = 0; 31 | return (str); 32 | } 33 | -------------------------------------------------------------------------------- /libft/srcs/ft_strsplit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strsplit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/03 11:55:23 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/03 12:46:40 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static int ft_words_counter(char const *str, char c) 16 | { 17 | int i; 18 | int indicator; 19 | int words; 20 | 21 | i = 0; 22 | words = 0; 23 | while (str[i] != '\0') 24 | { 25 | indicator = 0; 26 | while (str[i] == c && str[i] != '\0') 27 | i++; 28 | while (str[i] != c && str[i] != '\0') 29 | { 30 | indicator = 1; 31 | i++; 32 | } 33 | while (str[i] == c && str[i] != '\0') 34 | i++; 35 | if (indicator == 1) 36 | words++; 37 | } 38 | return (words); 39 | } 40 | 41 | static int new_str_size(char const *str, char c) 42 | { 43 | int i; 44 | 45 | i = 0; 46 | while (str[i] == c && str[i] != '\0') 47 | str++; 48 | while (str[i] != c && str[i] != '\0') 49 | i++; 50 | return (i); 51 | } 52 | 53 | static char **mk_split(char **array, char const *s, char c, int words_counter) 54 | { 55 | char *new_str; 56 | int key; 57 | int i; 58 | 59 | i = 0; 60 | key = 0; 61 | while (key < words_counter) 62 | { 63 | new_str = (char*)malloc(sizeof(char) * (new_str_size(s, c) + 1)); 64 | if (new_str == NULL) 65 | return (NULL); 66 | while (s[0] == c && s[0] != '\0') 67 | s++; 68 | while (s[0] != c && s[0] != '\0') 69 | new_str[i++] = *s++; 70 | new_str[i] = '\0'; 71 | array[key++] = new_str; 72 | i = 0; 73 | } 74 | array[words_counter] = 0; 75 | return (array); 76 | } 77 | 78 | char **ft_strsplit(char const *s, char c) 79 | { 80 | int words_counter; 81 | char **array; 82 | 83 | if (s == NULL) 84 | return (NULL); 85 | words_counter = ft_words_counter(s, c); 86 | array = (char**)malloc(sizeof(char*) * (words_counter + 1)); 87 | if (array == NULL) 88 | return (NULL); 89 | array = mk_split(array, s, c, words_counter); 90 | return (array); 91 | } 92 | -------------------------------------------------------------------------------- /libft/srcs/ft_strstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/10/25 17:07:13 by sshiling #+# #+# */ 9 | /* Updated: 2017/10/30 14:56:52 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strstr(const char *haystack, const char *needle) 16 | { 17 | size_t i; 18 | 19 | if (ft_strlen(needle) == 0) 20 | return ((char*)haystack); 21 | while (*haystack != '\0') 22 | { 23 | i = 0; 24 | while (haystack[i] == needle[i] && 25 | haystack[i] != '\0' && needle[i] != '\0') 26 | i++; 27 | if (needle[i] == '\0') 28 | return ((char*)haystack); 29 | haystack++; 30 | } 31 | return (NULL); 32 | } 33 | -------------------------------------------------------------------------------- /libft/srcs/ft_strsub.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strsub.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/02 20:46:18 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/02 20:59:53 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *ft_strsub(char const *s, unsigned int start, size_t len) 16 | { 17 | char *substring; 18 | unsigned int i; 19 | 20 | i = 0; 21 | if (s == NULL) 22 | return (NULL); 23 | substring = (char*)malloc(sizeof(char) * (len + 1)); 24 | if (substring == NULL) 25 | return (NULL); 26 | while (i < (unsigned int)len) 27 | { 28 | substring[i] = s[start]; 29 | start++; 30 | i++; 31 | } 32 | substring[i] = '\0'; 33 | return (substring); 34 | } 35 | -------------------------------------------------------------------------------- /libft/srcs/ft_strtrim.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strtrim.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/03 11:00:25 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/03 11:54:21 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static char *mk_string(const char *s, char *s_copy, int j, int i) 16 | { 17 | int k; 18 | 19 | k = 0; 20 | while (j <= i) 21 | { 22 | s_copy[k] = s[j]; 23 | j++; 24 | k++; 25 | } 26 | s_copy[k] = '\0'; 27 | return (s_copy); 28 | } 29 | 30 | char *ft_strtrim(char const *s) 31 | { 32 | char *s_copy; 33 | int i; 34 | int j; 35 | 36 | if (s == NULL) 37 | return (NULL); 38 | i = ft_strlen(s) - 1; 39 | j = 0; 40 | while (s[i] == ' ' || s[i] == '\t' || s[i] == '\n') 41 | i--; 42 | while ((s[j] == ' ' || s[j] == '\t' || s[j] == '\n') && j < i) 43 | j++; 44 | s_copy = (char*)malloc(sizeof(char) * (i - j + 2)); 45 | if (s_copy == NULL) 46 | return (NULL); 47 | s_copy = mk_string(s, s_copy, j, i); 48 | return (s_copy); 49 | } 50 | -------------------------------------------------------------------------------- /libft/srcs/ft_tolower.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_tolower.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/01 11:47:54 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/01 11:52:26 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_tolower(int c) 16 | { 17 | unsigned char ch; 18 | 19 | ch = (unsigned char)c; 20 | if (c >= 65 && c <= 90) 21 | { 22 | ch = ch + 32; 23 | c = (int)ch; 24 | } 25 | return (c); 26 | } 27 | -------------------------------------------------------------------------------- /libft/srcs/ft_toupper.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_toupper.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/01 11:33:29 by sshiling #+# #+# */ 9 | /* Updated: 2017/11/01 11:46:20 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_toupper(int c) 16 | { 17 | unsigned char ch; 18 | 19 | ch = (unsigned char)c; 20 | if (c >= 97 && c <= 122) 21 | { 22 | ch = ch - 32; 23 | c = (int)ch; 24 | } 25 | return (c); 26 | } 27 | -------------------------------------------------------------------------------- /libft/srcs/get_next_line.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/12/12 11:17:42 by sshiling #+# #+# */ 9 | /* Updated: 2017/12/12 11:17:45 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_line *new_tail(int fd) 16 | { 17 | t_line *new; 18 | 19 | new = (t_line*)malloc(sizeof(t_line)); 20 | new->data = ft_strnew(0); 21 | new->fd = fd; 22 | new->next = NULL; 23 | return (new); 24 | } 25 | 26 | t_line *fd_finder(t_line *tail, int fd) 27 | { 28 | t_line *tmp; 29 | 30 | tmp = tail; 31 | while (tmp) 32 | { 33 | if (tmp->fd == fd) 34 | return (tmp); 35 | tmp = tmp->next; 36 | } 37 | return (NULL); 38 | } 39 | 40 | int str_finder(char **s, char **line, int key) 41 | { 42 | int i; 43 | char *str; 44 | 45 | i = -1; 46 | str = *s; 47 | while (str[++i]) 48 | { 49 | if (str[i] == '\n') 50 | { 51 | *line = ft_strsub(str, 0, i); 52 | *s = ft_memmove(*s, &str[i + 1], ft_strlen(&str[i + 1]) + 1); 53 | return (-1); 54 | } 55 | } 56 | if (str[i] == '\0' && str[i - 1] != '\n' && key == 0) 57 | { 58 | *line = ft_strsub(str, 0, i); 59 | *s = ft_memmove(*s, &str[i], ft_strlen(&str[i]) + 1); 60 | return (-1); 61 | } 62 | return (0); 63 | } 64 | 65 | t_line *tmp_fd_in(t_line *tmp, t_line *tail, int fd) 66 | { 67 | tmp = tail; 68 | while (tmp->next != NULL) 69 | tmp = tmp->next; 70 | tmp->next = new_tail(fd); 71 | tmp = tmp->next; 72 | return (tmp); 73 | } 74 | 75 | int get_next_line(const int fd, char **line) 76 | { 77 | static t_line *tail; 78 | t_line *tmp; 79 | char *del; 80 | char buf[BUFF_SIZE + 1]; 81 | int key; 82 | 83 | if (tail == NULL) 84 | tail = new_tail(fd); 85 | if ((tmp = fd_finder(tail, fd)) == NULL) 86 | tmp = tmp_fd_in(tmp, tail, fd); 87 | ft_bzero(buf, BUFF_SIZE + 1); 88 | while ((key = read(fd, buf, BUFF_SIZE)) > 0) 89 | { 90 | del = tmp->data; 91 | tmp->data = ft_strjoin(tmp->data, buf); 92 | ft_strdel(&del); 93 | ft_bzero(buf, BUFF_SIZE + 1); 94 | if (str_finder(&(tmp->data), line, key) == -1) 95 | return (1); 96 | } 97 | if (key == -1 || (key == 0 && tmp->data[0] == '\0')) 98 | return (key = key == -1 ? -1 : 0); 99 | if (str_finder(&(tmp->data), line, key) == -1) 100 | return (1); 101 | return (0); 102 | } 103 | -------------------------------------------------------------------------------- /libft/srcs/mod_int_uox.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* mod_int_uox.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/01/22 18:44:26 by sshiling #+# #+# */ 9 | /* Updated: 2018/01/22 18:44:28 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t mod_int_uox(va_list list, t_res *tmp) 16 | { 17 | if (MOD_Z) 18 | return ((size_t)(va_arg(list, size_t))); 19 | else if (MOD_J) 20 | return ((intmax_t)(va_arg(list, size_t))); 21 | else if (MOD_LL) 22 | return ((long long unsigned int)(va_arg(list, size_t))); 23 | else if (MOD_L) 24 | return ((long unsigned int)(va_arg(list, size_t))); 25 | else if (MOD_H) 26 | return ((short unsigned int)(va_arg(list, size_t))); 27 | else if (MOD_HH) 28 | return ((unsigned char)(va_arg(list, size_t))); 29 | else 30 | return ((unsigned int)(va_arg(list, size_t))); 31 | } 32 | -------------------------------------------------------------------------------- /libft/srcs/modifiers.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* modifiers.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/01/22 18:10:58 by sshiling #+# #+# */ 9 | /* Updated: 2018/01/22 18:11:00 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t find_h(t_res *tmp, const char *format, size_t i) 16 | { 17 | if (format[i] == 'h' && format[i + 1] != 'h') 18 | { 19 | tmp->spec[7] = ON; 20 | i++; 21 | } 22 | else if (format[i] == 'h' && format[i + 1] == 'h') 23 | { 24 | tmp->spec[6] = ON; 25 | i = i + 2; 26 | } 27 | return (i); 28 | } 29 | 30 | size_t find_l(t_res *tmp, const char *format, size_t i) 31 | { 32 | if (format[i] == 'l' && format[i + 1] != 'l') 33 | { 34 | tmp->spec[9] = ON; 35 | i++; 36 | } 37 | else if (format[i] == 'l' && format[i + 1] == 'l') 38 | { 39 | tmp->spec[8] = ON; 40 | i = i + 2; 41 | } 42 | return (i); 43 | } 44 | 45 | size_t find_j(t_res *tmp, const char *format, size_t i) 46 | { 47 | if (format[i] == 'j') 48 | { 49 | tmp->spec[10] = ON; 50 | i++; 51 | } 52 | return (i); 53 | } 54 | 55 | size_t find_z(t_res *tmp, const char *format, size_t i) 56 | { 57 | if (format[i] == 'z') 58 | { 59 | tmp->spec[11] = ON; 60 | i++; 61 | } 62 | return (i); 63 | } 64 | 65 | size_t find_t(t_res *tmp, const char *format, size_t i) 66 | { 67 | if (format[i] == 't' && format[i + 1] != 't') 68 | { 69 | tmp->spec[12] = ON; 70 | i++; 71 | } 72 | else if (format[i] == 't' && format[i + 1] == 't') 73 | { 74 | tmp->spec[13] = ON; 75 | i = i + 2; 76 | } 77 | return (i); 78 | } 79 | -------------------------------------------------------------------------------- /libft/srcs/pre_res.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* pre_res.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/12/21 13:23:18 by sshiling #+# #+# */ 9 | /* Updated: 2017/12/21 13:23:20 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | t_res *find_specif(t_res *tmp, const char *format, size_t i) 16 | { 17 | if (format[i]) 18 | tmp->c = format[i]; 19 | tmp->i = format[i] == '\0' ? i : i + 1; 20 | return (tmp); 21 | } 22 | 23 | t_res *pre_res(t_res *tmp, const char *format, size_t i, va_list list) 24 | { 25 | while (ALL(format[i])) 26 | { 27 | i = find_dot(tmp, format, i); 28 | i = find_minus(tmp, format, i); 29 | i = find_plus(tmp, format, i); 30 | i = find_sharp(tmp, format, i); 31 | i = find_space(tmp, format, i); 32 | i = find_h(tmp, format, i); 33 | i = find_l(tmp, format, i); 34 | i = find_j(tmp, format, i); 35 | i = find_z(tmp, format, i); 36 | i = find_zero(tmp, format, i); 37 | i = find_precision(tmp, format, i, list); 38 | i = find_width(tmp, format, i, list); 39 | i = find_t(tmp, format, i); 40 | } 41 | return (find_specif(tmp, format, i)); 42 | } 43 | -------------------------------------------------------------------------------- /libft/srcs/print_in_color.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* print_in_color.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/01/24 15:38:49 by sshiling #+# #+# */ 9 | /* Updated: 2018/01/24 15:38:51 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | char *color_setter(char *s) 16 | { 17 | if ((ft_strstr(s, "{eoc}")) != NULL) 18 | return ("\e[m"); 19 | if ((ft_strstr(s, "{red}")) != NULL) 20 | return ("\e[31m"); 21 | if ((ft_strstr(s, "{green}")) != NULL) 22 | return ("\e[32m"); 23 | if ((ft_strstr(s, "{yellow}")) != NULL) 24 | return ("\e[33m"); 25 | if ((ft_strstr(s, "{blue}")) != NULL) 26 | return ("\e[34m"); 27 | if ((ft_strstr(s, "{magenta}")) != NULL) 28 | return ("\e[35m"); 29 | if ((ft_strstr(s, "{cyan}")) != NULL) 30 | return ("\e[36m"); 31 | if ((ft_strstr(s, "{white}")) != NULL) 32 | return ("\e[37m"); 33 | return (NULL); 34 | } 35 | 36 | char *color_finder(char *s) 37 | { 38 | char *finder; 39 | 40 | if ((finder = ft_strstr(s, "{eoc}")) != NULL) 41 | return (finder); 42 | if ((finder = ft_strstr(s, "{red}")) != NULL) 43 | return (finder); 44 | if ((finder = ft_strstr(s, "{green}")) != NULL) 45 | return (finder); 46 | if ((finder = ft_strstr(s, "{yellow}")) != NULL) 47 | return (finder); 48 | if ((finder = ft_strstr(s, "{blue}")) != NULL) 49 | return (finder); 50 | if ((finder = ft_strstr(s, "{magenta}")) != NULL) 51 | return (finder); 52 | if ((finder = ft_strstr(s, "{cyan}")) != NULL) 53 | return (finder); 54 | if ((finder = ft_strstr(s, "{white}")) != NULL) 55 | return (finder); 56 | return (NULL); 57 | } 58 | 59 | char *create_final_color(char *finder, char *s) 60 | { 61 | char *res; 62 | char *f2; 63 | 64 | f2 = ft_strchr(finder, '}'); 65 | res = ft_strnew(finder - s + ft_strlen(f2) + ft_strlen(color_setter(s))); 66 | ft_strncpy(res, s, finder - s); 67 | ft_strncpy(&res[finder - s], color_setter(s), ft_strlen(color_setter(s))); 68 | ft_strncpy(&res[ft_strlen(res)], &f2[1], (ft_strlen(f2) - 1)); 69 | s = ft_strdup(res); 70 | free(res); 71 | return (s); 72 | } 73 | 74 | int ft_putstr_data(char *s) 75 | { 76 | char *finder; 77 | int i; 78 | int j; 79 | 80 | if (g_fd <= 0) 81 | return (0); 82 | i = 0; 83 | j = 1; 84 | while (j == 1) 85 | { 86 | if ((finder = color_finder(s)) != NULL) 87 | s = create_final_color(finder, s); 88 | else 89 | { 90 | write(g_fd, s, ft_strlen(s)); 91 | j = 0; 92 | } 93 | } 94 | return (ft_strlen(s)); 95 | } 96 | -------------------------------------------------------------------------------- /libft/srcs/result_creator.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* result_creator.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/12/22 14:35:25 by sshiling #+# #+# */ 9 | /* Updated: 2017/12/22 14:35:28 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | static void func_pdou(va_list list, t_res *tmp) 16 | { 17 | if (tmp->c == 'p') 18 | { 19 | SHARP = ON; 20 | MOD_Z = ON; 21 | tmp->result = create_x(list, tmp); 22 | } 23 | if (tmp->c == 'D') 24 | { 25 | MOD_L = ON; 26 | tmp->result = create_i_d(list, tmp); 27 | } 28 | if (tmp->c == 'O') 29 | { 30 | MOD_L = ON; 31 | tmp->result = create_o(list, tmp); 32 | } 33 | if (tmp->c == 'U') 34 | { 35 | MOD_L = ON; 36 | tmp->result = create_u(list, tmp); 37 | } 38 | } 39 | 40 | static void func_ints(va_list list, t_res *tmp) 41 | { 42 | if (tmp->c == 'd' || tmp->c == 'i') 43 | tmp->result = create_i_d(list, tmp); 44 | if (tmp->c == 'o') 45 | tmp->result = create_o(list, tmp); 46 | if (tmp->c == 'u') 47 | tmp->result = create_u(list, tmp); 48 | if (tmp->c == 'x' || tmp->c == 'X') 49 | tmp->result = create_x(list, tmp); 50 | } 51 | 52 | static void func_finder(va_list list, t_res *tmp) 53 | { 54 | if (tmp->c == 'c' && !(MOD_L)) 55 | tmp->result = create_c(list, tmp); 56 | if (tmp->c == 'C' || (tmp->c == 'c' && MOD_L)) 57 | tmp->result = create_b_c(list, tmp); 58 | if (tmp->c == 's' && !(MOD_L)) 59 | tmp->result = create_s(list, tmp); 60 | if (tmp->c == 'S' || (tmp->c == 's' && MOD_L)) 61 | tmp->result = create_b_s(list, tmp); 62 | if (tmp->c == 'p' || tmp->c == 'D' || tmp->c == 'O' || tmp->c == 'U') 63 | func_pdou(list, tmp); 64 | if (tmp->c == 'd' || tmp->c == 'i' || tmp->c == 'o' || tmp->c == 'u' || 65 | tmp->c == 'x' || tmp->c == 'X') 66 | func_ints(list, tmp); 67 | if (tmp->c == 'n') 68 | tmp->result = create_n(list, tmp); 69 | if (tmp->c == 'b') 70 | tmp->result = create_binary(list, tmp); 71 | if (tmp->c == 'r') 72 | tmp->result = create_nprntbl(list, tmp); 73 | if (tmp->c == 'w') 74 | g_fd = va_arg(list, int); 75 | if (tmp->c == 'm') 76 | g_fd = 2; 77 | } 78 | 79 | t_res *result_creator(va_list list, t_res *res) 80 | { 81 | t_res *tmp; 82 | 83 | tmp = res; 84 | while (tmp) 85 | { 86 | if (SPECIFIER(tmp->c)) 87 | func_finder(list, tmp); 88 | else 89 | tmp->result = create_c(list, tmp); 90 | tmp = tmp->next; 91 | } 92 | return (res); 93 | } 94 | -------------------------------------------------------------------------------- /libft/srcs/result_printer.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* result_printer.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/01/22 19:36:49 by sshiling #+# #+# */ 9 | /* Updated: 2018/01/22 19:36:51 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | int ft_putstr_new(char const *s) 16 | { 17 | int i; 18 | 19 | if (g_fd <= 0) 20 | return (0); 21 | i = 0; 22 | if (s == NULL) 23 | return (i); 24 | i = ft_strlen(s); 25 | ft_putstr_fd(s, g_fd); 26 | return (i); 27 | } 28 | 29 | int ft_putstr_res(char *s, t_res *tmp) 30 | { 31 | if (g_fd <= 0) 32 | return (0); 33 | write(g_fd, s, tmp->length); 34 | return (tmp->length); 35 | } 36 | 37 | int result_printer(t_res *res) 38 | { 39 | t_res *tmp; 40 | int i; 41 | 42 | i = 0; 43 | tmp = res; 44 | while (tmp->next) 45 | { 46 | if (tmp->data) 47 | i = i + ft_putstr_data(tmp->data); 48 | if (tmp->result && (tmp->c == 'c' || tmp->c == 'C' || tmp->c == 'r')) 49 | i = i + ft_putstr_res(tmp->result, tmp); 50 | else if (tmp->result && tmp->c != 'n') 51 | i = i + ft_putstr_new(tmp->result); 52 | if (tmp->c == 'y') 53 | ft_putnbr(i); 54 | else if (tmp->c == 'n') 55 | { 56 | if (tmp->result) 57 | *((size_t*)(tmp->result)) = i; 58 | tmp->result = ft_strnew(0); 59 | } 60 | tmp = tmp->next; 61 | } 62 | return (i); 63 | } 64 | 65 | void t_res_del(t_res *src) 66 | { 67 | t_res *tmp; 68 | t_res *del; 69 | 70 | tmp = src; 71 | while (tmp) 72 | { 73 | del = tmp; 74 | tmp = tmp->next; 75 | free(del->spec); 76 | free(del->data); 77 | free(del->result); 78 | free(del); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /libft/srcs/width_precision.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* width_precision.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/01/22 17:57:17 by sshiling #+# #+# */ 9 | /* Updated: 2018/01/22 17:57:19 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "libft.h" 14 | 15 | size_t find_dot(t_res *tmp, const char *format, size_t i) 16 | { 17 | if (format[i] == '.') 18 | { 19 | tmp->spec[1] = ON; 20 | i++; 21 | } 22 | return (i); 23 | } 24 | 25 | size_t wildcard_precision(t_res *tmp, va_list list, size_t i) 26 | { 27 | tmp->precision = va_arg(list, size_t); 28 | if (tmp->precision < 0) 29 | { 30 | tmp->precision = 0; 31 | DOT = OFF; 32 | } 33 | i++; 34 | return (i); 35 | } 36 | 37 | size_t find_precision(t_res *tmp, const char *format, size_t i, va_list list) 38 | { 39 | char *str; 40 | 41 | if (NUM(format[i]) && format[i - 1] == '.') 42 | { 43 | str = ft_strnew(2); 44 | while (NUM(format[i])) 45 | { 46 | str = add_one_char(str, format[i]); 47 | i++; 48 | } 49 | tmp->precision = ft_atoi(str); 50 | free(str); 51 | } 52 | if (format[i] == '*' && format[i - 1] == '.') 53 | i = wildcard_precision(tmp, list, i); 54 | else if (!(NUM(format[i])) && format[i - 1] == '.') 55 | tmp->precision = 0; 56 | return (i); 57 | } 58 | 59 | size_t wildcard_width(t_res *tmp, va_list list, size_t i) 60 | { 61 | tmp->width = va_arg(list, size_t); 62 | if (tmp->width < 0) 63 | { 64 | MINUS = ON; 65 | tmp->width = -(tmp->width); 66 | } 67 | i++; 68 | return (i); 69 | } 70 | 71 | size_t find_width(t_res *tmp, const char *format, size_t i, va_list list) 72 | { 73 | char *str; 74 | 75 | if (NUM(format[i]) && format[i] != '0' && format[i - 1] != '.') 76 | { 77 | str = ft_strnew(sizeof(char)); 78 | while (NUM(format[i])) 79 | { 80 | str = add_one_char(str, format[i]); 81 | i++; 82 | } 83 | tmp->width = ft_atoi(str); 84 | free(str); 85 | } 86 | if (format[i] == '*' && format[i - 1] != '.') 87 | i = wildcard_width(tmp, list, i); 88 | return (i); 89 | } 90 | -------------------------------------------------------------------------------- /srcs/checker.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* checker.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/01/26 18:17:37 by sshiling #+# #+# */ 9 | /* Updated: 2018/02/20 15:33:03 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | int do_commands(char *line, t_stack **first, t_stack **second) 16 | { 17 | if (!(ft_strcmp(line, "sa"))) 18 | return (sab(first)); 19 | if (!(ft_strcmp(line, "sb"))) 20 | return (sab(second)); 21 | if (!(ft_strcmp(line, "ss"))) 22 | return (ss(first, second)); 23 | if (!(ft_strcmp(line, "pa"))) 24 | return (pab(first, second)); 25 | if (!(ft_strcmp(line, "pb"))) 26 | return (pab(second, first)); 27 | if (!(ft_strcmp(line, "ra"))) 28 | return (rab(first)); 29 | if (!(ft_strcmp(line, "rb"))) 30 | return (rab(second)); 31 | if (!(ft_strcmp(line, "rr"))) 32 | return (rr(first, second)); 33 | if (!(ft_strcmp(line, "rra"))) 34 | return (rrab(first)); 35 | if (!(ft_strcmp(line, "rrb"))) 36 | return (rrab(second)); 37 | if (!(ft_strcmp(line, "rrr"))) 38 | return (rrr(first, second)); 39 | return (1); 40 | } 41 | 42 | int return_errors(char **line, t_stack **stack1, t_stack **stack2) 43 | { 44 | ft_strdel(line); 45 | if (*stack1) 46 | stack_del(stack1); 47 | if (*stack2) 48 | stack_del(stack2); 49 | write(2, "Error\n", 6); 50 | return (0); 51 | } 52 | 53 | int stack_is_sorted(t_stack *first) 54 | { 55 | t_stack *stack; 56 | 57 | stack = first; 58 | while (stack->next) 59 | { 60 | if (stack->num >= stack->next->num) 61 | return (1); 62 | stack = stack->next; 63 | } 64 | return (0); 65 | } 66 | 67 | void print_checker_res(t_stack **first, t_stack **second) 68 | { 69 | if (!(stack_is_sorted(*first)) && !(*second)) 70 | ft_printf("OK\n"); 71 | else 72 | ft_printf("KO\n"); 73 | if (*first) 74 | stack_del(first); 75 | if (*second) 76 | stack_del(second); 77 | } 78 | 79 | int main(int argc, char **argv) 80 | { 81 | t_stack *first; 82 | t_stack *second; 83 | char *line; 84 | 85 | if (argc < 2) 86 | return (0); 87 | if (!(first = copy_args_in_stack(argc, argv))) 88 | { 89 | write(2, "Error\n", 6); 90 | return (0); 91 | } 92 | second = NULL; 93 | while (get_next_line(0, &line)) 94 | { 95 | if (do_commands(line, &first, &second)) 96 | return (return_errors(&line, &first, &second)); 97 | ft_strdel(&line); 98 | } 99 | print_checker_res(&first, &second); 100 | return (0); 101 | } 102 | -------------------------------------------------------------------------------- /srcs/comb.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* comb.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/02/15 18:55:09 by sshiling #+# #+# */ 9 | /* Updated: 2018/02/15 18:55:14 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | void comb_1(t_stack **stack, t_com **result) 16 | { 17 | sab(stack); 18 | if ((*stack)->diff == 1) 19 | (*result)->final = add_to_string((*result)->final, "sa\n"); 20 | else 21 | (*result)->final = add_to_string((*result)->final, "sb\n"); 22 | } 23 | 24 | void comb_2(t_stack **stack, t_com **result) 25 | { 26 | rab(stack); 27 | sab(stack); 28 | rrab(stack); 29 | if ((*stack)->diff == 1) 30 | (*result)->final = add_to_string((*result)->final, "ra\nsa\nrra\n"); 31 | else 32 | (*result)->final = add_to_string((*result)->final, "rb\nsb\nrrb\n"); 33 | } 34 | 35 | void comb_3(t_stack **stack, t_com **result) 36 | { 37 | sab(stack); 38 | rab(stack); 39 | sab(stack); 40 | rrab(stack); 41 | if ((*stack)->diff == 1) 42 | (*result)->final = add_to_string((*result)->final, "sa\nra\nsa\nrra\n"); 43 | else 44 | (*result)->final = add_to_string((*result)->final, "sb\nrb\nsb\nrrb\n"); 45 | } 46 | 47 | void comb_4(t_stack **stack, t_com **result) 48 | { 49 | rab(stack); 50 | sab(stack); 51 | rrab(stack); 52 | sab(stack); 53 | if ((*stack)->diff == 1) 54 | (*result)->final = add_to_string((*result)->final, "ra\nsa\nrra\nsa\n"); 55 | else 56 | (*result)->final = add_to_string((*result)->final, "rb\nsb\nrrb\nsb\n"); 57 | } 58 | 59 | void comb_5(t_stack **stack, t_com **result) 60 | { 61 | sab(stack); 62 | rab(stack); 63 | sab(stack); 64 | rrab(stack); 65 | sab(stack); 66 | if ((*stack)->diff == 1) 67 | (*result)->final = add_to_string((*result)->final, 68 | "sa\nra\nsa\nrra\nsa\n"); 69 | else 70 | (*result)->final = add_to_string((*result)->final, 71 | "sb\nrb\nsb\nrrb\nsb\n"); 72 | } 73 | -------------------------------------------------------------------------------- /srcs/comb_2.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* comb_2.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/02/19 21:21:31 by sshiling #+# #+# */ 9 | /* Updated: 2018/02/19 21:21:34 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | void comb_6(t_stack **stack, t_com **result) 16 | { 17 | rrab(stack); 18 | sab(stack); 19 | if ((*stack)->diff == 1) 20 | (*result)->final = add_to_string((*result)->final, "rra\nsa\n"); 21 | else 22 | (*result)->final = add_to_string((*result)->final, "rrb\nsb\n"); 23 | } 24 | 25 | void comb_7(t_stack **stack, t_com **result) 26 | { 27 | rab(stack); 28 | if ((*stack)->diff == 1) 29 | (*result)->final = add_to_string((*result)->final, "ra\n"); 30 | else 31 | (*result)->final = add_to_string((*result)->final, "rb\n"); 32 | } 33 | 34 | void comb_8(t_stack **stack, t_com **result) 35 | { 36 | rrab(stack); 37 | if ((*stack)->diff == 1) 38 | (*result)->final = add_to_string((*result)->final, "rra\n"); 39 | else 40 | (*result)->final = add_to_string((*result)->final, "rrb\n"); 41 | } 42 | 43 | void comb_9(t_stack **stack, t_com **result) 44 | { 45 | rab(stack); 46 | sab(stack); 47 | if ((*stack)->diff == 1) 48 | (*result)->final = add_to_string((*result)->final, "ra\nsa\n"); 49 | else 50 | (*result)->final = add_to_string((*result)->final, "rb\nsb\n"); 51 | } 52 | -------------------------------------------------------------------------------- /srcs/commands.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* commands.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/01/31 20:35:47 by sshiling #+# #+# */ 9 | /* Updated: 2018/01/31 20:35:48 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | int sab(t_stack **head) 16 | { 17 | t_stack *stack; 18 | 19 | stack = *head; 20 | if (stack && stack->next) 21 | ft_swap(&stack->num, &stack->next->num); 22 | return (0); 23 | } 24 | 25 | int pab(t_stack **head_to, t_stack **head_from) 26 | { 27 | t_stack *tmp; 28 | t_stack *to; 29 | t_stack *from; 30 | 31 | to = *head_to; 32 | from = *head_from; 33 | if (!from) 34 | return (0); 35 | tmp = from; 36 | from = from->next; 37 | *head_from = from; 38 | if (!to) 39 | { 40 | to = tmp; 41 | to->next = NULL; 42 | *head_to = to; 43 | } 44 | else 45 | { 46 | tmp->next = to; 47 | *head_to = tmp; 48 | } 49 | return (0); 50 | } 51 | 52 | int rab(t_stack **head) 53 | { 54 | t_stack *tmp_first; 55 | t_stack *tmp_last; 56 | t_stack *stack; 57 | 58 | stack = *head; 59 | if (!(stack && stack->next)) 60 | return (0); 61 | tmp_first = stack; 62 | stack = stack->next; 63 | tmp_last = stack; 64 | while (tmp_last->next) 65 | { 66 | tmp_last = tmp_last->next; 67 | } 68 | tmp_last->next = tmp_first; 69 | tmp_first->next = NULL; 70 | *head = stack; 71 | return (0); 72 | } 73 | 74 | int rrab(t_stack **head) 75 | { 76 | t_stack *tmp_last; 77 | t_stack *previous; 78 | t_stack *stack; 79 | 80 | stack = *head; 81 | if (!(stack && stack->next)) 82 | return (0); 83 | tmp_last = stack; 84 | while (tmp_last->next) 85 | { 86 | previous = tmp_last; 87 | tmp_last = tmp_last->next; 88 | } 89 | tmp_last->next = stack; 90 | previous->next = NULL; 91 | *head = tmp_last; 92 | return (0); 93 | } 94 | -------------------------------------------------------------------------------- /srcs/commands_2.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* commands_2.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/02/19 21:31:09 by sshiling #+# #+# */ 9 | /* Updated: 2018/02/19 21:31:11 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | int ss(t_stack **stack1, t_stack **stack2) 16 | { 17 | sab(stack1); 18 | sab(stack2); 19 | return (0); 20 | } 21 | 22 | int rr(t_stack **stack1, t_stack **stack2) 23 | { 24 | rab(stack1); 25 | rab(stack2); 26 | return (0); 27 | } 28 | 29 | int rrr(t_stack **stack1, t_stack **stack2) 30 | { 31 | rrab(stack1); 32 | rrab(stack2); 33 | return (0); 34 | } 35 | -------------------------------------------------------------------------------- /srcs/divide_stacks.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* divide_stacks.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/02/15 18:48:17 by sshiling #+# #+# */ 9 | /* Updated: 2018/02/15 18:48:19 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | static int stack_rotator(t_stack **stack, t_com **result, int rot) 16 | { 17 | rab(stack); 18 | if ((*stack)->diff == 1) 19 | (*result)->final = add_to_string((*result)->final, "ra\n"); 20 | else 21 | (*result)->final = add_to_string((*result)->final, "rb\n"); 22 | rot++; 23 | return (rot); 24 | } 25 | 26 | static int stack_pusher(t_stack **stack1, t_stack **stack2, 27 | t_com **result, int half) 28 | { 29 | (*stack1)->diff = (*stack1)->diff == 1 ? 2 : 1; 30 | pab(stack2, stack1); 31 | if ((*stack1)->diff == 1) 32 | (*result)->final = add_to_string((*result)->final, "pb\n"); 33 | else 34 | (*result)->final = add_to_string((*result)->final, "pa\n"); 35 | half--; 36 | return (half); 37 | } 38 | 39 | int div_stack_a(t_stack **stack1, t_stack **stack2, 40 | t_com **result, int len) 41 | { 42 | int rot; 43 | int mid; 44 | t_stack *tmp; 45 | int half; 46 | 47 | half = len / 2; 48 | tmp = *stack1; 49 | rot = 0; 50 | mid = find_mid_value(*stack1, len); 51 | while (tmp && half >= 0) 52 | { 53 | if (((*stack1)->diff == 1 && tmp->num < mid) || 54 | ((*stack1)->diff == 2 && tmp->num <= mid)) 55 | { 56 | while ((*stack1)->num != tmp->num) 57 | rot = stack_rotator(stack1, result, rot); 58 | tmp = tmp->next; 59 | half = stack_pusher(stack1, stack2, result, half); 60 | } 61 | else 62 | tmp = tmp->next; 63 | } 64 | return (rot); 65 | } 66 | 67 | int div_stack_b(t_stack **stack1, t_stack **stack2, 68 | t_com **result, int len) 69 | { 70 | int rot; 71 | int mid; 72 | t_stack *tmp; 73 | int half; 74 | 75 | half = len - len / 2; 76 | tmp = *stack1; 77 | rot = 0; 78 | mid = find_mid_value(*stack1, len); 79 | while (tmp && half > 0) 80 | { 81 | if (((*stack1)->diff == 1 && tmp->num > mid) || 82 | ((*stack1)->diff == 2 && tmp->num >= mid)) 83 | { 84 | while ((*stack1)->num != tmp->num) 85 | rot = stack_rotator(stack1, result, rot); 86 | tmp = tmp->next; 87 | half = stack_pusher(stack1, stack2, result, half); 88 | } 89 | else 90 | tmp = tmp->next; 91 | } 92 | return (rot); 93 | } 94 | -------------------------------------------------------------------------------- /srcs/error_check.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* error_check.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/02/20 15:34:14 by sshiling #+# #+# */ 9 | /* Updated: 2018/02/20 15:34:16 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | void doubles_checker(t_stack **stack) 16 | { 17 | int *array; 18 | int len; 19 | int i; 20 | 21 | i = 0; 22 | len = s_len(*stack); 23 | array = create_array_from_list(*stack, len); 24 | quick_sort(array, 0, len - 1); 25 | while (i < len - 1) 26 | { 27 | if (array[i] >= array[i + 1]) 28 | { 29 | stack_del(stack); 30 | free(array); 31 | return ; 32 | } 33 | i++; 34 | } 35 | free(array); 36 | } 37 | 38 | int check_int_overflow(char *str) 39 | { 40 | if (str[0] == '-') 41 | { 42 | if (ft_atoi(str) > 0) 43 | return (1); 44 | } 45 | if (str[0] != '-') 46 | { 47 | if (ft_atoi(str) < 0) 48 | return (1); 49 | } 50 | return (0); 51 | } 52 | 53 | int check_argv(char *str) 54 | { 55 | int i; 56 | int len; 57 | 58 | i = 0; 59 | if (str[i] == '-' && !NUM(str[i + 1])) 60 | return (1); 61 | if (str[i] == '-' && NUM(str[i + 1])) 62 | i++; 63 | len = ft_strlen(&str[i]); 64 | if (len > 10) 65 | return (1); 66 | while (str[i]) 67 | { 68 | if (!NUM(str[i])) 69 | return (1); 70 | i++; 71 | } 72 | if (len == 10 && check_int_overflow(str)) 73 | return (1); 74 | return (0); 75 | } 76 | -------------------------------------------------------------------------------- /srcs/helpers.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* helpers.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/02/15 18:54:45 by sshiling #+# #+# */ 9 | /* Updated: 2018/02/15 18:54:48 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | char *add_to_string(char *to, char *add) 16 | { 17 | char *del; 18 | 19 | del = to; 20 | to = ft_strjoin(to, add); 21 | ft_strdel(&del); 22 | return (to); 23 | } 24 | 25 | int *create_array_from_list(t_stack *stack, int len) 26 | { 27 | int *array; 28 | int i; 29 | 30 | array = (int*)malloc(sizeof(int) * len); 31 | i = 0; 32 | while (i < len) 33 | { 34 | array[i] = stack->num; 35 | stack = stack->next; 36 | i++; 37 | } 38 | return (array); 39 | } 40 | 41 | t_com *new_result(void) 42 | { 43 | t_com *new; 44 | int i; 45 | 46 | i = 0; 47 | new = (t_com*)malloc(sizeof(t_com)); 48 | new->rotator = 0; 49 | new->final = ft_strdup("\0"); 50 | return (new); 51 | } 52 | 53 | int find_mid_value(t_stack *stack, int len) 54 | { 55 | int *array; 56 | int mid_value; 57 | 58 | len = s_len(stack) < len ? s_len(stack) : len; 59 | array = create_array_from_list(stack, len); 60 | quick_sort(array, 0, len - 1); 61 | mid_value = array[len / 2]; 62 | free(array); 63 | return (mid_value); 64 | } 65 | 66 | int s_len(t_stack *stack) 67 | { 68 | int len; 69 | t_stack *tmp; 70 | 71 | len = 0; 72 | tmp = stack; 73 | while (tmp) 74 | { 75 | tmp = tmp->next; 76 | len++; 77 | } 78 | return (len); 79 | } 80 | -------------------------------------------------------------------------------- /srcs/printer.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* printer.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/01/31 20:08:44 by sshiling #+# #+# */ 9 | /* Updated: 2018/01/31 20:08:45 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | static void free_memory(char **tmp, char **origin, char **result) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | while (tmp[i]) 21 | { 22 | free(tmp[i]); 23 | i++; 24 | } 25 | free(tmp); 26 | ft_strdel(origin); 27 | ft_strdel(result); 28 | } 29 | 30 | void print_final_result(char *origin) 31 | { 32 | char **tmp; 33 | char *result; 34 | int i; 35 | 36 | i = 0; 37 | result = ft_strdup("\0"); 38 | tmp = ft_strsplit(origin, '\n'); 39 | while (tmp[i]) 40 | { 41 | if (tmp[i] && (ft_strequ(tmp[i], "pa") || ft_strequ(tmp[i], "pb"))) 42 | i = add_pa_pb(tmp, &result, i); 43 | if (tmp[i] && (ft_strequ(tmp[i], "ra") || ft_strequ(tmp[i], "rra"))) 44 | i = add_ra_rra(tmp, &result, i); 45 | if (tmp[i] && (ft_strequ(tmp[i], "rb") || ft_strequ(tmp[i], "rrb"))) 46 | i = add_rb_rrb(tmp, &result, i); 47 | if (tmp[i] && !(ft_strequ(tmp[i], "pa") || ft_strequ(tmp[i], "pb") || 48 | ft_strequ(tmp[i], "ra") || ft_strequ(tmp[i], "rb") || 49 | ft_strequ(tmp[i], "rra") || ft_strequ(tmp[i], "rrb"))) 50 | i = add_others(tmp, &result, i); 51 | } 52 | ft_printf("%s", result); 53 | free_memory(tmp, &origin, &result); 54 | } 55 | -------------------------------------------------------------------------------- /srcs/printer_helper.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* printer_helper.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/02/20 20:56:27 by sshiling #+# #+# */ 9 | /* Updated: 2018/02/20 20:56:30 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | int add_pa_pb(char **origin, char **res, int i) 16 | { 17 | int pa; 18 | int pb; 19 | int x; 20 | 21 | pa = 0; 22 | pb = 0; 23 | x = 0; 24 | while (origin[i] && (ft_strequ(origin[i], "pa") || 25 | ft_strequ(origin[i], "pb"))) 26 | { 27 | if (ft_strequ(origin[i], "pa")) 28 | pa++; 29 | if (ft_strequ(origin[i], "pb")) 30 | pb++; 31 | i++; 32 | } 33 | x = pa > pb ? pa - pb : 0; 34 | while (x-- > 0) 35 | *res = add_to_string(*res, "pa\n"); 36 | x = pa < pb ? pb - pa : 0; 37 | while (x-- > 0) 38 | *res = add_to_string(*res, "pb\n"); 39 | return (i); 40 | } 41 | 42 | int add_ra_rra(char **origin, char **res, int i) 43 | { 44 | int ra; 45 | int rra; 46 | int x; 47 | 48 | ra = 0; 49 | rra = 0; 50 | x = 0; 51 | while (origin[i] && (ft_strequ(origin[i], "ra") || 52 | ft_strequ(origin[i], "rra"))) 53 | { 54 | if (ft_strequ(origin[i], "ra")) 55 | ra++; 56 | if (ft_strequ(origin[i], "rra")) 57 | rra++; 58 | i++; 59 | } 60 | x = ra > rra ? ra - rra : 0; 61 | while (x-- > 0) 62 | *res = add_to_string(*res, "ra\n"); 63 | x = ra < rra ? rra - ra : 0; 64 | while (x-- > 0) 65 | *res = add_to_string(*res, "rra\n"); 66 | return (i); 67 | } 68 | 69 | int add_rb_rrb(char **origin, char **res, int i) 70 | { 71 | int rb; 72 | int rrb; 73 | int x; 74 | 75 | rb = 0; 76 | rrb = 0; 77 | x = 0; 78 | while (origin[i] && (ft_strequ(origin[i], "rb") || 79 | ft_strequ(origin[i], "rrb"))) 80 | { 81 | if (ft_strequ(origin[i], "rb")) 82 | rb++; 83 | if (ft_strequ(origin[i], "rrb")) 84 | rrb++; 85 | i++; 86 | } 87 | x = rb > rrb ? rb - rrb : 0; 88 | while (x-- > 0) 89 | *res = add_to_string(*res, "rb\n"); 90 | x = rb < rrb ? rrb - rb : 0; 91 | while (x-- > 0) 92 | *res = add_to_string(*res, "rrb\n"); 93 | return (i); 94 | } 95 | 96 | int add_others(char **tmp, char **res, int i) 97 | { 98 | while (tmp[i] && !(ft_strequ(tmp[i], "pa") || ft_strequ(tmp[i], "pb") || 99 | ft_strequ(tmp[i], "ra") || ft_strequ(tmp[i], "rb") || 100 | ft_strequ(tmp[i], "rra") || ft_strequ(tmp[i], "rrb"))) 101 | { 102 | *res = add_to_string(*res, tmp[i]); 103 | *res = add_to_string(*res, "\n"); 104 | i++; 105 | } 106 | return (i); 107 | } 108 | -------------------------------------------------------------------------------- /srcs/push_back.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* push_back.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/02/15 18:56:30 by sshiling #+# #+# */ 9 | /* Updated: 2018/02/15 18:56:32 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | void push_back(t_stack **stack1, t_stack **stack2, t_com **result, int half) 16 | { 17 | while (half > 0) 18 | { 19 | (*stack2)->diff = (*stack2)->diff == 1 ? 2 : 1; 20 | pab(stack1, stack2); 21 | if ((*stack1)->diff == 2) 22 | (*result)->final = add_to_string((*result)->final, "pb\n"); 23 | else 24 | (*result)->final = add_to_string((*result)->final, "pa\n"); 25 | half--; 26 | } 27 | } 28 | 29 | void rotate_back(t_stack **stack, t_com **result, int back) 30 | { 31 | if (s_len(*stack) == 2) 32 | { 33 | swap_two(stack, result); 34 | return ; 35 | } 36 | if (s_len(*stack) == 3) 37 | { 38 | swap_three(stack, result); 39 | return ; 40 | } 41 | while (back != 0) 42 | { 43 | rrab(stack); 44 | if ((*stack)->diff == 1) 45 | (*result)->final = add_to_string((*result)->final, "rra\n"); 46 | else 47 | (*result)->final = add_to_string((*result)->final, "rrb\n"); 48 | back--; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /srcs/push_swap.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* push_swap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/02/12 20:08:01 by sshiling #+# #+# */ 9 | /* Updated: 2018/02/12 20:08:03 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | static void do_three(t_stack **stack) 16 | { 17 | if ((*stack)->num < (*stack)->next->num && (*stack)->next->num < 18 | (*stack)->next->next->num) 19 | return ; 20 | if ((*stack)->num > (*stack)->next->num && (*stack)->next->num < 21 | (*stack)->next->next->num && 22 | (*stack)->num < (*stack)->next->next->num) 23 | ft_printf("sa\n"); 24 | else if ((*stack)->num < (*stack)->next->num && (*stack)->next->num > 25 | (*stack)->next->next->num && 26 | (*stack)->num < (*stack)->next->next->num) 27 | ft_printf("rra\nsa\n"); 28 | else if ((*stack)->num > (*stack)->next->num && (*stack)->num > 29 | (*stack)->next->next->num && 30 | (*stack)->next->num < (*stack)->next->next->num) 31 | ft_printf("ra\n"); 32 | else if ((*stack)->num < (*stack)->next->num && (*stack)->num > 33 | (*stack)->next->next->num && 34 | (*stack)->next->num > (*stack)->next->next->num) 35 | ft_printf("rra\n"); 36 | else if ((*stack)->num > (*stack)->next->num && (*stack)->next->num > 37 | (*stack)->next->next->num) 38 | ft_printf("ra\nsa\n"); 39 | } 40 | 41 | void short_sort(t_stack **stack, int len) 42 | { 43 | if (len == 1) 44 | return ; 45 | if (len == 2 && (*stack)->num > (*stack)->next->num) 46 | ft_printf("sa\n"); 47 | if (len == 3) 48 | do_three(stack); 49 | return ; 50 | } 51 | 52 | void sort_stacks(t_stack **stack1, t_stack **stack2, t_com **result, 53 | int len) 54 | { 55 | int rot; 56 | 57 | if (len == 2 || len == 3) 58 | return ; 59 | if ((*stack1)->diff == 1) 60 | rot = div_stack_a(stack1, stack2, result, len); 61 | else 62 | rot = div_stack_b(stack1, stack2, result, len); 63 | if ((*result)->rotator == 1) 64 | rotate_back(stack1, result, rot); 65 | if ((*stack1)->diff == 1 && (len / 2 == 3 || len / 2 == 2)) 66 | swap_elements(stack1, stack2, result, len); 67 | else if ((*stack1)->diff == 2 && (len / 2 == 3 || len / 2 == 2)) 68 | swap_elements(stack1, stack2, result, len); 69 | sort_stacks(stack1, stack2, result, (*stack1)->diff != 1 ? 70 | len / 2 : len - len / 2); 71 | sort_stacks(stack2, stack1, result, (*stack1)->diff == 1 ? 72 | len / 2 : len - len / 2); 73 | if ((*stack1)->diff == 1) 74 | push_back(stack1, stack2, result, len / 2); 75 | else 76 | push_back(stack1, stack2, result, len - len / 2); 77 | } 78 | 79 | int main(int argc, char **argv) 80 | { 81 | t_stack *stack1; 82 | t_stack *stack2; 83 | t_com *result; 84 | int len; 85 | 86 | if (!(stack1 = copy_args_in_stack(argc, argv))) 87 | { 88 | write(2, "Error\n", 6); 89 | return (0); 90 | } 91 | len = s_len(stack1); 92 | stack2 = NULL; 93 | result = new_result(); 94 | if (len < 4) 95 | { 96 | short_sort(&stack1, len); 97 | stack_del(&stack1); 98 | return (0); 99 | } 100 | else 101 | sort_stacks(&stack1, &stack2, &result, len); 102 | print_final_result(result->final); 103 | stack_del(&stack1); 104 | free(result); 105 | return (0); 106 | } 107 | -------------------------------------------------------------------------------- /srcs/quick_sort.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* quick_sort.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/01/31 20:07:29 by sshiling #+# #+# */ 9 | /* Updated: 2018/01/31 20:07:31 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | void ft_swap(int *a, int *b) 16 | { 17 | int tmp; 18 | 19 | tmp = *a; 20 | *a = *b; 21 | *b = tmp; 22 | } 23 | 24 | int partition(int *stack1, int start, int end) 25 | { 26 | int end_value; 27 | int strt_indx; 28 | int curr_indx; 29 | 30 | end_value = stack1[end]; 31 | strt_indx = start - 1; 32 | curr_indx = start; 33 | while (curr_indx <= end - 1) 34 | { 35 | if (stack1[curr_indx] <= end_value) 36 | { 37 | strt_indx++; 38 | ft_swap(&stack1[strt_indx], &stack1[curr_indx]); 39 | } 40 | curr_indx++; 41 | } 42 | ft_swap(&stack1[strt_indx + 1], &stack1[end]); 43 | return (strt_indx + 1); 44 | } 45 | 46 | void quick_sort(int *stack1, int start, int end) 47 | { 48 | int stack2[end + 1]; 49 | int top; 50 | int pivot; 51 | 52 | top = -1; 53 | stack2[++top] = start; 54 | stack2[++top] = end; 55 | while (top >= 0) 56 | { 57 | end = stack2[top--]; 58 | start = stack2[top--]; 59 | pivot = partition(stack1, start, end); 60 | if (pivot - 1 > start) 61 | { 62 | stack2[++top] = start; 63 | stack2[++top] = pivot - 1; 64 | } 65 | if (pivot + 1 < end) 66 | { 67 | stack2[++top] = pivot + 1; 68 | stack2[++top] = end; 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /srcs/stack_creator.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* helpers.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/01/31 20:04:48 by sshiling #+# #+# */ 9 | /* Updated: 2018/01/31 20:04:50 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | void stack_del(t_stack **stack) 16 | { 17 | t_stack *del; 18 | t_stack *tmp; 19 | 20 | tmp = *stack; 21 | while (tmp) 22 | { 23 | del = tmp; 24 | tmp = tmp->next; 25 | free(del); 26 | } 27 | *stack = NULL; 28 | } 29 | 30 | t_stack *create_new_node(void) 31 | { 32 | t_stack *first; 33 | 34 | first = (t_stack*)malloc(sizeof(t_stack)); 35 | first->num = 0; 36 | first->diff = 1; 37 | first->next = NULL; 38 | return (first); 39 | } 40 | 41 | t_stack *copy_args_in_stack(int argc, char **argv) 42 | { 43 | int i; 44 | t_stack *first; 45 | t_stack *tmp; 46 | 47 | i = 1; 48 | first = create_new_node(); 49 | tmp = first; 50 | while (i < argc) 51 | { 52 | if (check_argv(argv[i])) 53 | { 54 | stack_del(&first); 55 | return (NULL); 56 | } 57 | tmp->num = ft_atoi(argv[i]); 58 | if (i < argc - 1) 59 | { 60 | tmp->next = create_new_node(); 61 | tmp = tmp->next; 62 | tmp->diff = 1; 63 | } 64 | i++; 65 | } 66 | doubles_checker(&first); 67 | return (first); 68 | } 69 | -------------------------------------------------------------------------------- /srcs/swap_elements.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* swap_elements.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/02/15 18:56:08 by sshiling #+# #+# */ 9 | /* Updated: 2018/02/15 18:56:10 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | static int len_a_creator(t_stack **stack1, t_com **result, int len) 16 | { 17 | int res; 18 | 19 | if ((*stack1)->diff == 1) 20 | { 21 | (*result)->rotator = 1; 22 | res = len - len / 2; 23 | } 24 | else 25 | res = len / 2; 26 | return (res); 27 | } 28 | 29 | static int len_b_creator(t_stack **stack1, t_com **result, int len) 30 | { 31 | int res; 32 | 33 | if ((*stack1)->diff == 1) 34 | { 35 | (*result)->rotator = 1; 36 | res = len / 2; 37 | } 38 | else 39 | res = len - len / 2; 40 | return (res); 41 | } 42 | 43 | static void swap_for_len1_two(t_stack **stack1, t_stack **stack2, 44 | t_com **result, int len) 45 | { 46 | int len_1; 47 | int len_2; 48 | 49 | len_1 = len_a_creator(stack1, result, len); 50 | len_2 = len_b_creator(stack1, result, len); 51 | if (len_1 == 2 && (!(*stack1) || s_len(*stack1) < 2)) 52 | return ; 53 | else if (len_1 == 2 && (!(*stack2) || s_len(*stack2) < 2 || len_2 > 3)) 54 | swap_two(stack1, result); 55 | else if (len_1 == 2 && len_2 == 2) 56 | { 57 | swap_two(stack1, result); 58 | swap_two(stack2, result); 59 | } 60 | else if (len_1 == 2 && len_2 == 3 && s_len(*stack2) > 2) 61 | { 62 | swap_two(stack1, result); 63 | swap_three(stack2, result); 64 | } 65 | } 66 | 67 | static void swap_for_len1_three(t_stack **stack1, t_stack **stack2, 68 | t_com **result, int len) 69 | { 70 | int len_1; 71 | int len_2; 72 | 73 | len_1 = len_a_creator(stack1, result, len); 74 | len_2 = len_b_creator(stack1, result, len); 75 | if (len_1 > 3 && len_2 == 2) 76 | swap_two(stack2, result); 77 | else if (len_1 == 3 && (!(*stack1) || s_len(*stack1) < 3)) 78 | return ; 79 | else if (len_1 == 3 && (!(*stack2) || s_len(*stack2) < 2 || len_2 > 3)) 80 | swap_three(stack1, result); 81 | else if (len_1 == 3 && len_2 == 2) 82 | { 83 | swap_three(stack1, result); 84 | swap_two(stack2, result); 85 | } 86 | else if (len_1 == 3 && len_2 == 3 && s_len(*stack2) > 2) 87 | { 88 | swap_three(stack1, result); 89 | swap_three(stack2, result); 90 | } 91 | else if (len_1 > 3 && len_2 == 3) 92 | swap_three(stack2, result); 93 | } 94 | 95 | void swap_elements(t_stack **stack1, t_stack **stack2, 96 | t_com **result, int len) 97 | { 98 | int len_1; 99 | 100 | len_1 = len_a_creator(stack1, result, len); 101 | if (len_1 == 2) 102 | swap_for_len1_two(stack1, stack2, result, len); 103 | else if (len_1 > 2) 104 | swap_for_len1_three(stack1, stack2, result, len); 105 | } 106 | -------------------------------------------------------------------------------- /srcs/swappers.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* swappers.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: sshiling +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/02/15 19:16:55 by sshiling #+# #+# */ 9 | /* Updated: 2018/02/15 19:16:57 by sshiling ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "push_swap.h" 14 | 15 | void swap_three_a(t_stack **stack, t_com **res) 16 | { 17 | if ((*stack)->num < (*stack)->next->num && 18 | (*stack)->next->num < (*stack)->next->next->num) 19 | return ; 20 | if ((*stack)->num > (*stack)->next->num && 21 | (*stack)->next->num < (*stack)->next->next->num && 22 | (*stack)->num < (*stack)->next->next->num) 23 | s_len(*stack) > 3 ? comb_1(stack, res) : swap_two(stack, res); 24 | else if ((*stack)->num < (*stack)->next->num && 25 | (*stack)->next->num > (*stack)->next->next->num && 26 | (*stack)->num < (*stack)->next->next->num) 27 | s_len(*stack) > 3 ? comb_2(stack, res) : comb_6(stack, res); 28 | else if ((*stack)->num > (*stack)->next->num && 29 | (*stack)->num > (*stack)->next->next->num && 30 | (*stack)->next->num < (*stack)->next->next->num) 31 | s_len(*stack) > 3 ? comb_3(stack, res) : comb_7(stack, res); 32 | else if ((*stack)->num < (*stack)->next->num && 33 | (*stack)->num > (*stack)->next->next->num && 34 | (*stack)->next->num > (*stack)->next->next->num) 35 | s_len(*stack) > 3 ? comb_4(stack, res) : comb_8(stack, res); 36 | else if ((*stack)->num > (*stack)->next->num && 37 | (*stack)->next->num > (*stack)->next->next->num) 38 | s_len(*stack) > 3 ? comb_5(stack, res) : comb_9(stack, res); 39 | } 40 | 41 | void swap_three_b(t_stack **stack, t_com **res) 42 | { 43 | if ((*stack)->num > (*stack)->next->num && 44 | (*stack)->next->num > (*stack)->next->next->num) 45 | return ; 46 | else if ((*stack)->num < (*stack)->next->num && 47 | (*stack)->num > (*stack)->next->next->num && 48 | (*stack)->next->num > (*stack)->next->next->num) 49 | s_len(*stack) > 3 ? comb_1(stack, res) : swap_two(stack, res); 50 | else if ((*stack)->num > (*stack)->next->num && 51 | (*stack)->num > (*stack)->next->next->num && 52 | (*stack)->next->num < (*stack)->next->next->num) 53 | s_len(*stack) > 3 ? comb_2(stack, res) : comb_6(stack, res); 54 | else if ((*stack)->num < (*stack)->next->num && 55 | (*stack)->next->num > (*stack)->next->next->num && 56 | (*stack)->num < (*stack)->next->next->num) 57 | s_len(*stack) > 3 ? comb_3(stack, res) : comb_7(stack, res); 58 | else if ((*stack)->num > (*stack)->next->num && 59 | (*stack)->next->num < (*stack)->next->next->num && 60 | (*stack)->num < (*stack)->next->next->num) 61 | s_len(*stack) > 3 ? comb_4(stack, res) : comb_8(stack, res); 62 | else if ((*stack)->num < (*stack)->next->num && 63 | (*stack)->next->num < (*stack)->next->next->num) 64 | s_len(*stack) > 3 ? comb_5(stack, res) : comb_9(stack, res); 65 | } 66 | 67 | void swap_three(t_stack **stack, t_com **result) 68 | { 69 | if ((*stack)->diff == 1) 70 | swap_three_a(stack, result); 71 | else 72 | swap_three_b(stack, result); 73 | } 74 | 75 | void swap_two(t_stack **stack, t_com **result) 76 | { 77 | if ((*stack)->diff == 1 && ((*stack)->num > (*stack)->next->num)) 78 | { 79 | sab(stack); 80 | (*result)->final = add_to_string((*result)->final, "sa\n"); 81 | } 82 | else if ((*stack)->diff == 2 && ((*stack)->num < (*stack)->next->num)) 83 | { 84 | sab(stack); 85 | (*result)->final = add_to_string((*result)->final, "sb\n"); 86 | } 87 | } 88 | --------------------------------------------------------------------------------