commit 748d10f4f37cb884538fca9359446c79522d5289 Author: Jeremy FLEURY Date: Wed Jul 17 11:22:24 2019 +0200 push diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5e0e360 --- /dev/null +++ b/Makefile @@ -0,0 +1,108 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: jfleury +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2019/05/30 14:54:49 by jfleury #+# #+# # +# Updated: 2019/07/17 11:19:50 by jfleury ### ########.fr # +# # +# **************************************************************************** # + +NAME1 = asm +NAME2 = corewar +CC = gcc +RM = rm -f +INCLUDE = -I ./include +IN_LIB = -I libft/include +IN_PRT = -I libft/printf +CFLAGS += -Wall -Wextra -Werror $(INCLUDE) $(IN_LIB) $(IN_PRT) +NCURS = -lncurses + +SRC_ASM = srcs/srcs_asm/main_asm.c \ + srcs/srcs_asm/op.c \ + srcs/srcs_asm/convert/convert.c \ + srcs/srcs_asm/convert/conv_instru.c \ + srcs/srcs_asm/convert/conv_utils.c \ + srcs/srcs_asm/lexer/src_variable/var_automaton.c \ + srcs/srcs_asm/lexer/ft_lexer.c \ + srcs/srcs_asm/lexer/ft_automaton.c \ + srcs/srcs_asm/lexer/lib_lexer/ft_append_token_label.c \ + srcs/srcs_asm/lexer/lib_lexer/ft_cut_line.c \ + srcs/srcs_asm/lexer/lib_lexer/ft_line_strchr.c \ + srcs/srcs_asm/lexer/lib_lexer/ft_create_string.c \ + srcs/srcs_asm/lexer/lib_lexer/ft_check_line.c \ + srcs/srcs_asm/lexer/lib_lexer/ft_append_lst_tk.c \ + srcs/srcs_asm/lexer/lib_lexer/ft_empty_line.c \ + srcs/srcs_asm/lexer/lib_lexer/ft_free_list.c \ + srcs/srcs_asm/lexer/lib_lexer/ft_create_token_label.c \ + srcs/srcs_asm/parser/ft_parser.c \ + srcs/srcs_asm/parser/parse_utils.c \ + srcs/srcs_asm/parser/ft_parse_command.c \ + srcs/srcs_asm/parser/ft_parse_instruc.c \ + srcs/srcs_asm/parser/ft_cal_progsize.c + +SRC_VM = srcs/srcs_vm/cw_parser.c \ + srcs/srcs_vm/cw_read_mem.c \ + srcs/srcs_vm/cw_write_mem.c \ + srcs/srcs_vm/cw_process.c \ + srcs/srcs_vm/cw_vm.c \ + srcs/srcs_vm/cw_live.c \ + srcs/srcs_vm/cw_add_sub.c \ + srcs/srcs_vm/cw_aff.c \ + srcs/srcs_vm/cw_and_or_xor.c \ + srcs/srcs_vm/cw_fork_lfork.c \ + srcs/srcs_vm/cw_ld_lld.c \ + srcs/srcs_vm/cw_ldi_lldi.c \ + srcs/srcs_vm/cw_st.c \ + srcs/srcs_vm/cw_sti.c \ + srcs/srcs_vm/cw_zjump.c \ + srcs/srcs_vm/check_args.c \ + srcs/srcs_vm/main.c \ + srcs/srcs_vm/cw_visual.c \ + srcs/srcs_vm/cw_init_ncurs.c \ + srcs/srcs_vm/cw_error.c \ + srcs/srcs_vm/cw_print_usage.c \ + srcs/srcs_vm/cw_ops_tools.c \ + srcs/srcs_vm/cw_args.c \ + srcs/srcs_vm/cw_visual_win.c + +LIB = libft/libft.a +PRT = libft/printf/libftprintf.a + +INCP = $(foreach dir, $(INCDIR), -I$(dir)) + +OBJ_ASM = $(SRC_ASM:.c=.o) +OBJ_VM = $(SRC_VM:.c=.o) + +all: libft $(NAME1) $(NAME2) + +libft : + make -C libft -f Makefile + make -C libft/printf -f Makefile + +$(NAME1): $(OBJ_ASM) + make -C libft -f Makefile + make -C libft/printf -f Makefile + $(CC) -o $(NAME1) $(CFLAGS) $(OBJ_ASM) $(LIB) $(PRT) + +$(NAME2): $(OBJ_VM) + make -C libft -f Makefile + make -C libft/printf -f Makefile + $(CC) -o $(NAME2) $(CFLAGS) $(OBJ_VM) $(LIB) $(PRT) -lncurses + + +clean: + make -C libft -f Makefile clean + make -C libft/printf -f Makefile clean + $(RM) $(OBJ_ASM) $(OBJ_VM) + +fclean: clean + make -C libft -f Makefile fclean + make -C libft/printf -f Makefile fclean + $(RM) $(NAME1) $(NAME2) + +re: fclean all + +.PHONY: all clean fclean re diff --git a/auteur b/auteur new file mode 100644 index 0000000..45276a9 --- /dev/null +++ b/auteur @@ -0,0 +1,4 @@ +jfleury +mdchane +tmaze +igarbuz diff --git a/include/asm.h b/include/asm.h new file mode 100644 index 0000000..247069a --- /dev/null +++ b/include/asm.h @@ -0,0 +1,138 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* asm.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/05/29 16:43:12 by jfleury #+# #+# */ +/* Updated: 2019/07/16 11:40:38 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ASM_H +# define ASM_H + +# define LABEL 1 +# define INSTRUCTION 2 +# define DIRECT 3 +# define DIRECT_LABEL 4 +# define INDIRECT 5 +# define INDIRECT_LABEL 6 +# define REGISTER 7 +# define STRING 8 +# define SEPERATOR 9 +# define COMMENT 10 +# define COMMAND 11 + +# include "libft.h" +# include "ft_printf.h" +# include "op.h" +# include +# include +# include + +/* +** Struct +*/ + +typedef struct s_token +{ + int type; + char *contents; + int line; + struct s_token *next; +} t_token; + +typedef struct s_label +{ + char *contents; + int place; + struct s_label *next; +} t_label; + +typedef struct s_lst_tk +{ + t_token *token; + struct s_lst_tk *next; +} t_lst_tk; + +typedef struct s_pos_lab +{ + t_label *lab; + int pos; +} t_pos_lab; + +typedef struct s_inst +{ + char *name; + int nb_params; + char poss[3]; +} t_inst; + +/* +** Global +*/ + +extern int g_matrice_automaton[30][17]; +extern char *g_string_automaton[16]; +extern int g_nb_line; +extern int g_nb_char; +extern int g_nb_token; +extern int g_place; +extern t_op g_tab[17]; + +/* +** ASM +*/ + +t_label *get_label(t_token *tk, t_label *label); +void ft_convert(t_lst_tk *lst, t_header *head, char **argv, + t_label *list_lab); +void ft_free_all(t_lst_tk *list, t_label *label, t_header *head); +unsigned char *ft_conv_instru(t_token *tk, int *size_instru, + t_pos_lab pos_lab); +unsigned char *ft_strextend_nm(unsigned char *s1, char unsigned *s2, + int n, int m); + +/* +** Lexer +*/ + +t_token *ft_lexer_token(char **line, t_label **list_label); +int ft_lexer(t_lst_tk **lst_tk, t_label **list_label, + char **argv); +int ft_automaton(char *line, t_token *token, t_label *label, + int *nb_char_token); +int ft_line_strchr(char *str, char c); +int ft_empty_line(char *line); +int ft_append_lst_tk(t_token *list_token, t_lst_tk **list); +char **ft_cut_line(char **line, int nb_char_token); +char *ft_create_string(char *line, int nb_char_token); +char *ft_check_line(char *line, int fd); +void ft_append_token_label(t_token *token, t_token **list_token, + t_label *label, t_label **list_label); +void ft_create_token_label(char *str, int type, t_token *token, + t_label *label); +void ft_free_list(t_lst_tk *list, t_label *label); + +/* +** Parser +*/ + +int ft_tklen(t_token *tk); +int ft_parse_instruct(t_token *tk, t_label *label, + unsigned int *size); +int parse_header(t_lst_tk **lst, t_header *head); +int ft_parser(t_lst_tk *lst, t_label *label, t_header *head); +int is_inst(char *name); +char *ft_error(char *msg); +char *ft_parse_command(t_token *tk); +char *ft_parse_command2(t_token *tk, t_header *head); +char *ft_strnew_1(size_t size); +char *ft_strcpy_1(char *dest, const char *src); +unsigned char *ft_strcpy_n(unsigned char *dest, const unsigned char *src, + int n, int m); +unsigned char *ft_conv_hexa(int nbr, int size); + +#endif diff --git a/include/corewar.h b/include/corewar.h new file mode 100644 index 0000000..6dd7a43 --- /dev/null +++ b/include/corewar.h @@ -0,0 +1,233 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* corewar.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/05/08 11:54:03 by tmaze #+# #+# */ +/* Updated: 2019/07/15 19:02:57 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef COREWAR_H +# define COREWAR_H + +# include +# include +# include +# include +# include +# include "libft.h" +# include "ft_printf.h" +# include + +# define IND_SIZE 2 +# define REG_SIZE 4 +# define DIR_SIZE REG_SIZE + +# define REG_CODE 1 +# define DIR_CODE 2 +# define IND_CODE 3 + +# define MAX_ARGS_NUMBER 4 +# define MAX_PLAYERS 4 +# define MEM_SIZE (4*1024) +# define IDX_MOD (MEM_SIZE / 8) +# define CHAMP_MAX_SIZE (MEM_SIZE / 6) + +/* +** Added MEM_SIZE overflow protection binary mask. MEM_SIZE must be 2^x number. +** MEM_SIZE_M must be MEM_SIZE - 1; +*/ +# define MEM_SIZE_M MEM_SIZE - 1 + +# define COMMENT_CHAR '#' +# define LABEL_CHAR ':' +# define DIRECT_CHAR '%' +# define SEPARATOR_CHAR ',' + +# define LABEL_CHARS "abcdefghijklmnopqrstuvwxyz_0123456789" + +# define NAME_CMD_STRING ".name" +# define COMMENT_CMD_STRING ".comment" + +# define REG_NUMBER 16 + +# define CYCLE_TO_DIE 1536 +# define CYCLE_DELTA 50 +# define NBR_LIVE 21 +# define MAX_CHECKS 10 + +# define T_REG 1 +# define T_DIR 2 +# define T_IND 4 + +# define PROG_NAME_LENGTH (128) +# define COMMENT_LENGTH (2048) +# define COREWAR_EXEC_MAGIC 0xea83f3 + +# define AG_VERB 0x0001 +# define AG_DUMP32 0x0002 +# define AG_DUMP64 0x0004 +# define AG_VISUAL 0x0008 +# define AG_AFF 0x0010 + +# define VB_LIVE 0x0001 +# define VB_CYCLES 0x0002 +# define VB_OPS 0x0004 +# define VB_DEATH 0x0008 +# define VB_PC 0x0010 + +# define ART_MAX_SIZE 1023 + +char g_buf[BUF_SIZE]; + +typedef struct s_player +{ + unsigned int magic; + char prog_name[PROG_NAME_LENGTH + 1]; + unsigned int prog_size; + char comment[COMMENT_LENGTH + 1]; + int num; + char col; + unsigned int live_ct; + unsigned int lst_live; + unsigned char code[CHAMP_MAX_SIZE]; +} t_player; + +typedef struct s_process +{ + size_t pid; + unsigned char op; + unsigned char carry; + char new; + unsigned int wait; + int regs[REG_NUMBER + 1]; + int lst_live; + unsigned short int pc; + struct s_process *next; +} t_process; + +typedef struct s_visual +{ + WINDOW *arena; + WINDOW *stat; + bool paused; + int delay; +} t_visual; + +typedef struct s_vm +{ + unsigned char mem[MEM_SIZE]; + unsigned char col[MEM_SIZE]; + t_player pls[MAX_PLAYERS]; + int args; + int dump; + int verb; + int live; + int live_ct; + int cycles; + int ctd_c; + int ctd; + short int nb_j; + size_t p_ct; + size_t pid_ct; + t_process *tl; + t_visual vis; +} t_vm; + +typedef struct s_ops +{ + unsigned int wait; + void (*func)(t_process*, t_vm*); +} t_ops; + +typedef struct s_args +{ + char t; + short int i; +} t_args; + +typedef struct s_pargs +{ + char *arg; + void (*func)(char**, t_vm*, int); + char nb_ag; +} t_pargs; + +void cw_print_usage(void); + +void clear_process(t_vm *vm); +void init_process(t_vm *vm); +void del_prcs(t_vm *vm); + +void init_vm(t_vm *vm, int i); +int load_champ(t_vm *vm); +void mem_dump(t_vm *vm); + +void arg_dump(char **av, t_vm *vm, int i); +void arg_verb(char **av, t_vm *vm, int i); +void arg_visual(char **av, t_vm *vm, int i); +void arg_aff(char **av, t_vm *vm, int i); +void fgqarg_aff(char **av, t_vm *vm, int i); + +int cw_parser(int ac, char **av, t_vm *vm); + +int check_file(char *path); + +int read_four(t_vm *vm, size_t index); +short int read_two(t_vm *vm, size_t index); +unsigned char read_mem(t_vm *vm, int ind); + +void write_mem(t_vm *vm, unsigned int index, char val + , unsigned short int pc); +void write_four(t_vm *vm, unsigned int index, int val + , unsigned short int pc); + +unsigned int l2b_endian(unsigned int num); +void cw_move_pc(t_vm *vm, t_process *p, int jp); +void init_args(t_args *ag, t_process *p, t_vm *vm); +int check_reg(t_vm *vm, int ind); +int cw_ocp(char nb_ag, t_args *ag, char size_dir); + +void cw_live(t_process *p, t_vm *vm); +void cw_ld(t_process *p, t_vm *vm); +void cw_st(t_process *p, t_vm *vm); +void cw_add(t_process *p, t_vm *vm); +void cw_sub(t_process *p, t_vm *vm); +void cw_and(t_process *p, t_vm *vm); +void cw_or(t_process *p, t_vm *vm); +void cw_xor(t_process *p, t_vm *vm); +void cw_zjump(t_process *p, t_vm *vm); +void cw_ldi(t_process *p, t_vm *vm); +void cw_sti(t_process *p, t_vm *vm); +void cw_fork(t_process *p, t_vm *vm); +void cw_lld(t_process *p, t_vm *vm); +void cw_lldi(t_process *p, t_vm *vm); +void cw_lfork(t_process *p, t_vm *vm); +void cw_aff(t_process *p, t_vm *vm); + +int err_msg(int err, t_vm *vm); +void error(t_vm *vm, int err); + +/* +** cw_visual.c +*/ +void print_arena(t_vm *vm); +void print_pc(t_vm *vm, unsigned short int pc, char col); +void control_delay(t_vm *vm); + +/* +** cw_init_ncurs.c +*/ +void init_ncurses(t_vm *vm); +void destroy_ncurses(t_vm *vm); +void destroy_ncurses_exit(t_vm *vm); +void check_user_exit(t_vm *vm, int key); +/* +** cw_visual_win.c +*/ +void visual_winners(t_vm *vm, int i); +void visual_delay_update(t_vm *vm); +#endif diff --git a/include/op.h b/include/op.h new file mode 100644 index 0000000..c65ed17 --- /dev/null +++ b/include/op.h @@ -0,0 +1,69 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* op.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2013/10/04 11:33:27 by zaz #+# #+# */ +/* Updated: 2019/07/15 18:14:53 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef OP_H +# define OP_H + +# define IND_SIZE 2 +# define REG_SIZE 4 +# define DIR_SIZE REG_SIZE +# define REG_CODE 1 +# define DIR_CODE 2 +# define IND_CODE 3 +# define MAX_ARGS_NUMBER 4 +# define MAX_PLAYERS 4 +# define MEM_SIZE (4*1024) +# define IDX_MOD (MEM_SIZE / 8) +# define CHAMP_MAX_SIZE (MEM_SIZE / 6) +# define COMMENT_CHAR '#' +# define LABEL_CHAR ':' +# define DIRECT_CHAR '%' +# define SEPARATOR_CHAR ',' +# define LABEL_CHARS "abcdefghijklmnopqrstuvwxyz_0123456789" +# define NAME_CMD_STRING ".name" +# define COMMENT_CMD_STRING ".comment" +# define REG_NUMBER 16 +# define CYCLE_TO_DIE 1536 +# define CYCLE_DELTA 50 +# define NBR_LIVE 21 +# define MAX_CHECKS 10 +# define T_REG 1 +# define T_DIR 2 +# define T_IND 4 +# define T_LAB 8 +# define PROG_NAME_LENGTH (128) +# define COMMENT_LENGTH (2048) +# define COREWAR_EXEC_MAGIC 0xea83f3 + +typedef char t_arg_type; + +typedef struct s_header +{ + unsigned int magic; + char prog_name[PROG_NAME_LENGTH + 1]; + unsigned int prog_size; + char comment[COMMENT_LENGTH + 1]; +} t_header; + +typedef struct s_op +{ + char *name; + int nb_param; + char poss[3]; + char op_code; + int cycle; + char *comp_name; + int byte; + int size; +} t_op; + +#endif diff --git a/libft/Makefile b/libft/Makefile new file mode 100644 index 0000000..5d4e0c3 --- /dev/null +++ b/libft/Makefile @@ -0,0 +1,124 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: jfleury +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2018/11/14 20:22:58 by jfleury #+# #+# # +# Updated: 2019/07/17 11:13:16 by jfleury ### ########.fr # +# # +# **************************************************************************** # + +NAME = libft.a +AR = ar rc +CC = gcc +RL = ranlib +RM = rm -f +INCLUDE = -I ./include +CFLAGS = -Wall -Werror -Wextra -c $(INCLUDE) + +SRC_LIBFT = libft/ft_memset.c \ + libft/ft_bzero.c \ + libft/ft_memcpy.c \ + libft/ft_memccpy.c \ + libft/ft_memmove.c \ + libft/ft_memchr.c \ + libft/ft_memcmp.c \ + libft/ft_strlen.c \ + libft/ft_strcmp.c \ + libft/ft_strdup.c \ + libft/ft_strcpy.c \ + libft/ft_strncpy.c \ + libft/ft_strcat.c \ + libft/ft_strncat.c \ + libft/ft_strlcat.c \ + libft/ft_strchr.c \ + libft/ft_strrchr.c \ + libft/ft_atoi.c \ + libft/ft_atoi_bin.c \ + libft/ft_atoi_long.c \ + libft/ft_isalpha.c \ + libft/ft_isdigit.c \ + libft/ft_isalnum.c \ + libft/ft_strstr.c \ + libft/ft_strnstr.c \ + libft/ft_strncmp.c \ + libft/ft_strnequ.c \ + libft/ft_isascii.c \ + libft/ft_isprint.c \ + libft/ft_toupper.c \ + libft/ft_tolower.c \ + libft/ft_memalloc.c \ + libft/ft_memdel.c \ + libft/ft_strnew.c \ + libft/ft_strdel.c \ + libft/ft_strclr.c \ + libft/ft_striter.c \ + libft/ft_striteri.c \ + libft/ft_strmap.c \ + libft/ft_strmapi.c \ + libft/ft_strequ.c \ + libft/ft_strsub.c \ + libft/ft_strtrim.c \ + libft/ft_strsplit.c \ + libft/ft_itoa.c \ + libft/ft_strjoin.c \ + libft/ft_putchar.c \ + libft/ft_putstr.c \ + libft/ft_putendl.c \ + libft/ft_putnbr.c \ + libft/ft_putchar_fd.c \ + libft/ft_putstr_fd.c \ + libft/ft_putendl_fd.c \ + libft/ft_putnbr_fd.c \ + libft/ft_lstnew.c \ + libft/ft_lstdelone.c \ + libft/ft_lstdel.c \ + libft/ft_lstadd.c \ + libft/ft_lstadd_end.c \ + libft/ft_lstiter.c \ + libft/ft_lstmap.c \ + libft/ft_strextend.c \ + libft/ft_strnextend.c \ + libft/ft_sstrnew.c \ + libft/ft_sstrcpy.c \ + libft/ft_sstrdel.c \ + libft/get_next_line.c \ + libft/ft_memtab.c \ + libft/ft_memint_tab.c \ + libft/ft_sstrprint.c \ + libft/ft_bubble_sort.c \ + libft/ft_char_replace.c \ + libft/ft_itoa_base.c \ + libft/ft_itoa_double.c \ + libft/ft_strnchr.c \ + libft/ft_strrnchr.c \ + libft/ft_strupcase.c \ + libft/ft_strndup.c \ + libft/ft_power.c \ + libft/ft_itoa_base_int.c \ + libft/ft_itoa_base_short.c \ + libft/ft_strrev.c \ + libft/ft_isnumb.c \ + libft/ft_atois.c \ + libft/ft_mod.c \ + libft/ft_iswhitespace.c + +OBJ_LIBFT = $(SRC_LIBFT:.c=.o) + +all: $(NAME) + +$(NAME): $(OBJ_LIBFT) + $(AR) $(NAME) $(OBJ_LIBFT) + $(RL) $(NAME) + +clean: + $(RM) $(OBJ_LIBFT) + +fclean: clean + $(RM) $(NAME) + +re: fclean all + +.SILENT: $(OBJ_LIBFT) $(NAME) all clean fclean re diff --git a/libft/include/libft.h b/libft/include/libft.h new file mode 100644 index 0000000..7b64067 --- /dev/null +++ b/libft/include/libft.h @@ -0,0 +1,199 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/05 15:35:42 by jfleury #+# #+# */ +/* Updated: 2019/07/15 19:15:18 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_H +# define LIBFT_H + +# include +# include +# include +# include + +# define BUFF_SIZE 32 +# define CONV_ID_NB 13 +# define FLAGS_NB 5 +# define PRECI_GROUPS 3 +# define RED "\x1B[31m" +# define GRN "\x1B[32m" +# define YEL "\x1B[33m" +# define BLU "\x1B[34m" +# define MAG "\x1B[35m" +# define CYN "\x1B[36m" +# define WHT "\x1B[37m" +# define RESET "\x1B[0m" +# define STRONG "\033[1m" + +typedef struct s_list +{ + void *content; + size_t content_size; + struct s_list *next; +} t_list; + +typedef struct s_conv_spec +{ + char conv_id; + int *flags; + int *modifier; + int field_width; + int precision; + char *converted; +} t_conv_spec; + +typedef struct s_fptr_id +{ + char *conv_id_tab; + char *(*fptr[CONV_ID_NB])(t_conv_spec c_s, va_list *list); +} t_fptr_id; + +typedef struct s_fptr_flag +{ + int *flags; + char *(*fptr[FLAGS_NB])(t_conv_spec c_s, char *str); +} t_fptr_flag; + +typedef struct s_fptr_preci +{ + char **preci_group; + char *(*fptr[PRECI_GROUPS])(t_conv_spec c_s, char *str); +} t_fptr_preci; + +int ft_atoi(const char *str); +int ft_atoi_bin(char *str); +long long ft_atoi_long(const char *str); +void ft_bzero(void *s, size_t n); +int ft_isalnum(int c); +int ft_isalpha(int c); +int ft_isascii(int c); +int ft_isdigit(int c); +int ft_isprint(int c); +char *ft_itoa(int n); +void *ft_memalloc(size_t size); +void *ft_memccpy(void *dest, const void *src, int c, size_t n); +void *ft_memchr(const void *s, int c, size_t n); +int ft_memcmp(const void *s1, const void *s2, size_t n); +void *ft_memcpy(void *dest, const void *src, size_t n); +void ft_memdel(void **ap); +void *ft_memmove(void *dest, const void *src, size_t n); +void *ft_memset(void *s, int c, size_t n); +void ft_putchar(char c); +void ft_putchar_fd(char c, int fd); +void ft_putendl(char const *s); +void ft_putendl_fd(char const *s, int fd); +void ft_putnbr(int n); +void ft_putnbr_fd(int n, int fd); +void ft_putstr(char const *s); +void ft_putstr_fd(char const *s, int fd); +char *ft_strcat(char *dest, const char *src); +char *ft_strchr(const char *s, int c); +void ft_strclr(char *s); +int ft_strcmp(const char *s1, const char *s2); +char *ft_strcpy(char *dest, const char *src); +void ft_strdel(char **as); +char *ft_strdup(const char *src); +int ft_strequ(char const *s1, char const *s2); +void ft_striter(char *s, void (*f)(char*)); +void ft_striteri(char *s, void (*f)(unsigned int, char*)); +char *ft_strjoin(char const *s1, char const *s2); +size_t ft_strlcat(char *dest, const char *src, size_t n); +size_t ft_strlen(const char *str); +char *ft_strmap(char const *s, char (*f)(char)); +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); +char *ft_strncat(char *dest, const char *src, size_t n); +int ft_strncmp(const char *s1, const char *s2, size_t n); +char *ft_strncpy(char *dest, const char *src, size_t n); +int ft_strnequ(char const *s1, char const *s2, size_t n); +char *ft_strnew(size_t size); +char *ft_strnstr(const char *s1, const char *s2, size_t n); +char *ft_strrchr(const char *s, int c); +char **ft_strsplit(char const *s, char c); +char *ft_strstr(const char *str, const char *to_find); +char *ft_strsub(char const *s, unsigned int start, size_t len); +char *ft_strtrim(char const *s); +int ft_tolower(int c); +char *ft_itoa_base_int(unsigned int nb, int base); +char *ft_itoa_base_short(unsigned short nb, int base); +int ft_toupper(int c); +char *ft_strextend(char *s1, char const *s2); +char *ft_strnextend(char *s1, char const *s2, size_t len); +char **ft_sstrnew(size_t row, size_t col); +char **ft_sstrcpy(char **dest, char const **src); +char **ft_memtab(size_t y, size_t x); +int **ft_memint_tab(size_t y, size_t x); +void ft_sstrprint(char **str); +int *ft_bubble_sort(int *tab, int size); +t_list *ft_lstnew(void const *content, size_t content_size); +void ft_lstdelone(t_list **alst, void (*del)(void*, size_t)); +void ft_lstdel(t_list **alst, void (*del)(void*, size_t)); +void ft_lstadd(t_list **alst, t_list *new); +int ft_lstadd_end(t_list **alst, t_list *elem); +void ft_lstiter(t_list *lst, void (*f)(t_list *elem)); +void ft_lstprint_str(t_list *lst); +t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)); +int ft_sstrdel(char **str, int size); +int get_next_line(const int fd, char **line, char **tmp); +unsigned long long ft_power(unsigned long long nb, unsigned long long power); +char *ft_char_replace(char *str, char c, char r); +char *ft_strupcase(char *str); +char *ft_strnchr(const char *s, int c, int n); +char *ft_strrnchr(const char *s, int c, int n); +char *ft_strndup(const char *src, size_t n); +char *ft_itoa_base(unsigned long long n, int base); +char *ft_itoa_double(t_conv_spec conv_spec, long double nb); +char *ft_strrev(char *str); +char *ft_data_conv_id(void); +int ft_store_conv_id(t_conv_spec *c_s, char *format, int i); +void ft_store_modifier(t_conv_spec *c_s, char *f, int i, int l); +void ft_store_flag(t_conv_spec *c_s, char *format, int i, int l); +void ft_store_wc_field_width(t_conv_spec *c_s, va_list *ap); +void ft_store_wc_precision(t_conv_spec *c_s, va_list *ap); +void ft_store_field_width + (t_conv_spec *c_s, char *format, int i, int len); +void ft_store_precision(t_conv_spec *c_s, char *f, int i, int l); +void ft_struct_init(t_conv_spec *c_s); +void ft_struct_del(t_conv_spec *c_s); +char *ft_data_conv_ids(void); +char *ft_process_c(t_conv_spec c_s, va_list *ap); +char *ft_process_s(t_conv_spec c_s, va_list *ap); +char *ft_process_p(t_conv_spec c_s, va_list *ap); +char *ft_process_di(t_conv_spec c_s, va_list *ap); +char *ft_process_o(t_conv_spec c_s, va_list *ap); +char *ft_process_u(t_conv_spec c_s, va_list *ap); +char *ft_process_x(t_conv_spec c_s, va_list *ap); +char *ft_process_xx(t_conv_spec c_s, va_list *ap); +char *ft_process_f(t_conv_spec c_s, va_list *ap); +char *ft_process_b(t_conv_spec c_s, va_list *ap); +char *ft_process_dd(t_conv_spec c_s, va_list *ap); +char *ft_process_percent(t_conv_spec c_s, va_list *ap); +char *ft_process_id(t_conv_spec c_s, va_list *ap); +char *ft_process_flags(t_conv_spec c_s, char *str); +char *ft_process_hash(t_conv_spec c_s, char *str); +char *ft_hash_xx_preci(char *str, int i); +char *ft_hash_xx_zero(char *str, int i); +char *ft_hash_x_preci(char *str, int i); +char *ft_hash_x_zero(char *str, int i); +char *ft_process_plus(t_conv_spec c_s, char *str); +char *ft_process_zero(t_conv_spec c_s, char *str); +char *ft_process_minus(t_conv_spec c_s, char *str); +char *ft_process_space(t_conv_spec c_s, char *str); +char *ft_process_min_width(t_conv_spec c_s, char *str); +char *ft_precision_diouxx(t_conv_spec c_s, char *str); +char *ft_precision_s(t_conv_spec c_s, char *str); +char *ft_precision_p(t_conv_spec c_s, char *str); +char *ft_process_preci(t_conv_spec c_s, char *str); + +int ft_atois(const char *str, int *nb); +int ft_isnumb(char *str); +int ft_mod(unsigned int val, unsigned int div); +int ft_iswhitespace(char c); + +#endif diff --git a/libft/libft/ft_atoi.c b/libft/libft/ft_atoi.c new file mode 100644 index 0000000..9f33265 --- /dev/null +++ b/libft/libft/ft_atoi.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/05 14:46:25 by jfleury #+# #+# */ +/* Updated: 2019/03/07 13:38:37 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_atoi(const char *str) +{ + int number; + int c; + int signe; + + number = 0; + c = 0; + signe = 0; + while (str[c] != '\0' && (str[c] == '\t' || str[c] == '\n' + || str[c] == '\v' || str[c] == '\f' || str[c] == '\r' || str[c] == ' ')) + c++; + if (str[c] == '-' || str[c] == '+') + { + if (str[c] == '-') + signe = 1; + c++; + } + while (str[c] != '\0' && (str[c] >= '0' && str[c] <= '9')) + { + number = ((number * 10) + (str[c] - 48)); + c++; + } + if (signe == 1) + number = -number; + return (number); +} diff --git a/libft/libft/ft_atoi_bin.c b/libft/libft/ft_atoi_bin.c new file mode 100644 index 0000000..7dc3963 --- /dev/null +++ b/libft/libft/ft_atoi_bin.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi_bin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/01 15:48:50 by jfleury #+# #+# */ +/* Updated: 2019/07/04 12:07:25 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_atoi_bin(char *str) +{ + int j; + int bin; + int result; + + j = ft_strlen(str) - 1; + result = 0; + bin = 1; + while (j >= 0) + { + if (str[j] == '1') + result = result + bin; + bin = bin * 2; + j--; + } + return (result); +} diff --git a/libft/libft/ft_atoi_long.c b/libft/libft/ft_atoi_long.c new file mode 100644 index 0000000..f668ab0 --- /dev/null +++ b/libft/libft/ft_atoi_long.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi_long.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/03/04 17:51:43 by jfleury #+# #+# */ +/* Updated: 2019/03/05 19:48:52 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +long long ft_atoi_long(const char *str) +{ + long long number; + long long c; + long long signe; + + number = 0; + c = 0; + signe = 0; + while (str[c] != '\0' && (str[c] == '\t' || str[c] == '\n' + || str[c] == '\v' || str[c] == '\f' || str[c] == '\r' || str[c] == ' ')) + c++; + if (str[c] == '-' || str[c] == '+') + { + if (str[c] == '-') + signe = 1; + c++; + } + while (str[c] != '\0' && (str[c] >= '0' && str[c] <= '9')) + { + number = ((number * 10) + (str[c] - 48)); + c++; + } + if (signe == 1) + number = -number; + return (number); +} diff --git a/libft/libft/ft_atois.c b/libft/libft/ft_atois.c new file mode 100644 index 0000000..7a6e1b9 --- /dev/null +++ b/libft/libft/ft_atois.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atois.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/27 11:58:44 by tmaze #+# #+# */ +/* Updated: 2019/06/19 15:23:42 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_atois(const char *str, int *nb) +{ + int num; + + num = 0; + while (ft_iswhitespace(str[num])) + num++; + if (str[num] == '+' || str[num] == '-') + num++; + if (ft_strlen(&(str[num])) < 10 || (num > 0 && str[num - 1] == '-' + && ft_strcmp("2147483648", &(str[num])) >= 0) + || ft_strcmp("2147483647", &(str[num])) >= 0) + { + *nb = ft_atoi(str); + return (0); + } + return (1); +} diff --git a/libft/libft/ft_bubble_sort.c b/libft/libft/ft_bubble_sort.c new file mode 100644 index 0000000..68ec505 --- /dev/null +++ b/libft/libft/ft_bubble_sort.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_bubble_sort.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/28 13:37:01 by jfleury #+# #+# */ +/* Updated: 2019/02/28 13:41:41 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int *ft_bubble_sort(int *tab, int size) +{ + int temp; + int c; + int check; + + c = 0; + check = 0; + while (check == 0) + { + check = 1; + while (c < size - 1) + { + if (tab[c] > tab[c + 1]) + { + temp = tab[c]; + tab[c] = tab[c + 1]; + tab[c + 1] = temp; + check = 0; + } + c++; + } + c = 0; + } + return (tab); +} diff --git a/libft/libft/ft_bzero.c b/libft/libft/ft_bzero.c new file mode 100644 index 0000000..3d305e4 --- /dev/null +++ b/libft/libft/ft_bzero.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_bzero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/05 17:45:47 by jfleury #+# #+# */ +/* Updated: 2018/11/13 18:25:51 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +void ft_bzero(void *s, size_t n) +{ + ft_memset(s, 0, n); +} diff --git a/libft/libft/ft_char_replace.c b/libft/libft/ft_char_replace.c new file mode 100644 index 0000000..1a7e3bd --- /dev/null +++ b/libft/libft/ft_char_replace.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_char_replace.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/16 18:50:02 by jfleury #+# #+# */ +/* Updated: 2019/01/16 18:54:31 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +char *ft_char_replace(char *str, char c, char r) +{ + int i; + + i = 0; + while (str[i] != 0) + { + if (str[i] == c) + str[i] = r; + i++; + } + return (str); +} diff --git a/libft/libft/ft_isalnum.c b/libft/libft/ft_isalnum.c new file mode 100644 index 0000000..8483c50 --- /dev/null +++ b/libft/libft/ft_isalnum.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/05 15:55:08 by jfleury #+# #+# */ +/* Updated: 2018/11/13 18:25:50 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isalnum(int c) +{ + if (c >= '0' && c <= '9') + return (1); + else if (c >= 'A' && c <= 'Z') + return (1); + else if (c >= 'a' && c <= 'z') + return (1); + else + return (0); +} diff --git a/libft/libft/ft_isalpha.c b/libft/libft/ft_isalpha.c new file mode 100644 index 0000000..52938d6 --- /dev/null +++ b/libft/libft/ft_isalpha.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/05 16:07:58 by jfleury #+# #+# */ +/* Updated: 2018/11/13 18:25:50 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isalpha(int c) +{ + if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) + return (1); + else + return (0); +} diff --git a/libft/libft/ft_isascii.c b/libft/libft/ft_isascii.c new file mode 100644 index 0000000..c325906 --- /dev/null +++ b/libft/libft/ft_isascii.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/05 16:19:58 by jfleury #+# #+# */ +/* Updated: 2018/11/13 18:25:49 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isascii(int c) +{ + if (c >= 0 && c <= 127) + return (1); + else + return (0); +} diff --git a/libft/libft/ft_isdigit.c b/libft/libft/ft_isdigit.c new file mode 100644 index 0000000..0c60781 --- /dev/null +++ b/libft/libft/ft_isdigit.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/05 16:22:16 by jfleury #+# #+# */ +/* Updated: 2018/11/13 18:25:48 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isdigit(int c) +{ + if (c >= '0' && c <= '9') + return (1); + else + return (0); +} diff --git a/libft/libft/ft_isnumb.c b/libft/libft/ft_isnumb.c new file mode 100644 index 0000000..3e74df2 --- /dev/null +++ b/libft/libft/ft_isnumb.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isnumb.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/06/17 15:00:23 by tmaze #+# #+# */ +/* Updated: 2019/07/12 12:09:41 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isnumb(char *str) +{ + int i; + + i = 0; + while (str[i]) + { + if ((i == 0 && str[i] != '-' && str[i] != '+' && !ft_isdigit(str[i])) + || (i != 0 && !ft_isdigit(str[i]))) + return (0); + i++; + } + return (1); +} diff --git a/libft/libft/ft_isprint.c b/libft/libft/ft_isprint.c new file mode 100644 index 0000000..2a1d1f4 --- /dev/null +++ b/libft/libft/ft_isprint.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/05 16:25:39 by jfleury #+# #+# */ +/* Updated: 2018/11/13 18:25:47 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isprint(int c) +{ + if (c >= 32 && c <= 126) + return (1); + else + return (0); +} diff --git a/libft/libft/ft_iswhitespace.c b/libft/libft/ft_iswhitespace.c new file mode 100644 index 0000000..48c7fc1 --- /dev/null +++ b/libft/libft/ft_iswhitespace.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_iswhitespace.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/25 16:11:19 by tmaze #+# #+# */ +/* Updated: 2019/02/25 16:13:25 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_iswhitespace(char c) +{ + return (c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' + || c == '\r'); +} diff --git a/libft/libft/ft_itoa.c b/libft/libft/ft_itoa.c new file mode 100644 index 0000000..0d03e25 --- /dev/null +++ b/libft/libft/ft_itoa.c @@ -0,0 +1,62 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/13 11:06:12 by jfleury #+# #+# */ +/* Updated: 2018/11/13 18:48:02 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +static int ft_isneg(int n) +{ + if (n >= 0) + return (0); + else + return (1); +} + +static int ft_nbrlen(int n) +{ + int i; + + i = 0; + while (n / 10 != 0) + { + i++; + n = n / 10; + } + i++; + return (i); +} + +char *ft_itoa(int n) +{ + char *str; + int len; + int nb; + + if (n == -2147483648) + return (ft_strdup("-2147483648")); + nb = n; + len = ft_nbrlen(n) + ft_isneg(n); + if (!(str = (char*)malloc(sizeof(char) * len + 1))) + return (NULL); + str[len] = '\0'; + if (n < 0) + n = -n; + while (len > ft_isneg(nb)) + { + len--; + str[len] = (n % 10) + 48; + n = n / 10; + } + if (ft_isneg(nb) == 1) + str[0] = '-'; + return (str); +} diff --git a/libft/libft/ft_itoa_base.c b/libft/libft/ft_itoa_base.c new file mode 100644 index 0000000..e143fb0 --- /dev/null +++ b/libft/libft/ft_itoa_base.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa_base.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/23 15:05:04 by allefebv #+# #+# */ +/* Updated: 2019/07/08 17:26:58 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include +#include + +static int ft_malloc_len(unsigned long long nb, int base) +{ + int i; + + i = 0; + while (nb != 0) + { + nb = nb / base; + i++; + } + return (i); +} + +char *ft_itoa_base(unsigned long long nb, int base) +{ + unsigned long long result; + int i; + char *str; + char *tab; + + tab = ft_strdup("0123456789abcdef"); + if (base < 2 || base > 16) + return (NULL); + i = ft_malloc_len(nb, base); + if (nb == 0) + i++; + if (!(str = (char*)malloc(sizeof(char*) * i + 1))) + return (NULL); + str[i] = '\0'; + if (nb == 0) + str[0] = '0'; + while (nb != 0) + { + i--; + result = nb % base; + str[i] = tab[result]; + nb = nb / base; + } + free(tab); + return (str); +} diff --git a/libft/libft/ft_itoa_base_int.c b/libft/libft/ft_itoa_base_int.c new file mode 100644 index 0000000..6b33315 --- /dev/null +++ b/libft/libft/ft_itoa_base_int.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa_base_int.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/23 15:05:04 by allefebv #+# #+# */ +/* Updated: 2019/07/09 15:26:17 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include +#include + +static int ft_malloc_len(unsigned int nb, int base) +{ + int i; + + i = 0; + while (nb != 0) + { + nb = nb / base; + i++; + } + return (i); +} + +char *ft_itoa_base_int(unsigned int nb, int base) +{ + unsigned int result; + int i; + char *str; + char *tab; + + tab = ft_strdup("0123456789abcdef"); + if (base < 2 || base > 16) + return (NULL); + i = ft_malloc_len(nb, base); + if (nb == 0) + i++; + if (!(str = (char*)malloc(sizeof(char*) * i + 1))) + return (NULL); + str[i] = '\0'; + if (nb == 0) + str[0] = '0'; + while (nb != 0) + { + i--; + result = nb % base; + str[i] = tab[result]; + nb = nb / base; + } + free(tab); + return (str); +} diff --git a/libft/libft/ft_itoa_base_short.c b/libft/libft/ft_itoa_base_short.c new file mode 100644 index 0000000..bf4f963 --- /dev/null +++ b/libft/libft/ft_itoa_base_short.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa_base_short.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/23 15:05:04 by allefebv #+# #+# */ +/* Updated: 2019/07/13 17:27:38 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include +#include + +static int ft_malloc_len(unsigned short nb, int base) +{ + short i; + + i = 0; + while (nb != 0) + { + nb = nb / base; + i++; + } + return (i); +} + +char *ft_itoa_base_short(unsigned short nb, int base) +{ + unsigned short result; + short i; + char *str; + char *tab; + + tab = ft_strdup("0123456789abcdef"); + if (base < 2 || base > 16) + return (NULL); + i = ft_malloc_len(nb, base); + if (nb == 0) + i++; + if (!(str = (char*)malloc(sizeof(char*) * i + 1))) + return (NULL); + str[i] = '\0'; + if (nb == 0) + str[0] = '0'; + while (nb != 0) + { + i--; + result = nb % base; + str[i] = tab[result]; + nb = nb / base; + } + free(tab); + return (str); +} diff --git a/libft/libft/ft_itoa_double.c b/libft/libft/ft_itoa_double.c new file mode 100644 index 0000000..2edef09 --- /dev/null +++ b/libft/libft/ft_itoa_double.c @@ -0,0 +1,88 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa_double.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/21 16:36:44 by jfleury #+# #+# */ +/* Updated: 2019/04/02 12:07:30 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static char *ft_zero(t_conv_spec conv_spec, char *str) +{ + int i; + + i = 2; + str = ft_strnew(conv_spec.precision + 2); + str[0] = '0'; + str[1] = '.'; + while (conv_spec.precision > 0) + { + str[i] = '0'; + conv_spec.precision--; + i++; + } + return (str); +} + +static char *ft_place_point(t_conv_spec conv_spec, char *str) +{ + char *final_str; + int i; + + if ((i = ft_strlen(str) - conv_spec.precision) <= 0) + { + free(str); + return (ft_strdup("(error)")); + } + final_str = ft_strnew(ft_strlen(str) + 1); + ft_strncpy(final_str, str, i); + final_str[i] = '.'; + ft_strcat(final_str, str + i); + free(str); + return (final_str); +} + +static char *ft_process(t_conv_spec conv_spec, char *str, int flag, + long double nb) +{ + long long result; + unsigned long long power; + + if (conv_spec.precision > 16) + conv_spec.precision = 16; + power = ft_power(10, conv_spec.precision); + result = nb * power; + if (((nb * power * 10) - (result * 10)) > 5) + result++; + if (flag == 1) + result = -result; + str = ft_itoa(result); + str = ft_place_point(conv_spec, str); + return (str); +} + +char *ft_itoa_double(t_conv_spec conv_spec, long double nb) +{ + char *str; + int flag; + + flag = 0; + str = NULL; + if (conv_spec.precision == -1) + conv_spec.precision = 6; + if (nb < 0) + { + flag = 1; + nb = -nb; + } + if (nb == 0) + str = ft_zero(conv_spec, str); + else + str = ft_process(conv_spec, str, flag, nb); + return (str); +} diff --git a/libft/libft/ft_lstadd.c b/libft/libft/ft_lstadd.c new file mode 100644 index 0000000..047d8bd --- /dev/null +++ b/libft/libft/ft_lstadd.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 16:31:08 by jfleury #+# #+# */ +/* Updated: 2019/05/15 19:49:23 by allefebv ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd(t_list **alst, t_list *new) +{ + if (new == NULL) + return ; + new->next = *alst; + *alst = new; +} diff --git a/libft/libft/ft_lstadd_end.c b/libft/libft/ft_lstadd_end.c new file mode 100644 index 0000000..398b646 --- /dev/null +++ b/libft/libft/ft_lstadd_end.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_end.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: allefebv +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/15 17:26:04 by allefebv #+# #+# */ +/* Updated: 2019/05/15 19:49:43 by allefebv ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_lstadd_end(t_list **alst, t_list *elem) +{ + t_list *end; + + if (elem == NULL) + return (0); + if (*alst == NULL) + ft_lstadd(alst, elem); + else + { + end = *alst; + while (end->next != NULL) + end = end->next; + end->next = elem; + elem->next = NULL; + } + return (1); +} diff --git a/libft/libft/ft_lstdel.c b/libft/libft/ft_lstdel.c new file mode 100644 index 0000000..3b49bf6 --- /dev/null +++ b/libft/libft/ft_lstdel.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 15:54:50 by jfleury #+# #+# */ +/* Updated: 2018/11/15 18:31:58 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +void ft_lstdel(t_list **alst, void (*del)(void*, size_t)) +{ + t_list *temp_alst; + t_list *temp_next; + + temp_alst = *alst; + while (temp_alst) + { + temp_next = temp_alst->next; + del(temp_alst->content, temp_alst->content_size); + free(temp_alst); + temp_alst = temp_next; + } + *alst = NULL; +} diff --git a/libft/libft/ft_lstdelone.c b/libft/libft/ft_lstdelone.c new file mode 100644 index 0000000..b14a46e --- /dev/null +++ b/libft/libft/ft_lstdelone.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 14:59:45 by jfleury #+# #+# */ +/* Updated: 2018/11/14 16:02:18 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include +#include + +void ft_lstdelone(t_list **alst, void (*del)(void*, size_t)) +{ + del((*alst)->content, (*alst)->content_size); + free(*alst); + *alst = NULL; +} diff --git a/libft/libft/ft_lstiter.c b/libft/libft/ft_lstiter.c new file mode 100644 index 0000000..035352e --- /dev/null +++ b/libft/libft/ft_lstiter.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstiter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 17:11:41 by jfleury #+# #+# */ +/* Updated: 2018/11/16 14:37:53 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstiter(t_list *lst, void (*f)(t_list *elem)) +{ + t_list *temp_lst; + t_list *temp_next; + + temp_lst = lst; + while (temp_lst) + { + temp_next = temp_lst->next; + f(temp_lst); + temp_lst = temp_next; + } +} diff --git a/libft/libft/ft_lstmap.c b/libft/libft/ft_lstmap.c new file mode 100644 index 0000000..0e1f2a2 --- /dev/null +++ b/libft/libft/ft_lstmap.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/14 18:20:50 by jfleury #+# #+# */ +/* Updated: 2018/11/15 18:30:30 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)) +{ + t_list *final_lst; + t_list *temp_lst; + t_list *new; + + if (lst == NULL || f == NULL) + return (NULL); + temp_lst = f(lst); + if (!(new = ft_lstnew(temp_lst->content, temp_lst->content_size))) + return (NULL); + final_lst = new; + lst = lst->next; + while (lst) + { + temp_lst = f(lst); + if (!(new->next = ft_lstnew(temp_lst->content, temp_lst->content_size))) + return (NULL); + new = new->next; + lst = lst->next; + } + return (final_lst); +} diff --git a/libft/libft/ft_lstnew.c b/libft/libft/ft_lstnew.c new file mode 100644 index 0000000..87fefbf --- /dev/null +++ b/libft/libft/ft_lstnew.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/03/12 13:54:39 by jfleury #+# #+# */ +/* Updated: 2019/04/23 14:13:31 by allefebv ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +t_list *ft_lstnew(void const *content, size_t content_size) +{ + t_list *newlst; + + if (!(newlst = (t_list*)malloc(sizeof(t_list)))) + return (NULL); + if (content == NULL) + { + newlst->content = NULL; + newlst->content_size = 0; + } + else + { + if (!(newlst->content = (void*)malloc(sizeof(content) * content_size))) + return (NULL); + ft_memcpy((newlst->content), content, content_size); + newlst->content_size = content_size; + } + newlst->next = NULL; + return (newlst); +} diff --git a/libft/libft/ft_lstprint_str.c b/libft/libft/ft_lstprint_str.c new file mode 100644 index 0000000..5cb0221 --- /dev/null +++ b/libft/libft/ft_lstprint_str.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstprint_char.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: allefebv +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/02/02 12:18:10 by allefebv #+# #+# */ +/* Updated: 2019/04/23 13:53:16 by allefebv ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstprint_str(t_list *lst) +{ + while (lst != NULL) + { + ft_printf("%s\n", (char*)(lst->content)); + lst = lst->next; + } +} diff --git a/libft/libft/ft_memalloc.c b/libft/libft/ft_memalloc.c new file mode 100644 index 0000000..ba57848 --- /dev/null +++ b/libft/libft/ft_memalloc.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memalloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/13 09:59:49 by jfleury #+# #+# */ +/* Updated: 2018/11/13 18:41:04 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +void *ft_memalloc(size_t size) +{ + void *temp; + + if (!(temp = (void*)malloc(size))) + return (NULL); + ft_memset(temp, 0, size); + return (temp); +} diff --git a/libft/libft/ft_memccpy.c b/libft/libft/ft_memccpy.c new file mode 100644 index 0000000..6eb72d3 --- /dev/null +++ b/libft/libft/ft_memccpy.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memccpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/08 11:09:19 by jfleury #+# #+# */ +/* Updated: 2018/11/13 18:00:31 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void *ft_memccpy(void *dest, const void *src, int c, size_t n) +{ + char *temp_dest; + char *temp_src; + int i; + + temp_dest = (char*)dest; + temp_src = (char*)src; + i = 0; + while (n > 0) + { + temp_dest[i] = temp_src[i]; + if (temp_src[i] == (char)c) + return (dest + i + 1); + i++; + n--; + } + return (NULL); +} diff --git a/libft/libft/ft_memchr.c b/libft/libft/ft_memchr.c new file mode 100644 index 0000000..600dc70 --- /dev/null +++ b/libft/libft/ft_memchr.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/08 11:09:29 by jfleury #+# #+# */ +/* Updated: 2018/11/13 18:00:43 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void *ft_memchr(const void *s, int c, size_t n) +{ + unsigned char *temp_s; + unsigned char temp_c; + int i; + + temp_s = (unsigned char*)s; + temp_c = (unsigned char)c; + i = 0; + while (n > 0) + { + if (temp_s[i] == temp_c) + return (temp_s + i); + i++; + n--; + } + return (NULL); +} diff --git a/libft/libft/ft_memcmp.c b/libft/libft/ft_memcmp.c new file mode 100644 index 0000000..0893b1d --- /dev/null +++ b/libft/libft/ft_memcmp.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/08 11:09:06 by jfleury #+# #+# */ +/* Updated: 2019/03/02 12:55:38 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +int ft_memcmp(const void *s1, const void *s2, size_t n) +{ + unsigned char *temp_s1; + unsigned char *temp_s2; + int i; + + temp_s1 = (unsigned char*)s1; + temp_s2 = (unsigned char*)s2; + i = 0; + while (n > 0) + { + if (temp_s1[i] != temp_s2[i]) + return (temp_s1[i] - temp_s2[i]); + else + { + i++; + n--; + } + } + return (0); +} diff --git a/libft/libft/ft_memcpy.c b/libft/libft/ft_memcpy.c new file mode 100644 index 0000000..f396ea4 --- /dev/null +++ b/libft/libft/ft_memcpy.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/08 11:11:11 by jfleury #+# #+# */ +/* Updated: 2018/11/13 18:25:47 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void *ft_memcpy(void *dest, const void *src, size_t n) +{ + char *temp_dest; + char *temp_src; + int i; + + temp_dest = (char*)dest; + temp_src = (char*)src; + i = 0; + while (n > 0) + { + temp_dest[i] = temp_src[i]; + i++; + n--; + } + return (dest); +} diff --git a/libft/libft/ft_memdel.c b/libft/libft/ft_memdel.c new file mode 100644 index 0000000..0afac50 --- /dev/null +++ b/libft/libft/ft_memdel.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/13 09:59:57 by jfleury #+# #+# */ +/* Updated: 2018/11/13 20:49:47 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void ft_memdel(void **ap) +{ + if (ap != NULL && *ap != NULL) + { + free(*ap); + *ap = NULL; + } +} diff --git a/libft/libft/ft_memint_tab.c b/libft/libft/ft_memint_tab.c new file mode 100644 index 0000000..4ec5ebf --- /dev/null +++ b/libft/libft/ft_memint_tab.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memint_tab.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/03/12 13:54:49 by jfleury #+# #+# */ +/* Updated: 2019/03/12 13:54:54 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +int **ft_memint_tab(size_t y, size_t x) +{ + int **tab; + size_t i; + + i = 0; + if (!(tab = (int**)ft_memalloc(sizeof(int*) * (y)))) + return (NULL); + while (i < y) + { + if (!(tab[i] = (int*)ft_memalloc(sizeof(int) * x))) + { + i++; + while (i > 0) + { + free(tab[i - 2]); + i--; + } + free(tab); + return (NULL); + } + i++; + } + return (tab); +} diff --git a/libft/libft/ft_memmove.c b/libft/libft/ft_memmove.c new file mode 100644 index 0000000..0086de6 --- /dev/null +++ b/libft/libft/ft_memmove.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memmove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/13 09:55:12 by jfleury #+# #+# */ +/* Updated: 2018/11/13 18:45:16 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +void *ft_memmove(void *dest, const void *src, size_t n) +{ + unsigned char *temp_dest; + unsigned char *temp_src; + size_t i; + + temp_dest = (unsigned char*)dest; + temp_src = (unsigned char*)src; + i = n; + if (temp_dest > temp_src) + while (n) + { + temp_dest[n - 1] = temp_src[n - 1]; + n--; + } + else + ft_memcpy(temp_dest, temp_src, i); + return (dest); +} diff --git a/libft/libft/ft_memset.c b/libft/libft/ft_memset.c new file mode 100644 index 0000000..1b522cc --- /dev/null +++ b/libft/libft/ft_memset.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/06 15:17:24 by jfleury #+# #+# */ +/* Updated: 2018/11/13 18:25:45 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void *ft_memset(void *s, int c, size_t n) +{ + unsigned char *temp; + int i; + + i = 0; + temp = (unsigned char*)s; + while (n > 0) + { + temp[i] = c; + i++; + n--; + } + return (s); +} diff --git a/libft/libft/ft_memtab.c b/libft/libft/ft_memtab.c new file mode 100644 index 0000000..5f8d96e --- /dev/null +++ b/libft/libft/ft_memtab.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memtab.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/03/12 13:55:02 by jfleury #+# #+# */ +/* Updated: 2019/03/12 13:55:07 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +char **ft_memtab(size_t y, size_t x) +{ + char **tab; + size_t i; + + i = 0; + if (!(tab = (char**)ft_memalloc(sizeof(char*) * (y + 1)))) + return (NULL); + while (i < y) + { + if (!(tab[i] = ft_strnew(x))) + { + i++; + while (i > 0) + { + free(tab[i - 1]); + i--; + } + free(tab); + return (NULL); + } + i++; + } + tab[y] = 0; + return (tab); +} diff --git a/libft/libft/ft_mod.c b/libft/libft/ft_mod.c new file mode 100644 index 0000000..3d87bfd --- /dev/null +++ b/libft/libft/ft_mod.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_mod.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/06/21 00:51:09 by tmaze #+# #+# */ +/* Updated: 2019/06/24 15:27:43 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_mod(unsigned int val, unsigned int div) +{ + int r; + + r = val % div; + return ((r < 0) ? r + div : r); +} diff --git a/libft/libft/ft_power.c b/libft/libft/ft_power.c new file mode 100644 index 0000000..63cc162 --- /dev/null +++ b/libft/libft/ft_power.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_power.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/21 17:34:08 by jfleury #+# #+# */ +/* Updated: 2019/01/23 15:07:35 by allefebv ### ########.fr */ +/* */ +/* ************************************************************************** */ + +unsigned long long ft_power(unsigned long long nb, int power) +{ + if (power == 0) + return (1); + if (power == 1) + return (nb); + if (power > 0) + return (nb * ft_power(nb, power - 1)); + else + return (0); +} diff --git a/libft/libft/ft_putchar.c b/libft/libft/ft_putchar.c new file mode 100644 index 0000000..c65269c --- /dev/null +++ b/libft/libft/ft_putchar.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/05 14:46:36 by jfleury #+# #+# */ +/* Updated: 2018/11/13 18:57:28 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void ft_putchar(char c) +{ + write(1, &c, 1); +} diff --git a/libft/libft/ft_putchar_fd.c b/libft/libft/ft_putchar_fd.c new file mode 100644 index 0000000..da60c4e --- /dev/null +++ b/libft/libft/ft_putchar_fd.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/13 10:03:58 by jfleury #+# #+# */ +/* Updated: 2018/11/13 18:49:12 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void ft_putchar_fd(char c, int fd) +{ + write(fd, &c, 1); +} diff --git a/libft/libft/ft_putendl.c b/libft/libft/ft_putendl.c new file mode 100644 index 0000000..7c1f656 --- /dev/null +++ b/libft/libft/ft_putendl.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/13 10:03:39 by jfleury #+# #+# */ +/* Updated: 2018/11/13 18:25:43 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putendl(char const *s) +{ + ft_putstr(s); + ft_putchar('\n'); +} diff --git a/libft/libft/ft_putendl_fd.c b/libft/libft/ft_putendl_fd.c new file mode 100644 index 0000000..4898597 --- /dev/null +++ b/libft/libft/ft_putendl_fd.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/13 10:03:45 by jfleury #+# #+# */ +/* Updated: 2018/11/13 18:51:53 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putendl_fd(char const *s, int fd) +{ + ft_putstr_fd(s, fd); + ft_putchar_fd('\n', fd); +} diff --git a/libft/libft/ft_putnbr.c b/libft/libft/ft_putnbr.c new file mode 100644 index 0000000..c05e10a --- /dev/null +++ b/libft/libft/ft_putnbr.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/05 14:47:14 by jfleury #+# #+# */ +/* Updated: 2018/11/13 18:25:42 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putnbr(int n) +{ + if (n == -2147483648) + { + ft_putchar('-'); + ft_putchar('2'); + n = 147483648; + } + if (n < 0) + { + ft_putchar('-'); + n = -n; + } + if (n < 10) + ft_putchar(n + 48); + if (n > 9) + { + ft_putnbr(n / 10); + ft_putnbr(n % 10); + } +} diff --git a/libft/libft/ft_putnbr_fd.c b/libft/libft/ft_putnbr_fd.c new file mode 100644 index 0000000..0296891 --- /dev/null +++ b/libft/libft/ft_putnbr_fd.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/05 14:47:14 by jfleury #+# #+# */ +/* Updated: 2018/11/06 09:47:31 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putnbr_fd(int n, int fd) +{ + if (n == -2147483648) + { + ft_putchar_fd('-', fd); + ft_putchar_fd('2', fd); + n = 147483648; + } + if (n < 0) + { + ft_putchar_fd('-', fd); + n = -n; + } + if (n < 10) + ft_putchar_fd(n + 48, fd); + if (n > 9) + { + ft_putnbr_fd(n / 10, fd); + ft_putnbr_fd(n % 10, fd); + } +} diff --git a/libft/libft/ft_putstr.c b/libft/libft/ft_putstr.c new file mode 100644 index 0000000..791dd2f --- /dev/null +++ b/libft/libft/ft_putstr.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/05 14:47:33 by jfleury #+# #+# */ +/* Updated: 2018/11/13 20:26:16 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putstr(char const *s) +{ + int i; + + i = 0; + if (s != NULL) + { + while (s[i] != 0) + { + ft_putchar(s[i]); + i++; + } + } +} diff --git a/libft/libft/ft_putstr_fd.c b/libft/libft/ft_putstr_fd.c new file mode 100644 index 0000000..9e7fb51 --- /dev/null +++ b/libft/libft/ft_putstr_fd.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/13 10:04:05 by jfleury #+# #+# */ +/* Updated: 2018/11/13 20:26:36 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +void ft_putstr_fd(char const *s, int fd) +{ + size_t i; + + i = 0; + if (s != NULL) + { + while (s[i] != '\0') + { + ft_putchar_fd(s[i], fd); + i++; + } + } +} diff --git a/libft/libft/ft_sstrcpy.c b/libft/libft/ft_sstrcpy.c new file mode 100644 index 0000000..84110d4 --- /dev/null +++ b/libft/libft/ft_sstrcpy.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sstrcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/03/12 13:55:16 by jfleury #+# #+# */ +/* Updated: 2019/03/12 13:55:19 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +char **ft_sstrcpy(char **dest, char const **src) +{ + while (*src != 0 && *dest != 0) + { + ft_strcpy(*dest, *src); + src++; + dest++; + } + return (dest); +} diff --git a/libft/libft/ft_sstrdel.c b/libft/libft/ft_sstrdel.c new file mode 100644 index 0000000..b2defba --- /dev/null +++ b/libft/libft/ft_sstrdel.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sstrdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/03/12 13:50:51 by jfleury #+# #+# */ +/* Updated: 2019/03/12 13:50:53 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +int ft_sstrdel(char **str, int size) +{ + while (size >= 0) + { + free(str[size]); + size--; + } + free(str); + str = NULL; + return (0); +} diff --git a/libft/libft/ft_sstrnew.c b/libft/libft/ft_sstrnew.c new file mode 100644 index 0000000..71f0be8 --- /dev/null +++ b/libft/libft/ft_sstrnew.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sstrnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/03/12 13:51:04 by jfleury #+# #+# */ +/* Updated: 2019/03/12 13:51:06 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char **ft_sstrnew(size_t y, size_t x) +{ + char **str; + size_t i; + + i = 0; + if (!(str = (char**)ft_memalloc(y + 1))) + return (NULL); + while (i < y) + { + str[i] = ft_strnew(x); + i++; + } + str[y] = 0; + return (str); +} diff --git a/libft/libft/ft_sstrprint.c b/libft/libft/ft_sstrprint.c new file mode 100644 index 0000000..8f499ff --- /dev/null +++ b/libft/libft/ft_sstrprint.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sstrprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/03/12 13:51:14 by jfleury #+# #+# */ +/* Updated: 2019/03/12 13:51:16 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_sstrprint(char **str) +{ + size_t i; + + i = 0; + while (str[i] != 0) + { + ft_putstr(str[i]); + ft_putchar('\n'); + i++; + } +} diff --git a/libft/libft/ft_strcat.c b/libft/libft/ft_strcat.c new file mode 100644 index 0000000..5d2e96d --- /dev/null +++ b/libft/libft/ft_strcat.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/05 14:47:58 by jfleury #+# #+# */ +/* Updated: 2018/11/13 18:46:03 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strcat(char *dest, const char *src) +{ + int i; + int j; + + j = 0; + i = ft_strlen(dest); + while (src[j] != '\0') + { + dest[i] = src[j]; + i++; + j++; + } + dest[i] = '\0'; + return (dest); +} diff --git a/libft/libft/ft_strchr.c b/libft/libft/ft_strchr.c new file mode 100644 index 0000000..ccd06cf --- /dev/null +++ b/libft/libft/ft_strchr.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/08 10:47:52 by jfleury #+# #+# */ +/* Updated: 2019/06/07 13:49:03 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +char *ft_strchr(const char *s, int c) +{ + while (*s != (char)c && *s != '\0') + s++; + if (*s == (char)c) + return ((char *)s); + return (NULL); +} diff --git a/libft/libft/ft_strclr.c b/libft/libft/ft_strclr.c new file mode 100644 index 0000000..b986307 --- /dev/null +++ b/libft/libft/ft_strclr.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strclr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/13 10:00:22 by jfleury #+# #+# */ +/* Updated: 2018/11/14 12:24:47 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_strclr(char *s) +{ + size_t i; + + if (s != NULL) + { + i = ft_strlen(s); + ft_bzero(s, i); + } +} diff --git a/libft/libft/ft_strcmp.c b/libft/libft/ft_strcmp.c new file mode 100644 index 0000000..4a47f79 --- /dev/null +++ b/libft/libft/ft_strcmp.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/05 14:48:13 by jfleury #+# #+# */ +/* Updated: 2019/03/04 14:38:51 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strcmp(const char *s1, const char *s2) +{ + size_t i; + size_t j; + + i = ft_strlen(s1); + j = ft_strlen(s2); + if (i > j) + return (ft_memcmp(s1, s2, i)); + else + return (ft_memcmp(s1, s2, j)); +} diff --git a/libft/libft/ft_strcpy.c b/libft/libft/ft_strcpy.c new file mode 100644 index 0000000..79316d7 --- /dev/null +++ b/libft/libft/ft_strcpy.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/05 14:48:33 by jfleury #+# #+# */ +/* Updated: 2018/11/13 18:19:06 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +char *ft_strcpy(char *dest, const char *src) +{ + unsigned int i; + + i = 0; + while (src[i] != '\0') + { + dest[i] = src[i]; + i++; + } + dest[i] = '\0'; + return (dest); +} diff --git a/libft/libft/ft_strdel.c b/libft/libft/ft_strdel.c new file mode 100644 index 0000000..1dbdcdd --- /dev/null +++ b/libft/libft/ft_strdel.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdel.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/13 10:07:18 by jfleury #+# #+# */ +/* Updated: 2019/05/23 12:22:31 by allefebv ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +void ft_strdel(char **as) +{ + if (as != NULL && *as != NULL) + { + free(*as); + *as = NULL; + } +} diff --git a/libft/libft/ft_strdup.c b/libft/libft/ft_strdup.c new file mode 100644 index 0000000..e3d3ab5 --- /dev/null +++ b/libft/libft/ft_strdup.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/05 14:48:56 by jfleury #+# #+# */ +/* Updated: 2018/11/13 18:45:44 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +char *ft_strdup(const char *src) +{ + char *str; + int i; + int j; + + i = 0; + j = 0; + while (src[j] != '\0') + j++; + str = (char*)malloc(sizeof(*str) * (j + 1)); + if (str == NULL) + return (NULL); + while (src[i] != '\0') + { + str[i] = src[i]; + i++; + } + str[i] = '\0'; + return (str); +} diff --git a/libft/libft/ft_strequ.c b/libft/libft/ft_strequ.c new file mode 100644 index 0000000..072e381 --- /dev/null +++ b/libft/libft/ft_strequ.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strequ.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/13 10:01:11 by jfleury #+# #+# */ +/* Updated: 2019/03/02 12:56:54 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strequ(char const *s1, char const *s2) +{ + if (!(s1) || !(s2)) + return (0); + if (!(ft_strcmp(s1, s2))) + return (1); + return (0); +} diff --git a/libft/libft/ft_strextend.c b/libft/libft/ft_strextend.c new file mode 100644 index 0000000..e476714 --- /dev/null +++ b/libft/libft/ft_strextend.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strextend.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/21 11:15:30 by jfleury #+# #+# */ +/* Updated: 2018/11/21 11:41:42 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +char *ft_strextend(char *s1, char const *s2) +{ + char *str; + int i; + + if (s1 == NULL || s2 == NULL) + return (NULL); + i = ft_strlen(s1) + ft_strlen(s2); + str = s1; + if (!(s1 = (char*)malloc(sizeof(s1) * i + 1))) + return (NULL); + ft_strcpy(s1, str); + ft_strcat(s1, s2); + free(str); + return (s1); +} diff --git a/libft/libft/ft_striter.c b/libft/libft/ft_striter.c new file mode 100644 index 0000000..41ad797 --- /dev/null +++ b/libft/libft/ft_striter.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/13 10:00:32 by jfleury #+# #+# */ +/* Updated: 2018/11/15 17:25:51 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void ft_striter(char *s, void (*f)(char*)) +{ + if (s != NULL && f != NULL) + { + while (*s != '\0') + { + f(s); + s++; + } + } +} diff --git a/libft/libft/ft_striteri.c b/libft/libft/ft_striteri.c new file mode 100644 index 0000000..a2ff533 --- /dev/null +++ b/libft/libft/ft_striteri.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striteri.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/13 10:00:37 by jfleury #+# #+# */ +/* Updated: 2018/11/15 17:26:01 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void ft_striteri(char *s, void (*f)(unsigned int, char*)) +{ + unsigned int i; + + i = 0; + if (s != NULL && f != NULL) + { + while (*s != '\0') + { + f(i, s); + i++; + s++; + } + } +} diff --git a/libft/libft/ft_strjoin.c b/libft/libft/ft_strjoin.c new file mode 100644 index 0000000..a631fe7 --- /dev/null +++ b/libft/libft/ft_strjoin.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/13 10:01:42 by jfleury #+# #+# */ +/* Updated: 2018/11/13 18:48:27 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "libft.h" + +char *ft_strjoin(char const *s1, char const *s2) +{ + char *str; + int i; + + if (s1 == NULL || s2 == NULL) + return (NULL); + i = ft_strlen(s1) + ft_strlen(s2); + if (!(str = (char*)malloc(sizeof(str) * i + 1))) + return (NULL); + ft_strcpy(str, s1); + ft_strcat(str, s2); + return (str); +} diff --git a/libft/libft/ft_strlcat.c b/libft/libft/ft_strlcat.c new file mode 100644 index 0000000..a8677a3 --- /dev/null +++ b/libft/libft/ft_strlcat.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/13 09:55:24 by jfleury #+# #+# */ +/* Updated: 2018/11/13 18:25:30 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +size_t ft_strlcat(char *dest, const char *src, size_t n) +{ + size_t i; + size_t j; + + i = 0; + if (n == 0) + return (ft_strlen(src)); + while (dest[i] && i < n) + i++; + j = i; + while (src[i - j] && i < n - 1) + { + dest[i] = src[i - j]; + i++; + } + if (j < n) + dest[i] = '\0'; + return (j + ft_strlen(src)); +} diff --git a/libft/libft/ft_strlen.c b/libft/libft/ft_strlen.c new file mode 100644 index 0000000..749c2ad --- /dev/null +++ b/libft/libft/ft_strlen.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/05 14:49:21 by jfleury #+# #+# */ +/* Updated: 2018/11/13 18:25:34 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlen(const char *str) +{ + int i; + + i = 0; + while (str[i] != 0) + i++; + return (i); +} diff --git a/libft/libft/ft_strmap.c b/libft/libft/ft_strmap.c new file mode 100644 index 0000000..b1e9ecc --- /dev/null +++ b/libft/libft/ft_strmap.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/13 10:00:49 by jfleury #+# #+# */ +/* Updated: 2018/11/13 20:55:38 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "libft.h" + +char *ft_strmap(char const *s, char (*f)(char)) +{ + char *str; + int len; + int i; + + if (!(s)) + return (NULL); + len = ft_strlen((char*)s); + if (!(str = (char*)malloc(sizeof(char) * len + 1))) + return (NULL); + i = 0; + while (s[i] != '\0') + { + if (!(str[i] = f(s[i]))) + return (NULL); + i++; + } + str[i] = '\0'; + return (str); +} diff --git a/libft/libft/ft_strmapi.c b/libft/libft/ft_strmapi.c new file mode 100644 index 0000000..5b12a9b --- /dev/null +++ b/libft/libft/ft_strmapi.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/13 10:00:59 by jfleury #+# #+# */ +/* Updated: 2018/11/13 20:57:38 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "libft.h" + +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) +{ + char *str; + int len; + int i; + + if (!(s)) + return (NULL); + len = ft_strlen((char*)s); + if (!(str = (char*)malloc(sizeof(char) * len + 1))) + return (NULL); + i = 0; + while (s[i] != '\0') + { + if (!(str[i] = f(i, s[i]))) + return (NULL); + i++; + } + str[i] = '\0'; + return (str); +} diff --git a/libft/libft/ft_strncat.c b/libft/libft/ft_strncat.c new file mode 100644 index 0000000..a204214 --- /dev/null +++ b/libft/libft/ft_strncat.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/05 14:49:31 by jfleury #+# #+# */ +/* Updated: 2018/11/13 18:26:56 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +char *ft_strncat(char *dest, const char *src, size_t n) +{ + size_t i; + size_t j; + + j = 0; + i = ft_strlen(dest); + while (src[j] != '\0' && j < n) + { + dest[i] = src[j]; + i++; + j++; + } + dest[i] = '\0'; + return (dest); +} diff --git a/libft/libft/ft_strnchr.c b/libft/libft/ft_strnchr.c new file mode 100644 index 0000000..23c241b --- /dev/null +++ b/libft/libft/ft_strnchr.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/07 17:53:03 by jfleury #+# #+# */ +/* Updated: 2019/04/02 12:08:05 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strnchr(const char *s, int c, int n) +{ + while (*s != (char)c && *s != '\0') + { + s++; + n--; + if (n == 0) + return (NULL); + } + if (*s == (char)c) + return ((char *)s); + return (NULL); +} diff --git a/libft/libft/ft_strncmp.c b/libft/libft/ft_strncmp.c new file mode 100644 index 0000000..aec4b73 --- /dev/null +++ b/libft/libft/ft_strncmp.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/05 14:49:43 by jfleury #+# #+# */ +/* Updated: 2018/11/15 16:46:42 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +int ft_strncmp(const char *s1, const char *s2, size_t n) +{ + int i; + + i = 0; + while (n > 0 && (s1[i] != '\0' || s2[i] != '\0')) + { + if (s1[i] != s2[i]) + return ((unsigned char)s1[i] - s2[i]); + else + { + i++; + n--; + } + } + return (0); +} diff --git a/libft/libft/ft_strncpy.c b/libft/libft/ft_strncpy.c new file mode 100644 index 0000000..b2f2dae --- /dev/null +++ b/libft/libft/ft_strncpy.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/05 14:49:53 by jfleury #+# #+# */ +/* Updated: 2018/11/13 18:27:16 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +char *ft_strncpy(char *dest, const char *src, size_t n) +{ + unsigned int i; + + i = 0; + while (i < n && src[i] != '\0') + { + dest[i] = src[i]; + i++; + } + while (i < n) + { + dest[i] = '\0'; + i++; + } + return (dest); +} diff --git a/libft/libft/ft_strndup.c b/libft/libft/ft_strndup.c new file mode 100644 index 0000000..c23c7d3 --- /dev/null +++ b/libft/libft/ft_strndup.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strndup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/15 17:33:47 by allefebv #+# #+# */ +/* Updated: 2019/04/02 12:09:00 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strndup(const char *s, size_t len) +{ + char *strcpy; + + if (ft_strlen(s) > len) + { + if (!(strcpy = (char*)malloc(sizeof(char) * (len + 1)))) + return (NULL); + ft_strncpy(strcpy, s, len); + } + else + { + if (!(strcpy = ft_strdup(s))) + return (NULL); + } + return (strcpy); +} diff --git a/libft/libft/ft_strnequ.c b/libft/libft/ft_strnequ.c new file mode 100644 index 0000000..c688d2e --- /dev/null +++ b/libft/libft/ft_strnequ.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnequ.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/13 10:01:20 by jfleury #+# #+# */ +/* Updated: 2018/11/15 17:23:25 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +int ft_strnequ(char const *s1, char const *s2, size_t n) +{ + if (!(s1) || !(s2)) + return (0); + if (!(ft_strncmp(s1, s2, (int)n))) + return (1); + return (0); +} diff --git a/libft/libft/ft_strnew.c b/libft/libft/ft_strnew.c new file mode 100644 index 0000000..3ed84b6 --- /dev/null +++ b/libft/libft/ft_strnew.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/13 10:00:08 by jfleury #+# #+# */ +/* Updated: 2018/11/13 18:28:06 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "libft.h" + +char *ft_strnew(size_t size) +{ + char *str; + + if (!(str = (char*)malloc(sizeof(char) * size + 1))) + return (NULL); + ft_bzero(str, size + 1); + return (str); +} diff --git a/libft/libft/ft_strnextend.c b/libft/libft/ft_strnextend.c new file mode 100644 index 0000000..e35d277 --- /dev/null +++ b/libft/libft/ft_strnextend.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnextend.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/21 11:16:19 by jfleury #+# #+# */ +/* Updated: 2018/11/21 11:42:05 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +char *ft_strnextend(char *s1, char const *s2, size_t len) +{ + char *tmp; + int i; + + if (!(s1) || !(s2)) + return (NULL); + if (len < ft_strlen(s2)) + i = ft_strlen(s1) + len; + else + i = ft_strlen(s1) + ft_strlen(s2); + tmp = s1; + if (!(s1 = (char*)malloc(sizeof(s1) * (i + 1)))) + return (NULL); + ft_strcpy(s1, tmp); + ft_strncat(s1, s2, len); + free(tmp); + return (s1); +} diff --git a/libft/libft/ft_strnstr.c b/libft/libft/ft_strnstr.c new file mode 100644 index 0000000..c62986b --- /dev/null +++ b/libft/libft/ft_strnstr.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/08 17:44:35 by jfleury #+# #+# */ +/* Updated: 2018/11/15 17:19:56 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +char *ft_strnstr(const char *s1, const char *s2, size_t n) +{ + size_t i; + size_t j; + + i = 0; + j = 0; + while (s1[i] != '\0' && i < n) + { + j = 0; + while (s1[i] == s2[j] && s2[j] != 0 && s1[i] != 0 && i < n) + { + i++; + j++; + } + if (s2[j] == 0) + return ((char*)s1 + i - j); + if (j == 0) + i++; + else + i = i - j + 1; + } + return (NULL); +} diff --git a/libft/libft/ft_strrchr.c b/libft/libft/ft_strrchr.c new file mode 100644 index 0000000..d0263bf --- /dev/null +++ b/libft/libft/ft_strrchr.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/08 14:47:08 by jfleury #+# #+# */ +/* Updated: 2018/11/13 18:28:35 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +char *ft_strrchr(const char *s, int c) +{ + int i; + + i = ft_strlen((char *)s); + while (i >= 0) + { + if (s[i] == c) + return ((char *)s + i); + i--; + } + return (NULL); +} diff --git a/libft/libft/ft_strrev.c b/libft/libft/ft_strrev.c new file mode 100644 index 0000000..ca8480a --- /dev/null +++ b/libft/libft/ft_strrev.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrev.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/09 17:12:12 by jfleury #+# #+# */ +/* Updated: 2019/04/02 12:09:13 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strrev(char *str) +{ + char stra; + int start; + int end; + + start = 0; + end = ft_strlen(str); + end--; + while (start < end) + { + stra = str[end]; + str[end] = str[start]; + str[start] = stra; + start++; + end--; + } + return (str); +} diff --git a/libft/libft/ft_strrnchr.c b/libft/libft/ft_strrnchr.c new file mode 100644 index 0000000..9893959 --- /dev/null +++ b/libft/libft/ft_strrnchr.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrnchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/09 10:33:56 by allefebv #+# #+# */ +/* Updated: 2019/04/02 12:08:23 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strrnchr(const char *s, int c, int n) +{ + while (s[n] != c) + n--; + if (s[n] == c) + return ((char*)s + n); + else + return (NULL); +} diff --git a/libft/libft/ft_strsplit.c b/libft/libft/ft_strsplit.c new file mode 100644 index 0000000..585ed44 --- /dev/null +++ b/libft/libft/ft_strsplit.c @@ -0,0 +1,80 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsplit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/13 10:02:06 by jfleury #+# #+# */ +/* Updated: 2019/03/02 12:36:51 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +static char *ft_strdup_split(char const *s, char c, int *i) +{ + int j; + char *new; + + j = 0; + while (s[j] != c && s[j] != '\0') + j++; + if (!(new = (char*)malloc(sizeof(char) * (j + 1)))) + return (NULL); + j = 0; + while (s[j] != c && s[j] != '\0') + { + new[j] = s[j]; + j++; + } + new[j] = '\0'; + *i = *i + j; + return (new); +} + +static int ft_size(char const *s, char c) +{ + int i; + int j; + + i = 0; + j = 0; + while (s[i] != '\0') + { + if (s[i] != c && (s[i + 1] == c || s[i + 1] == '\0')) + j++; + i++; + } + return (j); +} + +char **ft_strsplit(char const *s, char c) +{ + char **s_str; + int size_s_str; + int i; + int j; + + if (s == NULL) + return (NULL); + size_s_str = ft_size(s, c); + if (!(s_str = (char**)malloc(sizeof(char*) * (size_s_str + 1)))) + return (NULL); + s_str[size_s_str] = 0; + i = 0; + j = 0; + while (s[i] != '\0') + { + while (s[i] == c && s[i] != '\0') + i++; + if (s[i] == '\0') + return (s_str); + if (!(s_str[j] = ft_strdup_split((s + i), c, &i))) + return (NULL); + else + j++; + } + return (s_str); +} diff --git a/libft/libft/ft_strstr.c b/libft/libft/ft_strstr.c new file mode 100644 index 0000000..e02c515 --- /dev/null +++ b/libft/libft/ft_strstr.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/05 14:50:07 by jfleury #+# #+# */ +/* Updated: 2018/11/13 19:37:48 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +char *ft_strstr(const char *str, const char *to_find) +{ + int i; + int j; + + i = 0; + j = 0; + if (to_find[0] == '\0') + return ((char *)str); + while (str[i] != '\0') + { + j = 0; + while (str[i + j] == to_find[j]) + { + if (to_find[j + 1] == '\0') + { + return ((char *)str + i); + } + j++; + } + i++; + } + return (NULL); +} diff --git a/libft/libft/ft_strsub.c b/libft/libft/ft_strsub.c new file mode 100644 index 0000000..eb7edea --- /dev/null +++ b/libft/libft/ft_strsub.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsub.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/13 10:01:30 by jfleury #+# #+# */ +/* Updated: 2018/11/13 18:47:30 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include +#include + +char *ft_strsub(char const *s, unsigned int start, size_t len) +{ + char *str; + size_t i; + + i = 0; + if (s == NULL || ft_strlen(s) < start) + return (NULL); + if (!(str = (char*)malloc(sizeof(char) * (len + 1)))) + return (NULL); + while (len > i && s[start] != 0) + { + str[i] = s[start]; + start++; + i++; + } + str[i] = 0; + return (str); +} diff --git a/libft/libft/ft_strtrim.c b/libft/libft/ft_strtrim.c new file mode 100644 index 0000000..1c7cd5e --- /dev/null +++ b/libft/libft/ft_strtrim.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtrim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/13 10:01:56 by jfleury #+# #+# */ +/* Updated: 2018/11/13 20:45:52 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +char *ft_strtrim(char const *s) +{ + char *str; + int i; + int len; + + if (s == NULL) + return (NULL); + i = 0; + len = ft_strlen(s) - 1; + while ((s[i] == ' ' || s[i] == '\n' || s[i] == '\t')) + i++; + if (s[i] == 0) + return (ft_strdup(s + i)); + while ((s[len] == ' ' || s[len] == '\n' || s[len] == '\t') && len > 0) + len--; + str = (ft_strsub(s, i, len - i + 1)); + return (str); +} diff --git a/libft/libft/ft_strupcase.c b/libft/libft/ft_strupcase.c new file mode 100644 index 0000000..3ecab72 --- /dev/null +++ b/libft/libft/ft_strupcase.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strupcase.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/14 17:23:47 by jfleury #+# #+# */ +/* Updated: 2019/04/02 12:08:35 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strupcase(char *str) +{ + int i; + + i = 0; + while (str[i] != '\0') + { + if (str[i] >= 'a' && str[i] <= 'z') + str[i] = str[i] - 32; + i++; + } + return (str); +} diff --git a/libft/libft/ft_tolower.c b/libft/libft/ft_tolower.c new file mode 100644 index 0000000..5e4d4d3 --- /dev/null +++ b/libft/libft/ft_tolower.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_tolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/05 17:31:11 by jfleury #+# #+# */ +/* Updated: 2018/11/13 18:29:39 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_tolower(int c) +{ + if (c >= 'A' && c <= 'Z') + { + c = c + 32; + return (c); + } + else + return (c); +} diff --git a/libft/libft/ft_toupper.c b/libft/libft/ft_toupper.c new file mode 100644 index 0000000..c2680f0 --- /dev/null +++ b/libft/libft/ft_toupper.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_toupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/11/05 17:37:05 by jfleury #+# #+# */ +/* Updated: 2018/11/13 18:29:41 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_toupper(int c) +{ + if (c >= 'a' && c <= 'z') + { + c = c - 32; + return (c); + } + else + return (c); +} diff --git a/libft/libft/get_next_line.c b/libft/libft/get_next_line.c new file mode 100644 index 0000000..a99543d --- /dev/null +++ b/libft/libft/get_next_line.c @@ -0,0 +1,91 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/03/12 13:50:20 by jfleury #+# #+# */ +/* Updated: 2019/07/01 15:23:57 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +char *ft_read(char *tmp, char **line, int fd, int *check_end) +{ + char buf[BUFF_SIZE + 1]; + int ret; + int i; + + while ((ret = read(fd, buf, BUFF_SIZE)) != 0) + { + i = 0; + buf[ret] = '\0'; + while (buf[i] != '\0' && buf[i] != '\n') + i++; + if (buf[i] == '\n') + { + if (!(*line = ft_strnextend(*line, buf, i))) + return (NULL); + ft_strcpy(tmp, buf + i + 1); + return (tmp); + } + if (!(*line = ft_strextend(*line, buf))) + return (NULL); + ft_bzero(buf, BUFF_SIZE + 1); + } + if (!ret && **line == '\0') + *check_end = 1; + return (tmp); +} + +char *ft_checktmp(char *tmp, char **line, int *check_n) +{ + int i; + int j; + + i = 0; + if (tmp == NULL) + { + if (!(tmp = ft_strnew(BUFF_SIZE))) + return (NULL); + } + while (tmp[i] != '\0' && tmp[i] != '\n') + { + line[0][i] = tmp[i]; + i++; + } + if (tmp[i] == '\n') + *check_n = 1; + j = 0; + while (tmp[i + j] != '\0') + j++; + ft_memmove(tmp, tmp + i + 1, j); + ft_bzero(tmp + j, i + 1); + return (tmp); +} + +int get_next_line(const int fd, char **line, char **tmp) +{ + char buf[1]; + int check_n; + int check_end; + + if (line == NULL || fd < 0 || read(fd, buf, 0) < 0) + return (-1); + if (!(*line = ft_strnew(BUFF_SIZE))) + *line = NULL; + check_n = 0; + check_end = 0; + if (!(*tmp = ft_checktmp(*tmp, line, &check_n))) + return (-1); + if (check_n == 1) + return (1); + if (!(*tmp = ft_read(*tmp, line, fd, &check_end))) + return (-1); + if (check_end == 0) + return (1); + return (0); +} diff --git a/libft/printf/Makefile b/libft/printf/Makefile new file mode 100644 index 0000000..9260316 --- /dev/null +++ b/libft/printf/Makefile @@ -0,0 +1,81 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: jfleury +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2018/12/14 16:59:10 by igarbuz #+# #+# # +# Updated: 2019/07/17 11:14:14 by jfleury ### ########.fr # +# # +# **************************************************************************** # + +NAME = libftprintf.a + +SRC = buf_cnt.c \ + ft_argcast.c \ + ft_arglen_f.c \ + ft_arglen_oct.c \ + ft_arglen.c \ + ft_buf_print_db.c \ + ft_buf_print_ldb.c \ + ft_error.c \ + ft_exp_dec.c \ + ft_init_param.c \ + ft_nd_div.c \ + ft_nd_long_div.c \ + ft_nd_long_mul.c \ + ft_nd_long_round.c \ + ft_nd_mul.c \ + ft_nd_round.c \ + ft_null.c \ + ft_parse.c \ + ft_printf_db.c \ + ft_printf_ldb.c \ + ft_printf.c \ + ft_unicode.c \ + libft_printf.c \ + pow10.c \ + print_buf_bit_float.c \ + print_buf_num.c \ + print_buf_prefix.c \ + print_buf_unicode.c \ + print_buf.c \ + print_cs_pad.c \ + print_num_pad.c + +HEAD = ft_printf.h + +OBJ = $(SRC:.c=.o) + +CC = gcc + +CFLAGS = -Wall -Werror -Wextra -O3 + +CPPFLAGS = -I . + +all: $(NAME) + +$(NAME): $(OBJ) + ar rcs $(NAME) $(OBJ) *.o + ranlib $(NAME) + +%.o : %.c $(HEAD) + $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@ + +norm: + norminette $(SRC) $(HEAD) | grep -B1 "Error*" + +clean: + rm -f $(OBJ) + +fclean: clean + rm -f $(NAME) + +debug: CFLAGS += -g +debug: re + +re: fclean all + +.PHONY: all clean fclean re norm debug +.SILENT: $(OBJ) $(NAME) all clean fclean re diff --git a/libft/printf/buf_cnt.c b/libft/printf/buf_cnt.c new file mode 100644 index 0000000..109116b --- /dev/null +++ b/libft/printf/buf_cnt.c @@ -0,0 +1,13 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* buf_cnt.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/24 13:03:33 by igarbuz #+# #+# */ +/* Updated: 2019/01/24 13:03:39 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int g_bcn = 0; diff --git a/libft/printf/ft_argcast.c b/libft/printf/ft_argcast.c new file mode 100644 index 0000000..1cfba6e --- /dev/null +++ b/libft/printf/ft_argcast.c @@ -0,0 +1,66 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_argcast.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/24 13:03:50 by igarbuz #+# #+# */ +/* Updated: 2019/01/24 13:03:56 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +static intmax_t ft_arg_cast_c(va_list valist) +{ + return (va_arg(valist, int)); +} + +static intmax_t ft_arg_cast_s(va_list valist) +{ + return (va_arg(valist, intptr_t)); +} + +static intmax_t ft_arg_cast_i(va_list valist, int p) +{ + if (p >> 8 & 1) + return (va_arg(valist, long long int)); + else if (p >> 7 & 1) + return (va_arg(valist, long int)); + else if (p >> 6 & 1) + return ((short int)va_arg(valist, int)); + else if (p >> 5 & 1) + return ((signed char)va_arg(valist, int)); + else + return (va_arg(valist, int)); +} + +static intmax_t ft_arg_cast_u(va_list valist, int p) +{ + if (p >> 8 & 1) + return (va_arg(valist, unsigned long long int)); + else if (p >> 7 & 1) + return (va_arg(valist, unsigned long int)); + else if (p >> 6 & 1) + return ((unsigned short int)va_arg(valist, int)); + else if (p >> 5 & 1) + return ((unsigned char)va_arg(valist, int)); + else + return (va_arg(valist, unsigned int)); +} + +intmax_t ft_arg_cast(va_list valist, int p, const char c) +{ + if (c == 'c') + return (ft_arg_cast_c(valist)); + else if (c == 's') + return (ft_arg_cast_s(valist)); + else if (c == 'd' || c == 'i') + return (ft_arg_cast_i(valist, p)); + else if (c == 'u' || c == 'o' || c == 'x' || c == 'X') + return (ft_arg_cast_u(valist, p)); + else if (c == 'p') + return (ft_arg_cast_u(valist, 0x100)); + return (0); +} diff --git a/libft/printf/ft_arglen.c b/libft/printf/ft_arglen.c new file mode 100644 index 0000000..0ea6b98 --- /dev/null +++ b/libft/printf/ft_arglen.c @@ -0,0 +1,74 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_arglen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/24 13:04:12 by igarbuz #+# #+# */ +/* Updated: 2019/01/24 13:04:16 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +static int ft_unbrlen(uintmax_t n) +{ + int i; + + i = 1; + while (n && n > 9) + { + n = n / 10; + i++; + } + return (i); +} + +static int ft_snbrlen(intmax_t n) +{ + if (n < 0) + return (ft_unbrlen(-n)); + return (ft_unbrlen(n)); +} + +static int ft_hexlen(uintmax_t x) +{ + int i; + + i = 1; + while (x > 15) + { + x = (x >> 4); + i++; + } + return (i); +} + +/* +** For string length, if an argument is an integer Zero, "(null)" is printed +** that corresponds to below returned precision of 6; +*/ + +int ft_arg_len(intmax_t arg, char c, t_param *prm) +{ + if (c == 'c' || c == '%') + return (1); + else if (!arg && !prm->prc && c != 'o') + return (0); + else if (c == 'd' || c == 'i') + return (ft_snbrlen((intmax_t)arg)); + else if (c == 'u') + return (ft_unbrlen((uintmax_t)arg)); + else if (c == 'o') + return (ft_arg_len_oct(arg, prm)); + else if (c == 'x' || c == 'X') + return (ft_hexlen((uintmax_t)arg)); + else if (c == 'p') + return (ft_hexlen((uintmax_t)arg)); + else if (c == 's' && prm->p >> 7 & 1) + return (ft_strlen_unicode((wchar_t *)arg)); + else if (c == 's') + return (ft_strlen_printf((char *)arg)); + return (0); +} diff --git a/libft/printf/ft_arglen_f.c b/libft/printf/ft_arglen_f.c new file mode 100644 index 0000000..c6a93c1 --- /dev/null +++ b/libft/printf/ft_arglen_f.c @@ -0,0 +1,62 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_arglen_f.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/24 13:04:25 by igarbuz #+# #+# */ +/* Updated: 2019/01/24 13:04:30 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_fract_len(const char *f) +{ + const char *t; + + t = f; + while (*t && *t != '.') + t++; + f = t + 1; + while (*t) + t++; + return (t - f); +} + +int ft_arg_db_len(const char *s, int p, double *arg) +{ + int len; + uintmax_t *ptr; + + ptr = (uintmax_t *)arg; + len = ft_strlen_printf(s); + if ((p & 1) || (p >> 3 & 1) || (*ptr >> 63)) + len += 1; + return (len); +} + +int ft_arg_long_db_len(const char *s, int p, long double *arg) +{ + int len; + uintmax_t *ptr; + + ptr = (uintmax_t *)arg; + len = ft_strlen_printf(s); + if ((p & 1) || (p >> 3 & 1) || (ptr[1] >> 15) & 1) + len += 1; + return (len); +} + +int ft_arg_flen(intmax_t arg, char c, int p, int len) +{ + if ((c == 'i' || c == 'd') + && (arg < 0 || (arg >= 0 && (p & 1 || p >> 3 & 1)))) + return (len + 1); + if ((c == 'x' || c == 'X') && (p >> 2 & 1) && arg) + return (len + 2); + else if (c == 'p') + return (len + 2); + return (len); +} diff --git a/libft/printf/ft_arglen_oct.c b/libft/printf/ft_arglen_oct.c new file mode 100644 index 0000000..b9c4e53 --- /dev/null +++ b/libft/printf/ft_arglen_oct.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_arglen_oct.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/29 19:52:21 by igarbuz #+# #+# */ +/* Updated: 2019/01/29 19:52:25 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +static int ft_octlen(uintmax_t o) +{ + int i; + + i = 1; + while (o > 7) + { + o = (o >> 3); + i++; + } + return (i); +} + +int ft_arg_len_oct(intmax_t arg, t_param *prm) +{ + if (arg && prm->prc) + return (ft_octlen((uintmax_t)arg) + (prm->p >> 2 & 1)); + else if (!arg && !prm->prc && !(prm->p >> 2 & 1)) + return (0); + return (ft_octlen((uintmax_t)arg)); +} diff --git a/libft/printf/ft_buf_print_db.c b/libft/printf/ft_buf_print_db.c new file mode 100644 index 0000000..837b20c --- /dev/null +++ b/libft/printf/ft_buf_print_db.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_buf_print_db.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/29 20:37:10 by igarbuz #+# #+# */ +/* Updated: 2019/01/29 20:37:15 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +static void ft_sprint(int up, char **p, uint32_t u, int r) +{ + while (up >= r) + { + **p = (u / g_pow10[up]) % 10 + 48U; + (*p)++; + up--; + } +} + +static void ft_print_hi(t_nd *nd, char **p) +{ + ft_sprint(ft_exp_dec(nd->n[nd->hi]), p, nd->n[nd->hi & 127], 0); + while (--(nd->hi) >= 0) + ft_sprint(8, p, nd->n[nd->hi & 127], 0); +} + +static void ft_print_lo(t_nd *nd, char **p, int r) +{ + int rloc; + + rloc = r % 9; + while (nd->hi > nd->lo || (!rloc && nd->hi >= nd->lo)) + { + ft_sprint(8, p, nd->n[nd->hi & 127], 0); + nd->hi--; + } + if (rloc && -nd->lo * 9 >= r) + ft_sprint(8, p, nd->n[nd->hi & 127], 9 - rloc); +} + +char *ft_buf_print_db(t_nd *nd, int r, char *tbl) +{ + char *p; + + p = tbl; + if (nd->hi && !nd->n[nd->hi]) + nd->hi--; + ft_print_hi(nd, &p); + *p++ = '.'; + ft_print_lo(nd, &p, r); + *p = '\0'; + return (tbl); +} diff --git a/libft/printf/ft_buf_print_ldb.c b/libft/printf/ft_buf_print_ldb.c new file mode 100644 index 0000000..9b78c2e --- /dev/null +++ b/libft/printf/ft_buf_print_ldb.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_buf_print_ldb.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/29 20:08:18 by igarbuz #+# #+# */ +/* Updated: 2019/01/29 20:08:21 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +static void ft_sprint(int up, char **p, uint32_t u, int r) +{ + while (up >= r) + { + **p = (u / g_pow10[up]) % 10 + 48U; + (*p)++; + up--; + } +} + +static void ft_print_hi(t_nd_long *nd, char **p) +{ + ft_sprint(ft_exp_dec(nd->n[nd->hi]), p, nd->n[nd->hi & 2047], 0); + while (--(nd->hi) >= 0) + ft_sprint(8, p, nd->n[nd->hi & 2047], 0); +} + +static void ft_print_lo(t_nd_long *nd, char **p, int r) +{ + int rloc; + + rloc = r % 9; + while (nd->hi > nd->lo || (!rloc && nd->hi >= nd->lo)) + { + ft_sprint(8, p, nd->n[nd->hi & 2047], 0); + nd->hi--; + } + if (rloc && -nd->lo * 9 >= r) + ft_sprint(8, p, nd->n[nd->hi & 2047], 9 - rloc); +} + +char *ft_buf_print_ldb(t_nd_long *nd, int r, char *tbl) +{ + char *p; + + p = tbl; + while (nd->hi && !nd->n[nd->hi]) + nd->hi--; + ft_print_hi(nd, &p); + *p++ = '.'; + ft_print_lo(nd, &p, r); + *p = '\0'; + return (tbl); +} diff --git a/libft/printf/ft_error.c b/libft/printf/ft_error.c new file mode 100644 index 0000000..f89cb2f --- /dev/null +++ b/libft/printf/ft_error.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* pf_error.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/24 13:04:44 by igarbuz #+# #+# */ +/* Updated: 2019/01/24 13:04:48 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +void pf_error(int er) +{ + if (er < 0) + { + if (er == -1) + ; + exit(EXIT_FAILURE); + } + else + write(1, "usage : pf_error(int er), er < 0\n", 33); +} diff --git a/libft/printf/ft_exp_dec.c b/libft/printf/ft_exp_dec.c new file mode 100644 index 0000000..a149059 --- /dev/null +++ b/libft/printf/ft_exp_dec.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_exp_dec.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/24 13:04:55 by igarbuz #+# #+# */ +/* Updated: 2019/01/24 13:04:59 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_exp_dec(unsigned int value) +{ + int exponent; + + exponent = 0; + if (value >= 10000 && (value /= 10000)) + exponent += 4; + if (value >= 1000 && (value /= 1000)) + exponent += 3; + if (value >= 100 && (value /= 100)) + exponent += 2; + if (value >= 10 && (value /= 10)) + exponent += 1; + return (exponent); +} diff --git a/libft/printf/ft_init_param.c b/libft/printf/ft_init_param.c new file mode 100644 index 0000000..668e28a --- /dev/null +++ b/libft/printf/ft_init_param.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_init_param.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/24 13:05:18 by igarbuz #+# #+# */ +/* Updated: 2019/01/24 13:05:20 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +void ft_init_param(t_param *prm) +{ + prm->p = 0; + prm->prc = -1; + prm->wdt = -1; +} + +void ft_init_nd(t_nd *nd) +{ + nd->hi = 0; + nd->lo = 0; +} diff --git a/libft/printf/ft_nd_div.c b/libft/printf/ft_nd_div.c new file mode 100644 index 0000000..a67d452 --- /dev/null +++ b/libft/printf/ft_nd_div.c @@ -0,0 +1,78 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_nd_div.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/29 20:15:58 by igarbuz #+# #+# */ +/* Updated: 2019/01/29 20:16:02 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +/* +** carry is a (value / 2^9) * 10^10 / 2^9, where +** value is divided by 512 and multiplied by 1953125, whick is the maximal +** decimal 9-digit decimal times 512; +*/ + +static void ft_nd_div2p_hi(t_nd *nd, uint32_t *carry, uint32_t *i) +{ + uint32_t val; + + while (1) + { + val = nd->n[*i]; + nd->n[*i] = (val >> 9) + *carry; + *carry = (val & 0x1ff) * 1953125; + if (*i == (nd->lo & 127)) + break ; + *i = (*i - 1) & 127; + } +} + +static void ft_nd_div2p_last(t_nd *nd, uint32_t *carry, + uint32_t *i, uint32_t p) +{ + uint32_t val; + uint32_t mask; + uint32_t mul; + + mask = (1U << p) - 1; + mul = 1000000000 >> p; + while (1) + { + val = nd->n[*i]; + nd->n[*i] = (val >> p) + *carry; + *carry = (val & mask) * mul; + if (*i == (nd->lo & 127)) + break ; + *i = (*i - 1) & 127; + } +} + +void ft_nd_div2p(t_nd *nd, uint32_t p) +{ + uint32_t carry; + uint32_t i; + + while (p >= 9) + { + carry = 0; + i = nd->hi & 127; + ft_nd_div2p_hi(nd, &carry, &i); + if (carry) + nd->n[--(nd->lo) & 127] = carry; + p -= 9; + } + if (p) + { + carry = 0; + i = nd->hi & 127; + ft_nd_div2p_last(nd, &carry, &i, p); + if (carry) + nd->n[--(nd->lo) & 127] = carry; + } +} diff --git a/libft/printf/ft_nd_long_div.c b/libft/printf/ft_nd_long_div.c new file mode 100644 index 0000000..f86c605 --- /dev/null +++ b/libft/printf/ft_nd_long_div.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_nd_long_div.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/29 20:21:44 by igarbuz #+# #+# */ +/* Updated: 2019/01/29 20:21:48 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +static void ft_nd_div2p_hi(t_nd_long *nd, uint32_t *carry, uint32_t *i) +{ + uint32_t val; + + while (1) + { + val = nd->n[*i]; + nd->n[*i] = (val >> 9) + *carry; + *carry = (val & 0x1ff) * 1953125; + if (*i == (nd->lo & 2047)) + break ; + *i = (*i - 1) & 2047; + } +} + +static void ft_nd_div2p_last(t_nd_long *nd, uint32_t *carry, + uint32_t *i, uint32_t p) +{ + uint32_t val; + uint32_t mask; + uint32_t mul; + + mask = (1U << p) - 1; + mul = 1000000000 >> p; + while (1) + { + val = nd->n[*i]; + nd->n[*i] = (val >> p) + *carry; + *carry = (val & mask) * mul; + if (*i == (nd->lo & 2047)) + break ; + *i = (*i - 1) & 2047; + } +} + +void ft_nd_long_div2p(t_nd_long *nd, uint32_t p) +{ + uint32_t carry; + uint32_t i; + + while (p >= 9) + { + carry = 0; + i = nd->hi & 2047; + ft_nd_div2p_hi(nd, &carry, &i); + if (carry) + nd->n[--(nd->lo) & 2047] = carry; + p -= 9; + } + if (p) + { + carry = 0; + i = nd->hi & 2047; + ft_nd_div2p_last(nd, &carry, &i, p); + if (carry) + nd->n[--(nd->lo) & 2047] = carry; + } +} diff --git a/libft/printf/ft_nd_long_mul.c b/libft/printf/ft_nd_long_mul.c new file mode 100644 index 0000000..c67f7a3 --- /dev/null +++ b/libft/printf/ft_nd_long_mul.c @@ -0,0 +1,62 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_nd_long_mul.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/29 20:23:07 by igarbuz #+# #+# */ +/* Updated: 2019/01/29 20:23:09 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +static void ft_nd_mul2p_main(t_nd_long *nd, uint32_t *carry, uint32_t *i) +{ + uint64_t val; + + while (++(*i) <= (uint32_t)nd->hi) + { + val = ((uint64_t)nd->n[*i] << 29) | *carry; + *carry = (uint32_t)(val / 1000000000); + nd->n[*i] = (uint32_t)val - *carry * 1000000000; + } +} + +static void ft_nd_mul2p_last(t_nd_long *nd, uint32_t *carry, + uint32_t *i, uint32_t p) +{ + uint64_t val; + + while (++(*i) <= (uint32_t)nd->hi) + { + val = ((uint64_t)nd->n[*i] << p) | *carry; + *carry = (uint32_t)(val / 1000000000); + nd->n[*i] = (uint32_t)val - *carry * 1000000000; + } +} + +void ft_nd_long_mul2p(t_nd_long *nd, uint32_t p, uint32_t carry) +{ + uint32_t i; + + while (p >= 29) + { + i = -1; + ft_nd_mul2p_main(nd, &carry, &i); + if (carry) + { + nd->n[++(nd->hi)] = carry; + carry = 0; + } + p -= 29; + } + if (p) + { + i = -1; + ft_nd_mul2p_last(nd, &carry, &i, p); + if (carry) + nd->n[++(nd->hi)] = carry; + } +} diff --git a/libft/printf/ft_nd_long_round.c b/libft/printf/ft_nd_long_round.c new file mode 100644 index 0000000..856b899 --- /dev/null +++ b/libft/printf/ft_nd_long_round.c @@ -0,0 +1,68 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_nd_long_round.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/29 20:25:42 by igarbuz #+# #+# */ +/* Updated: 2019/01/29 20:25:45 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +static void ft_round_up(uint32_t *nd, int ndlo) +{ + while (nd[ndlo & 2047] >= 1000000000) + { + ndlo++; + nd[ndlo & 2047] += 1; + } +} + +static void ft_round_ndlo(int r, int rloc, int *ndlo) +{ + if (!r) + *ndlo = 0; + else if (!rloc && -*ndlo * 9 >= r) + *ndlo = -r / 9; + else if (rloc && -*ndlo * 9 >= r) + *ndlo = -r / 9 - 1; +} + +static int ft_round_last(int ndlo, int r, uint32_t *nd) +{ + int rloc; + int tlo; + + tlo = ndlo; + rloc = (r % 9); + ft_round_ndlo(r, rloc, &ndlo); + if (((r / 9) + 1) == -tlo && !(nd[tlo & 2047] % g_pow10[8 - rloc])) + { + if (rloc && (nd[ndlo & 2047] / g_pow10[9 - rloc] % 10) & 1 + && (nd[ndlo & 2047] / g_pow10[8 - rloc]) % 10 == 5) + nd[ndlo & 2047] += g_pow10[9 - rloc]; + else if (!rloc && (tlo <= ndlo - 1) && (nd[ndlo & 2047] % 10) & 1 + && nd[(ndlo - 1) & 2047] / g_pow10[8] == 5) + nd[ndlo & 2047] += 1; + } + else + { + if (rloc && (nd[ndlo & 2047] / g_pow10[8 - rloc]) % 10 >= 5) + nd[ndlo & 2047] += g_pow10[9 - rloc]; + else if (!rloc && (tlo <= ndlo - 1) + && nd[(ndlo - 1) & 2047] / g_pow10[8] >= 5) + nd[ndlo & 2047] += 1; + } + return (ndlo); +} + +void ft_long_round(t_nd_long *nd, int *r) +{ + if (*r > -nd->lo * 9) + *r = -nd->lo * 9; + nd->lo = ft_round_last(nd->lo, *r, nd->n); + ft_round_up(nd->n, nd->lo); +} diff --git a/libft/printf/ft_nd_mul.c b/libft/printf/ft_nd_mul.c new file mode 100644 index 0000000..1b87cce --- /dev/null +++ b/libft/printf/ft_nd_mul.c @@ -0,0 +1,62 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_nd_mul.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/29 20:11:14 by igarbuz #+# #+# */ +/* Updated: 2019/01/29 20:11:17 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +static void ft_nd_mul2p_main(t_nd *nd, uint32_t *carry, uint32_t *i) +{ + uint64_t val; + + while (++(*i) <= (uint32_t)nd->hi) + { + val = ((uint64_t)nd->n[*i] << 29) | *carry; + *carry = (uint32_t)(val / 1000000000); + nd->n[*i] = (uint32_t)val - *carry * 1000000000; + } +} + +static void ft_nd_mul2p_last(t_nd *nd, uint32_t *carry, + uint32_t *i, uint32_t p) +{ + uint64_t val; + + while (++(*i) <= (uint32_t)nd->hi) + { + val = ((uint64_t)nd->n[*i] << p) | *carry; + *carry = (uint32_t)(val / 1000000000); + nd->n[*i] = (uint32_t)val - *carry * 1000000000; + } +} + +void ft_nd_mul2p(t_nd *nd, uint32_t p, uint32_t carry) +{ + uint32_t i; + + while (p >= 29) + { + i = -1; + ft_nd_mul2p_main(nd, &carry, &i); + if (carry) + { + nd->n[++(nd->hi)] = carry; + carry = 0; + } + p -= 29; + } + if (p) + { + i = -1; + ft_nd_mul2p_last(nd, &carry, &i, p); + if (carry) + nd->n[++(nd->hi)] = carry; + } +} diff --git a/libft/printf/ft_nd_round.c b/libft/printf/ft_nd_round.c new file mode 100644 index 0000000..36cf1f6 --- /dev/null +++ b/libft/printf/ft_nd_round.c @@ -0,0 +1,68 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_nd_round.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/29 20:07:19 by igarbuz #+# #+# */ +/* Updated: 2019/01/29 20:07:22 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +static void ft_round_up(uint32_t *nd, int ndlo) +{ + while (nd[ndlo & 127] >= 1000000000) + { + ndlo++; + nd[ndlo & 127] += 1; + } +} + +static void ft_round_ndlo(int r, int rloc, int *ndlo) +{ + if (!r) + *ndlo = 0; + else if (!rloc && -*ndlo * 9 >= r) + *ndlo = -r / 9; + else if (rloc && -*ndlo * 9 >= r) + *ndlo = -r / 9 - 1; +} + +static int ft_round_last(int ndlo, int r, uint32_t *nd) +{ + int rloc; + int tlo; + + tlo = ndlo; + rloc = (r % 9); + ft_round_ndlo(r, rloc, &ndlo); + if (((r / 9) + 1) == -tlo && !(nd[tlo & 127] % g_pow10[8 - rloc])) + { + if (rloc && (nd[ndlo & 127] / g_pow10[9 - rloc] % 10) & 1 + && (nd[ndlo & 127] / g_pow10[8 - rloc]) % 10 == 5) + nd[ndlo & 127] += g_pow10[9 - rloc]; + else if (!rloc && (tlo <= ndlo - 1) && (nd[ndlo & 127] % 10) & 1 + && nd[(ndlo - 1) & 127] / g_pow10[8] == 5) + nd[ndlo & 127] += 1; + } + else + { + if (rloc && (nd[ndlo & 127] / g_pow10[8 - rloc]) % 10 >= 5) + nd[ndlo & 127] += g_pow10[9 - rloc]; + else if (!rloc && (tlo <= ndlo - 1) + && nd[(ndlo - 1) & 127] / g_pow10[8] >= 5) + nd[ndlo & 127] += 1; + } + return (ndlo); +} + +void ft_round(t_nd *nd, int *r) +{ + if (*r > -nd->lo * 9) + *r = -nd->lo * 9; + nd->lo = ft_round_last(nd->lo, *r, nd->n); + ft_round_up(nd->n, nd->lo); +} diff --git a/libft/printf/ft_null.c b/libft/printf/ft_null.c new file mode 100644 index 0000000..d2cb22e --- /dev/null +++ b/libft/printf/ft_null.c @@ -0,0 +1,16 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_null.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/24 22:17:39 by igarbuz #+# #+# */ +/* Updated: 2019/01/24 22:17:43 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +const char *ft_null(void) +{ + return ("(null)"); +} diff --git a/libft/printf/ft_parse.c b/libft/printf/ft_parse.c new file mode 100644 index 0000000..4343be2 --- /dev/null +++ b/libft/printf/ft_parse.c @@ -0,0 +1,91 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_parse.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/24 23:54:23 by igarbuz #+# #+# */ +/* Updated: 2019/01/24 23:54:27 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +static int ft_istype(char c) +{ + return (c == '%' || c == 'c' || c == 's' || c == 'p' || c == 'd' || c == 'i' + || c == 'o' || c == 'u' || c == 'x' || c == 'X' || c == 'f' || c == 'B'); +} + +static void ft_parse_prefix(const char **s, t_param *prm) +{ + if (**s == '+') + prm->p = prm->p | 1; + else if (**s == '-') + prm->p = prm->p | 1 << 1; + else if (**s == '#') + prm->p = prm->p | 1 << 2; + else if (**s == ' ') + prm->p = prm->p | 1 << 3; + else if (**s == '0') + prm->p = prm->p | 1 << 4; +} + +static void ft_parse_type(const char **s, t_param *prm) +{ + if (**s == 'h' && *(*s + 1) == 'h') + { + prm->p = prm->p | 1 << 5; + (*s)++; + } + else if (**s == 'h') + prm->p = prm->p | 1 << 6; + else if (**s == 'l' && *(*s + 1) == 'l') + { + prm->p = prm->p | 1 << 8; + (*s)++; + } + else if (**s == 'l') + prm->p = prm->p | 1 << 7; + else if (**s == 'L') + prm->p = prm->p | 1 << 9; +} + +static void ft_parse_wdpr(const char **s, t_param *prm) +{ + if (ft_isdigit_printf(**s)) + { + prm->wdt = ft_atoi_printf(*s); + while (ft_isdigit_printf(*(*s + 1))) + (*s)++; + } + else if (**s == '.') + { + if (ft_isdigit_printf(*(*s + 1))) + prm->prc = ft_atoi_printf(*s + 1); + else + prm->prc = 0; + while (ft_isdigit_printf(*(*s + 1))) + (*s)++; + } +} + +int ft_parse(const char **s, t_param *prm) +{ + while (**s) + { + (*s)++; + if (**s == '+' || **s == '-' || **s == '#' || **s == ' ' || **s == '0') + ft_parse_prefix(s, prm); + else if (**s == 'h' || **s == 'l' || **s == 'L') + ft_parse_type(s, prm); + else if (**s == '.' || ft_isdigit_printf(**s)) + ft_parse_wdpr(s, prm); + else if (ft_istype(**s)) + return (1); + else + return (0); + } + return (0); +} diff --git a/libft/printf/ft_printf.c b/libft/printf/ft_printf.c new file mode 100644 index 0000000..1096793 --- /dev/null +++ b/libft/printf/ft_printf.c @@ -0,0 +1,77 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/24 13:05:37 by igarbuz #+# #+# */ +/* Updated: 2019/01/24 13:05:40 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +static void ft_print(const char c, char *b, t_param *prm, va_list valist) +{ + intmax_t arg; + double db; + long double ldb; + + if (c != '%' && c != 'f') + arg = ft_arg_cast(valist, prm->p, c); + else if (c == 'f' && (prm->p >> 9 & 1)) + ldb = va_arg(valist, long double); + else if (c == 'f') + db = va_arg(valist, double); + if (c == 'c' || c == 's' || c == '%') + ft_print_c_pad(c, b, prm, arg); + else if ((c == 'd' || c == 'i' || c == 'u' || c == 'o' || c == 'x' + || c == 'X' || c == 'p')) + ft_print_i_pad(c, b, prm, arg); + else if (c == 'f' && (prm->p >> 9 & 1)) + ft_print_long_db_pad(b, prm, &ldb); + else if (c == 'f') + ft_print_db_pad(b, prm, &db); + else if (c == 'B') + ft_print_bit_float(b, prm, valist); +} + +static void ft_read_input(va_list valist, char *buf, + t_param *prm, const char *format) +{ + while (*format) + { + ft_init_param(prm); + ft_printbuf_c(buf, &format, '%'); + if (!*format) + break ; + if (*format == '%' && ft_parse(&format, prm)) + ft_print(*format, buf, prm, valist); + else if (*format) + { + ft_print_c_pad('c', buf, prm, *format); + format++; + if (*format) + ft_printbuf_c(buf, &format, '%'); + } + if (*format && (*format != '%' || *(format - 1) == '%')) + format++; + } +} + +int ft_printf(const char *format, ...) +{ + va_list valist; + t_param prm; + char buf[BUF_SIZE]; + + g_bcn = 0; + if (format == 0 || *format == '\0') + return (0); + va_start(valist, format); + ft_read_input(valist, buf, &prm, format); + va_end(valist); + write(1, buf, g_bcn); + return (g_bcn); +} diff --git a/libft/printf/ft_printf.h b/libft/printf/ft_printf.h new file mode 100644 index 0000000..83cc6db --- /dev/null +++ b/libft/printf/ft_printf.h @@ -0,0 +1,258 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/24 13:05:59 by igarbuz #+# #+# */ +/* Updated: 2019/01/24 13:06:04 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_PRINTF_H +# define FT_PRINTF_H + +# include +# include +# include +# include + +/* +** BUF_M is muffer mask; must be a divisible PAGESIZE minus 1; +** exception for BUF_M 0x1, BUF_SIZE must be 0x1; +** BUF_S is a buffer size - must be a divisible PAGESIZE; +** PAGESIZE default 2^12; +** default 0x7ffff is equivalent to 524287 bytes; Examples of values : +** BUF_M : 0x7ffff, 0x3ffff, 0x1ffff, 0x0ffff, 0x7fff, 0x3fff, 0x1fff, 0x0fff; +** BUF_S : 0x80000, 0x40000, 0x20000, 0x10000, 0x8000, 0x4000, 0x2000, 0x1000 +*/ +# define BUF_M 0x0fff +# define BUF_SIZE 0x1000 + +/* +** do not modify +*/ +# define F_ROUND 6 +# define DB_MAX_L 1076 +# define DBL_MAX_L 16447 + +typedef struct s_param +{ + int p; + int prc; + int wdt; +} t_param; + +typedef union u_value +{ + double n; + struct + { + uint32_t lo; + uint32_t hi; + } u32; +} t_value; + +typedef union u_value64 +{ + long double n; + struct + { + uint64_t lo; + uint64_t hi; + } u64; +} t_value64; + +typedef struct s_nd +{ + uint32_t n[128]; + int32_t lo; + int32_t hi; +} t_nd; + +typedef struct s_nd_long +{ + uint32_t n[2048]; + int32_t lo; + int32_t hi; +} t_nd_long; + +/* +** buf_cnt.c +*/ +extern int g_bcn; +extern unsigned int g_pow10[]; + +/* +** ft_str_null.c +*/ +const char *ft_null(void); + +/* +** ft_printf.c +*/ +int ft_printf(const char *format, ...); + +/* +** print_num_pad.c +*/ +void ft_print_long_db_pad(char *b, t_param *prm, + long double *arg); +void ft_print_db_pad(char *b, t_param *prm, double *arg); +void ft_print_i_pad(char c, char *b, t_param *prm, intmax_t arg); + +/* +** print_cs_pad.c +*/ +void ft_print_c_pad(char c, char *b, t_param *prm, intmax_t arg); + +/* +** ft_printf_db.c +*/ +char *ft_printf_db(double *n, int r, char *tbl); + +/* +** ft_printf_ldb.c +*/ +char *ft_printf_long_db(long double *n, int r, char *tbl); + +/* +** ft_parse.c +*/ +int ft_parse(const char **s, t_param *prm); + +/* +** print_buf_prefix.c +*/ +void ft_printbuf_long_fprefix(char *b, int p, long double *arg); +void ft_printbuf_fprefix(char *b, int p, double *arg); +void ft_printbuf_xprefix(char *b, char c, + t_param *prm, intmax_t *arg); +void ft_printbuf_sprefix(char *b, int p, intmax_t *arg); + +/* +** libft_printf.c +*/ +int ft_strlen_printf(const char *s); +int ft_strlen_unicode(const wchar_t *s); +int ft_atoi_printf(const char *str); +int ft_isdigit_printf(char c); + +/* +** print_buf.c +*/ +void ft_flush(char *dst); +void ft_printbuf_char(char *dst, char src); +void ft_printbuf_pad(char *dst, char c, int len); +void ft_printbuf_c(char *dst, const char **src, char c); +void ft_printbuf_prc(char *dst, char **src, int prc); + +/* +** print_buf_unicode.c +*/ +void ft_printbuf_wchar_t(char *dst, wchar_t src, int size); +void ft_printbuf_unicode_prc(char *dst, wchar_t **src, int prc); + +/* +** print_buf_num.c +*/ +void ft_printbuf_snum(char *dst, intmax_t n); +void ft_printbuf_unum(char *dst, uintmax_t n); +void ft_printbuf_oct(char *dst, uintmax_t o); +void ft_printbuf_hex(char *dst, uintmax_t x, char c); + +/* +** ft_arglen.c +*/ +int ft_arg_len(intmax_t arg, char c, t_param *prm); + +/* +** ft_arglen_oct.c +*/ +int ft_arg_len_oct(intmax_t arg, t_param *prm); + +/* +** ft_arglen_f.c +*/ +int ft_fract_len(const char *f); +int ft_arg_db_len(const char *s, int p, double *arg); +int ft_arg_long_db_len(const char *s, int p, long double *arg); +int ft_arg_flen(intmax_t arg, char c, int p, int len); + +/* +** ft_argcast.c +*/ +intmax_t ft_arg_cast(va_list valist, int p, const char c); + +/* +** ft_init_param.c +*/ +void ft_init_param(t_param *prm); +void ft_init_nd(t_nd *nd); + +/* +** ft_exp_dec.c +*/ +int ft_exp_dec(unsigned int value); + +/* +** pf_error.c +*/ +void pf_error(int er); + +/* +** nd_round.c +*/ +void ft_round(t_nd *nd, int *r); + +/* +** ft_buf_print_db.c +*/ +char *ft_buf_print_db(t_nd *nd, int r, char *tbl); + +/* +** ft_buf_print_ldb.c +*/ +char *ft_buf_print_ldb(t_nd_long *nd, int r, char *tbl); + +/* +** ft_nd_div.c +*/ +void ft_nd_div2p(t_nd *nd, uint32_t p); + +/* +** ft_nd_mul.c +*/ +void ft_nd_mul2p(t_nd *nd, uint32_t p, uint32_t carry); + +/* +** ft_nd_long_div.c +*/ +void ft_nd_long_div2p(t_nd_long *nd, uint32_t p); + +/* +** ft_nd_long_mul.c +*/ +void ft_nd_long_mul2p(t_nd_long *nd, uint32_t p, uint32_t carry); + +/* +** ft_nd_long_round.c +*/ +void ft_long_round(t_nd_long *nd, int *r); + +/* +** ft_unicode.c +*/ +int ft_wcsize(wchar_t wc); +wchar_t ft_utf8_encode(unsigned int wchar); + +/* +** print_buf_bit_float.c +*/ +void ft_print_buf_bit_float(char *b, float f); +void ft_print_buf_bit_double(char *b, double db); +void ft_print_buf_bit_long_double(char *b, long double ldb); +void ft_print_bit_float(char *b, t_param *prm, va_list valist); + +#endif diff --git a/libft/printf/ft_printf_db.c b/libft/printf/ft_printf_db.c new file mode 100644 index 0000000..75d032d --- /dev/null +++ b/libft/printf/ft_printf_db.c @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf_db.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/24 13:06:20 by igarbuz #+# #+# */ +/* Updated: 2019/01/24 13:06:22 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +static char *ft_naninf_db(t_value *t) +{ + if (((t->u32.hi & 0x000fffff) | t->u32.lo) != 0) + return ("nan"); + else + return ("inf"); +} + +static void ft_fract_to_nd(t_value *t, t_nd *nd, int32_t *e) +{ + if (t->u32.lo) + { + *e -= 32; + nd->n[0] = (nd->n[0] << 3) | (t->u32.lo >> 29); + ft_nd_mul2p(nd, 29, t->u32.lo & 0x1fffffff); + } +} + +static void ft_implicit_one(int32_t *e, t_nd *nd) +{ + if (*e == 0) + (*e)++; + else + nd->n[0] |= 0x100000; +} + +char *ft_printf_db(double *n, int r, char *tbl) +{ + t_value t; + int32_t e; + t_nd nd; + + t.n = *n; + ft_init_nd(&nd); + if ((t.u32.hi << 1) >= 0xffe00000) + return (ft_naninf_db(&t)); + else + { + e = (t.u32.hi >> 20) & 0x7ff; + nd.n[0] = t.u32.hi & 0xfffff; + ft_implicit_one(&e, &nd); + e -= 1043; + ft_fract_to_nd(&t, &nd, &e); + if (e >= 0) + ft_nd_mul2p(&nd, (uint32_t)e, 0); + else + ft_nd_div2p(&nd, (uint32_t)-e); + if (n && nd.lo) + ft_round(&nd, &r); + return (ft_buf_print_db(&nd, r, tbl)); + } + return (0); +} diff --git a/libft/printf/ft_printf_ldb.c b/libft/printf/ft_printf_ldb.c new file mode 100644 index 0000000..fcd5275 --- /dev/null +++ b/libft/printf/ft_printf_ldb.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf_ldb.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/24 13:06:44 by igarbuz #+# #+# */ +/* Updated: 2019/01/24 13:06:47 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +static char *ft_naninf_ldb(t_value64 *t) +{ + if (t->u64.lo != 0) + return ("nan"); + else + return ("inf"); +} + +static void ft_fract_to_nd(t_value64 *t, t_nd_long *nd, int32_t *e) +{ + if (t->u64.lo & 0x3FFFFFFFFFFFFFF) + { + *e -= 58; + ft_nd_long_mul2p(nd, 29, (t->u64.lo >> 29) & 0x1FFFFFFF); + ft_nd_long_mul2p(nd, 29, t->u64.lo & 0x1FFFFFFF); + } +} + +char *ft_printf_long_db(long double *n, int r, char *tbl) +{ + t_nd_long nd; + t_value64 t; + int32_t e; + + t.n = *n; + if ((t.u64.hi << 49) >= 0xfffe000000000000) + return (ft_naninf_ldb(&t)); + else + { + e = (t.u64.hi & 0x7fff); + nd.n[0] = (t.u64.lo >> 58) & 0x3f; + e -= 16388; + ft_fract_to_nd(&t, &nd, &e); + if (e >= 0) + ft_nd_long_mul2p(&nd, (uint32_t)e, 0); + else if (1) + ft_nd_long_div2p(&nd, (uint32_t)-e); + if (*n && nd.lo) + ft_long_round(&nd, &r); + return (ft_buf_print_ldb(&nd, r, tbl)); + } +} diff --git a/libft/printf/ft_unicode.c b/libft/printf/ft_unicode.c new file mode 100644 index 0000000..75c73c6 --- /dev/null +++ b/libft/printf/ft_unicode.c @@ -0,0 +1,70 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_unicode.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/30 01:52:28 by igarbuz #+# #+# */ +/* Updated: 2019/01/30 01:52:31 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_wcsize(wchar_t wc) +{ + if (wc <= 0x007F) + return (1); + if (wc <= 0x07FF) + return (2); + if (wc <= 0xFFFF) + return (3); + if (wc <= 0x10FFFF) + return (4); + return (0); +} + +static int ft_wc_to_tbl(unsigned char *s, unsigned int wchar) +{ + int len; + unsigned int rshift; + unsigned int mask[2]; + int i; + + if (!s || !(len = ft_wcsize(wchar))) + return (0); + rshift = (len - 1) * 6; + mask[0] = 0xFF; + mask[1] = 0xC0; + if (len > 2) + mask[1] += (len - 1) * 16; + i = 0; + while (i < len) + { + s[i++] = (wchar >> rshift & mask[0]) | mask[1]; + mask[0] = 0x3F; + mask[1] = 0x80; + rshift -= 6; + } + return (len); +} + +wchar_t ft_utf8_encode(unsigned int wchar) +{ + int len; + unsigned char s[4]; + unsigned int lshift; + + if (!(len = ft_wc_to_tbl(s, wchar))) + return (0); + if (!(lshift = (len - 1) * 8)) + return (wchar); + wchar = 0; + while (len--) + { + wchar |= s[len] << lshift; + lshift -= 8; + } + return (wchar); +} diff --git a/libft/printf/libft_printf.c b/libft/printf/libft_printf.c new file mode 100644 index 0000000..2c17975 --- /dev/null +++ b/libft/printf/libft_printf.c @@ -0,0 +1,75 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft_printf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/29 20:40:42 by igarbuz #+# #+# */ +/* Updated: 2019/01/29 20:40:44 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_strlen_unicode(const wchar_t *s) +{ + const wchar_t *tmp; + + if (!s) + return (6); + tmp = s; + while (*s) + s++; + return (s - tmp); +} + +int ft_strlen_printf(const char *s) +{ + size_t len; + unsigned int *x; + + if (!s) + return (6); + len = 0; + while (1) + { + x = (unsigned int *)s; + if ((*x & 0xFF) == 0) + return (len); + if ((*x & 0xFF00) == 0) + return (len + 1); + if ((*x & 0xFF0000) == 0) + return (len + 2); + if ((*x & 0xFF000000) == 0) + return (len + 3); + s += 4; + len += 4; + } +} + +int ft_atoi_printf(const char *str) +{ + int d; + int integer; + + d = 1; + integer = 0; + while ((*str >= 9 && *str <= 13) || *str == 32) + str++; + if (*str == '-' && (d = -1)) + str++; + else if (*str == '+') + str++; + str--; + while (++str && (*str >= 48 && *str <= 57)) + integer = integer * 10 + *str - 48; + if (d == -1) + return (integer * d); + return (integer); +} + +int ft_isdigit_printf(char c) +{ + return (c >= '0' && c <= '9'); +} diff --git a/libft/printf/pow10.c b/libft/printf/pow10.c new file mode 100644 index 0000000..715a1af --- /dev/null +++ b/libft/printf/pow10.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* pow10.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/29 19:58:41 by igarbuz #+# #+# */ +/* Updated: 2019/01/29 19:58:45 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +unsigned int g_pow10[] = { + 1, + 10, + 100, + 1000, + 10000, + 100000, + 1000000, + 10000000, + 100000000, +}; diff --git a/libft/printf/print_buf.c b/libft/printf/print_buf.c new file mode 100644 index 0000000..687ac4d --- /dev/null +++ b/libft/printf/print_buf.c @@ -0,0 +1,70 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* print_buf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/29 21:34:43 by igarbuz #+# #+# */ +/* Updated: 2019/01/29 21:34:47 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +void ft_flush(char *dst) +{ + write(1, dst, BUF_M); + g_bcn++; +} + +void ft_printbuf_char(char *dst, char src) +{ + if ((g_bcn & BUF_M) < BUF_M) + dst[g_bcn & BUF_M] = src; + else if ((g_bcn & BUF_M) == BUF_M) + { + ft_flush(dst); + dst[g_bcn & BUF_M] = src; + } + g_bcn++; +} + +void ft_printbuf_prc(char *dst, char **src, int prc) +{ + if (!dst || !src) + pf_error(-1); + while (**src && prc) + { + if ((g_bcn & BUF_M) == BUF_M) + ft_flush(dst); + dst[g_bcn & BUF_M] = **src; + g_bcn++; + (*src)++; + prc--; + } +} + +void ft_printbuf_c(char *dst, const char **src, char c) +{ + while (**src && **src != c) + { + if ((g_bcn & BUF_M) == BUF_M) + ft_flush(dst); + dst[g_bcn & BUF_M] = **src; + g_bcn++; + (*src)++; + } +} + +void ft_printbuf_pad(char *dst, char c, int len) +{ + while (len > 0) + { + if ((g_bcn & BUF_M) == BUF_M) + ft_flush(dst); + dst[g_bcn & BUF_M] = c; + g_bcn++; + len--; + } +} diff --git a/libft/printf/print_buf_bit_float.c b/libft/printf/print_buf_bit_float.c new file mode 100644 index 0000000..c7b0f78 --- /dev/null +++ b/libft/printf/print_buf_bit_float.c @@ -0,0 +1,109 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* print_bit_float.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/12/26 16:51:17 by igarbuz #+# #+# */ +/* Updated: 2019/01/02 18:44:46 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +void ft_print_buf_bit_float(char *b, float f) +{ + int size; + int sztmp; + unsigned char *ptr; + int j; + + if (!f) + return ; + size = sizeof(float) - 1; + sztmp = size; + ptr = (unsigned char *)&f; + while (size >= 0) + { + j = 8; + while (--j >= 0) + { + if ((size == sztmp || size == (sztmp - 1)) && j == 6) + ft_printbuf_char(b, ' '); + ft_printbuf_char(b, (ptr[size] >> j & 1) + '0'); + } + size--; + } +} + +void ft_print_buf_bit_double(char *b, double db) +{ + int size; + int sztmp; + unsigned char *ptr; + int j; + + if (!db) + return ; + size = sizeof(double) - 1; + sztmp = size; + ptr = (unsigned char *)&db; + while (size >= 0) + { + j = 8; + while (--j >= 0) + { + if (size == sztmp && j == 6) + ft_printbuf_char(b, ' '); + else if (size == (sztmp - 1) && j == 3) + ft_printbuf_char(b, ' '); + ft_printbuf_char(b, (ptr[size] >> j & 1) + '0'); + } + size--; + } +} + +void ft_print_buf_bit_long_double(char *b, long double ldb) +{ + int size; + int sztmp; + unsigned char *ptr; + int j; + + if (!ldb) + return ; + size = sizeof(long double) - 7; + sztmp = size; + ptr = (unsigned char *)&ldb; + while (size >= 0) + { + j = 8; + while (--j >= 0) + { + if (size == sztmp && j == 6) + ft_printbuf_char(b, ' '); + else if (size == (sztmp - 2) && j == 7) + ft_printbuf_char(b, ' '); + ft_printbuf_char(b, (ptr[size] >> j & 1) + '0'); + } + size--; + } +} + +void ft_print_bit_float(char *b, t_param *prm, va_list valist) +{ + double db; + long double ldb; + + if (prm->p >> 9 & 1) + ldb = va_arg(valist, long double); + else + db = va_arg(valist, double); + if (prm->p >> 9 & 1) + ft_print_buf_bit_long_double(b, ldb); + else if ((prm->p >> 7 & 1) || (prm->p >> 8 & 1)) + ft_print_buf_bit_double(b, db); + else + ft_print_buf_bit_float(b, (float)db); +} diff --git a/libft/printf/print_buf_num.c b/libft/printf/print_buf_num.c new file mode 100644 index 0000000..85fc097 --- /dev/null +++ b/libft/printf/print_buf_num.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft_printf_f.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/24 13:07:04 by igarbuz #+# #+# */ +/* Updated: 2019/01/24 13:07:07 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +void ft_printbuf_unum(char *dst, uintmax_t n) +{ + if (n / 10) + ft_printbuf_unum(dst, n / 10); + ft_printbuf_char(dst, n % 10 + '0'); +} + +void ft_printbuf_snum(char *dst, intmax_t n) +{ + if (n < 0) + ft_printbuf_unum(dst, -n); + else + ft_printbuf_unum(dst, n); +} + +void ft_printbuf_oct(char *dst, uintmax_t o) +{ + if (o >> 3) + ft_printbuf_oct(dst, o >> 3); + ft_printbuf_char(dst, (o & 7) + '0'); +} + +void ft_printbuf_hex(char *dst, uintmax_t x, char c) +{ + if (x >> 4) + ft_printbuf_hex(dst, x >> 4, c); + if ((x & 15) > 9) + ft_printbuf_char(dst, (x & 15) + c); + else + ft_printbuf_char(dst, (x & 15) + '0'); +} diff --git a/libft/printf/print_buf_prefix.c b/libft/printf/print_buf_prefix.c new file mode 100644 index 0000000..eff792f --- /dev/null +++ b/libft/printf/print_buf_prefix.c @@ -0,0 +1,70 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* print_buf_prefix.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/29 20:59:34 by igarbuz #+# #+# */ +/* Updated: 2019/01/29 20:59:36 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +void ft_printbuf_sprefix(char *b, int p, intmax_t *arg) +{ + if (*arg < 0) + ft_printbuf_char(b, '-'); + else if (*arg >= 0 && (p & 1)) + ft_printbuf_char(b, '+'); + else if (*arg >= 0 && (p >> 3 & 1)) + ft_printbuf_char(b, ' '); +} + +/* +** c must be 55 (65 - 10) for HEX upper case; +** 87 (97 - 10) for hex lower case; +*/ + +void ft_printbuf_xprefix(char *b, char c, t_param *prm, intmax_t *arg) +{ + if ((c == 'x' || c == 'X') && (prm->p >> 2 & 1) && *arg) + { + ft_printbuf_char(b, '0'); + ft_printbuf_char(b, c); + } + else if (c == 'p') + { + ft_printbuf_char(b, '0'); + ft_printbuf_char(b, 'x'); + } + else if (c == 'o' && (prm->p >> 2 & 1) && (*arg || !prm->prc)) + ft_printbuf_char(b, '0'); +} + +void ft_printbuf_fprefix(char *b, int p, double *arg) +{ + uintmax_t *ptr; + + ptr = (uintmax_t *)arg; + if (!(*ptr >> 63) && (p & 1)) + ft_printbuf_char(b, '+'); + else if (!(*ptr >> 63) && (p >> 3 & 1)) + ft_printbuf_char(b, ' '); + else if (*ptr >> 63) + ft_printbuf_char(b, '-'); +} + +void ft_printbuf_long_fprefix(char *b, int p, long double *arg) +{ + uintmax_t *ptr; + + ptr = (uintmax_t *)arg; + if (!((ptr[1] >> 15) & 1) && (p & 1)) + ft_printbuf_char(b, '+'); + else if (!((ptr[1] >> 15) & 1) && (p >> 3 & 1)) + ft_printbuf_char(b, ' '); + else if ((ptr[1] >> 15) & 1) + ft_printbuf_char(b, '-'); +} diff --git a/libft/printf/print_buf_unicode.c b/libft/printf/print_buf_unicode.c new file mode 100644 index 0000000..e37ddc9 --- /dev/null +++ b/libft/printf/print_buf_unicode.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_print_buf_unicode.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/30 01:50:29 by igarbuz #+# #+# */ +/* Updated: 2019/01/30 01:50:34 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +void ft_printbuf_wchar_t(char *dst, wchar_t src, int size) +{ + int shift; + + if (!size) + return ; + if ((g_bcn & BUF_M) > BUF_M - size) + ft_flush(dst); + shift = 0; + while (--size >= 0) + { + dst[g_bcn & BUF_M] = src >> shift & 0xFF; + g_bcn++; + shift += 8; + } +} + +void ft_printbuf_unicode_prc(char *dst, wchar_t **src, int prc) +{ + while (**src && prc) + { + ft_printbuf_wchar_t(dst, ft_utf8_encode(**src), ft_wcsize(**src)); + (*src)++; + prc--; + } +} diff --git a/libft/printf/print_cs_pad.c b/libft/printf/print_cs_pad.c new file mode 100644 index 0000000..b3238fc --- /dev/null +++ b/libft/printf/print_cs_pad.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* print_cs_pad.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/29 21:09:48 by igarbuz #+# #+# */ +/* Updated: 2019/01/29 21:09:50 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +static void ft_print_c(char c, char *b, intmax_t arg, t_param *prm) +{ + char *s; + + if (c == 's') + { + if (!arg) + { + s = (char *)ft_null(); + ft_printbuf_prc(b, &s, prm->prc); + } + else if (prm->p >> 7 & 1) + ft_printbuf_unicode_prc(b, (wchar_t **)&arg, prm->prc); + else + ft_printbuf_prc(b, (char **)&arg, prm->prc); + } + else if (c == 'c') + { + if (prm->p >> 7 & 1) + ft_printbuf_wchar_t(b, ft_utf8_encode(arg), ft_wcsize(arg)); + else + ft_printbuf_char(b, (char)arg); + } + else if (c == '%') + ft_printbuf_char(b, '%'); +} + +void ft_print_c_pad(char c, char *b, t_param *prm, intmax_t arg) +{ + int len; + + len = ft_arg_len(arg, c, prm); + if (prm->prc != -1 && c == 's' && prm->prc < len) + len = prm->prc; + if (!(prm->p >> 1 & 1) && (prm->p >> 4 & 1)) + ft_printbuf_pad(b, '0', prm->wdt - len); + else if (!(prm->p >> 1 & 1)) + ft_printbuf_pad(b, ' ', prm->wdt - len); + ft_print_c(c, b, arg, prm); + if (prm->p >> 1 & 1) + ft_printbuf_pad(b, ' ', prm->wdt - len); +} diff --git a/libft/printf/print_num_pad.c b/libft/printf/print_num_pad.c new file mode 100644 index 0000000..c6ed14d --- /dev/null +++ b/libft/printf/print_num_pad.c @@ -0,0 +1,132 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* print_num_pad.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/01/29 21:05:47 by igarbuz #+# #+# */ +/* Updated: 2019/01/29 21:05:50 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +static void ft_float_default_prc(t_param *prm) +{ + if (prm->prc == -1) + prm->prc = F_ROUND; +} + +static void ft_print_i(char c, char *dst, intmax_t arg, t_param *prm) +{ + if (!arg && !prm->prc) + return ; + else if (c == 'd' || c == 'i') + ft_printbuf_snum(dst, arg); + else if (c == 'u') + ft_printbuf_unum(dst, (uintmax_t)arg); + else if (c == 'o') + ft_printbuf_oct(dst, (uintmax_t)arg); + else if (c == 'x' || c == 'p') + ft_printbuf_hex(dst, (uintmax_t)arg, 87); + else if (c == 'X') + ft_printbuf_hex(dst, (uintmax_t)arg, 55); +} + +/* +** if arg == 0 and a precision is specified : empty is preanted +** ft_arg_len() : real length rlen without format prefixes +** ft_arg_flen() : full length len +*/ + +void ft_print_i_pad(char c, char *b, t_param *prm, intmax_t arg) +{ + int flen; + int rlen; + + rlen = ft_arg_len(arg, c, prm); + flen = rlen; + if (prm->prc > rlen) + flen = prm->prc; + flen = ft_arg_flen(arg, c, prm->p, flen); + if (!(prm->p >> 1 & 1) && (!(prm->p >> 4 & 1) || prm->prc != -1)) + ft_printbuf_pad(b, ' ', prm->wdt - flen); + if (c == 'i' || c == 'd') + ft_printbuf_sprefix(b, prm->p, &arg); + else if (c == 'x' || c == 'X' || c == 'o' || c == 'p') + ft_printbuf_xprefix(b, c, prm, &arg); + if (!(prm->p >> 1 & 1) && (prm->p >> 4 & 1) && prm->prc == -1) + ft_printbuf_pad(b, '0', prm->wdt - flen); + ft_printbuf_pad(b, '0', prm->prc - rlen); + ft_print_i(c, b, arg, prm); + if ((prm->p >> 1 & 1)) + ft_printbuf_pad(b, ' ', prm->wdt - flen); +} + +/* +** ft_printf_db_pad return a string of a max length nor more than decimal LSB +*/ + +void ft_print_db_pad(char *b, t_param *prm, double *arg) +{ + const char *f; + int len; + int frc; + char tbl[DB_MAX_L]; + + ft_float_default_prc(prm); + if ((f = ft_printf_db(arg, prm->prc, tbl))) + { + len = ft_arg_db_len(f, prm->p, arg); + if ((frc = ft_fract_len(f)) >= 0 && prm->prc > frc) + len += (prm->prc - frc); + if (!(prm->p >> 1 & 1) && (!(prm->p >> 4 & 1))) + ft_printbuf_pad(b, ' ', prm->wdt - len); + ft_printbuf_fprefix(b, prm->p, arg); + if (!(prm->p >> 1 & 1) && (prm->p >> 4 & 1)) + ft_printbuf_pad(b, '0', prm->wdt - len); + if (!frc && !(prm->p >> 2 & 1) && prm->prc == 0) + ft_printbuf_c(b, &f, '.'); + else + ft_printbuf_c(b, &f, '\0'); + if (frc != -1) + ft_printbuf_pad(b, '0', prm->prc - frc); + if ((prm->p >> 1 & 1)) + ft_printbuf_pad(b, ' ', prm->wdt - len); + } +} + +/* +** ft_printf_ldb return a string of a max length nor more than +** lond double LSB in decimal; Equivalent to ft_print_db_pad +*/ + +void ft_print_long_db_pad(char *b, t_param *prm, long double *arg) +{ + const char *f; + int len; + int frc; + char tbl[DBL_MAX_L]; + + ft_float_default_prc(prm); + if ((f = ft_printf_long_db(arg, prm->prc, tbl))) + { + len = ft_arg_long_db_len(f, prm->p, arg); + if ((frc = ft_fract_len(f)) >= 0 && prm->prc > frc) + len += (prm->prc - frc); + if (!(prm->p >> 1 & 1) && (!(prm->p >> 4 & 1))) + ft_printbuf_pad(b, ' ', prm->wdt - len); + ft_printbuf_long_fprefix(b, prm->p, arg); + if (!(prm->p >> 1 & 1) && (prm->p >> 4 & 1)) + ft_printbuf_pad(b, '0', prm->wdt - len); + if (!frc && !(prm->p >> 2 & 1) && prm->prc == 0) + ft_printbuf_c(b, &f, '.'); + else + ft_printbuf_c(b, &f, '\0'); + if (frc != -1) + ft_printbuf_pad(b, '0', prm->prc - frc); + if ((prm->p >> 1 & 1)) + ft_printbuf_pad(b, ' ', prm->wdt - len); + } +} diff --git a/srcs/art/corewar_3D_logo.txt b/srcs/art/corewar_3D_logo.txt new file mode 100644 index 0000000..c864d94 --- /dev/null +++ b/srcs/art/corewar_3D_logo.txt @@ -0,0 +1,10 @@ + + ________ ________ ________ _______ ___ __ ________ ________ + |\ ____\|\ __ \|\ __ \|\ ___ \ |\ \ |\ \|\ __ \|\ __ \ + \ \ \___|\ \ \|\ \ \ \|\ \ \ __/| \ \ \ \ \ \ \ \|\ \ \ \|\ \ + \ \ \ \ \ \\\ \ \ _ _\ \ \_|/__ \ \ \ __\ \ \ \ __ \ \ _ _\ + \ \ \____\ \ \\\ \ \ \\ \\ \ \_|\ \ \ \ \|\__\_\ \ \ \ \ \ \ \\ \| + \ \_______\ \_______\ \__\\ _\\ \_______\ \ \____________\ \__\ \__\ \__\\ _\ + \|_______|\|_______|\|__|\|__|\|_______| \|____________|\|__|\|__|\|__|\|__| + +*************************************************************************************************** \ No newline at end of file diff --git a/srcs/srcs_asm/convert/conv_instru.c b/srcs/srcs_asm/convert/conv_instru.c new file mode 100644 index 0000000..142f94e --- /dev/null +++ b/srcs/srcs_asm/convert/conv_instru.c @@ -0,0 +1,140 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* conv_instru.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mdchane +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/09 11:56:37 by mdchane #+# #+# */ +/* Updated: 2019/07/16 12:44:33 by mdchane ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "asm.h" + +int ft_calc_byte(t_token *tk) +{ + char *str_size; + int size; + char *complete; + + str_size = ft_strnew(0); + while (tk) + { + if (tk && ft_strcmp(tk->contents, ",") == 0) + tk = tk->next; + if (tk->type == DIRECT || tk->type == DIRECT_LABEL) + str_size = ft_strextend(str_size, "10"); + else if (tk->type == INDIRECT || tk->type == INDIRECT_LABEL) + str_size = ft_strextend(str_size, "11"); + else if (tk->type == REGISTER) + str_size = ft_strextend(str_size, "01"); + tk = tk->next; + } + complete = ft_strnew_1(8 - ft_strlen(str_size)); + str_size = ft_strextend(str_size, complete); + size = ft_atoi_bin(str_size); + ft_strdel(&complete); + ft_strdel(&str_size); + return (size); +} + +unsigned char *ft_convert_lab(t_token *tk, int inst, + int *size_param, t_pos_lab pos_lab) +{ + unsigned char *param; + + param = NULL; + if (tk->type == DIRECT_LABEL) + { + *size_param = (g_tab[inst].size) ? 2 : 4; + param = ft_conv_hexa(get_label(tk, pos_lab.lab)->place + - pos_lab.pos, *size_param); + } + else if (tk->type == INDIRECT_LABEL) + { + *size_param = 2; + param = ft_conv_hexa(get_label(tk, pos_lab.lab)->place + - pos_lab.pos, *size_param); + } + return (param); +} + +unsigned char *ft_convert_one(t_token *tk, int inst, + int *size_param, t_pos_lab pos_lab) +{ + unsigned char *param; + + *size_param = 0; + param = NULL; + if (tk->type == DIRECT) + { + *size_param = (g_tab[inst].size) ? 2 : 4; + param = ft_conv_hexa(ft_atoi(tk->contents + 1), *size_param); + } + else if (tk->type == INDIRECT) + { + *size_param = 2; + param = ft_conv_hexa(ft_atoi(tk->contents), *size_param); + } + else if (tk->type == REGISTER) + { + *size_param = 1; + param = ft_conv_hexa(ft_atoi(tk->contents + 1), *size_param); + } + else + param = ft_convert_lab(tk, inst, size_param, pos_lab); + return (param); +} + +unsigned char *ft_str_params(t_token *tk, int inst, + int *size_params, t_pos_lab pos_lab) +{ + unsigned char *params; + unsigned char *one; + int size_one; + + *size_params = 0; + if (!(params = (unsigned char *)malloc(sizeof(*params)))) + return (NULL); + while (tk) + { + if (tk && ft_strcmp(tk->contents, ",") == 0) + tk = tk->next; + else if (tk->type == COMMENT) + break ; + one = ft_convert_one(tk, inst, &size_one, pos_lab); + params = ft_strextend_nm(params, one, *size_params, size_one); + tk = tk->next; + *size_params += size_one; + } + return (params); +} + +unsigned char *ft_conv_instru(t_token *tk, int *size_instru, + t_pos_lab pos_lab) +{ + int inst; + unsigned char *instru; + unsigned char *params; + int size_params; + + inst = is_inst(tk->contents); + if (!(instru = (unsigned char *)malloc(sizeof(*instru) + * (1 + g_tab[inst].byte)))) + return (NULL); + instru[0] = g_tab[inst].op_code; + params = ft_str_params(tk->next, inst, &size_params, pos_lab); + if (g_tab[inst].byte) + { + instru[1] = ft_calc_byte(tk); + instru = ft_strextend_nm(instru, params, 2, size_params); + *size_instru = 2 + size_params; + } + else + { + instru = ft_strextend_nm(instru, params, 1, size_params); + *size_instru = 1 + size_params; + } + return (instru); +} diff --git a/srcs/srcs_asm/convert/conv_utils.c b/srcs/srcs_asm/convert/conv_utils.c new file mode 100644 index 0000000..51acfe1 --- /dev/null +++ b/srcs/srcs_asm/convert/conv_utils.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* conv_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mdchane +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/11 12:17:14 by mdchane #+# #+# */ +/* Updated: 2019/07/11 13:28:14 by mdchane ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "asm.h" + +t_label *get_label(t_token *tk, t_label *label) +{ + char *name; + + if (tk->type == INDIRECT_LABEL) + name = ft_strdup(tk->contents + 1); + else if (tk->type == DIRECT_LABEL) + name = ft_strdup(tk->contents + 2); + else + return (NULL); + while (label) + { + if (ft_strequ(label->contents, name)) + { + ft_strdel(&name); + return (label); + } + label = label->next; + } + ft_strdel(&name); + return (NULL); +} diff --git a/srcs/srcs_asm/convert/convert.c b/srcs/srcs_asm/convert/convert.c new file mode 100644 index 0000000..e21ac5c --- /dev/null +++ b/srcs/srcs_asm/convert/convert.c @@ -0,0 +1,101 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* convert.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mdchane +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/08 11:56:22 by mdchane #+# #+# */ +/* Updated: 2019/07/16 12:45:37 by mdchane ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "asm.h" + +static int ft_create_file(char **argv, unsigned char *buffer, int len) +{ + char *str; + int len_argv; + int fd; + int i; + + len_argv = ft_strlen(argv[1]); + if (!(str = malloc(sizeof(char) * len_argv - 1))) + return (0); + str[len_argv - 2] = 0; + i = 0; + while (argv[1][i] != '.') + { + str[i] = argv[1][i]; + i++; + } + str = ft_strextend(str, ".cor"); + ft_printf("Writing output program to %s\n", str); + fd = open(str, O_CREAT, S_IRWXU, O_RDWR); + close(fd); + fd = open(str, O_RDWR); + free(str); + write(fd, buffer, len); + close(fd); + return (0); +} + +unsigned char *ft_convert_head(t_header *head) +{ + unsigned char *buffer; + unsigned char *conv_progsize; + + if (!(buffer = (unsigned char *)ft_strnew(sizeof(*buffer) * 2192))) + return (NULL); + buffer[0] = 0; + buffer[1] = 234; + buffer[2] = 131; + buffer[3] = 243; + ft_strcpy_n(buffer, (unsigned char *)head->prog_name, 4, 128); + conv_progsize = ft_conv_hexa(head->prog_size, 4); + ft_strcpy_n(buffer, conv_progsize, 136, 4); + ft_strcpy_n(buffer, (unsigned char *)head->comment, 140, 2048); + ft_strdel((char **)&conv_progsize); + return (buffer); +} + +void init_convert(unsigned char **buffer, t_header *head, int *pos) +{ + unsigned char *conv_head; + + *buffer = (unsigned char *)ft_strnew(2192 + head->prog_size); + conv_head = ft_convert_head(head); + *buffer = ft_strcpy_n(*buffer, conv_head, 0, 2192); + *pos = 2192; + ft_strdel((char **)&conv_head); +} + +void ft_convert(t_lst_tk *lst, t_header *head, char **argv, + t_label *lab) +{ + unsigned char *buffer; + int pos; + int size_instru; + t_pos_lab pos_lab; + + init_convert(&buffer, head, &pos); + pos_lab.lab = lab; + while (lst) + { + pos_lab.pos = pos - 2192; + size_instru = 0; + if (lst->token->type == INSTRUCTION) + buffer = ft_strextend_nm(buffer, ft_conv_instru(lst->token, + &size_instru, pos_lab), pos, size_instru); + else if (lst->token->type == LABEL) + { + if (lst->token->next && lst->token->next->type != COMMENT) + buffer = ft_strextend_nm(buffer, ft_conv_instru( + lst->token->next, &size_instru, pos_lab), pos, size_instru); + } + lst = lst->next; + pos += size_instru; + } + ft_create_file(argv, buffer, 2192 + head->prog_size); + ft_strdel((char **)&buffer); +} diff --git a/srcs/srcs_asm/lexer/ft_automaton.c b/srcs/srcs_asm/lexer/ft_automaton.c new file mode 100644 index 0000000..0e448b7 --- /dev/null +++ b/srcs/srcs_asm/lexer/ft_automaton.c @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_automaton.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/06/06 14:48:26 by jfleury #+# #+# */ +/* Updated: 2019/07/16 14:30:23 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "asm.h" + +static int ft_cal_i_string(int i_matrice, char *line, int *nb_char_token) +{ + int i_string; + + i_string = 0; + if (line[*nb_char_token] == 0 + || (i_matrice == 21 && line[*nb_char_token] != '\n')) + i_string = 16; + else + while (!(ft_line_strchr(g_string_automaton[i_string], + line[*nb_char_token])) && i_string < 15) + i_string++; + return (i_string); +} + +static int ft_end_error(int *nb_char_token, char *line) +{ + g_nb_char = g_nb_char + *nb_char_token; + ft_printf("error: syntax %d:%d char: %c\n", + g_nb_line, g_nb_char + 1, line[*nb_char_token]); + return (0); +} + +int ft_automaton(char *line, t_token *token, t_label *label, + int *nb_char_token) +{ + int i_matrice; + int i_string; + int type; + + i_matrice = 0; + while (1) + { + type = i_matrice; + i_string = ft_cal_i_string(i_matrice, line, nb_char_token); + i_matrice = g_matrice_automaton[i_matrice][i_string]; + if (i_matrice == -2 || i_matrice == -3) + { + if (i_matrice == -3) + *nb_char_token = *nb_char_token - 1; + g_nb_char = g_nb_char + *nb_char_token; + ft_create_token_label(ft_create_string(line, *nb_char_token), + type, token, label); + return (1); + } + else if (i_matrice == -1) + return (ft_end_error(nb_char_token, line)); + else + *nb_char_token = *nb_char_token + 1; + } +} diff --git a/srcs/srcs_asm/lexer/ft_lexer.c b/srcs/srcs_asm/lexer/ft_lexer.c new file mode 100644 index 0000000..e27bdc2 --- /dev/null +++ b/srcs/srcs_asm/lexer/ft_lexer.c @@ -0,0 +1,95 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lexer.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/05/30 10:48:23 by jfleury #+# #+# */ +/* Updated: 2019/07/16 16:20:57 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "asm.h" + +static int ft_free_line(char **line, char **tmp) +{ + free(*line); + free(*tmp); + return (0); +} + +static void ft_free_error(t_token *token, t_label *label, + t_token *list_token) +{ + t_token *tmp; + + free(token); + free(label); + if (list_token != NULL) + { + while (list_token != NULL) + { + tmp = list_token->next; + free(list_token->contents); + free(list_token); + list_token = tmp; + } + } +} + +t_token *ft_lexer_token(char **line, t_label **list_label) +{ + t_token *token; + t_label *label; + t_token *list_token; + int nb_char_token; + + list_token = NULL; + while ((*line)[0] != '\n' && (*line)[0] != 0) + { + if (!(token = malloc(sizeof(t_token)))) + return (NULL); + if (!(label = malloc(sizeof(t_label)))) + return (NULL); + nb_char_token = 0; + if (!(ft_automaton(*line, token, label, &nb_char_token))) + { + ft_free_error(token, label, list_token); + return (NULL); + } + ft_append_token_label(token, &list_token, label, list_label); + if (!(line = ft_cut_line(line, nb_char_token))) + break ; + } + return (list_token); +} + +int ft_lexer(t_lst_tk **list, t_label **list_label, char **argv) +{ + char *line; + int fd; + static char *tmp; + + fd = open(argv[1], O_RDONLY); + line = NULL; + while (get_next_line(fd, &line, &tmp) == 1) + { + g_nb_char = 0; + g_nb_line++; + if (!(line = ft_check_line(line, fd))) + return (ft_free_line(&line, &tmp)); + if (!(ft_empty_line(line))) + { + free(line); + continue ; + } + if (!(ft_append_lst_tk(ft_lexer_token(&line, list_label), list))) + return (ft_free_line(&line, &tmp)); + free(line); + } + if (line != NULL) + free(line); + free(tmp); + return (1); +} diff --git a/srcs/srcs_asm/lexer/lib_lexer/ft_append_lst_tk.c b/srcs/srcs_asm/lexer/lib_lexer/ft_append_lst_tk.c new file mode 100644 index 0000000..44c50db --- /dev/null +++ b/srcs/srcs_asm/lexer/lib_lexer/ft_append_lst_tk.c @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_append_lst_tk.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/06/24 11:38:38 by jfleury #+# #+# */ +/* Updated: 2019/07/01 11:22:10 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "asm.h" + +t_lst_tk *ft_create_lst_tk(t_token *token) +{ + t_lst_tk *new; + + if (token == NULL) + return (NULL); + if (!(new = (t_lst_tk*)malloc(sizeof(t_lst_tk)))) + return (NULL); + new->token = token; + new->next = NULL; + return (new); +} + +int ft_append_lst_tk(t_token *token, t_lst_tk **list) +{ + t_lst_tk *begin; + + begin = *list; + if (token == NULL) + return (0); + if (*list == NULL) + { + if (!(*list = ft_create_lst_tk(token))) + return (1); + } + else + { + while ((*list)->next != NULL) + *list = (*list)->next; + if (!((*list)->next = ft_create_lst_tk(token))) + return (1); + *list = begin; + } + return (1); +} diff --git a/srcs/srcs_asm/lexer/lib_lexer/ft_append_token_label.c b/srcs/srcs_asm/lexer/lib_lexer/ft_append_token_label.c new file mode 100644 index 0000000..9d6412a --- /dev/null +++ b/srcs/srcs_asm/lexer/lib_lexer/ft_append_token_label.c @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_append_token_label.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/06/14 13:42:16 by jfleury #+# #+# */ +/* Updated: 2019/07/15 12:56:53 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "asm.h" + +static void ft_append_label(t_label *label, t_label **list_label) +{ + t_label *tmp_label; + + if (*list_label == NULL) + *list_label = label; + else + { + tmp_label = *list_label; + while (tmp_label->next != NULL) + tmp_label = tmp_label->next; + tmp_label->next = label; + } +} + +void ft_append_token_label(t_token *token, t_token **list_token, + t_label *label, t_label **list_label) +{ + t_token *tmp_token; + + if (*list_token == NULL) + *list_token = token; + else + { + tmp_token = *list_token; + while (tmp_token->next != NULL) + tmp_token = tmp_token->next; + tmp_token->next = token; + tmp_token->line = g_nb_line; + } + if (token->type == 1) + ft_append_label(label, list_label); + else + free(label); +} diff --git a/srcs/srcs_asm/lexer/lib_lexer/ft_check_line.c b/srcs/srcs_asm/lexer/lib_lexer/ft_check_line.c new file mode 100644 index 0000000..2b44ffe --- /dev/null +++ b/srcs/srcs_asm/lexer/lib_lexer/ft_check_line.c @@ -0,0 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_check_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/06/24 11:24:43 by jfleury #+# #+# */ +/* Updated: 2019/07/15 18:19:05 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "asm.h" + +static int ft_cal_state(char *line, int state) +{ + int i; + + i = 0; + state = 0; + while (line[i] != 0) + { + if (line[i] == '"') + state = state + 1; + i++; + } + return (state); +} + +static char *ft_error_gnl(char *str, char *line, char *tmp) +{ + ft_strdel(&str); + ft_strdel(&line); + ft_strdel(&tmp); + ft_printf("error: quote `\n"); + return (NULL); +} + +char *ft_check_line(char *line, int fd) +{ + char *str; + static char *tmp; + int i; + int state; + + i = 0; + state = 0; + state = ft_cal_state(line, state); + while (state % 2 == 1) + { + if (get_next_line(fd, &str, &tmp) != 1) + return (ft_error_gnl(str, line, tmp)); + if (!(line = ft_strextend(line, "\n"))) + return (NULL); + if (!(line = ft_strextend(line, str))) + return (NULL); + ft_strdel(&str); + g_nb_line++; + g_nb_char = 0; + state = ft_cal_state(line, state); + } + line = ft_strextend(line, "\n"); + return (line); +} diff --git a/srcs/srcs_asm/lexer/lib_lexer/ft_create_string.c b/srcs/srcs_asm/lexer/lib_lexer/ft_create_string.c new file mode 100644 index 0000000..4054ae4 --- /dev/null +++ b/srcs/srcs_asm/lexer/lib_lexer/ft_create_string.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_create_string.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/06/14 13:57:44 by jfleury #+# #+# */ +/* Updated: 2019/07/09 10:44:33 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "asm.h" + +static char *ft_cut_string(char *str) +{ + char *str2; + int i; + int j; + + i = ft_strlen(str); + j = 0; + while (str[i - 1] == ' ' || str[i - 1] == '\t' + || str[i - 1] == '\v' || str[i - 1] == '\n') + i--; + if (!(str2 = ft_strnew(i + 1))) + return (NULL); + while (j < i) + { + str2[j] = str[j]; + j++; + } + free(str); + str = str2; + return (str); +} + +char *ft_create_string(char *line, int nb_char_token) +{ + char *str; + int i; + int j; + + i = 0; + j = 0; + while (line[i] == ' ' || line[i] == '\t' || line[i] == '\v') + i++; + if (!(str = ft_strnew(nb_char_token - i))) + return (NULL); + while (j <= nb_char_token - i - 1) + { + str[j] = line[j + i]; + j++; + } + str = ft_cut_string(str); + return (str); +} diff --git a/srcs/srcs_asm/lexer/lib_lexer/ft_create_token_label.c b/srcs/srcs_asm/lexer/lib_lexer/ft_create_token_label.c new file mode 100644 index 0000000..61a8fe4 --- /dev/null +++ b/srcs/srcs_asm/lexer/lib_lexer/ft_create_token_label.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_create_token_label.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/06/14 14:03:27 by jfleury #+# #+# */ +/* Updated: 2019/07/15 11:03:20 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "asm.h" + +void ft_create_token_label(char *str, int type, t_token *token, + t_label *label) +{ + if (type == 1) + { + label->contents = str; + label->next = NULL; + } + token->type = type; + token->line = g_nb_line; + token->contents = str; + token->next = NULL; +} diff --git a/srcs/srcs_asm/lexer/lib_lexer/ft_cut_line.c b/srcs/srcs_asm/lexer/lib_lexer/ft_cut_line.c new file mode 100644 index 0000000..de8da43 --- /dev/null +++ b/srcs/srcs_asm/lexer/lib_lexer/ft_cut_line.c @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_cut_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/06/14 13:53:27 by jfleury #+# #+# */ +/* Updated: 2019/07/09 10:44:50 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "asm.h" + +static int ft_check_line2(char *str) +{ + int i; + + i = 0; + while (str[i] != 0) + { + if (str[i] != ' ' && str[i] != '\n' && str[i] != '\t' && str[i] != '\v') + return (1); + i++; + } + return (0); +} + +char **ft_cut_line(char **line, int nb_char_token) +{ + int len; + int i; + char *str; + + i = 0; + len = ft_strlen(*line); + if (len < 1 || len == nb_char_token) + return (NULL); + if (!(str = ft_strnew(len - nb_char_token + 1))) + return (NULL); + while ((*line)[i + nb_char_token] != 0) + { + str[i] = (*line)[nb_char_token + i]; + i++; + } + if (!(ft_check_line2(str))) + { + free(str); + return (NULL); + } + free(*line); + *line = str; + return (line); +} diff --git a/srcs/srcs_asm/lexer/lib_lexer/ft_empty_line.c b/srcs/srcs_asm/lexer/lib_lexer/ft_empty_line.c new file mode 100644 index 0000000..a8d0fe9 --- /dev/null +++ b/srcs/srcs_asm/lexer/lib_lexer/ft_empty_line.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_empty_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/06/24 15:13:26 by jfleury #+# #+# */ +/* Updated: 2019/06/24 15:16:17 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "asm.h" + +int ft_empty_line(char *line) +{ + int i; + + i = 0; + while (line[i] == ' ' || line[i] == '\t' || line[i] == '\v' + || line[i] == '\n') + i++; + if (line[i] == 0) + return (0); + return (1); +} diff --git a/srcs/srcs_asm/lexer/lib_lexer/ft_free_list.c b/srcs/srcs_asm/lexer/lib_lexer/ft_free_list.c new file mode 100644 index 0000000..81807c6 --- /dev/null +++ b/srcs/srcs_asm/lexer/lib_lexer/ft_free_list.c @@ -0,0 +1,62 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_free_list.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mdchane +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/06/25 11:04:41 by jfleury #+# #+# */ +/* Updated: 2019/07/13 15:11:14 by mdchane ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "asm.h" + +static void ft_free_token(t_token *token) +{ + t_token *tmp; + + tmp = NULL; + while (token != NULL) + { + tmp = token->next; + ft_strdel(&token->contents); + free(token); + token = tmp; + } +} + +static void ft_free_label(t_label *label) +{ + t_label *tmp; + + tmp = NULL; + while (label != NULL) + { + tmp = label->next; + free(label); + label = tmp; + } +} + +void ft_free_list(t_lst_tk *list, t_label *label) +{ + t_lst_tk *tmp; + + tmp = NULL; + while (list != NULL) + { + tmp = list->next; + ft_free_token(list->token); + free(list); + list = tmp; + } + ft_free_label(label); +} + +void ft_free_all(t_lst_tk *list, t_label *label, t_header *head) +{ + if (head) + free(head); + ft_free_list(list, label); +} diff --git a/srcs/srcs_asm/lexer/lib_lexer/ft_line_strchr.c b/srcs/srcs_asm/lexer/lib_lexer/ft_line_strchr.c new file mode 100644 index 0000000..4056789 --- /dev/null +++ b/srcs/srcs_asm/lexer/lib_lexer/ft_line_strchr.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_line_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/06/14 13:55:57 by jfleury #+# #+# */ +/* Updated: 2019/06/14 13:56:11 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "asm.h" + +int ft_line_strchr(char *str, char c) +{ + int i; + + i = 0; + while (str[i] != 0) + { + if (str[i] == c) + return (1); + i++; + } + return (0); +} diff --git a/srcs/srcs_asm/lexer/src_variable/var_automaton.c b/srcs/srcs_asm/lexer/src_variable/var_automaton.c new file mode 100644 index 0000000..a959e2d --- /dev/null +++ b/srcs/srcs_asm/lexer/src_variable/var_automaton.c @@ -0,0 +1,73 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* var_automaton.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/06/06 10:13:09 by jfleury #+# #+# */ +/* Updated: 2019/07/02 17:22:36 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "asm.h" + +int g_nb_line = 0; + +int g_nb_char = 0; + +int g_nb_token = 0; + +int g_matrice_automaton[30][17] = +{ + {23, 23, -1, 24, 26, 17, 19, 9, 29, -1, 12, 21, 21, 22, 0, 0, -1}, + {-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2}, + {-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2}, + {-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3}, + {-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3}, + {-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3}, + {-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3}, + {-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3}, + {-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2}, + {-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2}, + {-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3}, + {-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2}, + {12, 12, -1, 12, -1, 1, -1, -1, -1, -1, 12, -1, -1, -1, -1, 1, -1}, + {13, 13, -1, 13, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, 2, -1}, + {-1, -1, -1, 14, -1, -1, -1, 3, -1, -1, -1, 3, 3, -1, 3, 3, -1}, + {15, 15, -1, 15, -1, -1, -1, 4, -1, -1, 15, 4, 4, -1, 4, 4, -1}, + {-1, -1, -1, 16, -1, -1, -1, 5, -1, -1, -1, 5, 5, -1, 5, 5, -1}, + {17, 17, -1, 17, -1, -1, -1, 6, -1, -1, 17, 6, 6, -1, 6, 6, -1}, + {-1, -1, -1, 18, -1, -1, -1, 7, -1, -1, -1, 7, 7, -1, 7, 7, -1}, + {19, 19, 19, 19, 19, 19, 8, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19}, + {-1, -1, -1, -1, -1, -1, -1, 9, -1, -1, -1, -1, -1, -1, 9, 9, -1}, + {21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 10, 21}, + {22, 22, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 11, 11, -1}, + {23, 23, -1, 25, -1, 1, -1, -1, -1, -1, 12, -1, -1, -1, 2, 2, -1}, + {12, 12, -1, 24, -1, 1, -1, 5, -1, -1, 12, -1, -1, -1, 5, 5, -1}, + {12, 12, -1, 25, -1, 1, -1, 7, -1, -1, 12, 7, 7, -1, 7, 7, -1}, + {-1, -1, -1, 14, -1, 15, -1, -1, 14, -1, -1, -1, -1, -1, -1, -1, -1}, + {-1, -1, -1, 16, -1, 17, -1, -1, 16, -1, -1, -1, -1, -1, -1, -1, -1}, + {-1, -1, -1, 28, -1, -1, -1, 6, -1, -1, -1, 6, 6, -1, 6, 6, -1}, + {-1, -1, -1, 16, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} +}; + +char *g_string_automaton[16] = +{ + "r", + "abcdefghijklmnopqstuvwxyz", + "ABCDEFGHIJKLMNOPQRSTUVWXYZ", + "0123456789", + "%", + ":", + "\"", + ",", + "-", + "+", + "_", + "#", + ";", + ".", + "\t\f ", + "\n" +}; diff --git a/srcs/srcs_asm/main_asm.c b/srcs/srcs_asm/main_asm.c new file mode 100644 index 0000000..6999e1a --- /dev/null +++ b/srcs/srcs_asm/main_asm.c @@ -0,0 +1,112 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main_asm.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/05/28 17:08:18 by jfleury #+# #+# */ +/* Updated: 2019/07/16 16:23:43 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "asm.h" + +static void ft_check_fd(int fd, char **argv) +{ + if (fd == -1) + { + ft_printf("error: no such file or directory: '%s'\n", argv[1]); + close(fd); + exit(0); + } +} + +static void ft_check_arg(int argc, char **argv) +{ + int i; + int fd; + + if (argc == 1) + { + ft_printf("usage: ./asm \n"); + exit(0); + } + if (argc > 2) + { + ft_printf("error: too many files / " + "only one file accepted\n"); + exit(0); + } + i = ft_strlen(argv[1]); + fd = open(argv[1], O_RDONLY); + ft_check_fd(fd, argv); + close(fd); + if (i >= 3 && argv[1][i - 1] == 's' && argv[1][i - 2] == '.') + return ; + else + { + ft_printf("error: invalid file\n"); + exit(0); + } +} + +t_header *init_header(void) +{ + t_header *head; + int i; + + if (!(head = (t_header *)ft_memalloc(sizeof(t_header)))) + return (NULL); + head->magic = 15369203; + head->prog_size = 0; + i = -1; + while (++i < PROG_NAME_LENGTH) + head->prog_name[i] = 0; + head->prog_name[i] = '\0'; + i = -1; + while (++i < COMMENT_LENGTH) + head->comment[i] = 0; + head->comment[i] = '\0'; + return (head); +} + +static int ft_lexer_parser(t_header *head, t_label *list_label, + t_lst_tk *lst_tk, char **argv) +{ + if (!(ft_lexer(&lst_tk, &list_label, argv))) + { + ft_free_all(lst_tk, list_label, head); + return (0); + } + if (lst_tk == NULL) + { + ft_free_all(lst_tk, list_label, head); + ft_printf("error: empty file / directory file\n"); + return (0); + } + if (!(ft_parser(lst_tk, list_label, head))) + { + ft_free_all(lst_tk, list_label, head); + return (0); + } + ft_convert(lst_tk, head, argv, list_label); + ft_free_all(lst_tk, list_label, head); + return (1); +} + +int main(int argc, char **argv) +{ + t_header *head; + t_label *list_label; + t_lst_tk *lst_tk; + + ft_check_arg(argc, argv); + if (!(head = init_header())) + return (0); + list_label = NULL; + lst_tk = NULL; + if (!(ft_lexer_parser(head, list_label, lst_tk, argv))) + return (0); + return (0); +} diff --git a/srcs/srcs_asm/op.c b/srcs/srcs_asm/op.c new file mode 100644 index 0000000..5a4e9da --- /dev/null +++ b/srcs/srcs_asm/op.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* op.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jfleury +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/13 12:13:23 by mdchane #+# #+# */ +/* Updated: 2019/07/15 18:30:09 by jfleury ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "asm.h" + +t_op g_tab[17] = +{ + {"live", 1, {T_DIR}, 1, 10, "alive", 0, 0}, + {"ld", 2, {T_DIR | T_IND, T_REG}, 2, 5, "load", 1, 0}, + {"st", 2, {T_REG, T_IND | T_REG}, 3, 5, "store", 1, 0}, + {"add", 3, {T_REG, T_REG, T_REG}, 4, 10, "addition", 1, 0}, + {"sub", 3, {T_REG, T_REG, T_REG}, 5, 10, "soustraction", 1, 0}, + {"and", 3, {T_REG | T_DIR | T_IND, T_REG | T_IND | T_DIR, T_REG}, 6, 6, + "et (and r1, r2, r3 r1&r2 -> r3", 1, 0}, + {"or", 3, {T_REG | T_IND | T_DIR, T_REG | T_IND | T_DIR, T_REG}, 7, 6, + "ou (or r1, r2, r3 r1 | r2 -> r3", 1, 0}, + {"xor", 3, {T_REG | T_IND | T_DIR, T_REG | T_IND | T_DIR, T_REG}, 8, 6, + "ou (xor r1, r2, r3 r1^r2 -> r3", 1, 0}, + {"zjmp", 1, {T_DIR | T_IND}, 9, 20, "jump if zero", 0, 1}, + {"ldi", 3, {T_REG | T_DIR | T_IND, T_DIR | T_REG | T_IND, T_REG}, 10, 25, + "load index", 1, 1}, + {"sti", 3, {T_REG, T_REG | T_DIR | T_IND, T_DIR | T_REG}, 11, 25, + "store index", 1, 1}, + {"fork", 1, {T_DIR | T_IND}, 12, 800, "fork", 0, 1}, + {"lld", 2, {T_DIR | T_IND, T_REG}, 13, 10, "long load", 1, 0}, + {"lldi", 3, {T_REG | T_DIR | T_IND, T_DIR | T_REG, T_REG}, 14, 50, + "long load index", 1, 1}, + {"lfork", 1, {T_DIR}, 15, 1000, "long fork", 0, 1}, + {"aff", 1, {T_REG}, 16, 2, "aff", 1, 0} +}; diff --git a/srcs/srcs_asm/parser/ft_cal_progsize.c b/srcs/srcs_asm/parser/ft_cal_progsize.c new file mode 100644 index 0000000..b8ee633 --- /dev/null +++ b/srcs/srcs_asm/parser/ft_cal_progsize.c @@ -0,0 +1,83 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_cal_progsize.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mdchane +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/02 15:20:20 by jfleury #+# #+# */ +/* Updated: 2019/07/13 15:26:28 by mdchane ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "asm.h" + +static unsigned char *ft_resize(unsigned char *str, int size) +{ + int len; + int i; + int j; + unsigned char *str2; + + i = 0; + j = 0; + len = ft_strlen((char*)str); + if (!(str2 = malloc(sizeof(char) * (size * 2)))) + return (NULL); + while (i < size * 2 - len) + { + str2[i] = '0'; + i++; + } + while (i < size * 2) + { + str2[i] = str[j]; + i++; + j++; + } + free(str); + return (str2); +} + +static unsigned char ft_atoi_hex(unsigned char *str) +{ + unsigned char nb; + + if (str[0] >= '0' && str[0] <= '9') + str[0] = str[0] - 48; + if (str[1] >= '0' && str[1] <= '9') + str[1] = str[1] - 48; + if (str[0] >= 'a' && str[0] <= 'f') + str[0] = str[0] - 87; + if (str[1] >= 'a' && str[1] <= 'f') + str[1] = str[1] - 87; + if (str[0] != 0) + nb = (str[0] * 16) + str[1]; + else + nb = str[1]; + return (nb); +} + +unsigned char *ft_conv_hexa(int nbr, int size) +{ + unsigned char *str; + unsigned char *final_str; + int len; + + if (!(final_str = (unsigned char*)ft_strnew(size))) + return (NULL); + if (size == 2) + str = (unsigned char *)ft_itoa_base_short(nbr, 16); + else + str = (unsigned char *)ft_itoa_base_int(nbr, 16); + len = ft_strlen((char*)str); + if (len < size * 2) + str = ft_resize(str, size); + while (size * 2 > 0) + { + final_str[size - 1] = ft_atoi_hex(str + ((size * 2) - 2)); + size--; + } + ft_strdel((char **)&str); + return (final_str); +} diff --git a/srcs/srcs_asm/parser/ft_parse_command.c b/srcs/srcs_asm/parser/ft_parse_command.c new file mode 100644 index 0000000..31ad4eb --- /dev/null +++ b/srcs/srcs_asm/parser/ft_parse_command.c @@ -0,0 +1,77 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_parse_command.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mdchane +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/06/17 11:24:46 by mdchane #+# #+# */ +/* Updated: 2019/07/16 15:35:23 by mdchane ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "asm.h" + +int ft_parse_name(t_token *tk, t_header *head) +{ + if (!tk || !tk->contents) + return (ft_printf("error: no champion name in the file\n") && 0); + if (ft_strlen(tk->contents) > PROG_NAME_LENGTH + 2) + { + return (ft_printf("error: Champion name too long (Max length 128)\n") + && 0); + } + if (tk->next && tk->next->type != COMMENT) + { + return (ft_printf("error: syntax: Line %d: " + "too mush arguments for champion name\n", tk->line) && 0); + } + ft_strcpy_1(head->prog_name, tk->contents); + return (1); +} + +int ft_parse_champ_cmt(t_token *tk, t_header *head) +{ + if (!tk || !tk->contents) + return (ft_printf("error: no champion comment in the file\n") && 0); + if (ft_strlen(tk->contents) > COMMENT_LENGTH + 2) + { + return (ft_printf("error: Champion comment too long " + "(Max length 2048)\n") + && 0); + } + if (tk->next && tk->next->type != COMMENT) + { + return (ft_printf("error: syntax: Line %d: " + "too mush arguments for champion comment\n", tk->line) && 0); + } + ft_strcpy_1(head->comment, tk->contents); + return (1); +} + +int parse_header(t_lst_tk **lst, t_header *head) +{ + while ((*lst) && (*lst)->token && (*lst)->token->type == COMMENT) + (*lst) = (*lst)->next; + if ((*lst) && (*lst)->token && (*lst)->token->type == COMMAND + && ft_strcmp((*lst)->token->contents, NAME_CMD_STRING) == 0) + { + if (!ft_parse_name((*lst)->token->next, head)) + return (0); + } + else + return (ft_printf("error: no name in the file\n") && 0); + (*lst) = (*lst)->next; + while ((*lst) && (*lst)->token && (*lst)->token->type == COMMENT) + (*lst) = (*lst)->next; + if ((*lst) && (*lst)->token && (*lst)->token->type == COMMAND + && ft_strcmp((*lst)->token->contents, COMMENT_CMD_STRING) == 0) + { + if (!ft_parse_champ_cmt((*lst)->token->next, head)) + return (0); + } + else + return (ft_printf("error: no champion comment in the file\n") && 0); + (*lst) = (*lst)->next; + return (1); +} diff --git a/srcs/srcs_asm/parser/ft_parse_instruc.c b/srcs/srcs_asm/parser/ft_parse_instruc.c new file mode 100644 index 0000000..35070a8 --- /dev/null +++ b/srcs/srcs_asm/parser/ft_parse_instruc.c @@ -0,0 +1,125 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_parse_instruc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mdchane +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/06/17 16:17:56 by mdchane #+# #+# */ +/* Updated: 2019/07/16 15:36:42 by mdchane ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "asm.h" + +int is_inst(char *name) +{ + int i; + + if (!name) + return (-1); + i = 0; + while (i < 16 && ft_strcmp(name, g_tab[i].name) != 0) + i++; + if (i == 16) + return (-1); + else + return (i); +} + +int ft_verif_param(t_token *tk, int inst, int num_param, t_label *label) +{ + int type; + + if (tk->type == INDIRECT + || (tk->type == INDIRECT_LABEL && get_label(tk, label))) + type = T_IND; + else if (tk->type == DIRECT + || (tk->type == DIRECT_LABEL && get_label(tk, label))) + type = T_DIR; + else if (tk->type == REGISTER && ft_atoi(tk->contents + 1) < 100) + type = T_REG; + else + type = 0; + if (g_tab[inst].poss[num_param] & type) + return (1); + else + { + ft_printf("error: l%d : Invalid parameter %d for instruction %s\n", + tk->line, num_param, g_tab[inst].name); + return (0); + } +} + +int ft_correct_instru(t_token *tk, int i, t_label *label, int line) +{ + int num_param; + + num_param = 0; + while (tk && num_param < g_tab[i].nb_param) + { + if (ft_verif_param(tk, i, num_param, label)) + { + tk = tk->next; + if (!tk && num_param == g_tab[i].nb_param - 1) + return (1); + else if (tk && tk->type == COMMENT + && !tk->next && num_param == g_tab[i].nb_param - 1) + return (1); + else if (tk && ft_strcmp(tk->contents, ",") == 0) + num_param++; + else + return (ft_printf("error: l%d: bad param %d for instru %s\n", + line, num_param + 1, g_tab[i].name) && 0); + } + else + return (0); + tk = tk->next; + } + return (ft_printf("error: l%d: bad params for instru %s\n", + line, g_tab[i].name) && 0); +} + +int ft_calc_size(t_token *tk, int inst) +{ + int size; + + size = 1 + g_tab[inst].byte; + while (tk) + { + if (tk->type == INDIRECT || tk->type == INDIRECT_LABEL) + size += 2; + else if (tk->type == DIRECT || tk->type == DIRECT_LABEL) + size += (g_tab[inst].size) ? 2 : 4; + else if (tk->type == REGISTER) + size += 1; + tk = tk->next; + } + return (size); +} + +int ft_parse_instruct(t_token *tk, t_label *label, unsigned int *size) +{ + int inst; + + inst = is_inst(tk->contents); + if (inst < 0) + { + return (ft_printf("error: l%d: invalid instruction '%s'\n", + tk->line, tk->contents) && 0); + } + if (!tk->next) + { + return (ft_printf("error: l%d: no params for instru %s\n", + tk->line, tk->contents) && 0); + } + if (ft_correct_instru(tk->next, inst, label, tk->line)) + { + *size += ft_calc_size(tk->next, inst); + return (1); + } + else + { + return (0); + } +} diff --git a/srcs/srcs_asm/parser/ft_parser.c b/srcs/srcs_asm/parser/ft_parser.c new file mode 100644 index 0000000..e79b690 --- /dev/null +++ b/srcs/srcs_asm/parser/ft_parser.c @@ -0,0 +1,73 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_parser.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mdchane +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/05/30 10:48:19 by jfleury #+# #+# */ +/* Updated: 2019/07/16 12:23:55 by mdchane ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "asm.h" + +void ft_label_pos(t_label *label, char *tk_lab, int size) +{ + while (label) + { + if (ft_strequ(label->contents, tk_lab)) + { + label->place = size; + } + label = label->next; + } +} + +void init_label(t_label *label) +{ + while (label) + { + label->contents[ft_strlen(label->contents) - 1] = '\0'; + label = label->next; + } +} + +int ft_parse_label(t_label *label, t_token *token, unsigned int *size) +{ + ft_label_pos(label, token->contents, *size); + if (!token->next || token->next->type == COMMENT) + return (1); + else if (!ft_parse_instruct(token->next, label, size)) + return (0); + return (1); +} + +int ft_parser(t_lst_tk *lst, t_label *label, t_header *head) +{ + unsigned int size; + + init_label(label); + size = 0; + if (!parse_header(&lst, head)) + return (0); + while (lst) + { + if (lst->token->type == INSTRUCTION) + { + if (!ft_parse_instruct(lst->token, label, &size)) + return (0); + } + else if (lst->token->type == LABEL) + { + if (!(ft_parse_label(label, lst->token, &size))) + return (0); + } + else if (lst->token->type != COMMENT) + return (0); + lst = lst->next; + } + if ((head->prog_size = size) == 0) + return (ft_printf("error: no instruction in the file\n") && 0); + return (1); +} diff --git a/srcs/srcs_asm/parser/parse_utils.c b/srcs/srcs_asm/parser/parse_utils.c new file mode 100644 index 0000000..022f61a --- /dev/null +++ b/srcs/srcs_asm/parser/parse_utils.c @@ -0,0 +1,95 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mdchane +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/06/06 17:08:25 by mdchane #+# #+# */ +/* Updated: 2019/07/13 17:14:11 by mdchane ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "asm.h" + +unsigned char *ft_strextend_nm(unsigned char *s1, + char unsigned *s2, int n, int m) +{ + unsigned char *str; + int i; + + if (s1 == NULL || s2 == NULL) + return (NULL); + i = n + m; + if (!(str = (unsigned char *)malloc(sizeof(*str) * i + 1))) + return (NULL); + ft_strcpy_n(str, s1, 0, n); + ft_strcpy_n(str, s2, n, m); + ft_strdel((char **)&s1); + ft_strdel((char **)&s2); + return (str); +} + +int ft_tklen(t_token *tk) +{ + int i; + + i = 0; + if (!tk) + return (0); + while (tk) + { + i++; + tk = tk->next; + } + return (i); +} + +unsigned char *ft_strcpy_n(unsigned char *dest, + const unsigned char *src, int n, int m) +{ + int i; + int j; + + i = 0; + while (i < n) + i++; + j = 0; + while (j < m) + { + dest[i] = src[j]; + i++; + j++; + } + return (dest); +} + +char *ft_strnew_1(size_t size) +{ + char *str; + + if (!(str = (char*)malloc(sizeof(char) * size + 1))) + return (NULL); + ft_memset(str, '0', size); + str[size] = '\0'; + return (str); +} + +char *ft_strcpy_1(char *dest, const char *src) +{ + unsigned int i; + unsigned int j; + + i = 0; + j = 0; + while (src[j] != '\0') + { + if (src[j] != '"') + { + dest[i] = src[j]; + i++; + } + j++; + } + return (dest); +} diff --git a/srcs/srcs_vm/check_args.c b/srcs/srcs_vm/check_args.c new file mode 100644 index 0000000..132196c --- /dev/null +++ b/srcs/srcs_vm/check_args.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* check_args.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/05/22 13:34:11 by tmaze #+# #+# */ +/* Updated: 2019/05/22 13:41:20 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "corewar.h" + +int check_file(char *path) +{ + char *ext; + + return (path && (ext = ft_strrchr(path, '.')) && ft_strequ(ext, ".cor")); +} diff --git a/srcs/srcs_vm/cw_add_sub.c b/srcs/srcs_vm/cw_add_sub.c new file mode 100644 index 0000000..0de8883 --- /dev/null +++ b/srcs/srcs_vm/cw_add_sub.c @@ -0,0 +1,61 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cw_add_sub.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/06/19 20:48:20 by tmaze #+# #+# */ +/* Updated: 2019/07/10 15:12:46 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "corewar.h" + +char cw_math_common(t_vm *vm, t_process *p, t_args *ag, int *res) +{ + init_args(ag, p, vm); + res[3] = cw_ocp(3, ag, 4); + if (ag[0].t == REG_CODE && check_reg(vm, p->pc + 2) + && ag[1].t == REG_CODE && check_reg(vm, p->pc + 3) + && ag[2].t == REG_CODE && check_reg(vm, p->pc + 4)) + { + res[0] = read_mem(vm, p->pc + 2); + res[1] = read_mem(vm, p->pc + 3); + res[2] = read_mem(vm, p->pc + 4); + return (1); + } + return (0); +} + +void cw_add(t_process *p, t_vm *vm) +{ + t_args ag[4]; + int res[4]; + + if (cw_math_common(vm, p, ag, res)) + { + if (vm->args & AG_VERB && vm->verb & VB_OPS) + ft_printf("P %4d | add r%d r%d r%d\n", p->pid, res[0], res[1] + , res[2]); + p->regs[res[2]] = p->regs[res[0]] + p->regs[res[1]]; + p->carry = (!p->regs[res[2]]) ? 1 : 0; + } + cw_move_pc(vm, p, res[3]); +} + +void cw_sub(t_process *p, t_vm *vm) +{ + t_args ag[4]; + int res[4]; + + if (cw_math_common(vm, p, ag, res)) + { + if (vm->args & AG_VERB && vm->verb & VB_OPS) + ft_printf("P %4d | sub r%d r%d r%d\n", p->pid, res[0], res[1] + , res[2]); + p->regs[res[2]] = p->regs[res[0]] - p->regs[res[1]]; + p->carry = (!p->regs[res[2]]) ? 1 : 0; + } + cw_move_pc(vm, p, res[3]); +} diff --git a/srcs/srcs_vm/cw_aff.c b/srcs/srcs_vm/cw_aff.c new file mode 100644 index 0000000..efc90ce --- /dev/null +++ b/srcs/srcs_vm/cw_aff.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cw_aff.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/06/19 20:58:20 by tmaze #+# #+# */ +/* Updated: 2019/07/10 17:23:37 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "corewar.h" + +void cw_aff(t_process *p, t_vm *vm) +{ + t_args ag[4]; + int jmp; + int val; + char c; + + init_args(ag, p, vm); + jmp = cw_ocp(1, ag, 4); + if (ag[0].t == REG_CODE && check_reg(vm, p->pc + 2)) + { + val = p->regs[read_mem(vm, p->pc + 2)]; + if (vm->args & AG_VERB && vm->verb & VB_OPS) + ft_printf("P %4d | aff %d\n", p->pid, val); + c = val % 256; + p->carry = (!c) ? 1 : 0; + if (vm->args & AG_AFF) + ft_printf("Aff: %c\n", c); + } + cw_move_pc(vm, p, jmp); +} diff --git a/srcs/srcs_vm/cw_and_or_xor.c b/srcs/srcs_vm/cw_and_or_xor.c new file mode 100644 index 0000000..ba74b13 --- /dev/null +++ b/srcs/srcs_vm/cw_and_or_xor.c @@ -0,0 +1,96 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cw_and_or_xor.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/06/19 20:52:19 by tmaze #+# #+# */ +/* Updated: 2019/07/10 15:09:34 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "corewar.h" + +char cw_logic_check_args(t_vm *vm, t_process *p, t_args *ag) +{ + return (((ag[0].t != 0 && ag[0].t != REG_CODE) + || (ag[0].t == REG_CODE && check_reg(vm, p->pc + 2))) + && ((ag[1].t != 0 && ag[1].t != REG_CODE) + || (ag[1].t == REG_CODE && check_reg(vm, p->pc + ag[1].i))) + && ag[1].t != 0 && ag[2].t == REG_CODE + && check_reg(vm, p->pc + ag[2].i)); +} + +char cw_logic_common(t_vm *vm, t_process *p, t_args *ag, int *res) +{ + init_args(ag, p, vm); + res[3] = cw_ocp(3, ag, 4); + if (cw_logic_check_args(vm, p, ag)) + { + if (ag[0].t == IND_CODE) + res[0] = read_four(vm, p->pc + (read_two(vm, p->pc + 2) % IDX_MOD)); + else if (ag[0].t == DIR_CODE) + res[0] = read_four(vm, p->pc + 2); + else + res[0] = p->regs[read_mem(vm, p->pc + 2)]; + if (ag[1].t == IND_CODE) + res[1] = read_four(vm, p->pc + (read_two(vm, p->pc + ag[1].i) + % IDX_MOD)); + else if (ag[1].t == DIR_CODE) + res[1] = read_four(vm, p->pc + ag[1].i); + else + res[1] = p->regs[read_mem(vm, p->pc + ag[1].i)]; + res[2] = read_mem(vm, p->pc + ag[2].i); + return (1); + } + return (0); +} + +void cw_and(t_process *p, t_vm *vm) +{ + t_args ag[4]; + int res[4]; + + if (cw_logic_common(vm, p, ag, res)) + { + if (vm->args & AG_VERB && vm->verb & VB_OPS) + ft_printf("P %4d | and %d %d r%d\n", p->pid, res[0], res[1] + , res[2]); + p->regs[res[2]] = res[0] & res[1]; + p->carry = (!p->regs[res[2]]) ? 1 : 0; + } + cw_move_pc(vm, p, res[3]); +} + +void cw_or(t_process *p, t_vm *vm) +{ + t_args ag[4]; + int res[4]; + + if (cw_logic_common(vm, p, ag, res)) + { + if (vm->args & AG_VERB && vm->verb & VB_OPS) + ft_printf("P %4d | or %d %d r%d\n", p->pid, res[0], res[1] + , res[2]); + p->regs[res[2]] = res[0] | res[1]; + p->carry = (!p->regs[res[2]]) ? 1 : 0; + } + cw_move_pc(vm, p, res[3]); +} + +void cw_xor(t_process *p, t_vm *vm) +{ + t_args ag[4]; + int res[4]; + + if (cw_logic_common(vm, p, ag, res)) + { + if (vm->args & AG_VERB && vm->verb & VB_OPS) + ft_printf("P %4d | xor %d %d r%d\n", p->pid, res[0], res[1] + , res[2]); + p->regs[res[2]] = res[0] ^ res[1]; + p->carry = (!p->regs[res[2]]) ? 1 : 0; + } + cw_move_pc(vm, p, res[3]); +} diff --git a/srcs/srcs_vm/cw_args.c b/srcs/srcs_vm/cw_args.c new file mode 100644 index 0000000..12a022e --- /dev/null +++ b/srcs/srcs_vm/cw_args.c @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cw_args.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/10 11:03:16 by tmaze #+# #+# */ +/* Updated: 2019/07/12 12:30:58 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "corewar.h" + +void arg_dump(char **av, t_vm *vm, int i) +{ + if (!(vm->args & AG_VISUAL) && ft_isnumb(av[i + 1]) + && !ft_atois(av[i + 1], &(vm->dump))) + { + if (ft_strequ(av[i], "-dump")) + { + vm->args &= ~AG_DUMP64; + vm->args |= AG_DUMP32; + } + else if (ft_strequ(av[i], "-d")) + { + vm->args &= ~AG_DUMP32; + vm->args |= AG_DUMP64; + } + } +} + +void arg_verb(char **av, t_vm *vm, int i) +{ + if (!(vm->args & AG_VISUAL) && ft_isnumb(av[i + 1]) + && !ft_atois(av[i + 1], &(vm->verb))) + vm->args |= AG_VERB; +} + +void arg_visual(char **av, t_vm *vm, int i) +{ + av = NULL; + i = 0; + vm->args &= ~(AG_VERB | AG_DUMP32 | AG_DUMP64 | AG_AFF); + vm->args |= AG_VISUAL; +} + +void arg_aff(char **av, t_vm *vm, int i) +{ + av = NULL; + i = 0; + if (!(vm->args & AG_VISUAL)) + vm->args |= AG_AFF; +} diff --git a/srcs/srcs_vm/cw_error.c b/srcs/srcs_vm/cw_error.c new file mode 100644 index 0000000..8503387 --- /dev/null +++ b/srcs/srcs_vm/cw_error.c @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cw_error.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/11 16:54:36 by igarbuz #+# #+# */ +/* Updated: 2019/07/13 17:21:07 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "corewar.h" + +int err_msg(int err, t_vm *vm) +{ + if (err == 1) + ft_printf("Error champ %d: %s\n", vm->nb_j, strerror(errno)); + else if (err == 2) + ft_printf("Error champ %d: Wrong magic number\n", vm->nb_j); + else if (err == 3) + ft_printf("Error champ %d: Champ too large (%d > %d)\n" + , vm->nb_j, vm->pls[vm->nb_j].prog_size, CHAMP_MAX_SIZE); + else if (err == 4) + ft_printf("Error champ no.%d: File is not .cor\n", vm->nb_j); + else if (err == 5) + ft_printf("Error champ no.%d: Number %d already in use\n", vm->nb_j + , vm->pls[vm->nb_j].num); + else if (err == 6) + { + ft_printf("Error dump: Missing or Invalid number\n"); + return (-1); + } + else if (err == 7) + ft_printf("Error: ncurses new window initialization\n"); + else if (err == 8) + ft_printf("Error: cannot open file with logo\n"); + else if (err == 9) + ft_printf("Program ended by user\n"); + return (1); +} + +void error(t_vm *vm, int err) +{ + clear_process(vm); + err_msg(err, vm); + exit(EXIT_FAILURE); +} diff --git a/srcs/srcs_vm/cw_fork_lfork.c b/srcs/srcs_vm/cw_fork_lfork.c new file mode 100644 index 0000000..53f3ff5 --- /dev/null +++ b/srcs/srcs_vm/cw_fork_lfork.c @@ -0,0 +1,61 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cw_fork_lfork.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/06/19 20:53:45 by tmaze #+# #+# */ +/* Updated: 2019/07/10 17:57:49 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "corewar.h" + +void cw_prcs_cpy(t_vm *vm, int *arg, t_process *p, t_process *new) +{ + new->op = 0; + new->pid = vm->pid_ct++; + new->new = 1; + new->carry = p->carry; + new->wait = 0; + arg[2] = -1; + while (++arg[2] <= REG_NUMBER) + new->regs[arg[2]] = p->regs[arg[2]]; + new->pc = arg[1]; + new->lst_live = p->lst_live; +} + +void cw_fork_common(t_vm *vm, t_process *p, char *name) +{ + t_process *new; + int arg[3]; + + if ((new = (t_process*)ft_memalloc(sizeof(t_process))) == NULL) + { + ft_printf("Error at process init: %s\n", strerror(errno)); + destroy_ncurses(vm); + clear_process(vm); + exit(0); + } + arg[0] = read_two(vm, p->pc + 1); + arg[1] = p->pc + ((name[0] == 'l') ? arg[0] : (arg[0] % IDX_MOD)); + if (vm->args & AG_VERB && vm->verb & VB_OPS) + ft_printf("P %4d | %s %d (%d)\n", p->pid, name, arg[0], arg[1]); + arg[1] = ft_mod(arg[1], MEM_SIZE); + cw_prcs_cpy(vm, arg, p, new); + new->next = vm->tl; + vm->tl = new; + vm->p_ct++; + cw_move_pc(vm, p, 3); +} + +void cw_fork(t_process *p, t_vm *vm) +{ + cw_fork_common(vm, p, "fork"); +} + +void cw_lfork(t_process *p, t_vm *vm) +{ + cw_fork_common(vm, p, "lfork"); +} diff --git a/srcs/srcs_vm/cw_init_ncurs.c b/srcs/srcs_vm/cw_init_ncurs.c new file mode 100644 index 0000000..7accae6 --- /dev/null +++ b/srcs/srcs_vm/cw_init_ncurs.c @@ -0,0 +1,100 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cw_init_ncurs.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/09 14:45:09 by igarbuz #+# #+# */ +/* Updated: 2019/07/13 17:41:48 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "corewar.h" + +static int print_logo(t_vm *vm) +{ + int fd; + char buf[ART_MAX_SIZE + 1]; + int rd; + + fd = open("./art/corewar_3D_logo.txt", O_RDONLY); + if ((rd = read(fd, &buf, ART_MAX_SIZE)) == -1) + { + destroy_ncurses_exit(vm); + error(vm, 8); + } + buf[rd] = '\0'; + wprintw(vm->vis.stat, "%s", &buf); + close(fd); + return (0); +} + +static void init_colors(void) +{ + init_color(COLOR_WHITE, 220, 220, 220); + init_pair(0, COLOR_WHITE, COLOR_BLACK); + init_pair(1, COLOR_BLUE, COLOR_BLACK); + init_pair(4, COLOR_YELLOW, COLOR_BLACK); + init_pair(3, COLOR_CYAN, COLOR_BLACK); + init_pair(2, COLOR_RED, COLOR_BLACK); + init_pair(5, COLOR_GREEN, COLOR_BLACK); + init_pair(6, COLOR_YELLOW, COLOR_BLACK); + init_pair(7, COLOR_BLACK, COLOR_BLUE); + init_pair(8, COLOR_BLACK, COLOR_MAGENTA); + init_pair(9, COLOR_BLACK, COLOR_CYAN); + init_pair(10, COLOR_BLACK, COLOR_RED); + init_pair(11, COLOR_BLACK, COLOR_GREEN); + init_pair(12, COLOR_BLACK, COLOR_YELLOW); + init_pair(13, COLOR_BLACK, COLOR_GREEN); + init_pair(14, COLOR_GREEN, COLOR_BLACK); +} + +void init_ncurses(t_vm *vm) +{ + if (vm->args & AG_VISUAL) + { + initscr(); + noecho(); + cbreak(); + start_color(); + init_colors(); + curs_set(FALSE); + if ((vm->vis.arena = newwin(66, 195, 0, 0)) == 0) + error(vm, 7); + if ((vm->vis.stat = newwin(66, 100, 0, 196)) == 0) + { + delwin(vm->vis.arena); + error(vm, 7); + } + keypad(vm->vis.arena, 1); + print_logo(vm); + wborder(vm->vis.arena, '|', '|', '-', '-', '+', '+', '+', '+'); + wborder(vm->vis.stat, '|', '|', '-', '-', '+', '+', '+', '+'); + } +} + +void destroy_ncurses_exit(t_vm *vm) +{ + if (vm->args & AG_VISUAL) + { + delwin(vm->vis.arena); + delwin(vm->vis.stat); + endwin(); + } +} + +void destroy_ncurses(t_vm *vm) +{ + if (vm->args & AG_VISUAL) + { + wrefresh(vm->vis.arena); + wrefresh(vm->vis.stat); + keypad(vm->vis.arena, 0); + nodelay(vm->vis.arena, 0); + wgetch(vm->vis.arena); + delwin(vm->vis.arena); + delwin(vm->vis.stat); + endwin(); + } +} diff --git a/srcs/srcs_vm/cw_ld_lld.c b/srcs/srcs_vm/cw_ld_lld.c new file mode 100644 index 0000000..3221c2f --- /dev/null +++ b/srcs/srcs_vm/cw_ld_lld.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cw_ld_lld.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/06/19 20:36:54 by tmaze #+# #+# */ +/* Updated: 2019/07/10 15:08:19 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "corewar.h" + +void cw_ld_common(t_process *p, t_vm *vm, char *name) +{ + int ag1; + int ag2; + int jmp; + t_args ag[4]; + + init_args(ag, p, vm); + jmp = cw_ocp(2, ag, 4); + if (ag[0].t == IND_CODE) + ag1 = read_two(vm, p->pc + 2); + if (ag[0].t != 0 && ag[0].t != REG_CODE && ag[1].t == REG_CODE + && (ag2 = check_reg(vm, p->pc + ag[1].i))) + { + if (ag[0].t == DIR_CODE) + ag1 = read_four(vm, p->pc + 2); + p->carry = (ag1 == 0) ? 1 : 0; + if (ag[0].t == IND_CODE) + ag1 = read_four(vm, p->pc + ((name[1] == 'd') ? (ag1 % IDX_MOD) + : ag1)); + if (vm->args & AG_VERB && vm->verb & VB_OPS) + ft_printf("P %4d | %s %d r%d\n", p->pid, name, ag1, ag2); + p->regs[ag2] = ag1; + } + cw_move_pc(vm, p, jmp); +} + +void cw_ld(t_process *p, t_vm *vm) +{ + cw_ld_common(p, vm, "ld"); +} + +void cw_lld(t_process *p, t_vm *vm) +{ + cw_ld_common(p, vm, "lld"); +} diff --git a/srcs/srcs_vm/cw_ldi_lldi.c b/srcs/srcs_vm/cw_ldi_lldi.c new file mode 100644 index 0000000..766380f --- /dev/null +++ b/srcs/srcs_vm/cw_ldi_lldi.c @@ -0,0 +1,74 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cw_ldi_lldi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/06/19 20:43:31 by tmaze #+# #+# */ +/* Updated: 2019/07/16 14:20:24 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "corewar.h" + +void print_ldi(t_process *p, char *name, int *arg) +{ + ft_printf("P %4d | %s %d %d r%d\n", \ + p->pid, name, arg[0], arg[1], arg[2]); + if (name[1] == 'd') + ft_printf("%7c| -> load from %d + %d = %d (with pc and mod %d)\n" + , ' ', arg[0], arg[1], arg[4], arg[5]); + else + ft_printf("%7c| -> load from %d + %d = %d (with pc %d)\n" + , ' ', arg[0], arg[1], arg[4], arg[5]); +} + +int cw_ldi_args(t_process *p, t_vm *vm, t_args *ag, int *arg) +{ + return (((ag[0].t == REG_CODE && (arg[0] = check_reg(vm, p->pc + 2))) + || (ag[0].t != 0 && ag[0].t != REG_CODE)) + && ((ag[1].t == REG_CODE + && (arg[1] = check_reg(vm, p->pc + ag[1].i))) + || ag[1].t == DIR_CODE) && ag[2].t == REG_CODE + && (arg[2] = check_reg(vm, p->pc + ag[2].i))); +} + +void cw_ldi_common(t_process *p, t_vm *vm, char *n) +{ + t_args ag[4]; + int arg[6]; + + init_args(ag, p, vm); + arg[3] = cw_ocp(3, ag, 2); + if (cw_ldi_args(p, vm, ag, arg)) + { + if (ag[0].t == DIR_CODE) + arg[0] = read_two(vm, p->pc + 2); + else if (ag[0].t == IND_CODE) + arg[0] = read_four(vm, p->pc + (read_two(vm, p->pc + 2) % IDX_MOD)); + else + arg[0] = p->regs[arg[0]]; + if (ag[1].t == DIR_CODE) + arg[1] = read_two(vm, p->pc + ag[1].i); + else + arg[1] = p->regs[arg[1]]; + arg[4] = arg[0] + arg[1]; + arg[5] = (n[1] != 'd') ? (p->pc + arg[4]) % MEM_SIZE + : (p->pc + (arg[4] % IDX_MOD)) % MEM_SIZE; + if (vm->args & AG_VERB && vm->verb & VB_OPS) + print_ldi(p, n, arg); + p->regs[arg[2]] = read_four(vm, arg[5]); + } + cw_move_pc(vm, p, arg[3]); +} + +void cw_ldi(t_process *p, t_vm *vm) +{ + cw_ldi_common(p, vm, "ldi"); +} + +void cw_lldi(t_process *p, t_vm *vm) +{ + cw_ldi_common(p, vm, "lldi"); +} diff --git a/srcs/srcs_vm/cw_live.c b/srcs/srcs_vm/cw_live.c new file mode 100644 index 0000000..f301ff0 --- /dev/null +++ b/srcs/srcs_vm/cw_live.c @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cw_live.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/06/15 12:38:01 by tmaze #+# #+# */ +/* Updated: 2019/07/14 14:10:52 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "corewar.h" + +static void cw_print_live(t_vm *vm, int j) +{ + if (vm->args & AG_VERB && vm->verb & VB_LIVE) + ft_printf("Player %d (%s) is said to be alive\n", j + 1 + , vm->pls[j].prog_name); + else if (!(vm->args & AG_VERB) && !(vm->args & AG_VISUAL)) + ft_printf("un processus dit que le joueur %d(%s) est en vie\n" + , vm->pls[j].num, vm->pls[j].prog_name); +} + +void cw_live(t_process *p, t_vm *vm) +{ + int pl; + short int j; + unsigned short int i; + + i = 0; + pl = read_four(vm, p->pc + 1); + j = 0; + if (vm->args & AG_VERB && vm->verb & VB_OPS) + ft_printf("P %4d | live %d\n", p->pid, pl); + vm->live_ct++; + p->lst_live = vm->cycles; + while (j < vm->nb_j) + { + if (pl == vm->pls[j].num) + { + cw_print_live(vm, j); + vm->pls[j].live_ct++; + vm->pls[j].lst_live = vm->cycles; + vm->live = pl; + break ; + } + j++; + } + cw_move_pc(vm, p, 5); +} diff --git a/srcs/srcs_vm/cw_ops_tools.c b/srcs/srcs_vm/cw_ops_tools.c new file mode 100644 index 0000000..84276e5 --- /dev/null +++ b/srcs/srcs_vm/cw_ops_tools.c @@ -0,0 +1,90 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cw_ops_tools.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/09 13:54:26 by tmaze #+# #+# */ +/* Updated: 2019/07/15 23:52:08 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "corewar.h" + +unsigned int l2b_endian(unsigned int num) +{ + uint32_t b0; + uint32_t b1; + uint32_t b2; + uint32_t b3; + uint32_t res; + + b0 = (num & 0x000000ff) << 24u; + b1 = (num & 0x0000ff00) << 8u; + b2 = (num & 0x00ff0000) >> 8u; + b3 = (num & 0xff000000) >> 24u; + res = b0 | b1 | b2 | b3; + return (res); +} + +void init_args(t_args *ag, t_process *p, t_vm *vm) +{ + int i; + + i = -1; + while (++i < 4) + { + ag[i].t = 0; + ag[i].i = 0; + } + ag[3].t = read_mem(vm, p->pc + 1); +} + +int check_reg(t_vm *vm, int ind) +{ + int reg; + + reg = read_mem(vm, ind); + return ((reg > 0 && reg <= REG_NUMBER) ? reg : 0); +} + +void cw_move_pc(t_vm *vm, t_process *p, int jp) +{ + if (vm->verb != -1 && vm->verb & VB_PC) + ft_printf("ADV %d (0x%04x -> 0x%04x)", jp, p->pc, p->pc + jp); + p->pc = (p->pc + jp) % MEM_SIZE; + while (jp && vm->verb != -1 && vm->verb & VB_PC) + ft_printf(" %02x", vm->mem[((unsigned)(p->pc - jp--)) % MEM_SIZE]); + if (vm->verb != -1 && vm->verb & VB_PC) + ft_printf(" \n"); +} + +int cw_ocp(char nb_ag, t_args *ag, char size_dir) +{ + int res; + int i; + + res = 2; + i = -1; + while (++i < nb_ag) + { + ag[i].i = res; + if (((ag[3].t & (0xC0 >> (2 * i))) >> (2 * (3 - i))) == IND_CODE) + { + res += 2; + ag[i].t = IND_CODE; + } + else if (((ag[3].t & (0xC0 >> (2 * i))) >> (2 * (3 - i))) == REG_CODE) + { + res += 1; + ag[i].t = REG_CODE; + } + else if (((ag[3].t & (0xC0 >> (2 * i))) >> (2 * (3 - i))) == DIR_CODE) + { + res += size_dir; + ag[i].t = DIR_CODE; + } + } + return (res); +} diff --git a/srcs/srcs_vm/cw_parser.c b/srcs/srcs_vm/cw_parser.c new file mode 100644 index 0000000..58eee5d --- /dev/null +++ b/srcs/srcs_vm/cw_parser.c @@ -0,0 +1,117 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cw_parser.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/05/20 18:20:48 by tmaze #+# #+# */ +/* Updated: 2019/07/13 17:22:28 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "corewar.h" + +static int p_parser(char *p, t_player *pl) +{ + int fd; + + if ((fd = open(p, O_RDONLY)) == -1 || read(fd, &(pl->magic), 4) == -1) + return (1); + pl->magic = l2b_endian(pl->magic); + if (pl->magic != COREWAR_EXEC_MAGIC) + return (2); + if (read(fd, &(pl->prog_name), PROG_NAME_LENGTH) < 1 + || lseek(fd, 136, SEEK_SET) == -1 + || read(fd, &(pl->prog_size), 4) < 1) + return (1); + pl->prog_size = l2b_endian(pl->prog_size); + if (pl->prog_size > CHAMP_MAX_SIZE) + return (3); + if (read(fd, &(pl->comment), COMMENT_LENGTH) < 1 + || lseek(fd, 2192, SEEK_SET) == -1 + || read(fd, &(pl->code), pl->prog_size) != pl->prog_size + || close(fd) == -1) + return (1); + return (0); +} + +static int get_num(t_vm *vm, int ac, char **av, int *i) +{ + static int pl_i = -1; + short int j; + + if (ft_strequ(av[*i], "-n") && (*i + 2 < ac) && ft_isnumb(av[*i + 1]) + && (j = -1) && !ft_atois(av[(*i) + 1], &(vm->pls[vm->nb_j].num))) + { + while (++j < vm->nb_j) + { + if (vm->pls[j].num == vm->pls[vm->nb_j].num) + return (1); + } + *i += 2; + } + else if (!ft_strequ(av[*i], "-n") && (j = -1)) + { + while (++j < vm->nb_j) + if (pl_i == vm->pls[j].num && (j = -1)) + pl_i--; + vm->pls[vm->nb_j].num = pl_i--; + } + else + return (1); + return (0); +} + +static int cw_get_args(int ac, char **av, t_vm *vm) +{ + int i; + int j; + static t_pargs args[5] = {{"-dump", &arg_dump, 2}, {"-d", &arg_dump, 2} + , {"-v", &arg_verb, 2}, {"-a", &arg_aff, 1} + , {"-visu", &arg_visual, 1}}; + + i = 1; + while (i < ac && av[i][0] == '-' && !ft_strequ("-n", av[i]) && !(j = 0)) + { + while (j < 5 && !ft_strequ(args[j].arg, av[i])) + j++; + if (j < 5 && i + args[j].nb_ag < ac) + { + args[j].func(av, vm, i); + i += args[j].nb_ag; + } + else + { + cw_print_usage(); + exit(1); + } + } + return (i); +} + +int cw_parser(int ac, char **av, t_vm *vm) +{ + int i; + int err; + + i = cw_get_args(ac, av, vm); + if (i >= ac) + { + cw_print_usage(); + return (1); + } + while (i < ac && vm->nb_j < 4) + { + if (get_num(vm, ac, av, &i)) + return (err_msg(5, vm)); + if (!check_file(av[i])) + return (err_msg(4, vm)); + if ((err = p_parser(av[i], &(vm->pls[vm->nb_j])))) + return (err_msg(err, vm)); + vm->live = vm->pls[vm->nb_j].num; + vm->nb_j++; + i++; + } + return (0); +} diff --git a/srcs/srcs_vm/cw_print_usage.c b/srcs/srcs_vm/cw_print_usage.c new file mode 100644 index 0000000..1dc87b7 --- /dev/null +++ b/srcs/srcs_vm/cw_print_usage.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cw_print_usage.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/11 13:30:52 by tmaze #+# #+# */ +/* Updated: 2019/07/16 16:11:24 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "corewar.h" + +void cw_print_usage(void) +{ + ft_putstr("Usage: ./corewar [[-dump N|-d N] -v Z -visu] [-n Z] <...>\n N being a natural number\n Z being an integer\n#### " + "TEXT OUTPUT MODE #####################################################" + "####\n -a : Prints output from \"aff\" (Default is to hide i" + "t)\n -d N : Dumps memory after N cycles w/ 64 byes per line\n " + " -dump N : Dumps memory after N cycles w/ 32 bytes per line\n -" + "v N : Verbosity levels, can be added together to enable several\n" + " - 0 : Show only essentials\n - 1 : Show" + "lives\n - 2 : Show cycles\n - 4 : Show o" + "perations (Params are NOT litteral ...)\n - 8 : Show de" + "aths\n - 16 : Show PC movements (Except for jumps)\n###" + "# VISUAL OUTPUT MODE #################################################" + "######\n -visu : Activates graphical visualizer\n " + "(OVERRIDES ALL TEXT OUTPUT OPTIONS)\n#### CONTROLS\n - " + "SPACE : Pause\n - KEY_UP : Increase speed\n " + " - KEY_DOWN : Decrease speed\n - q :" + " Exit\n"); +} diff --git a/srcs/srcs_vm/cw_process.c b/srcs/srcs_vm/cw_process.c new file mode 100644 index 0000000..a9997dc --- /dev/null +++ b/srcs/srcs_vm/cw_process.c @@ -0,0 +1,115 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cw_process.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/06/14 12:46:19 by tmaze #+# #+# */ +/* Updated: 2019/07/16 17:30:57 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "corewar.h" + +void clear_process(t_vm *vm) +{ + t_process *tmp; + + while (vm->tl) + { + tmp = vm->tl; + vm->tl = tmp->next; + ft_memdel((void**)&tmp); + } +} + +void init_process(t_vm *vm) +{ + t_process *new; + short int i; + + i = -1; + while (++i < vm->nb_j) + { + if ((new = (t_process*)ft_memalloc(sizeof(t_process))) == NULL) + { + ft_printf("Error at process init: %s\n", strerror(errno)); + clear_process(vm); + exit(2); + } + new->op = 0; + new->pid = vm->pid_ct++; + new->new = 1; + new->carry = 0; + new->wait = 0; + ft_bzero(new->regs, (REG_NUMBER + 1) * REG_SIZE); + new->pc = (MEM_SIZE / vm->nb_j) * i; + new->regs[1] = vm->pls[i].num; + new->lst_live = 0; + new->next = vm->tl; + vm->tl = new; + vm->p_ct++; + } +} + +void del_prcs_start(t_vm *vm) +{ + t_process *tmp; + + while (vm->tl && (vm->cycles - vm->tl->lst_live) >= vm->ctd) + { + if (vm->verb != -1 && vm->verb & VB_DEATH) + ft_printf("Process %d hasn't lived for %lu cycles (CTD %d)\n", + vm->tl->pid, vm->cycles - vm->tl->lst_live, vm->ctd); + tmp = vm->tl; + vm->tl = vm->tl->next; + ft_memdel((void**)&tmp); + vm->p_ct--; + } +} + +void del_prcs_mid(t_vm *vm) +{ + t_process *it; + t_process *tmp; + + it = vm->tl; + while (it && it->next) + { + if ((vm->cycles - it->next->lst_live) >= vm->ctd) + { + if (vm->verb != -1 && vm->verb & VB_DEATH) + ft_printf("Process %d hasn't lived for %lu cycles (CTD %d)\n", + it->next->pid, vm->cycles - it->next->lst_live, vm->ctd); + tmp = it->next; + it->next = it->next->next; + ft_memdel((void**)&tmp); + vm->p_ct--; + } + else + it = it->next; + } +} + +void del_prcs(t_vm *vm) +{ + static int nb_checks = 0; + int i; + + del_prcs_start(vm); + del_prcs_mid(vm); + nb_checks++; + vm->ctd_c = 0; + if (vm->live_ct >= NBR_LIVE || nb_checks == MAX_CHECKS - 1) + { + vm->ctd -= CYCLE_DELTA; + nb_checks = 0; + if (vm->verb != -1 && vm->verb & VB_CYCLES) + ft_printf("Cycle to die is now %d\n", vm->ctd); + } + vm->live_ct = 0; + i = -1; + while (++i < vm->nb_j) + vm->pls[i].live_ct = 0; +} diff --git a/srcs/srcs_vm/cw_read_mem.c b/srcs/srcs_vm/cw_read_mem.c new file mode 100644 index 0000000..aca7479 --- /dev/null +++ b/srcs/srcs_vm/cw_read_mem.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cw_read_mem.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/06/04 14:30:46 by tmaze #+# #+# */ +/* Updated: 2019/07/04 23:04:16 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "corewar.h" + +unsigned char read_mem(t_vm *vm, int ind) +{ + return (vm->mem[ft_mod(ind, MEM_SIZE)]); +} + +int read_four(t_vm *vm, size_t index) +{ + int i; + int ret; + + i = -1; + while (++i < 4) + ret = ret << 8 | read_mem(vm, index + i); + return (ret); +} + +short int read_two(t_vm *vm, size_t index) +{ + int i; + short int ret; + + i = -1; + ret = 0; + while (++i < 2) + ret = ret << 8 | read_mem(vm, index + i); + return (ret); +} diff --git a/srcs/srcs_vm/cw_st.c b/srcs/srcs_vm/cw_st.c new file mode 100644 index 0000000..a42897d --- /dev/null +++ b/srcs/srcs_vm/cw_st.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cw_st.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/06/19 20:40:53 by tmaze #+# #+# */ +/* Updated: 2019/06/27 11:07:38 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "corewar.h" + +char cw_st_check_args(t_vm *vm, t_process *p, t_args *ag, int *arg) +{ + return (ag[0].t == REG_CODE && (arg[0] = check_reg(vm, p->pc + 2)) + && ((ag[1].t == REG_CODE && (arg[1] = check_reg(vm, p->pc + 3))) + || ag[1].t == IND_CODE)); +} + +void cw_st(t_process *p, t_vm *vm) +{ + int arg[3]; + t_args ag[4]; + + init_args(ag, p, vm); + arg[2] = cw_ocp(2, ag, 4); + arg[0] = 0; + if (cw_st_check_args(vm, p, ag, arg)) + { + if (ag[1].t == IND_CODE) + arg[1] = read_two(vm, p->pc + 3); + if (vm->verb != -1 && vm->verb & VB_OPS) + ft_printf("P %4d | st r%d %d\n", p->pid, arg[0], arg[1]); + if (ag[1].t == REG_CODE) + p->regs[arg[1]] = p->regs[arg[0]]; + else + write_four(vm, p->pc + (arg[1] % IDX_MOD), p->regs[arg[0]], p->pc); + } + cw_move_pc(vm, p, arg[2]); +} diff --git a/srcs/srcs_vm/cw_sti.c b/srcs/srcs_vm/cw_sti.c new file mode 100644 index 0000000..64527db --- /dev/null +++ b/srcs/srcs_vm/cw_sti.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cw_sti.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/06/19 20:46:59 by tmaze #+# #+# */ +/* Updated: 2019/07/15 23:59:22 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "corewar.h" + +static int cw_check_sti(t_process *p, t_vm *vm, t_args *ag, int *arg) +{ + return (ag[0].t == REG_CODE && (arg[0] = check_reg(vm, p->pc + 2)) + && ((ag[1].t == REG_CODE && (arg[1] = check_reg(vm, p->pc + ag[1].i))) + || (ag[1].t != 0 && ag[1].t != REG_CODE)) + && ((ag[2].t == REG_CODE && (arg[2] = check_reg(vm, p->pc + ag[2].i))) + || ag[2].t == DIR_CODE)); +} + +static void cw_print_sti(t_process *p, int *arg) +{ + ft_printf("P %4d | sti r%d %d %d\n", p->pid, arg[0], arg[1], arg[2]); + ft_printf("%7c| -> store to %d + %d = %d (with pc and mod %d)\n", ' ' + , arg[1], arg[2], arg[4], arg[5]); +} + +void cw_sti(t_process *p, t_vm *vm) +{ + t_args ag[4]; + int arg[6]; + + init_args(ag, p, vm); + arg[3] = cw_ocp(3, ag, 2); + if (cw_check_sti(p, vm, ag, arg)) + { + if (ag[1].t == REG_CODE) + arg[1] = p->regs[arg[1]]; + else if (ag[1].t == IND_CODE) + arg[1] = read_four(vm, p->pc + (read_two(vm, p->pc + 3) % IDX_MOD)); + else + arg[1] = read_two(vm, p->pc + 3); + if (ag[2].t == DIR_CODE) + arg[2] = read_two(vm, p->pc + ag[2].i); + else + arg[2] = p->regs[arg[2]]; + arg[4] = arg[1] + arg[2]; + arg[5] = p->pc + (arg[4] % IDX_MOD); + if (vm->args & AG_VERB && vm->verb & VB_OPS) + cw_print_sti(p, arg); + write_four(vm, ft_mod(arg[5], MEM_SIZE), p->regs[arg[0]], p->pc); + } + cw_move_pc(vm, p, arg[3]); +} diff --git a/srcs/srcs_vm/cw_visual.c b/srcs/srcs_vm/cw_visual.c new file mode 100644 index 0000000..80e7080 --- /dev/null +++ b/srcs/srcs_vm/cw_visual.c @@ -0,0 +1,122 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cw_visual.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/08 12:12:51 by igarbuz #+# #+# */ +/* Updated: 2019/07/13 19:14:56 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "corewar.h" + +static void print_lives(t_player *pl, WINDOW *stat) +{ + int i; + int cycles; + + if (pl->live_ct < 560) + cycles = pl->live_ct / 10; + else + cycles = 56; + wattron(stat, COLOR_PAIR(pl->col)); + wprintw(stat, "\n\n Lives for: "); + wprintw(stat, "%s", pl->prog_name); + wprintw(stat, " => %-5d \n", pl->live_ct); + waddstr(stat, " |"); + i = -1; + while (++i < cycles) + waddch(stat, ACS_HLINE | A_REVERSE); + while (i++ < 56) + waddch(stat, ACS_HLINE); + waddch(stat, '|'); + wattroff(stat, COLOR_PAIR(pl->col)); +} + +static void print_stats(t_vm *vm, WINDOW *stat) +{ + short int i; + + wmove(stat, 10, 1); + wclrtobot(stat); + i = -1; + while (++i < vm->nb_j) + print_lives(&vm->pls[i], stat); + wattron(stat, COLOR_PAIR(14)); + wprintw(stat, "\n\n Cycle: %-10lld\n\n \ + Processes: %-10u\n\n \ + Total Number of lives: %3d/%-10d\n\n \ + Players number: %hd/%d\n\n \ + Decrease cycle to die with: %d\n\n \ + Cycles to die: %d/%d\n\n", + vm->cycles, vm->p_ct, vm->live_ct, NBR_LIVE, \ + vm->nb_j, MAX_PLAYERS, CYCLE_DELTA, vm->ctd_c, vm->ctd); + wprintw(stat, "\n\n Delay: %8dμs ", vm->vis.delay & 0x3FFFFF); + wattroff(stat, COLOR_PAIR(14)); + wborder(stat, '|', '|', '-', '-', '+', '+', '+', '+'); + wrefresh(stat); +} + +void control_delay(t_vm *vm) +{ + int key; + + while (vm->args & AG_VISUAL) + { + key = wgetch(vm->vis.arena); + if (key == ' ' && vm->vis.paused == 0) + { + nodelay(vm->vis.arena, 0); + vm->vis.paused = 1; + } + else if (key == ' ' && vm->vis.paused == 1) + { + nodelay(vm->vis.arena, 1); + vm->vis.paused = 0; + } + else if (key == KEY_DOWN && vm->vis.delay < 100000) + vm->vis.delay = (vm->vis.delay + 1) * 1.1; + else if (key == KEY_UP) + vm->vis.delay = (vm->vis.delay * 0.9); + check_user_exit(vm, key); + if (vm->vis.paused == 1) + visual_delay_update(vm); + if (vm->vis.paused == 0) + break ; + } +} + +void print_arena(t_vm *vm) +{ + int i; + + if (vm->args & AG_VISUAL) + { + i = 0; + werase(vm->vis.arena); + while (i < MEM_SIZE) + { + if ((i & 0x3F) == 0) + wprintw(vm->vis.arena, "\n "); + wattron(vm->vis.arena, COLOR_PAIR(vm->col[i])); + wprintw(vm->vis.arena, " %02x", 0xFF & vm->mem[i]); + wattroff(vm->vis.arena, COLOR_PAIR(vm->col[i])); + wattroff(vm->vis.arena, COLOR_PAIR(1)); + i++; + } + wborder(vm->vis.arena, '|', '|', '-', '-', '+', '+', '+', '+'); + print_stats(vm, vm->vis.stat); + } +} + +void print_pc(t_vm *vm, unsigned short int pc, char col) +{ + if (vm->args & AG_VISUAL) + { + mvwchgat(vm->vis.arena, 1 + pc / 64, 2 + 3 * (pc & 63),\ + 2, A_REVERSE, col, NULL); + usleep(vm->vis.delay); + } +} diff --git a/srcs/srcs_vm/cw_visual_win.c b/srcs/srcs_vm/cw_visual_win.c new file mode 100644 index 0000000..e7dc50d --- /dev/null +++ b/srcs/srcs_vm/cw_visual_win.c @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cw_visual_win.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: igarbuz +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/13 21:15:56 by igarbuz #+# #+# */ +/* Updated: 2019/07/16 19:09:19 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "corewar.h" + +static void print_logo_win(t_vm *vm, int i) +{ + int j; + + wmove(vm->vis.stat, 0, 0); + j = 0; + while (++j < 9) + mvwchgat(vm->vis.stat, j, 1, 93, A_BLINK, vm->pls[i].col, NULL); +} + +void visual_delay_update(t_vm *vm) +{ + wmove(vm->vis.stat, (vm->nb_j - 1) * 3 + 29, 15); + wattron(vm->vis.stat, COLOR_PAIR(14)); + wprintw(vm->vis.stat, "%8dμs ", vm->vis.delay & 0x3FFFFF); + wattroff(vm->vis.stat, COLOR_PAIR(14)); + wrefresh(vm->vis.stat); +} + +void check_user_exit(t_vm *vm, int key) +{ + if (key == 'q') + { + destroy_ncurses_exit(vm); + error(vm, 9); + } +} + +void visual_winners(t_vm *vm, int i) +{ + int k; + + wmove(vm->vis.stat, 40, 5); + wclrtobot(vm->vis.stat); + wattron(vm->vis.stat, COLOR_PAIR(14)); + k = -1; + while (++k < 90) + waddch(vm->vis.stat, '*'); + wprintw(vm->vis.stat, "\n\n Contestant %lu, \"%s\", has won !\n", \ + i + 1, vm->pls[i].prog_name); + wprintw(vm->vis.stat, "\n\n Press any key to exit\n"); + wattroff(vm->vis.stat, COLOR_PAIR(14)); + print_logo_win(vm, i); + wborder(vm->vis.stat, '|', '|', '-', '-', '+', '+', '+', '+'); + wrefresh(vm->vis.stat); +} diff --git a/srcs/srcs_vm/cw_vm.c b/srcs/srcs_vm/cw_vm.c new file mode 100644 index 0000000..e17f2c0 --- /dev/null +++ b/srcs/srcs_vm/cw_vm.c @@ -0,0 +1,91 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cw_vm.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/06/14 12:52:51 by tmaze #+# #+# */ +/* Updated: 2019/07/16 14:22:34 by igarbuz ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "corewar.h" + +static void init_vm_vars(t_vm *vm) +{ + g_bcn = 0; + g_buf[g_bcn] = '\0'; + vm->args = 0; + vm->pid_ct = 1; + vm->cycles = 0; + vm->ctd = CYCLE_TO_DIE; + vm->ctd_c = 0; + vm->nb_j = 0; + vm->tl = NULL; + vm->live = 0; + vm->live_ct = 0; + vm->dump = -1; + vm->verb = -1; + vm->p_ct = 0; + vm->vis.delay = 10000; + vm->vis.paused = TRUE; + vm->vis.arena = NULL; + vm->vis.stat = NULL; +} + +void init_vm(t_vm *vm, int i) +{ + ft_bzero(vm->mem, MEM_SIZE); + ft_bzero(vm->col, MEM_SIZE); + init_vm_vars(vm); + i = -1; + while (++i < MAX_PLAYERS) + { + vm->pls[i].magic = 0; + ft_bzero(vm->pls[i].prog_name, PROG_NAME_LENGTH + 1); + vm->pls[i].prog_size = 0; + ft_bzero(vm->pls[i].comment, COMMENT_LENGTH + 1); + vm->pls[i].num = 0; + ft_bzero(vm->pls[i].code, CHAMP_MAX_SIZE); + vm->pls[i].col = 0; + vm->pls[i].live_ct = 0; + vm->pls[i].lst_live = 0; + } +} + +int load_champ(t_vm *vm) +{ + short int i; + int inc; + + inc = 0; + i = 0; + while (i < vm->nb_j) + { + ft_memmove(&(vm->mem[inc]), vm->pls[i].code, vm->pls[i].prog_size); + ft_memset(&(vm->col[inc]), i + 1, vm->pls[i].prog_size); + vm->pls[i].col = i + 1; + inc += (MEM_SIZE / vm->nb_j); + i++; + } + return (0); +} + +void mem_dump(t_vm *vm) +{ + int i; + int nb; + + i = 0; + nb = (vm->args & AG_DUMP64) ? 64 : 32; + while (i < MEM_SIZE) + { + if (i % nb == 0) + ft_printf("0x%04x : ", i); + ft_printf("%02x ", vm->mem[i]); + if ((i + 1) % nb == 0) + ft_printf("\n"); + i++; + } +} diff --git a/srcs/srcs_vm/cw_write_mem.c b/srcs/srcs_vm/cw_write_mem.c new file mode 100644 index 0000000..ad6f51a --- /dev/null +++ b/srcs/srcs_vm/cw_write_mem.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cw_write_mem.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/06/14 11:31:10 by tmaze #+# #+# */ +/* Updated: 2019/07/13 17:17:08 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "corewar.h" + +void write_mem(t_vm *vm, unsigned int index, char val, unsigned short int pc) +{ + vm->mem[ft_mod(index, MEM_SIZE)] = val; + vm->col[index & MEM_SIZE_M] = vm->col[pc & MEM_SIZE_M]; +} + +void write_four(t_vm *vm, unsigned int index, int val, unsigned short int pc) +{ + int mask; + int i; + + mask = 0x000000FF; + i = 0; + while (i < 4) + { + write_mem(vm, (index + (3 - i)), (val & mask) >> (8 * i), pc); + mask = mask << 8; + i++; + } +} diff --git a/srcs/srcs_vm/cw_zjump.c b/srcs/srcs_vm/cw_zjump.c new file mode 100644 index 0000000..4453e63 --- /dev/null +++ b/srcs/srcs_vm/cw_zjump.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cw_zjump.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/06/19 20:57:44 by tmaze #+# #+# */ +/* Updated: 2019/07/10 15:05:17 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "corewar.h" + +void cw_zjump(t_process *p, t_vm *vm) +{ + int ind; + + ind = read_two(vm, p->pc + 1); + if (vm->args & AG_VERB && vm->verb & VB_OPS) + ft_printf("P %4d | zjmp %d %s\n", p->pid, ind + , (p->carry) ? "OK" : "FAILED"); + if (p->carry) + p->pc = ft_mod(p->pc + (read_two(vm, p->pc + 1) % IDX_MOD), MEM_SIZE); + else + cw_move_pc(vm, p, 3); +} diff --git a/srcs/srcs_vm/main.c b/srcs/srcs_vm/main.c new file mode 100644 index 0000000..7ffe645 --- /dev/null +++ b/srcs/srcs_vm/main.c @@ -0,0 +1,117 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/05/16 18:54:57 by tmaze #+# #+# */ +/* Updated: 2019/07/16 17:44:18 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "corewar.h" + +void cw_intro(t_vm *vm) +{ + short int i; + + ft_printf("Introducing contestants...\n"); + i = 0; + while (i < vm->nb_j) + { + ft_printf("* Player %hd, weighing %d bytes, \"%s\" (\"%s\") !\n" + , i + 1, vm->pls[i].prog_size, vm->pls[i].prog_name + , vm->pls[i].comment); + i++; + } +} + +static void corewar_run(t_vm *vm, t_process *it) +{ + static t_ops ops[16] = {{10, &cw_live}, {5, &cw_ld}, {5, &cw_st} + , {10, &cw_add}, {10, &cw_sub}, {6, &cw_and}, {6, &cw_or} + , {6, &cw_xor}, {20, &cw_zjump}, {25, &cw_ldi} + , {25, &cw_sti}, {800, &cw_fork}, {10, &cw_lld} + , {50, &cw_lldi}, {1000, &cw_lfork}, {2, &cw_aff}}; + + while (it) + { + print_pc(vm, it->pc, vm->col[it->pc]); + if (it->wait) + { + it->wait--; + if (it->wait == 0) + ops[it->op - 1].func(it, vm); + } + else if (vm->mem[it->pc] > 0 && vm->mem[it->pc] < 17) + { + it->op = vm->mem[it->pc]; + it->wait = ops[it->op - 1].wait - 1; + } + else + it->pc = (it->pc + 1) % MEM_SIZE; + if (it) + it = it->next; + } +} + +static void corewar(t_vm *vm) +{ + init_ncurses(vm); + while (vm->tl && (!(vm->args & (AG_DUMP32 | AG_DUMP64)) + || vm->cycles < vm->dump)) + { + print_arena(vm); + vm->cycles++; + vm->ctd_c++; + if (vm->args & AG_VERB && vm->verb & VB_CYCLES) + ft_printf("It is now cycle %d\n", vm->cycles); + corewar_run(vm, vm->tl); + wrefresh(vm->vis.arena); + if (vm->ctd_c == vm->ctd || vm->ctd <= 0) + del_prcs(vm); + control_delay(vm); + } + if (!vm->tl) + vm->args &= ~(AG_DUMP32 & AG_DUMP64); +} + +static void cw_print_win(t_vm *vm, int i) +{ + if (vm->args & AG_VISUAL) + visual_winners(vm, i); + else if (vm->args & AG_VERB) + ft_printf("Contestant %d, \"%s\", has won !\n", i + 1 + , vm->pls[i].prog_name); + else + ft_printf("le joueur %d(%s) a gagne\n", vm->pls[i].num + , vm->pls[i].prog_name); +} + +int main(int ac, char **av) +{ + t_vm vm; + short int i; + + init_vm(&vm, 0); + if (cw_parser(ac, av, &vm)) + return (1); + init_process(&vm); + load_champ(&vm); + if (vm.args & AG_VERB) + cw_intro(&vm); + corewar(&vm); + if (vm.args & AG_DUMP32 || vm.args & AG_DUMP64) + mem_dump(&vm); + else + { + i = 0; + while (i < vm.nb_j && vm.pls[i].num != vm.live) + i++; + cw_print_win(&vm, i); + } + destroy_ncurses(&vm); + clear_process(&vm); + return (0); +}