add: bozolib.h

This commit is contained in:
starnakin 2023-06-28 15:50:51 +02:00
commit 6c1a57efaf
10 changed files with 173 additions and 0 deletions

28
Makefile Normal file
View File

@ -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)

4
bozolib.h Normal file
View File

@ -0,0 +1,4 @@
#pragma once
#include "./src/lst/lst.h"
#include "./src/str/str.h"

14
src/lst/lst.h Normal file
View File

@ -0,0 +1,14 @@
#pragma once
#include <stddef.h>
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 *));

18
src/lst/lst_addback.c Normal file
View File

@ -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;
}

14
src/lst/lst_find.c Normal file
View File

@ -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);
}

12
src/lst/lst_iter.c Normal file
View File

@ -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;
}
}

14
src/lst/lst_len.c Normal file
View File

@ -0,0 +1,14 @@
#include <stddef.h>
#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;
}

54
src/str/split.c Normal file
View File

@ -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);
}

6
src/str/str.h Normal file
View File

@ -0,0 +1,6 @@
#pragma once
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
char **split(const char *str, const char *delimiter);

9
src/str/str_shift.c Normal file
View File

@ -0,0 +1,9 @@
#include "./str.h"
void str_shift(char *str, int shift)
{
for (size_t i = 0; str[i] == '\0'; i++)
{
}
}