libft norme

This commit is contained in:
Jack Halford 2017-10-07 18:24:39 +02:00
parent 488d622ed1
commit 44b23be0a0
217 changed files with 1879 additions and 307 deletions

View file

@ -6,7 +6,7 @@
# By: wescande <wescande@student.42.fr> +#+ +:+ +#+ # # By: wescande <wescande@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2016/08/29 21:32:58 by wescande #+# #+# # # Created: 2016/08/29 21:32:58 by wescande #+# #+# #
# Updated: 2017/03/31 21:05:56 by jhalford ### ########.fr # # Updated: 2017/10/07 17:50:04 by jhalford ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #

View file

@ -23,7 +23,6 @@ INC_DIR = includes/
OBJ_DIR = objs/ OBJ_DIR = objs/
SRC_BASE = \ SRC_BASE = \
rs/rs.c\
btree/btree_apply_by_level.c\ btree/btree_apply_by_level.c\
btree/btree_apply_infix.c\ btree/btree_apply_infix.c\
btree/btree_apply_prefix.c\ btree/btree_apply_prefix.c\
@ -75,6 +74,7 @@ ft_printf/lib_pad.c\
ft_printf/lib_pad_sharp.c\ ft_printf/lib_pad_sharp.c\
get_next_line/get_next_line.c\ get_next_line/get_next_line.c\
lst/ft_id.c\ lst/ft_id.c\
lst/ft_lst2str.c\
lst/ft_lst_at.c\ lst/ft_lst_at.c\
lst/ft_lst_bfree.c\ lst/ft_lst_bfree.c\
lst/ft_lst_cfree.c\ lst/ft_lst_cfree.c\
@ -85,6 +85,7 @@ lst/ft_lst_filterout.c\
lst/ft_lst_find.c\ lst/ft_lst_find.c\
lst/ft_lst_merge.c\ lst/ft_lst_merge.c\
lst/ft_lst_order_delsub.c\ lst/ft_lst_order_delsub.c\
lst/ft_lst_pop.c\
lst/ft_lst_print.c\ lst/ft_lst_print.c\
lst/ft_lst_print2.c\ lst/ft_lst_print2.c\
lst/ft_lst_removeif.c\ lst/ft_lst_removeif.c\
@ -104,11 +105,9 @@ lst/ft_lstnew.c\
lst/ft_lstnew_range.c\ lst/ft_lstnew_range.c\
lst/ft_lstsort.c\ lst/ft_lstsort.c\
lst/lst_insert_sort.c\ lst/lst_insert_sort.c\
lst/ft_lst_pop.c\
lst/pop.c\ lst/pop.c\
lst/push.c\ lst/push.c\
lst/top.c\ lst/top.c\
lst/ft_lst2str.c\
math/ft_addrcmp.c\ math/ft_addrcmp.c\
math/ft_ilen.c\ math/ft_ilen.c\
math/ft_ilen_base.c\ math/ft_ilen_base.c\
@ -134,10 +133,10 @@ mem/ft_memset.c\
mem/ft_realloc.c\ mem/ft_realloc.c\
net/create_client.c\ net/create_client.c\
net/create_server.c\ net/create_server.c\
net/forge_ip.c\
net/forge_tcp.c\
net/net_get.c\ net/net_get.c\
net/net_send.c\ net/net_send.c\
net/forge_tcp.c\
net/forge_ip.c\
net/reserve_port.c\ net/reserve_port.c\
path/ft_path_notdir.c\ path/ft_path_notdir.c\
printing/ft_putchar.c\ printing/ft_putchar.c\
@ -145,6 +144,7 @@ printing/ft_putendl.c\
printing/ft_putnbr.c\ printing/ft_putnbr.c\
printing/ft_putstr.c\ printing/ft_putstr.c\
printing/hexdump.c\ printing/hexdump.c\
rs/rs.c\
sstr/ft_sstradd.c\ sstr/ft_sstradd.c\
sstr/ft_sstrcat.c\ sstr/ft_sstrcat.c\
sstr/ft_sstrdel.c\ sstr/ft_sstrdel.c\
@ -155,7 +155,6 @@ sstr/ft_sstrprint.c\
sstr/ft_sstrprint_fd.c\ sstr/ft_sstrprint_fd.c\
sstr/ft_sstrsort.c\ sstr/ft_sstrsort.c\
sstr/ft_sstrstr.c\ sstr/ft_sstrstr.c\
str/ft_strsepjoin.c\
str/ft_atoi.c\ str/ft_atoi.c\
str/ft_convert_base.c\ str/ft_convert_base.c\
str/ft_putaddr_fd.c\ str/ft_putaddr_fd.c\
@ -174,7 +173,6 @@ str/ft_strcspn.c\
str/ft_strcut.c\ str/ft_strcut.c\
str/ft_strdel.c\ str/ft_strdel.c\
str/ft_strdup.c\ str/ft_strdup.c\
str/ft_strndup.c\
str/ft_strdupchr.c\ str/ft_strdupchr.c\
str/ft_strduptr.c\ str/ft_strduptr.c\
str/ft_strequ.c\ str/ft_strequ.c\
@ -191,31 +189,24 @@ str/ft_strmapi.c\
str/ft_strncat.c\ str/ft_strncat.c\
str/ft_strncmp.c\ str/ft_strncmp.c\
str/ft_strncpy.c\ str/ft_strncpy.c\
str/ft_strndup.c\
str/ft_strnequ.c\ str/ft_strnequ.c\
str/ft_strnew.c\ str/ft_strnew.c\
str/ft_strnstr.c\ str/ft_strnstr.c\
str/ft_strrchr.c\ str/ft_strrchr.c\
str/ft_strreplace.c\ str/ft_strreplace.c\
str/ft_strrev.c\ str/ft_strrev.c\
str/ft_strsepjoin.c\
str/ft_strsplit.c\ str/ft_strsplit.c\
str/ft_strstr.c\ str/ft_strstr.c\
str/ft_strsub.c\ str/ft_strsub.c\
str/ft_strtok.c\ str/ft_strtok.c\
str/ft_strtrim.c\ str/ft_strtrim.c\
sys/open_new.c\
time/epoch.c\
time/ft_mytime_free.c\ time/ft_mytime_free.c\
time/ft_mytime_get.c\ time/ft_mytime_get.c\
time/ft_time_isrecent.c\ time/ft_time_isrecent.c
time/epoch.c\
sys/open_new.c\
htb/ft_hash_string.c\
htb/hashtab_init.c\
htb/hashtab_insert.c\
htb/hashtab_lookup.c\
htb/hashtab_remove.c\
htb/hashtab_destroy.c\
htb/hashtab_print.c\
htb/hashtab_generator.c\
htb/hashtab_del.c\
SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE))
OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o)) OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o))
@ -241,7 +232,7 @@ $(OBJ_DIR)%.o : $(SRC_DIR)%.c | $(OBJ_DIR)
@$(eval COLOR=$(shell list=(160 196 202 208 215 221 226 227 190 154 118 82 46); index=$$(($(PERCENT) * $${#list[@]} / 100)); echo "$${list[$$index]}")) @$(eval COLOR=$(shell list=(160 196 202 208 215 221 226 227 190 154 118 82 46); index=$$(($(PERCENT) * $${#list[@]} / 100)); echo "$${list[$$index]}"))
@printf "\r\033[38;5;%dm⌛ [%s]: %2d%% `printf '█%.0s' {0..$(DONE)}`%*s❙%*.*s\033[0m\033[K" $(COLOR) $(NAME) $(PERCENT) $(TO_DO) "" $(DELTA) $(DELTA) "$(shell echo "$@" | sed 's/^.*\///')" @printf "\r\033[38;5;%dm⌛ [%s]: %2d%% `printf '█%.0s' {0..$(DONE)}`%*s❙%*.*s\033[0m\033[K" $(COLOR) $(NAME) $(PERCENT) $(TO_DO) "" $(DELTA) $(DELTA) "$(shell echo "$@" | sed 's/^.*\///')"
@$(CC) $(FLAGS) -MMD -c $< -o $@\ @$(CC) $(FLAGS) -MMD -c $< -o $@\
-I $(INC_DIR) -lm -I $(INC_DIR)
@$(eval INDEX=$(shell echo $$(($(INDEX)+1)))) @$(eval INDEX=$(shell echo $$(($(INDEX)+1))))
clean : clean :

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/14 20:22:56 by jhalford #+# #+# */ /* Created: 2017/03/14 20:22:56 by jhalford #+# #+# */
/* Updated: 2017/03/25 14:59:53 by jhalford ### ########.fr */ /* Updated: 2017/10/07 18:04:53 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,7 +20,7 @@ typedef struct s_cliopts t_cliopts;
typedef struct s_data_template t_data_template; typedef struct s_data_template t_data_template;
typedef long long t_flag; typedef long long t_flag;
struct s_cliopts struct s_cliopts
{ {
char c; char c;
char *str; char *str;
@ -30,15 +30,15 @@ struct s_cliopts
int arg_required:1; int arg_required:1;
}; };
struct s_data_template struct s_data_template
{ {
t_flag flag; t_flag flag;
char **av_data; char **av_data;
}; };
int cliopts_get(char **av, t_cliopts opt_map[], void *data); int cliopts_get(char **av, t_cliopts opt_map[], void *data);
t_cliopts *cliopts_getmap_long(t_cliopts opt_map[], char *arg); t_cliopts *cliopts_getmap_long(t_cliopts opt_map[], char *arg);
t_cliopts *cliopts_getmap_short(t_cliopts opt_map[], char arg); t_cliopts *cliopts_getmap_short(t_cliopts opt_map[], char arg);
int cliopts_has(char **av, char c); int cliopts_has(char **av, char c);
#endif #endif

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 13:49:04 by jhalford #+# #+# */ /* Created: 2016/11/07 13:49:04 by jhalford #+# #+# */
/* Updated: 2017/05/16 17:47:18 by ariard ### ########.fr */ /* Updated: 2017/10/07 18:07:31 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -31,7 +31,6 @@
# include "lst.h" # include "lst.h"
# include "dlst.h" # include "dlst.h"
# include "btree.h" # include "btree.h"
# include "hashtab.h"
# include "str.h" # include "str.h"
# include "sstr.h" # include "sstr.h"
@ -90,7 +89,7 @@ int ft_putendl_fd(char const *s, int fd);
int ft_putnbr_fd(long n, int fd); int ft_putnbr_fd(long n, int fd);
int ft_putnbr_hex_fd(long n, int fd); int ft_putnbr_hex_fd(long n, int fd);
void hexdump(void *pAddressIn, long lSize); void *hexdump(void *addr, unsigned int offset, unsigned int size);
void *ft_realloc(void *data, int size); void *ft_realloc(void *data, int size);

View file

@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* net.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/07 18:06:12 by jhalford #+# #+# */
/* Updated: 2017/10/07 18:14:42 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef NET_H
# define NET_H
# define ACK 2
# define NACK 3
# define NET_MAXSIZE 512
# include <sys/socket.h>
# include <netdb.h>
# include <net/if.h>
# include <netinet/in.h>
# include <netinet/ip.h>
# include <netinet/tcp.h>
# include <netinet/ip_icmp.h>
# include <arpa/inet.h>
# include "mytime.h"
int create_server(int port, int backlog, char *protoname);
int create_client(char *addr, int port, char *protoname);
void listener(int domain, int sock, int proto,
void (*handler)(void *buf, int bytes, struct sockaddr_in *addr));
int net_send(int sock, char *msg, int size);
int net_send_large(int sock, char *msg, int size);
int net_get(int sock, char *msg, int size);
int net_get_fd(int sock, int fd, int size);
int net_get_large(int sock, int fd);
int reserve_port(int *port);
void tcphdr_init(struct tcphdr *header);
void iphdr_init(struct ip *header);
#endif

View file

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* rs.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/07 18:05:30 by jhalford #+# #+# */
/* Updated: 2017/10/07 18:18:16 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFT_RS_H
# define LIBFT_RS_H
# include <float.h>
# include <dlfcn.h>
# include <math.h>
extern struct s_stats {
int count;
double min;
double max;
double avg;
double m;
double stdev;
double var;
} g_rs;
void rs_clear();
void rs_push(double n);
void rs_calcmore();
#endif

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/14 17:24:23 by jhalford #+# #+# */ /* Created: 2017/03/14 17:24:23 by jhalford #+# #+# */
/* Updated: 2017/05/16 17:30:50 by ariard ### ########.fr */ /* Updated: 2017/10/07 18:10:34 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,23 +24,9 @@
# include <sys/types.h> # include <sys/types.h>
# include <sys/xattr.h> # include <sys/xattr.h>
/* # if HAVE_SYS_ACL_H */
/* # include <sys/acl.h> */
/* # endif */
# include <sys/stat.h> # include <sys/stat.h>
# include <fcntl.h> # include <fcntl.h>
/* int ft_xattr_print(char *path); */
/* int ft_xattr_count(char *path); */
/* char *ft_getenv(char **env, char *key); */
/* int open_access(char *file, t_flag a_flag, t_flag o_flag, t_flag o_perm); */
int open_new(char *filename, int oflag); int open_new(char *filename, int oflag);
/* int is_directory(const char *path); */
/* char *create_directory(const char *path, const char *old_pathnames); */
/* int dup2_close(int fd1, int fd2); */
/* int fd_replace(int fd1, int fd2); */
#endif #endif

View file

@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* btree_apply_by_level.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/08/19 12:06:15 by jhalford #+# #+# */
/* Updated: 2017/03/14 17:25:46 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "btree.h"
int g_level = 0;
static void btree_apply_to_level(
t_btree *root,
int level,
int is_first_elem,
void (*applyf)(void *item, int current_level, int is_first_elem))
{
if (level == g_level)
{
(*applyf)(root->item, level, is_first_elem);
return ;
}
if (root->left)
btree_apply_to_level(root->left, level + 1, is_first_elem, applyf);
if (root->right)
btree_apply_to_level(root->right, level + 1, 0, applyf);
}
void btree_apply_by_level(
t_btree *root,
void (*applyf)(void *item, int current_level, int is_first_elem))
{
int height;
height = btree_level_count(root);
while (g_level < height)
{
btree_apply_to_level(root, 0, 1, applyf);
g_level++;
}
}

View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* btree_create_node.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/08/16 13:43:51 by jhalford #+# #+# */
/* Updated: 2017/03/06 18:22:31 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "btree.h"
void btree_apply_infix(t_btree *root, void (*applyf)(void *))
{
if (root->left)
btree_apply_infix(root->left, applyf);
(*applyf)(root->item);
if (root->right)
btree_apply_infix(root->right, applyf);
return ;
}

View file

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* btree_create_node.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/08/16 13:43:51 by jhalford #+# #+# */
/* Updated: 2016/08/18 21:06:44 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "btree.h"
void btree_apply_prefix(t_btree *root, void (*applyf)(void *))
{
(*applyf)(root->item);
if (root->left)
btree_apply_prefix(root->left, applyf);
if (root->right)
btree_apply_prefix(root->right, applyf);
}

View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* btree_apply_suffix.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/05 11:43:08 by jhalford #+# #+# */
/* Updated: 2016/12/05 11:43:13 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "btree.h"
void btree_apply_suffix(t_btree *root, void (*applyf)(void *))
{
if (root->left)
btree_apply_suffix(root->left, applyf);
if (root->right)
btree_apply_suffix(root->right, applyf);
(*applyf)(root->item);
return ;
}

View file

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* btree_create_node.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/08/16 13:43:51 by jhalford #+# #+# */
/* Updated: 2017/03/21 15:43:50 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "btree.h"
t_btree *btree_create_node(void const *item, size_t content_size)
{
t_btree *new;
if (!(new = (t_btree *)ft_malloc(sizeof(t_btree))))
return (NULL);
new->left = 0;
new->right = 0;
if (!item)
{
new->content_size = 0;
new->item = NULL;
}
else
{
new->content_size = content_size;
new->item = ft_memalloc(content_size + 1);
ft_memcpy(new->item, item, content_size);
}
return (new);
}

View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* btree_del.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/05 11:43:03 by jhalford #+# #+# */
/* Updated: 2017/03/21 19:43:51 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "btree.h"
void btree_del(t_btree **root, void (*del)(void *, size_t))
{
if (root && *root && del)
{
btree_del(&(*root)->left, del);
btree_del(&(*root)->right, del);
btree_delone(root, del);
}
}

View file

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* btree_delone.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/05 11:45:51 by jhalford #+# #+# */
/* Updated: 2016/12/15 12:48:55 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "btree.h"
void btree_delone(t_btree **root, void (*del)(void *, size_t))
{
if (root && *root)
{
if (del)
(*del)((*root)->item, (*root)->content_size);
free(*root);
*root = NULL;
}
}

View file

@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* btree_create_node.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/08/16 13:43:51 by jhalford #+# #+# */
/* Updated: 2016/11/14 16:12:47 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "btree.h"
void btree_insert_data(
t_btree **root,
void *item,
size_t content_size,
int (*cmpf)(void *, void *))
{
t_btree *node;
if (!*root)
{
*root = btree_create_node(item, content_size);
return ;
}
node = *root;
if ((*cmpf)(item, node->item) < 0)
{
if (node->left)
btree_insert_data(&node->left, item, content_size, cmpf);
else
node->left = btree_create_node(item, content_size);
}
else
{
if (node->right)
btree_insert_data(&node->right, item, content_size, cmpf);
else
node->right = btree_create_node(item, content_size);
}
}

View file

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* btree_create_node.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/08/16 13:43:51 by jhalford #+# #+# */
/* Updated: 2016/08/25 17:46:00 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "btree.h"
int btree_level_count(t_btree *root)
{
return (root
? 1 + FT_MAX(btree_level_count(root->left),
btree_level_count(root->right))
: 0);
}

View file

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* btree_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/08 11:50:46 by jhalford #+# #+# */
/* Updated: 2017/03/24 15:49:42 by gwojda ### ########.fr */
/* */
/* ************************************************************************** */
#include "btree.h"
t_btree *btree_map(t_btree *root, void *(*f)(void *))
{
t_btree *new;
if (!root)
return (NULL);
if (!(new = (t_btree *)ft_malloc(sizeof(*new))))
return (NULL);
new->item = (*f)(root->item);
new->left = btree_map(root->left, f);
new->right = btree_map(root->right, f);
return (new);
}

View file

@ -0,0 +1,82 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* btree_print.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 18:06:24 by jhalford #+# #+# */
/* Updated: 2017/05/15 19:08:35 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "btree.h"
static int print_t2(t_printdata data, char s[20][255], char b[20])
{
int width;
int i;
width = 5;
i = -1;
while (++i < width)
s[2 * data.depth][data.offset + data.left + i] = b[i];
i = -1;
if (data.depth && data.is_left)
{
while (++i < width + data.right)
s[2 * data.depth - 1]
[data.offset + data.left + width / 2 + i] = '-';
s[2 * data.depth - 1][data.offset + data.left + width / 2] = '+';
s[2 * data.depth - 1]
[data.offset + data.left + data.right + 3 * width / 2] = '+';
}
else if (data.depth && !data.is_left)
{
while (++i < width + data.left)
s[2 * data.depth - 1][data.offset - width / 2 + i] = '-';
s[2 * data.depth - 1][data.offset + data.left + width / 2] = '+';
s[2 * data.depth - 1][data.offset - width / 2 - 1] = '+';
}
return (data.left + width + data.right);
}
static int print_t(t_btree *tree,
t_printdata data, char s[20][255], char *(*printer)(void *))
{
char b[20];
int width;
width = 5;
if (!tree)
return (0);
sprintf(b, "%5s", printer(tree->item));
data.left = print_t(tree->left, (t_printdata){
1, data.offset, data.depth + 1, data.left, data.right}, s, printer);
data.right = print_t(tree->right, (t_printdata){
0, data.offset + data.left + width, data.depth + 1,
data.left, data.right}, s, printer);
return (print_t2(data, s, b));
}
void btree_print(int fd, t_btree *tree, char *(*printer)(void *))
{
char s[20][255];
char empty[255];
int i;
i = -1;
while (++i < 20)
sprintf(s[i], "%80s", " ");
sprintf(empty, "%80s", " ");
print_t(tree, (t_printdata){0, 0, 0, 0, 0}, s, printer);
i = -1;
while (++i < 20)
{
if (ft_strcmp(s[i], empty) == 0)
break ;
if (i == 0 && ft_strcmp(s[1], empty) == 0)
break ;
ft_dprintf(fd, "%s\n", s[i]);
}
}

View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* btree_create_node.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/08/16 13:43:51 by jhalford #+# #+# */
/* Updated: 2016/08/23 19:04:56 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "btree.h"
void *btree_search_item(t_btree *root,
void *data_ref, int (*cmpf)(void *, void *))
{
void *out;
out = NULL;
if (root)
{
out = btree_search_item(root->left, data_ref, cmpf);
if (!out && ((*cmpf)(root->item, data_ref) == 0))
out = root->item;
if (!out)
out = btree_search_item(root->right, data_ref, cmpf);
}
return (out);
}

View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_color_mk.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 11:35:18 by jhalford #+# #+# */
/* Updated: 2016/12/12 11:54:49 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_color_mk(t_color *color, char fg[7], char bg[7])
{
ft_strcpy(color->fg, fg);
ft_strcpy(color->bg, bg);
}

View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_color_mk.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/27 11:35:18 by jhalford #+# #+# */
/* Updated: 2016/12/03 12:17:14 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_color_mkif(t_color *color, int cond, char fg[7], char bg[7])
{
if (cond)
ft_color_mk(color, fg, bg);
}

View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_color_reset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/25 13:48:05 by jhalford #+# #+# */
/* Updated: 2016/11/25 14:06:26 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_color_reset(void)
{
ft_putstr(FG_DEFAULT);
ft_putstr(BG_DEFAULT);
}

View file

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_color_set.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/25 13:53:46 by jhalford #+# #+# */
/* Updated: 2016/12/12 11:55:03 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_color_set(t_color color)
{
char out[20];
ft_strcpy(out, color.fg);
ft_strcat(out, color.bg);
ft_putstr(out);
}

View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dlst_add_after.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 13:27:04 by jhalford #+# #+# */
/* Updated: 2016/11/07 13:27:36 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_dlstadd_after(t_dlist **alst, t_dlist *new)
{
if (new)
{
new->prev = (*alst);
if (*alst)
new->next = (*alst)->next;
else
new->next = NULL;
if (new->next)
new->next->prev = new;
if (new->prev)
new->prev->next = new;
*alst = new;
}
}

View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dlst_add_before.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 13:27:09 by jhalford #+# #+# */
/* Updated: 2016/11/07 13:27:10 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_dlstadd_before(t_dlist **alst, t_dlist *new)
{
if (new)
{
new->next = (*alst);
if (*alst)
new->prev = (*alst)->prev;
if (new->next)
new->next->prev = new;
if (new->prev)
new->prev->next = new;
*alst = new;
}
}

View file

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dlstdel.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/14 17:55:40 by jhalford #+# #+# */
/* Updated: 2016/12/07 17:29:54 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static void ft_dlstdelback(t_dlist **alst, void (*del)(void *, size_t))
{
if (alst && *alst && del)
{
ft_dlstdelback(&(*alst)->prev, del);
ft_dlstdelone(alst, del);
}
}
static void ft_dlstdelfront(t_dlist **alst, void (*del)(void *, size_t))
{
if (alst && *alst && del)
{
ft_dlstdelfront(&(*alst)->next, del);
ft_dlstdelone(alst, del);
}
}
void ft_dlstdel(t_dlist **alst, void (*del)(void *, size_t))
{
if (alst && *alst && del)
{
ft_dlstdelback(&(*alst)->prev, del);
ft_dlstdelfront(&(*alst)->next, del);
ft_dlstdelone(alst, del);
}
}

View file

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dlst_delone.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 13:27:13 by jhalford #+# #+# */
/* Updated: 2016/11/14 17:52:58 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_dlstdelone(t_dlist **alst, void (*del)(void *, size_t))
{
t_dlist *tmp;
tmp = *alst;
if (tmp)
{
if (del)
(*del)(tmp->content, tmp->content_size);
if (tmp->next)
tmp->next->prev = tmp->prev;
if (tmp->prev)
tmp->prev->next = tmp->next;
if (tmp->prev)
*alst = tmp->prev;
else
*alst = tmp->next;
free(tmp);
}
}

View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dlst_last.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 13:27:15 by jhalford #+# #+# */
/* Updated: 2016/11/07 13:27:15 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_dlist *ft_dlstlast(t_dlist *list)
{
while (list && list->next)
list = list->next;
return (list);
}

View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dlst_new.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 13:27:20 by jhalford #+# #+# */
/* Updated: 2017/03/21 15:43:50 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_dlist *ft_dlstnew(void const *content, size_t content_size)
{
t_dlist *new;
if (!content)
{
new = ft_malloc(sizeof(*new));
if (!new)
return (NULL);
new->content_size = 0;
new->content = NULL;
}
else
{
new = (t_dlist *)ft_malloc(sizeof(*new));
if (!new)
return (NULL);
new->content_size = content_size;
new->content = ft_memalloc(content_size + 1);
ft_memcpy(new->content, content, content_size);
}
new->next = NULL;
new->prev = NULL;
return (new);
}

View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dlstrtostr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 13:27:29 by jhalford #+# #+# */
/* Updated: 2016/11/14 16:13:24 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_dlsttostr(t_dlist *list)
{
char *str;
if (!list)
return (NULL);
while (list->prev)
list = list->prev;
str = (char *)ft_strnew(sizeof(char) * (ft_dlstsize(list) + 2));
while (list)
{
ft_strcat(str, (char *)list->content);
list = list->next;
}
return (str);
}

View file

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dlst_size.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 13:27:23 by jhalford #+# #+# */
/* Updated: 2016/11/07 13:27:23 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_dlstsize(t_dlist *list)
{
int size;
t_dlist *tmp;
size = 0;
if (list)
size++;
tmp = list;
while (tmp->next)
{
size++;
tmp = tmp->next;
}
tmp = list;
while (tmp->prev)
{
size++;
tmp = tmp->prev;
}
return (size);
}

View file

@ -0,0 +1,89 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/15 13:12:06 by jhalford #+# #+# */
/* Updated: 2017/03/21 15:43:51 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
static int ft_fdcmp(t_save *a, int *b)
{
return (a->fd - *b);
}
static void ft_gnlfree(void *a, size_t size)
{
(void)size;
ft_strdel(&((t_save*)a)->str);
free(a);
}
static t_list *ft_newfd(t_list **head, int fd)
{
t_save new;
new.fd = fd;
new.str = ft_memalloc((BUFF_SIZE > 0 ? BUFF_SIZE : 0) + 1);
ft_lstadd(head, ft_lstnew(&new, sizeof(t_save)));
return (*head);
}
static int ft_loop_read(int fd, char **line, char *save)
{
char buf[BUFF_SIZE + 1];
char *pos;
char *tmp;
int ret;
while ((ret = read(fd, buf, BUFF_SIZE)) > 0)
{
buf[ret] = 0;
tmp = *line;
if ((pos = ft_strchr(buf, '\n')))
{
ft_strcpy(save, pos + 1);
*pos = 0;
}
if (!(*line = ft_strjoin(*line, buf)))
return (-1);
ft_strdel(&tmp);
if (pos)
return (1);
}
if (ret < 0)
return (-1);
return (**line ? 1 : 0);
}
int get_next_line(int const fd, char **line)
{
static t_list *head;
t_list *tmp;
char *pos;
char *save;
int ret;
if (fd < 0 || !line)
return (-1);
if (!(tmp = ft_lst_find(head, (void *)&fd, &ft_fdcmp)))
tmp = ft_newfd(&head, fd);
save = ((t_save*)tmp->content)->str;
if (!(*line = ft_strdup(save)))
return (-1);
ft_bzero(save, BUFF_SIZE + 1);
if ((pos = ft_strchr(*line, '\n')))
{
ft_strcpy(save, pos + 1);
*pos = 0;
return (1);
}
if (!(ret = ft_loop_read(fd, line, save)))
ft_lst_delif(&head, (int *)&fd, ft_fdcmp, &ft_gnlfree);
return (ret);
}

View file

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lst2str.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/07 17:53:18 by jhalford #+# #+# */
/* Updated: 2017/10/07 17:53:19 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_lst2str(t_list *list)
{
size_t size;
t_list *tmp;
char *new;
size = 0;
tmp = list;
while (tmp)
{
size += ft_strlen((char *)tmp->content) + 1;
tmp = tmp->next;
}
new = ft_memalloc(size);
while (list)
{
ft_strcat(new, (char *)list->content);
if (list->next)
ft_strcat(new, " ");
list = list->next;
}
return (new);
}

View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lst_pop.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/07 17:53:30 by jhalford #+# #+# */
/* Updated: 2017/10/07 17:53:31 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lst_pop(t_list **lst)
{
t_list *top;
top = *lst;
if (lst && *lst)
*lst = (*lst)->next;
return (top);
}

Some files were not shown because too many files have changed in this diff Show more