libdev.a

C Helper library
Log | Files | Refs | README | LICENSE | git clone https://git.ne02ptzero.me/git/libdev.a

commit c4bf78c222a2f207b0dfca940a1b9c4103e11c4c
parent 80f664a86f347be5e941451f86bad895e7395cfb
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Fri,  7 Oct 2016 18:17:42 +0200

Add(Linked lists helpers):

Not tested, at all.

Diffstat:
AMakefile | 23+++++++++++++++++++++++
Alibdev.h | 40++++++++++++++++++++++++++++++++++++++++
Alists.c | 84+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 147 insertions(+), 0 deletions(-)

diff --git a/Makefile b/Makefile @@ -0,0 +1,23 @@ +# Makefile +NAME = libdev +CC = gcc +LIB = ar +CFLAGS = -Wall -Wextra -Werror -I. +LFLAGS = -cq +SRCS = $(wildcard *.c) +OBJS = $(SRCS:%.c=%.o) + +all: $(NAME) + +$(NAME): $(OBJS) + $(LIB) $(LFLAGS) $(NAME).a $(OBJS) + +clean: + rm -f $(OBJS) + +fclean: clean + rm -f $(NAME).a + +re: fclean all + +.PHONY: re fclean all diff --git a/libdev.h b/libdev.h @@ -0,0 +1,40 @@ +#ifndef __LIBDEV__ +# define __LIBDEV__ +# include <stdlib.h> +# include <assert.h> +# include <string.h> + +/* DEFINES */ +# define list_add(ptr, member, sizeZ) ptr = list_add_member(ptr, member, sizeZ); + +# define list_for_each(org_list, temp, p_tr)\ + for (temp = org_list, p_tr = temp->member;\ + temp && (p_tr = temp->member); temp = temp->next) + +# define list_for_each_rev(org_list, temp, p_tr)\ + for (temp = list_get_last(org_list), p_tr = temp->member;\ + temp != temp->head && (p_tr = temp->member); temp = temp->prev) + +# define list_tail(org_list, p_tr) ptr = list_get_last(org_list)->member; + +# define list_add_after(org_list, p_tr1, p_tr2, sizeZ) org_list = list_insert_after(org_list, p_tr1, p_tr2, sizeZ); + +# define list_add_before(org_list, p_tr1, p_tr2, sizeZ) org_list = list_insert_before(org_list, p_tr1, p_tr2, sizeZ); + +/* STRUCTS */ +typedef struct s_list { + void *member; // Actual member + size_t size; // Size of the member + struct s_list *next; // Next in list + struct s_list *prev; // Prev in list + struct s_list *head; // Head of the list +} t_list; + +/* FUNCTIONS DEFINITIONS */ +// lists.c +t_list *list_add_member(t_list *list, void *member, size_t size); +t_list *list_get_last(t_list *list); +t_list *list_insert_after(t_list *org, t_list *ptr, void *member, size_t size); +t_list *list_insert_before(t_list *org, t_list *ptr, void *member, size_t size); + +#endif /* __LIBDEV__ */ diff --git a/lists.c b/lists.c @@ -0,0 +1,84 @@ +#include <libdev.h> + +t_list *list_add_member(t_list *list, void *member, size_t size) { + t_list *n_member, *tmp; + + n_member = malloc(sizeof(t_list)); + n_member->member = malloc(size); + assert(n_member && n_member->member); + memcpy(n_member->member, member, size); + n_member->size = size; + n_member->next = n_member->prev = 0x0; + if (!list) { + n_member->head = n_member; + return n_member; + } + for (tmp = list; tmp->next; tmp = tmp->next); + tmp->next = n_member; + n_member->prev = tmp; + n_member->head = list; + return list; +} + +t_list *list_get_last(t_list *list) { + t_list *tmp; + + if (!list) + return 0x0; + for (tmp = list; tmp->next; tmp = tmp->next); + return tmp; +} + +t_list *list_insert_after(t_list *org, t_list *ptr, void *member, size_t size) { + t_list *n_member, *tmp, *tmp2; + + n_member = malloc(sizeof(t_list)); + n_member->member = malloc(size); + assert(n_member && n_member->member); + memcpy(n_member->member, member, size); + n_member->size = size; + n_member->next = n_member->prev = 0x0; + if (!org) { + n_member->head = n_member; + return n_member; + } + for (tmp = org; tmp->next && tmp != ptr; tmp = tmp->next); + if (!tmp->next) { + tmp->next = n_member; + n_member->prev = tmp; + } else { + tmp2 = tmp->next; + tmp->next = n_member; + n_member->prev = tmp; + tmp2->prev = n_member; + } + n_member->head = org; + return org; +} + +t_list *list_insert_before(t_list *org, t_list *ptr, void *member, size_t size) { + t_list *n_member, *tmp, *tmp2; + + n_member = malloc(sizeof(t_list)); + n_member->member = malloc(size); + assert(n_member && n_member->member); + memcpy(n_member->member, member, size); + n_member->size = size; + n_member->next = n_member->prev = 0x0; + if (!org) { + n_member->head = n_member; + return n_member; + } + for (tmp = org; tmp->next && tmp != ptr; tmp = tmp->next); + if (!tmp->next) { + tmp->next = n_member; + n_member->prev = tmp; + } else { + tmp2 = tmp->prev; + n_member->next = tmp; + tmp->prev = n_member; + tmp2->next = n_member; + } + n_member->head = org; + return org; +}