commit 6c1a57efaf5b4ffdb4078b41406ea7f213a82d8e Author: starnakin Date: Wed Jun 28 15:50:51 2023 +0200 add: bozolib.h diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3b4ff47 --- /dev/null +++ b/Makefile @@ -0,0 +1,28 @@ +CC = gcc +CFLAGS = -Wall -Wextra -Werror + +NAME = bozolib +SRC_DIR = src +OBJ_DIR = obj +SOURCES = $(shell find $(SRC_DIR) -type f -name '*.c') +OBJECTS = $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SOURCES)) +DEPS = $(OBJECTS:.o=.d) + +all: $(NAME) + +$(NAME): $(OBJECTS) + ar -rc $(NAME) $(OBJECTS) + +-include $(DEPS) + +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c + @mkdir -p $(@D) + $(CC) $(CFLAGS) -c $< -o $@ + +clean: + rm -rf $(OBJ_DIR) + +fclean: clean + rm -f $(NAME) + +re: fclean $(NAME) diff --git a/bozolib.h b/bozolib.h new file mode 100644 index 0000000..d53029f --- /dev/null +++ b/bozolib.h @@ -0,0 +1,4 @@ +#pragma once + +#include "./src/lst/lst.h" +#include "./src/str/str.h" diff --git a/src/lst/lst.h b/src/lst/lst.h new file mode 100644 index 0000000..d14f38a --- /dev/null +++ b/src/lst/lst.h @@ -0,0 +1,14 @@ +#pragma once +#include + +typedef struct s_lst +{ + void* content; + struct s_lst* next; +} lst; + +size_t lst_len(const lst* root); +void lst_iter(lst** root, void (*f)(void *)); +lst* lst_find(lst** root, const lst* to_find, int (*cmp)(const lst* lst1, const lst* lst2)); +void lst_addback(lst** root, lst* nouveau); +void lst_clear(lst** root, void (*del)(void *)); diff --git a/src/lst/lst_addback.c b/src/lst/lst_addback.c new file mode 100644 index 0000000..5f80d92 --- /dev/null +++ b/src/lst/lst_addback.c @@ -0,0 +1,18 @@ +#include "lst.h" + +void lst_addback(lst **root, lst *nouveau) +{ + lst* current = *root; + + if (root == NULL || nouveau == NULL) + return; + nouveau->next = NULL; + if (current == NULL) + { + current = nouveau; + return; + } + while (current->next != NULL) + current = current->next; + current->next = nouveau; +} diff --git a/src/lst/lst_find.c b/src/lst/lst_find.c new file mode 100644 index 0000000..bc279b1 --- /dev/null +++ b/src/lst/lst_find.c @@ -0,0 +1,14 @@ +#include "./lst.h" + +lst* lst_find(lst** root, const lst* to_find, int (*cmp)(const lst* lst1, const lst* lst2)) +{ + lst* current = *root; + + while (current != NULL) + { + if ((*cmp)(to_find, current) == 0) + return current; + current = current->next; + } + return (NULL); +} diff --git a/src/lst/lst_iter.c b/src/lst/lst_iter.c new file mode 100644 index 0000000..fb8210c --- /dev/null +++ b/src/lst/lst_iter.c @@ -0,0 +1,12 @@ +#include "./lst.h" + +void lst_iter(lst** root, void (*f)(void *)) +{ + lst *current = *root; + + while (current != NULL) + { + (*f)(current); + current = current->next; + } +} diff --git a/src/lst/lst_len.c b/src/lst/lst_len.c new file mode 100644 index 0000000..999a93f --- /dev/null +++ b/src/lst/lst_len.c @@ -0,0 +1,14 @@ +#include +#include "./lst.h" + +size_t lst_len(const lst* root) +{ + size_t i = 0; + const lst *current = root; + while (current != NULL) + { + current = current->next; + i++; + } + return i; +} diff --git a/src/str/split.c b/src/str/split.c new file mode 100644 index 0000000..0e44dd0 --- /dev/null +++ b/src/str/split.c @@ -0,0 +1,54 @@ +#include "str.h" + +static void free_tab(char **tab) +{ + for (size_t i = 0; tab[i] != NULL; i++) + free(tab); + free(tab); +} + +static ssize_t fill_tab(const char *str, const char *delimiter, char **tab) +{ + size_t len = 0; + size_t delimiter_len = strlen(delimiter); + const char* tmp = str; + const char* next; + + while (tmp != NULL) + { + while (strcmp(tmp, delimiter) == 0) + tmp += delimiter_len; + next = strstr(tmp, delimiter); + if (tab != NULL) + { + if (next == NULL) + tab[len] = strndup(tmp, strlen(tmp)); + else + tab[len] = strndup(tmp, next - tmp); + if (tab[len] == NULL) + { + free_tab(tab); + return (-1); + } + } + len++; + tmp = next; + } + if (tab != NULL) + tab[len] = NULL; + return (len); +} + +char **split(const char *str, const char *delimiter) +{ + ssize_t len; + char **tab; + + len = fill_tab(str, delimiter, NULL); + tab = malloc((len + 1) * sizeof(char *)); + if (tab == NULL) + return (NULL); + if (fill_tab(str, delimiter, tab) == -1) + return (NULL); + return (tab); +} diff --git a/src/str/str.h b/src/str/str.h new file mode 100644 index 0000000..e89c368 --- /dev/null +++ b/src/str/str.h @@ -0,0 +1,6 @@ +#pragma once +#include +#include +#include + +char **split(const char *str, const char *delimiter); diff --git a/src/str/str_shift.c b/src/str/str_shift.c new file mode 100644 index 0000000..d735746 --- /dev/null +++ b/src/str/str_shift.c @@ -0,0 +1,9 @@ +#include "./str.h" + +void str_shift(char *str, int shift) +{ + for (size_t i = 0; str[i] == '\0'; i++) + { + + } +}