initial commit

This commit is contained in:
Your Name 2025-09-22 18:53:12 +02:00
commit 2365171e8e
106 changed files with 5136 additions and 0 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
/build
/minirootfs.tar.gz
/alpine
/assets
*.a
*.o

53
Makefile Normal file
View File

@ -0,0 +1,53 @@
CC = clang
SRC_DIR := src
SRC := $(SRC_DIR)/main.c
BUILD_DIR := build
ASSETS := assets
TARGET := $(BUILD_DIR)/hello
CTOOLS := $(SRC_DIR)/ctools
LIBFT_DIR := $(CTOOLS)/libft
LIBFT := $(LIBFT_DIR)/lib/libft.a
LIB := $(LIBFT)
CFLAGS := -Wall -Wextra -Werror --target=arm-linux-gnueabihf
DOCKER_CMD := /bin/sh -c 'hello "host.docker.internal" "8000"'
MINIROOTFS := $(ASSETS)/minirootfs.tar.gz
ALPINE := alpine/
ALPINE_BIN := $(ALPINE)/bin
get: $(ALPINE) $(ASSETS)
wget https://dl-cdn.alpinelinux.org/alpine/v3.22/releases/armhf/alpine-minirootfs-3.22.1-armhf.tar.gz -O $(MINIROOTFS)
tar xvf $(MINIROOTFS) -C $^
all: $(SRC) $(BUILD_DIR) $(LIB)
$(CC) $(CFLAGS) -o $(TARGET) -static $(SRC) $(LIB)
cp $(TARGET) $(ALPINE_BIN)
tar czf $(MINIROOTFS) -C alpine ./
docker import --platform linux/arm/v7 $(MINIROOTFS) alpine-custom:latest
run: all
docker run --add-host=host.docker.internal:host-gateway --platform linux/arm/v7 -it alpine-custom $(DOCKER_CMD)
$(LIB):
make -C $(LIBFT_DIR)
$(BUILD_DIR):
mkdir -p $@
$(ALPINE):
mkdir -p $@
$(ASSETS):
mkdir -p $@
clean:
rm -rf $(BUILD_DIR)

3
src/ctools/README.md Normal file
View File

@ -0,0 +1,3 @@
# ctools
A collection of tool for C Programming, mostly for Linux

View File

@ -0,0 +1,109 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/18 20:20:17 by vvobis #+# #+# */
/* Updated: 2024/05/27 17:44:59 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
void *buffer_extend(void *ptr_old, size_t size_new, size_t size_old)
{
void *ptr_new;
void *ptr_back;
void *ptr_old_free;
ptr_new = g_calloc(size_new + 1, 1);
if (!ptr_new)
return (free(ptr_old), ptr_old = NULL, NULL);
ptr_back = ptr_new;
ptr_old_free = ptr_old;
while (size_old--)
*(char *)ptr_new++ = *(char *)ptr_old++;
return (free(ptr_old_free), ptr_old_free = NULL, ptr_back);
}
char *handle_no_nl(char **buf, char **left)
{
if (left)
{
free(*left);
*left = NULL;
}
if (!buf)
return (NULL);
if (g_strlen(*buf))
return (*buf);
free(*buf);
*buf = NULL;
return (NULL);
}
char *line_extract(char **buf_joined, char **left, size_t line_len)
{
char *line;
line = g_substr(*buf_joined, 0, line_len);
if (!line)
return (free(*buf_joined), *buf_joined = NULL, NULL);
*left = g_substr(*buf_joined, line_len, g_strlen(*buf_joined) - line_len);
free(*buf_joined);
*buf_joined = NULL;
if (!*left)
return (free(line), line = NULL, NULL);
return (line);
}
char *line_handle(char **buf_fetch)
{
char *buf_joined;
size_t line_len;
static char *left;
if (!buf_fetch)
return (free(left), NULL);
buf_joined = g_strjoin(left, *buf_fetch);
free(*buf_fetch);
*buf_fetch = NULL;
free(left);
left = NULL;
if (!buf_joined)
return (NULL);
line_len = find_newline(buf_joined);
if (line_len)
return (line_extract(&buf_joined, &left, line_len));
return (handle_no_nl(&buf_joined, &left));
}
char *get_next_line(int fd)
{
char *buf;
ssize_t bytes_read;
size_t buf_size_cur;
size_t buf_size_prev;
if (fd < 0)
return (line_handle(NULL), NULL);
buf = g_calloc(sizeof(*buf), BUFFER_SIZE + 1);
buf_size_prev = 0;
buf_size_cur = BUFFER_SIZE;
while (1)
{
if (!buf)
return (line_handle(NULL));
bytes_read = read(fd, buf + buf_size_prev, BUFFER_SIZE);
if (bytes_read < 0)
return (free(buf), buf = NULL, line_handle(NULL));
if (find_newline(buf) || bytes_read == 0)
break ;
buf_size_prev = buf_size_cur;
buf_size_cur += BUFFER_SIZE;
buf = buffer_extend(buf, buf_size_cur, buf_size_prev);
}
return (line_handle(&buf));
}

View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/15 14:07:36 by vvobis #+# #+# */
/* Updated: 2024/05/27 17:43:21 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef GET_NEXT_LINE_H
# define GET_NEXT_LINE_H
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 50
# endif
# define MAX_FD 1024
# include <stdlib.h>
# include <unistd.h>
# include <fcntl.h>
# include <stdint.h>
char *get_next_line(int fd);
char *g_strjoin(char const *s1, char const *s2);
size_t g_strlen(char const *str);
void *g_calloc(size_t n, size_t s);
char *g_substr(char const *s, unsigned int start, size_t len);
char *line_handle(char **buf_fetch);
size_t find_newline(char *buf);
#endif

View File

@ -0,0 +1,118 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/15 14:14:13 by vvobis #+# #+# */
/* Updated: 2024/05/20 11:38:03 by victor ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
size_t g_strlen(char const *str)
{
size_t i;
if (!str)
return (0);
i = 0;
while (*str)
{
i++;
str++;
}
return (i);
}
size_t find_newline(char *buf)
{
size_t i;
if (!buf)
return (0);
i = 0;
while (*buf)
{
if (*buf == '\n')
return (i + 1);
i++;
buf++;
}
return (0);
}
char *g_strjoin(char const *s1, char const *s2)
{
char *tmp;
unsigned int i;
unsigned int j;
if (!s2)
return (NULL);
if (!s1)
return (g_substr(s2, 0, g_strlen(s2)));
tmp = g_calloc(g_strlen(s1) + g_strlen(s2) + 1, sizeof(*tmp));
if (!tmp)
return (NULL);
i = 0;
if (s1)
{
while (s1[i])
{
tmp[i] = s1[i];
i++;
}
}
j = 0;
if (s2)
while (s2[j])
tmp[i++] = s2[j++];
return (tmp);
}
char *g_substr(char const *s, unsigned int start, size_t len)
{
char *tmp;
unsigned int i;
i = 0;
if (!s || start >= g_strlen(s) || len <= 0)
{
tmp = malloc(1);
if (!tmp)
return (NULL);
tmp[i] = 0;
return (tmp);
}
if (len + start > g_strlen(s))
len = g_strlen(s) - start;
tmp = g_calloc(len + 1, sizeof(*tmp));
if (!tmp)
return (NULL);
while (i < len && s[i])
{
tmp[i] = s[i + start];
i++;
}
return (tmp);
}
void *g_calloc(size_t n, size_t s)
{
char *tmp;
unsigned long i;
i = 0;
if (n == 0)
return (malloc(0));
if (SIZE_MAX / n < s)
return (NULL);
tmp = malloc(n * s);
if (tmp)
while (i < n * s)
tmp[i++] = 0;
return ((void *)tmp);
}

View File

@ -0,0 +1,9 @@
[[project]]
path = "./"
version = "0.10.0"
assembler = "nasm"
instruction_set = "x86-64"
[project.opts]
diagnostics = true
default_diagnostics = true

16
src/ctools/lang/Makefile Normal file
View File

@ -0,0 +1,16 @@
SRCDIR := src
SRC := $(addprefix $(SRCDIR)/, $(addsuffix .s, asem balloc split))
OBJDIR := obj
OBJ := $(addprefix $(OBJDIR)/, $(notdir $(SRC:.s=.o)))
$(shell mkdir -p $(OBJDIR))
all: $(OBJ)
ld -o debug $(OBJ) -nostdlib -static
$(OBJDIR)/%.o: $(SRCDIR)/%.s
nasm -felf64 -g $< -o $@
clean:
rm -rf $(OBJDIR) debug

100
src/ctools/lang/src/asem.s Normal file
View File

@ -0,0 +1,100 @@
%define DEF_BUFFER_LEN 4096
%define INPUT_LENGTH 64
extern split
section .data
message: db "Hello World!", 0xA, 0
message_length: equ $-message
filepath: db "./test.c"
section .bss
file_content_buffer: resb DEF_BUFFER_LEN
section .text
global _start
open_file:
mov rax, 2 ; sys_open
syscall
ret
close_file:
mov rax, 3 ; sys_close
syscall
ret
read_file_bytes: ; read(RDI: int fd, RSI: char *buffer, RDX: size_t size)
mov rax, 0 ; sys_read
syscall ;
ret
print_string: ; print_string(RDI: char *string)
call strlen
mov rdx, rax ; string length
mov rsi, rdi
mov rdi, 1 ; stdout
mov rax, 1 ; sys_write
syscall
ret
exit:
mov rax, 60
syscall
strlen: ; strlen(RDI: char *str)
push rcx ; save counter
xor rcx, rcx
.loop: ; loop until '\0' character
cmp byte [rdi + rcx], 0
jz .done
inc rcx
jmp .loop
.done:
mov rax, rcx ; mov return value to rax
pop rcx ; restore counter
ret
_start:
mov rdi, message ; prepare first arg -> string addr to rdi
call print_string
mov rdi, rax
push rax ; save rax
push rdi ; save rdi since arg comes into it
mov rdi, filepath ; filepath for open call
mov rsi, 0 ; O_RDONLY flag
call open_file
mov rbx, rax ; mov new fd to rbx
mov rdi, rbx ; first arg: mov fd to rdi for read call
mov rsi, file_content_buffer ; 2nd arg: buffer
mov rdx, 15 ; 3rd arg: read len
call read_file_bytes
mov byte [file_content_buffer + 15], 0xA ; newline
mov byte [file_content_buffer + 16], 0x0 ; 0 terminate string
push rdi ; save fd
mov rdi, rsi ; stdout fd
call print_string
pop rdi ; restore fd
call close_file
mov rdi, rsi
mov rsi, 0x20 ; ' ' space character
call split
.exit_early:
mov rdi, rax
call exit

View File

@ -0,0 +1,26 @@
global balloc
brk_find_break: ; RAX: long brk(0)
mov rdi, 0x0
mov rax, 0xC ; sys_brk
syscall
ret
balloc: ; RAX: long basic_malloc(RDI: size_t n, RSI void **heap_begin_ptr)
cmp dword [rsi], 0 ; check if heap_begin_ptr exist
jne .allocate ; allocate normally if exists
push rdi
call brk_find_break
mov qword [rsi], rax ; get heap beginning
pop rdi
.allocate:
mov rax, qword [rsi]
add rdi, rax
mov rax, 0xC ; sys_brk
syscall
ret

View File

@ -0,0 +1,43 @@
global split ; export split
section .text
count_splits: ; RAX: int count_splits(RDI: char *, RSI: int c)
push rbx
xor rcx, rcx
xor rbx, rbx
.loop:
cmp byte [rdi + rcx], 0
jz .done
cmp byte [rdi + rcx], sil
jne .skip
inc rbx
.skip:
inc rcx
jmp .loop
.done:
mov rax, rbx
pop rbx
ret
split: ; RAX: char ** split(RDI: char *str, RSI: int c)
push rbp
mov rbp, rsp ; save base pointer
; int count = [ rbp - 4 ]
sub rsp, 4 ; allocate local vars
call count_splits
mov rsp, rbp
pop rbp
ret
; count = count_splits()
mov [rbp - 4], rax
pop rbp
ret

5
src/ctools/lang/test.c Normal file
View File

@ -0,0 +1,5 @@
extern int write();
int main() {
write();
}

77
src/ctools/libft/Makefile Normal file
View File

@ -0,0 +1,77 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: vvobis <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/09/17 14:23:19 by vvobis #+# #+# #
# Updated: 2025/09/22 12:17:40 by victor ### ########.fr #
# #
# **************************************************************************** #
NAME := libft.a
CC := clang
CFLAGS := -Wall -Wextra -Werror
SRC := ft_bzero.c ft_isalnum.c ft_isalpha.c ft_isdigit.c \
ft_isprint.c ft_memcpy.c ft_memmove.c ft_memset.c \
ft_strlcat.c ft_strlcpy.c ft_strlen.c \
ft_isascii.c ft_strchr.c ft_strrchr.c ft_strncmp.c \
ft_toupper.c ft_tolower.c ft_memchr.c ft_strnstr.c \
ft_atoi.c ft_atod.c ft_memcmp.c ft_calloc.c ft_strdup.c \
ft_substr.c ft_strjoin.c ft_strtrim.c ft_split.c \
ft_itoa.c ft_strmapi.c ft_striteri.c ft_putchar_fd.c \
ft_putstr_fd.c ft_putendl_fd.c ft_putnbr_fd.c \
ft_isspace.c ft_free.c ft_read.c
SRCGNL := ../gnl/get_next_line.c ../gnl/get_next_line_utils.c
SRCPRINT := printf/ft_fprintf.c printf/ft_printf.c printf/ft_putascii.c printf/ft_putptr.c printf/ft_puthex.c printf/ft_putfloat.c
SRCBON := ft_lstnew_bonus.c ft_lstadd_front_bonus.c ft_lstsize_bonus.c ft_lstlast_bonus.c ft_lstadd_back_bonus.c ft_lstdelone_bonus.c ft_lstclear_bonus.c ft_lstiter_bonus.c ft_lstmap_bonus.c
OBJBON := $(SRCBON:%.c=%.o)
OBJ := $(SRC:%.c=%.o)
OBJGNL := $(SRCGNL:%.c=%.o)
OBJPRINT := $(SRCPRINT:%.c=%.o)
LIBDIR := lib
LIBS := printf
all: $(NAME)
arch-armhf: CFLAGS += --target=arm-linux-gnueabihf -static
arch-armhf: fclean all
bonus: $(OBJBON) $(OBJ)
ar rcs libft.a $(OBJ) $(OBJBON)
$(NAME): $(LIBDIR) $(OBJ) $(OBJGNL) $(OBJPRINT) $(LIBS)
ar rcs $(LIBDIR)/$@ $(OBJ) $(OBJGNL) $(OBJPRINT)
$(LIBDIR):
mkdir -p $@
%.o: %.c
$(CC) -c $(CFLAGS) $^ -o $@
re: fclean all
so:
$(CC) -fPIC $(CFLAGS) $(SRC) test.c
gcc -shared -o libft.so $(OBJ) $(OBJBON)
fclean: clean
rm -rf $(LIBDIR)
make fclean -C $(LIBS)
clean:
rm -f $(OBJ) $(OBJBON) $(OBJGNL)
make clean -C $(LIBS)

View File

@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atod.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: victor </var/spool/mail/victor> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/08 22:25:35 by victor #+# #+# */
/* Updated: 2024/12/05 20:08:42 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
double atod_collect_fraction(char *n, double d)
{
int period;
int mult;
mult = 1;
period = 0;
while (*n && !ft_isspace(*n))
{
if (*n == '.' && period == 0)
{
period = 1;
n++;
continue ;
}
else if (((*n == '.' && period == 1) || (*n != '.' && !ft_isdigit(*n))))
return (ft_putendl_fd("Invalid double format: Too many periods", \
2), exit(1), 0);
if (period)
mult *= 10;
d *= 10;
d += (*n - '0');
n++;
}
return (d / mult);
}
double ft_atod(char *n)
{
double d;
int sign;
if (!n)
exit(1);
sign = 1;
if (*n == '-')
{
sign = -1;
n++;
}
d = 0;
return (atod_collect_fraction(n, d) * sign);
}

View File

@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bszilas <bszilas@student.42vienna.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/10 10:49:04 by vvobis #+# #+# */
/* Updated: 2024/09/13 22:19:12 by bszilas ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int is_space(char const c)
{
if ((c >= 9 && c <= 13) || c == ' ')
return (1);
return (0);
}
int ft_atoi(char const *s)
{
int nb;
char const *tmp;
nb = 0;
while (is_space(*s))
s++;
tmp = s;
if (*tmp == '+' || *tmp == '-')
tmp++;
while (*tmp >= '0' && *tmp <= '9')
{
nb *= 10;
nb += (*tmp - '0');
tmp++;
}
if (*s == '-')
nb = -nb;
return (nb);
}
long ft_atol(const char *nptr)
{
int64_t n;
int sign;
n = 0;
sign = 1;
while (ft_isspace(*nptr))
nptr++;
if (*nptr == '-')
sign = -1;
if (sign == -1 || *nptr == '+')
nptr++;
while (*nptr >= '0' && *nptr <= '9')
n = n * 10 + (*nptr++ - '0');
return (n * sign);
}

View File

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_bzero.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 16:04:55 by vvobis #+# #+# */
/* Updated: 2024/04/09 20:54:53 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_bzero(void *s, size_t n)
{
while (n--)
*(char *)s++ = 0;
}

View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/04 13:44:00 by vvobis #+# #+# */
/* Updated: 2024/04/10 16:39:56 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_calloc(size_t n, size_t s)
{
char *tmp;
unsigned long i;
i = 0;
if (n == 0)
return (malloc(0));
if (SIZE_MAX / n < s)
return (NULL);
tmp = malloc(n * s);
if (tmp)
while (i < n * s)
tmp[i++] = 0;
return ((void *)tmp);
}

View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_free.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/10 12:16:39 by vvobis #+# #+# */
/* Updated: 2024/09/10 12:17:34 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_free(void *ptr_ptr)
{
void **ptr;
ptr = ptr_ptr;
free(*ptr);
*ptr = NULL;
}

View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalnum.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 15:59:18 by vvobis #+# #+# */
/* Updated: 2024/04/02 15:59:23 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalnum(int c)
{
if (ft_isdigit(c) || ft_isalpha(c))
return (1);
return (0);
}

View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalpha.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 15:59:49 by vvobis #+# #+# */
/* Updated: 2024/04/02 16:00:04 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalpha(int c)
{
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
return (1);
return (0);
}

View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isascii.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 19:48:35 by vvobis #+# #+# */
/* Updated: 2024/04/02 19:49:36 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isascii(int c)
{
if (c >= 0 && c <= 127)
return (1);
return (0);
}

View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 16:14:56 by vvobis #+# #+# */
/* Updated: 2024/04/02 19:38:45 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isdigit(int c)
{
if (c >= '0' && c <= '9')
return (1);
return (0);
}

View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isprint.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 16:00:49 by vvobis #+# #+# */
/* Updated: 2024/04/02 16:00:52 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isprint(int c)
{
if (c >= 32 && c <= 126)
return (1);
return (0);
}

View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isspace.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: victor </var/spool/mail/victor> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/08 18:27:49 by victor #+# #+# */
/* Updated: 2024/09/08 18:27:55 by victor ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isspace(int c)
{
return (c == ' ' || (c > 9 && c < 14));
}

View File

@ -0,0 +1,75 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/05 20:16:48 by vvobis #+# #+# */
/* Updated: 2024/04/11 14:48:59 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int get_len(int n)
{
int i;
if (n < 0)
n = -n;
i = 0;
while (n)
{
i++;
n /= 10;
}
return (i);
}
static char *is_negative(int n)
{
int i;
char *num;
i = get_len(n);
num = malloc(sizeof(*num) * i + 2);
if (!num)
return (NULL);
num[i + 1] = 0;
while (n)
{
num[i--] = -(n % 10) + 48;
n /= 10;
}
num[i] = 0x2d;
return (num);
}
static char *is_positive(int n)
{
int i;
char *num;
i = get_len(n);
num = malloc(sizeof(*num) * i + 1);
if (!num)
return (NULL);
num[i--] = 0;
while (n)
{
num[i--] = (n % 10) + 48;
n /= 10;
}
return (num);
}
char *ft_itoa(int n)
{
if (n == 0)
return (ft_strdup("0"));
else if (n < 0)
return (is_negative(n));
else
return (is_positive(n));
}

View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_back.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 13:51:32 by vvobis #+# #+# */
/* Updated: 2024/04/09 18:17:12 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstadd_back(t_list **lst, t_list *new)
{
t_list *tmp;
if (!new)
return ;
if (*lst)
{
tmp = *lst;
while (tmp->next)
tmp = tmp->next;
tmp->next = new;
}
else
*lst = new;
}

View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_front.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 12:53:08 by vvobis #+# #+# */
/* Updated: 2024/04/09 18:17:50 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstadd_front(t_list **lst, t_list *new)
{
if (!lst || !new)
return ;
if (!*lst)
*lst = new;
else
{
new->next = *lst;
*lst = new;
}
}

View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstclear.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 15:44:23 by vvobis #+# #+# */
/* Updated: 2024/04/09 22:35:41 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstclear(t_list **lst, void (*del)(void *))
{
t_list *tmp;
if (!*lst || !del || !lst)
return ;
while (*lst)
{
tmp = (*lst)->next;
del((*lst)->content);
free(*lst);
*lst = tmp;
}
}

View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstdelone.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 14:24:03 by vvobis #+# #+# */
/* Updated: 2024/04/09 15:42:04 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstdelone(t_list *lst, void (*del)(void *))
{
if (!lst || !del)
return ;
del(lst->content);
free(lst);
}

View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstiter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 15:57:51 by vvobis #+# #+# */
/* Updated: 2024/04/09 18:20:05 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstiter(t_list *lst, void (*f)(void *))
{
if (!lst || !f)
return ;
while (lst)
{
f(lst->content);
lst = lst->next;
}
}

View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstlast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 13:47:08 by vvobis #+# #+# */
/* Updated: 2024/04/09 15:11:51 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstlast(t_list *lst)
{
if (!lst)
return (NULL);
while (lst->next)
lst = lst->next;
return (lst);
}

View File

@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 16:18:03 by vvobis #+# #+# */
/* Updated: 2024/04/09 23:40:57 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static void *free_list(t_list **head, void (*del)(void *), void *fcontent)
{
if (fcontent)
del(fcontent);
ft_lstclear(head, del);
return (NULL);
}
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *tmp;
t_list *head;
void *fcontent;
if (!lst || !f || !del)
return (NULL);
head = NULL;
while (lst)
{
fcontent = f(lst->content);
if (!fcontent)
return (free_list(&head, del, fcontent));
tmp = ft_lstnew(fcontent);
if (!tmp)
return (free_list(&head, del, fcontent));
ft_lstadd_back(&head, tmp);
lst = lst->next;
}
return (head);
}

View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstnew.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 11:10:05 by vvobis #+# #+# */
/* Updated: 2024/04/09 18:59:48 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstnew(void *content)
{
t_list *tmp;
tmp = malloc(sizeof(*tmp));
if (!tmp)
return (NULL);
tmp->next = NULL;
tmp->content = content;
return (tmp);
}

View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstsize.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 13:43:08 by vvobis #+# #+# */
/* Updated: 2024/04/09 18:21:11 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_lstsize(t_list *lst)
{
int i;
if (!lst)
return (0);
i = 1;
while (lst->next)
{
lst = lst->next;
i++;
}
return (i);
}

View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/03 15:13:59 by vvobis #+# #+# */
/* Updated: 2024/04/09 20:55:24 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memchr(void *s, int c, size_t n)
{
unsigned char *p;
size_t i;
i = 0;
p = (unsigned char *)s;
while (i < n)
{
if (p[i] == (unsigned char)c)
return ((void *)&p[i]);
i++;
}
if (p[i - 1] == (unsigned char)c)
return ((void *)&p[i]);
return (NULL);
}

View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/03 15:27:15 by vvobis #+# #+# */
/* Updated: 2024/04/09 19:21:02 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_memcmp(void *s1, void const *s2, size_t n)
{
unsigned char *p1;
unsigned char *p2;
unsigned int i;
p1 = (unsigned char *)s1;
p2 = (unsigned char *)s2;
i = 0;
while (i < n)
{
if (p1[i] != p2[i])
return (p1[i] - p2[i]);
i++;
}
return (0);
}

View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 17:13:18 by vvobis #+# #+# */
/* Updated: 2024/04/09 19:21:29 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memcpy(void *dest, void const *src, size_t n)
{
char *d;
char *s;
if (n == 0 || (dest == NULL && src == NULL))
return (dest);
if (dest == NULL || src == NULL)
{
*(char *)dest = 1;
*(char *)src = 1;
}
d = (char *) dest;
s = (char *) src;
while (n--)
*d++ = *s++;
return (dest);
}

View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memmove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 17:42:28 by vvobis #+# #+# */
/* Updated: 2024/04/09 19:21:54 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memmove(void *dest, void const *src, size_t n)
{
unsigned char *d;
unsigned char *s;
if (!dest && !src)
return (dest);
d = (unsigned char *)dest;
s = (unsigned char *)src;
if (s < d)
while (n--)
d[n] = s[n];
else
ft_memcpy(dest, (void *)src, n);
return (dest);
}

View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 15:57:27 by vvobis #+# #+# */
/* Updated: 2024/04/03 11:18:10 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memset(void *s, int c, size_t n)
{
char *str;
str = (char *)s;
while (n--)
str[n] = c;
return (s);
}

View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/07 17:34:30 by vvobis #+# #+# */
/* Updated: 2024/04/07 17:53:33 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putchar_fd(char c, int fd)
{
write(fd, &c, 1);
}

View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putendl_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/07 17:57:33 by vvobis #+# #+# */
/* Updated: 2024/04/07 18:00:49 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putendl_fd(char *str, int fd)
{
if (!str)
return ;
ft_putstr_fd(str, fd);
ft_putchar_fd(0x0a, fd);
}

View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/07 18:02:14 by vvobis #+# #+# */
/* Updated: 2024/04/11 11:23:10 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putnbr_fd(int n, int fd)
{
if (n < 0)
ft_putchar_fd(0x2d, fd);
if (n <= -10)
ft_putnbr_fd(n / -10, fd);
if (n >= 10)
ft_putnbr_fd(n / 10, fd);
if (n >= 0)
ft_putchar_fd(n % 10 + 0x30, fd);
if (n < 0)
ft_putchar_fd(-(n % -10) + 0x30, fd);
}

View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/07 17:48:41 by vvobis #+# #+# */
/* Updated: 2024/04/07 17:52:16 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putstr_fd(char *str, int fd)
{
if (!str)
return ;
while (*str)
ft_putchar_fd(*str++, fd);
}

View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_read.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/18 12:49:30 by vvobis #+# #+# */
/* Updated: 2024/12/10 21:18:38 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_read(int fd, char *character, unsigned int size_to_read)
{
int bytes_read;
bytes_read = read(fd, character, size_to_read);
if (bytes_read == -1)
{
ft_fprintf(STDERR_FILENO, "Read failed\n");
}
return (bytes_read);
}

View File

@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/05 20:14:20 by vvobis #+# #+# */
/* Updated: 2024/04/09 23:55:49 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int count(char const *s, char const c)
{
int n;
n = 0;
if (*s != c && *s)
n = 1;
while (*s)
{
if (*s == c && *(s + 1) != c && *(s + 1))
n++;
s++;
}
return (n);
}
static int count_sub(char const *s, char const c)
{
int i;
i = 0;
while (*s != c && *s)
{
i++;
s++;
}
return (i);
}
static char **free_all(char **back)
{
char **tmp;
tmp = back;
while (*back)
{
free(*back);
back++;
}
free(tmp);
return (NULL);
}
char **ft_split(char const *s, char const c)
{
char **tmp;
char **back;
if (!s)
return (NULL);
tmp = (char **)ft_calloc(sizeof(*tmp), count(s, c) + 1);
if (!tmp)
return (NULL);
back = tmp;
while (*s && tmp)
{
while (*s == c && *s)
s++;
if (*s)
{
*tmp = ft_substr(s, 0, count_sub(s, c));
if (!*tmp)
return (free_all(back));
}
tmp++;
while (*s != c && *s)
s++;
}
return (back);
}

View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/03 12:20:50 by vvobis #+# #+# */
/* Updated: 2024/04/09 20:54:14 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strchr(char const *s, int c)
{
int i;
i = 0;
while (s[i])
{
if (s[i] == (char)c)
return ((char *)&s[i]);
i++;
}
if ((char)c == 0)
return ((char *)&s[i]);
return (NULL);
}

View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/04 18:44:05 by vvobis #+# #+# */
/* Updated: 2024/04/12 10:44:23 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strdup(char const *s)
{
char *tmp;
int i;
i = 0;
tmp = ft_calloc(ft_strlen(s) + 1, sizeof(*tmp));
if (!tmp)
return (NULL);
while (s[i])
{
tmp[i] = s[i];
i++;
}
tmp[i] = 0;
return (tmp);
}

View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_striteri.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/07 17:24:08 by vvobis #+# #+# */
/* Updated: 2024/04/09 19:27:07 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_striteri(char const *s, void (*f)(unsigned int, char *))
{
unsigned int i;
if (!s || !f)
return ;
i = 0;
while (s[i])
{
f(i, (char *)&s[i]);
i++;
}
}

View File

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/04 19:08:03 by vvobis #+# #+# */
/* Updated: 2024/04/12 10:07:26 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strjoin(char const *s1, char const *s2)
{
char *tmp;
unsigned int i;
unsigned int j;
if (!s1 || !s2)
return (NULL);
tmp = ft_calloc(ft_strlen(s1) + ft_strlen(s2) + 1, sizeof(*tmp));
if (!tmp)
return (NULL);
i = 0;
while (s1[i])
{
tmp[i] = s1[i];
i++;
}
j = 0;
while (s2[j])
{
tmp[i] = s2[j];
i++;
j++;
}
tmp[i] = 0;
return (tmp);
}

View File

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/04 16:10:59 by vvobis #+# #+# */
/* Updated: 2024/04/10 16:41:14 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlcat(char *dest, char const *src, size_t size)
{
size_t dlen;
size_t slen;
if (size == 0)
return (ft_strlen(src));
dlen = ft_strlen(dest);
slen = ft_strlen(src);
if (size <= dlen)
return (slen + size);
if (dlen + slen < size)
{
ft_memcpy(&dest[dlen], src, slen);
dest[dlen + slen] = 0;
}
else
{
ft_memcpy(&dest[dlen], src, size - dlen);
dest[size - 1] = 0;
}
return (dlen + slen);
}

View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 19:03:18 by vvobis #+# #+# */
/* Updated: 2024/04/09 19:28:44 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlcpy(char *dest, char const *src, size_t size)
{
size_t i;
i = 0;
if (size != 0)
{
while (src[i] != '\0' && i < size - 1)
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
}
while (src[i] != '\0')
i++;
return (i);
}

View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 16:01:03 by vvobis #+# #+# */
/* Updated: 2024/04/12 10:40:59 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlen(char const *str)
{
size_t i;
i = 0;
while (*str++)
{
i++;
if (i == SIZE_MAX)
break ;
}
return (i);
}

View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/07 17:01:35 by vvobis #+# #+# */
/* Updated: 2024/04/12 10:13:57 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
char *tmp;
unsigned int i;
if (!s || !f)
return (NULL);
tmp = ft_calloc(ft_strlen(s) + 1, sizeof(*tmp));
if (!tmp)
return (NULL);
i = 0;
while (s[i])
{
tmp[i] = f(i, s[i]);
i++;
}
tmp[i] = 0;
return (tmp);
}

View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/03 13:33:22 by vvobis #+# #+# */
/* Updated: 2024/04/12 10:29:59 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_strncmp(char const *s1, char const *s2, size_t n)
{
size_t i;
if (n == 0)
return (0);
i = 0;
while ((s1[i] || s2[i]) && i < n)
{
if (s1[i] < s2[i])
return ((unsigned char)s1[i] - (unsigned char)s2[i]);
else if (s1[i] > s2[i])
return ((unsigned char)s1[i] - (unsigned char)s2[i]);
i++;
}
return (0);
}

View File

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/03 15:36:41 by vvobis #+# #+# */
/* Updated: 2024/04/10 16:42:02 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strnstr(char const *s1, char const *s2, size_t n)
{
size_t i;
size_t j;
if (!*s2)
return ((char *)s1);
i = 0;
while (i < n && s1[i])
{
j = 0;
if (s1[i + j] == s2[j])
{
while (s1[i + j] == s2[j] && s1[i + j] && i + j < n)
j++;
if (!s2[j])
return ((char *)&s1[i]);
}
i++;
}
return (NULL);
}

View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strrchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/03 12:20:50 by vvobis #+# #+# */
/* Updated: 2024/04/09 20:54:22 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strrchr(char const *s, int c)
{
char *n;
int i;
i = 0;
n = NULL;
while (s[i] != 0)
{
if (s[i] == (char)c)
n = (char *)&s[i];
i++;
}
if ((char)c == 0)
return ((char *)&s[i]);
if (n)
return (n);
return (NULL);
}

View File

@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/04 19:30:35 by vvobis #+# #+# */
/* Updated: 2024/04/15 13:07:55 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int is_set(char c, char const *s)
{
while (*s)
if (c == *s++)
return (1);
return (0);
}
static int find_end(char const *s1, char const *set)
{
int i;
i = 0;
while (*s1)
s1++;
while (is_set(*--s1, set))
i++;
return (i);
}
char *ft_strtrim(char const *s1, char const *set)
{
if (!s1)
return (NULL);
if (!set)
return ((char *)s1);
while (is_set(*s1, set) && *s1)
s1++;
if ((int)ft_strlen(s1) > find_end(s1, set))
return (ft_substr(s1, 0, ft_strlen(s1) - find_end(s1, set)));
else
return (ft_calloc(1, 1));
}

View File

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/04 18:58:25 by vvobis #+# #+# */
/* Updated: 2024/04/09 21:50:44 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *tmp;
unsigned int i;
i = 0;
if (!s || start >= ft_strlen(s) || len <= 0)
{
tmp = malloc(1);
if (!tmp)
return (NULL);
tmp[i] = 0;
return (tmp);
}
if (len + start > ft_strlen(s))
len = ft_strlen(s) - start;
tmp = malloc(sizeof(*tmp) * len + 1);
if (!tmp)
return (NULL);
while (i < len && s[i])
{
tmp[i] = s[i + start];
i++;
}
tmp[i] = 0;
return (tmp);
}

View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_tolower.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/03 12:01:54 by vvobis #+# #+# */
/* Updated: 2024/04/03 12:15:18 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_tolower(int c)
{
if (c >= 65 && c <= 90)
c += 32;
return (c);
}

View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_toupper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/03 12:18:44 by vvobis #+# #+# */
/* Updated: 2024/04/03 12:19:05 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_toupper(int c)
{
if (c >= 97 && c <= 122)
c -= 32;
return (c);
}

92
src/ctools/libft/libft.h Normal file
View File

@ -0,0 +1,92 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bszilas <bszilas@student.42vienna.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 23:43:36 by vvobis #+# #+# */
/* Updated: 2024/12/10 21:12:01 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFT_H
# define LIBFT_H
# include "../gnl/get_next_line.h"
# include "./printf/ft_printf.h"
# include <stdlib.h>
# include <unistd.h>
# include <stddef.h>
# include <stdint.h>
# include <limits.h>
typedef struct s_list
{
void *content;
struct s_list *next;
} t_list;
int ft_isalpha(int c);
int ft_isdigit(int c);
int ft_isalnum(int c);
int ft_isprint(int c);
int ft_isascii(int c);
int ft_isspace(int c);
/*Memory Management*/
int ft_memcmp(void *s1, void const *s2, size_t n);
void *ft_memset(void *s, int c, size_t n);
void ft_bzero(void *s, size_t n);
void *ft_memcpy(void *dest, void const *src, size_t n);
void *ft_memmove(void *dest, void const *src, size_t n);
void *ft_memchr(void *s, int c, size_t n);
void *ft_calloc(size_t n, size_t s);
/*Info Conversion*/
int ft_atoi(char const *s);
double ft_atod(char *s);
char *ft_itoa(int n);
char **ft_split(char const *s, char c);
void ft_free(void *ptr_ptr);
long ft_atol(const char *nptr);
/*String Manip*/
int ft_toupper(int c);
int ft_tolower(int c);
int ft_strncmp(char const *s1, char const *s2, size_t n);
char *ft_strnstr(char const *s1, char const *s2, size_t n);
char *ft_strchr(char const *s, int c);
char *ft_strrchr(char const *s, int c);
char *ft_strdup(char const *s);
char *ft_substr(char const *s, unsigned int start, size_t len);
char *ft_strjoin(char const *s1, char const *s2);
char *ft_strtrim(char const *s1, char const *set);
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
void ft_striteri(char const *s, void (*f)(unsigned int, char *));
size_t ft_strlcpy(char *dest, char const *src, size_t size);
size_t ft_strlcat(char *dest, char const *src, size_t size);
size_t ft_strlen(char const *str);
/*List manip*/
int ft_lstsize(t_list *lst);
void ft_lstdelone(t_list *lst, void (*del)(void*));
void ft_lstclear(t_list **lst, void (*del)(void*));
void ft_lstiter(t_list *lst, void (*f)(void *));
void ft_lstadd_front(t_list **lst, t_list *node_new);
void ft_lstadd_back(t_list **lst, t_list *node_new);
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
t_list *ft_lstnew(void *content);
t_list *ft_lstlast(t_list *lst);
/*Output*/
void ft_putchar_fd(char c, int fd);
void ft_putstr_fd(char *str, int fd);
void ft_putendl_fd(char *str, int fd);
void ft_putnbr_fd(int n, int fd);
/* Get Next Line */
char *get_next_line(int fd);
#endif

View File

@ -0,0 +1,50 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: victor </var/spool/mail/victor> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/09/14 09:21:46 by victor #+# #+# #
# Updated: 2024/11/11 11:52:05 by marvin ### ########.fr #
# #
# **************************************************************************** #
LIBDIR := lib
NAME := $(LIBDIR)/libftprintf.a
CC := gcc
CFLAGS = -Wall -Werror -Wextra -g3
SRC := ft_fprintf.c ft_printf.c ft_putascii.c ft_puthex.c ft_putptr.c ft_strlen.c ft_putfloat.c
OBJ := $(SRC:.c=.o)
ifdef RELEASE
CFLAGS -= -g3
endif
MKDIR = mkdir
ifeq ($(OS), Windows_NT)
RM = del /Q
else
RM = rm -rf
MKDIR += -p
endif
all: $(NAME)
$(NAME): $(OBJ)
ar rsc $@ $(OBJ)
$(OBJ): $(SRC) $(LIBDIR)
$(CC) $(CFLAGS) -c $(SRC)
$(LIBDIR):
$(MKDIR) $@
clean:
$(RM) $(OBJ)
fclean: clean
$(RM) $(LIBDIR)
re: fclean all

View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_fprintf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/17 12:04:30 by vvobis #+# #+# */
/* Updated: 2024/09/17 12:09:29 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "./ft_printf.h"
int ft_fprintf(int fd, const char *format, ...)
{
va_list args;
int count;
if (!format || fd < 0)
return (-1);
va_start(args, format);
count = 0;
while (1)
{
while (*format != '%' && *format)
ft_putchar(*format++, &count, fd);
if (!*format)
break ;
else
format++;
if (!*format || !handle_arg(args, *format, &count, fd))
return (-1);
format++;
}
va_end(args);
return (count);
}

View File

@ -0,0 +1,63 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/18 17:50:35 by vvobis #+# #+# */
/* Updated: 2024/09/17 14:11:57 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int handle_arg(va_list args, char format, int *count, int fd)
{
if (format == 'd' || format == 'i')
ft_putnbr(va_arg(args, int), count, fd);
else if (format == 'u')
ft_putnbr(va_arg(args, unsigned int), count, fd);
else if (format == 's')
ft_putstr(va_arg(args, char *), count, fd);
else if (format == 'X')
ft_puthex_upper(va_arg(args, unsigned int), count, fd);
else if (format == 'x')
ft_puthex_lower(va_arg(args, unsigned int), count, fd);
else if (format == 'p')
ft_putptr(va_arg(args, void *), count, fd);
else if (format == 'c')
ft_putchar(va_arg(args, int), count, fd);
else if (format == '%')
ft_putchar('%', count, fd);
else if (format == 'f')
ft_putnbrf(va_arg(args, double), count, 5, fd);
else
return (0);
return (1);
}
int ft_printf(const char *format, ...)
{
va_list args;
int count;
if (!format)
return (-1);
va_start(args, format);
count = 0;
while (1)
{
while (*format != '%' && *format)
ft_putchar(*format++, &count, STDOUT_FILENO);
if (!*format)
break ;
else
format++;
if (!*format || !handle_arg(args, *format, &count, STDOUT_FILENO))
return (-1);
format++;
}
va_end(args);
return (count);
}

View File

@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/18 12:50:35 by vvobis #+# #+# */
/* Updated: 2024/11/11 11:42:50 by marvin ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_PRINTF_H
# define FT_PRINTF_H
# include <stdarg.h>
# include <stdlib.h>
# include <stdio.h>
# ifdef _WIN32
# include <io.h>
# undef STDIN_FILENO
# define STDIN_FILENO _fileno(stdin)
# undef STDOUT_FILENO
# define STDOUT_FILENO _fileno(stdout)
# define write _write
# else
# include <unistd.h>
# endif
int ft_fprintf(int fd, const char *format, ...);
int ft_printf(const char *format, ...);
int handle_arg(va_list args, char format, int *count, int fd);
void ft_puthex_lower(unsigned long nbr, int *count, int fd);
void ft_puthex_upper(unsigned long nbr, int *count, int fd);
void ft_putchar(int c, int *count, int fd);
void ft_putnbr(long n, int *count, int fd);
void ft_putnbrf(double f, int *count, int precision, int fd);
void ft_putstr(const char *str, int *count, int fd);
void ft_putptr(void *ptr, int *count, int fd);
size_t ft_strlen(const char *s);
#endif

View File

@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putascii.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/18 17:54:53 by vvobis #+# #+# */
/* Updated: 2024/09/17 16:35:10 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_putchar(int c, int *count, int fd)
{
write(fd, &c, 1);
*count += 1;
}
void ft_putnbr(long n, int *count, int fd)
{
if (n < 0)
ft_putchar(0x2d, count, fd);
if (n <= -10)
ft_putnbr(n / -10, count, fd);
if (n >= 10)
ft_putnbr(n / 10, count, fd);
if (n >= 0)
ft_putchar(n % 10 + 0x30, count, fd);
if (n < 0)
ft_putchar(-(n % -10) + 0x30, count, fd);
}
void ft_putstr(const char *str, int *count, int fd)
{
if (!str)
{
*count += ft_fprintf(fd, "(null)");
return ;
}
*count += ft_fprintf(fd, str);
}

View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putfloat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/17 11:50:45 by vvobis #+# #+# */
/* Updated: 2024/09/17 12:07:47 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_putnbrf(double f, int *count, int precision, int fd)
{
int number;
double fraction;
unsigned int mult;
mult = 1;
while (precision-- > 0)
{
mult *= 10;
}
number = (int)f;
fraction = f - (double)number;
if (fraction < 0)
fraction = -fraction;
ft_putnbr(number, count, fd);
ft_putchar('.', count, fd);
number = fraction * mult;
ft_putnbr(number, count, fd);
}

View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_puthex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/18 17:52:38 by vvobis #+# #+# */
/* Updated: 2024/09/17 12:09:29 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_puthex_upper(unsigned long nbr, int *count, int fd)
{
char *base_str;
base_str = "0123456789ABCDEF";
if (nbr >= 16)
ft_puthex_upper(nbr / 16, count, fd);
ft_putchar(base_str[(nbr % 16)], count, fd);
}
void ft_puthex_lower(unsigned long nbr, int *count, int fd)
{
char *base_str;
base_str = "0123456789abcdef";
if (nbr >= 16)
ft_puthex_lower(nbr / 16, count, fd);
ft_putchar(base_str[(nbr % 16)], count, fd);
}

View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putptr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/18 17:53:53 by vvobis #+# #+# */
/* Updated: 2024/11/10 20:42:58 by marvin ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_putptr(void *ptr, int *count, int fd)
{
void **to_print;
if (!ptr)
{
*count += ft_fprintf(fd, "(nil)");
return ;
}
to_print = &ptr;
*count += ft_printf("0x");
ft_puthex_lower((unsigned long long)*to_print, count, fd);
}

View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 16:01:03 by vvobis #+# #+# */
/* Updated: 2024/09/14 09:24:02 by victor ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
#include "limits.h"
size_t ft_strlen(char const *str)
{
size_t i;
i = 0;
while (*str++)
{
i++;
if (i == ULONG_MAX)
break ;
}
return (i);
}

View File

@ -0,0 +1,204 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* allocator.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: victor </var/spool/mail/victor> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/05 17:04:18 by victor #+# #+# */
/* Updated: 2025/04/06 16:30:24 by victor ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ALLOCATOR_H
# define ALLOCATOR_H
# include "inc/arena.h"
typedef struct s_alloc {
void *mem;
size_t size;
} t_alloc;
typedef t_arena t_vector ;
typedef struct s_allocator {
void *mem;
t_vector allocs;
size_t cap;
t_vector free; // this will be used as Array of t_alloc
} t_allocator;
//global allocator that will be used by this lib
static t_allocator *allocator_get(void) {
static t_allocator a = { 0 };
return &a;
}
void allocator_init(size_t base_mem);
void *alloc(uint64_t size);
void *zalloc(uint32_t size);
void dealloc(void *ptr);
void allocator_shutdown(void);
#ifdef ALLOCATOR_IMPLEMENTATION
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include "allocator.h"
#include <stddef.h>
void allocator_init(size_t base_mem) {
t_allocator *allocator = allocator_get();
allocator->free = arena_create_growing(sizeof(t_alloc) * 1000);
allocator->mem = calloc(base_mem, sizeof((char)0));
allocator->allocs = arena_create_growing(sizeof(t_alloc) * 1000);
allocator->cap = base_mem;
}
void vector_push(t_vector *vec, void *mem, size_t memsize) {
assert(memsize < (vec->cap - vec->used));
void *ptr = arena_alloc(vec, memsize);
assert(ptr);
memmove(ptr, mem, memsize);
}
void vector_pop(t_vector *vec, size_t memsize) {
assert(memsize <= vec->used);
memset(&((uint8_t *)vec->mem)[vec->used - memsize], 0, memsize);
}
// offset is used to know where to dereference the memory to find the value to be sorted by
void vector_insert_sorted(t_vector *vec, void *mem, uint32_t memsize, uint32_t offset) {
uint32_t low_pos = 0;
uint32_t high_pos = vec->count - (vec->count > 0);
int32_t position = 0;
t_alloc *cursor = &((t_alloc *)vec->mem)[high_pos];
uint32_t new_alloc_size = *((uint8_t *)mem + offset);
if (new_alloc_size > cursor->size || vec->count == 0) {
vector_push(vec, mem, memsize);
} else {
while (low_pos < high_pos) {
position = low_pos + (high_pos - low_pos) / 2;
cursor = &((t_alloc *)vec->mem)[position];
assert((uint8_t *)cursor - (uint8_t *)vec->mem >= 0);
assert((uint8_t *)vec->mem + vec->used - (uint8_t *)cursor >= 0);
if (cursor->size < new_alloc_size) {
low_pos = position + 1;
} else if (cursor->size > new_alloc_size) {
high_pos = position - 1;
} else {
break ;
}
}
void *ptr = arena_alloc(vec, memsize);
memmove(ptr, &cursor, sizeof(*cursor) * (vec->count - low_pos));
memmove(&cursor, mem, memsize);
}
}
void vector_remove(t_vector *vec, uint32_t index, size_t memsize) {
assert(index <= vec->count);
void *item = (void *)&((uint8_t *)vec->mem)[index * memsize];
memmove(item, (uint8_t *)item + memsize, (vec->count - index) * memsize);
vec->count--;
vec->used -= memsize;
}
void *alloc(uint64_t size) {
void *ptr = NULL;
t_allocator *allocator = allocator_get();
if (size > 0) {
t_alloc *tail = &((t_alloc *)allocator->free.mem)[allocator->free.count - (allocator->free.count > 0)];
if (allocator->free.count > 0 && tail->size >= size) {
assert(tail->mem);
uint32_t position = allocator->free.count / 2;
t_alloc *candidate = NULL;
uint32_t range = size / 20;
uint32_t last_pos = position;
while (1) {
t_alloc *cursor = &((t_alloc *)allocator->free.mem)[position];
if (cursor->size > size + range) {
if (cursor->size < candidate->size) {
candidate = cursor;
}
position <<= 1;
} else if (cursor->size < size) {
position += position >> 1;
} else if ((cursor->size >= size && cursor->size <= size + range) || position == last_pos) {
ptr = cursor->mem;
t_alloc new = { ptr, size };
t_alloc old = { (uint8_t *)ptr + size, cursor->size - size };
vector_insert_sorted(&allocator->allocs, &new, sizeof(new), offsetof(t_alloc, size));
vector_insert_sorted(&allocator->allocs, &old, sizeof(old), offsetof(t_alloc, size));
vector_insert_sorted(&allocator->free, &old, sizeof(old), offsetof(t_alloc, size));
vector_remove(&allocator->free, position, sizeof(*cursor));
break ;
}
}
} else {
t_alloc *tail = &((t_alloc *)allocator->allocs.mem)[allocator->allocs.count - (allocator->allocs.count > 0)];
if (tail->mem == NULL) {
ptr = allocator->mem;
} else {
ptr = (uint8_t *)tail->mem + tail->size;
}
t_alloc new = (t_alloc){ ptr, size };
vector_push(&allocator->allocs, &new, sizeof(new));
}
}
return ptr;
}
void *zalloc(uint32_t size) {
uint8_t *ptr = NULL;
ptr = alloc(size);
for (uint i = 0; i < size; ++i) {
ptr[i] = 0;
}
return ptr;
}
void dealloc(void *ptr) {
t_allocator *allocator = allocator_get();
for (uint32_t i = 0; i < allocator->allocs.count; ++i) {
if (((t_alloc *)allocator->allocs.mem)[i].mem == ptr) {
t_alloc move = ((t_alloc *)allocator->allocs.mem)[i];
memset(move.mem, 'X', move.size);
vector_remove(&allocator->allocs, i, sizeof(move));
vector_insert_sorted(&allocator->free, &move, sizeof(move), offsetof(t_alloc, size));
break ;
}
}
}
void allocator_print(void) {
t_allocator *allocator = allocator_get();
printf( "---- Allocator ----\n"
"capacity: %lu\n", allocator->cap);
printf("currently used mem: \n");
arena_print(&allocator->allocs);
printf("currently freed mem: \n");
arena_print(&allocator->free);
printf("alloc mem start: %p\n", allocator->mem);
print_memory(allocator->mem, 0, allocator->cap);
}
void allocator_shutdown() {
t_allocator *allocator = allocator_get();
arena_destroy(&allocator->free);
arena_destroy(&allocator->allocs);
free(allocator->mem);
memset(allocator, 0, sizeof(*allocator));
}
#endif // ALLOCATOR_IMPLEMENTATION
#endif // !ALLOCATOR_H

View File

@ -0,0 +1,152 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* arena.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: victor </var/spool/mail/victor> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/05 12:45:17 by victor #+# #+# */
/* Updated: 2025/04/06 16:05:54 by victor ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ARENA_H
# define ARENA_H
# include <stdlib.h>
# include <stdbool.h>
# include <assert.h>
# include <string.h>
# include <stdio.h>
# include <stdint.h>
# include <ctype.h>
# define BYTE_WIDTH 8
# define DISPLAY_WIDTH sizeof((void *)0) * 2
typedef struct s_arena {
void *mem;
size_t cap;
size_t used;
bool grow;
uint32_t count;
} t_arena;
t_arena arena_create(size_t size);
t_arena arena_create_growing(size_t size);
void *arena_alloc(t_arena *arena, size_t size);
void arena_destroy(t_arena *arena);
void arena_clear(t_arena *arena);
void arena_reset(t_arena *arena);
void arena_grow(t_arena *arena, size_t size);
void arena_print(t_arena *arena);
void print_memory(void *mem, size_t memstart, size_t memend);
#define ARENA_IMPLEMENTATION
# ifdef ARENA_IMPLEMENTATION
t_arena arena_create_growing(size_t size) {
t_arena arena = {0};
arena.cap = size;
arena.used = 0;
arena.mem = calloc(1, size);
arena.grow = true;
if (!arena.mem) {
return (t_arena){0};
}
return (arena);
}
t_arena arena_create(size_t size) {
t_arena arena = {0};
arena.cap = size;
arena.used = 0;
arena.mem = calloc(1, size);
arena.grow = false;
if (!arena.mem) {
return (t_arena){0};
}
return (arena);
}
void arena_grow(t_arena *arena, size_t size) {
void *new_mem;
uint new_size;
new_size = arena->cap * (size / arena->cap + 1);
new_mem = realloc(arena->mem, new_size);
arena->mem = new_mem;
assert(new_mem != NULL);
arena->cap = new_size;
// size = 58, cap = 8
}
void *arena_alloc(t_arena *arena, size_t size) {
void *ptr = NULL;
if (arena->used + size > arena->cap) {
if (arena->grow == true) {
arena_grow(arena, size);
} else {
assert(false && "Arena overflow detected");
}
}
ptr = (uint8_t *)arena->mem + arena->used;
arena->used += size;
arena->count++;
return ptr;
}
void arena_clear(t_arena *arena) {
arena->used = 0;
arena->count = 0;
}
void arena_reset(t_arena *arena) {
arena_clear(arena);
memset(arena->mem, 0, arena->cap);
}
void arena_destroy(t_arena *arena) {
free(arena->mem);
memset(arena, 0, sizeof(*arena));
}
void print_memory(void *mem, size_t memstart, size_t memend) {
printf("---- Memory Section Start ----\n");
for (uint32_t i = memstart; i < memend; i += DISPLAY_WIDTH) {
printf("%p:", &((uint8_t *)mem)[i]);
for (uint32_t j = 0; j < DISPLAY_WIDTH / 2; ++j) {
uint16_t val = ((uint16_t *)&(((uint8_t *)mem)[i]))[j];
printf(" %04x", val);
}
printf(" ");
printf("|");
for (uint32_t j = 0; j < DISPLAY_WIDTH; ++j) {
uint8_t value = ((uint8_t *)mem)[i + j];
if (isalnum(value)) {
printf("%c", value);
}
else{
printf(".");
}
}
printf("|");
printf("\n");
}
printf("---- Memory Section End ----\n");
}
void arena_print(t_arena *arena) {
print_memory(arena->mem, 0, arena->cap);
}
# endif // !ARENA_IMPLEMENTATION
#endif // !ARENA_H

View File

@ -0,0 +1,144 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* arena.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: victor </var/spool/mail/victor> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/05 12:45:17 by victor #+# #+# */
/* Updated: 2025/04/05 17:07:03 by victor ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ARENA_H
# define ARENA_H
# include <stdlib.h>
# include <stdbool.h>
# include <assert.h>
# include <string.h>
# include <stdio.h>
# include <stdint.h>
# include <ctype.h>
# define BYTE_WIDTH 8
# define DISPLAY_WIDTH sizeof((void *)0) * 2
typedef struct s_alloc {
void *ptr;
size_t size;
} t_alloc;
typedef struct s_arena {
void *mem;
size_t cap;
size_t used;
bool grow;
} t_arena;
t_arena arena_create(size_t size);
t_arena arena_create_growing(size_t size);
void *arena_alloc(t_arena *arena, size_t size);
void arena_destroy(t_arena *arena);
void arena_clear(t_arena *arena);
void arena_reset(t_arena *arena);
void arena_grow(t_arena *arena, size_t size);
void arena_print(t_arena *arena);
# ifdef ARENA_IMPLEMENTATION
t_arena arena_create_growing(size_t size) {
t_arena arena;
arena.cap = size;
arena.used = 0;
arena.mem = calloc(1, size);
arena.grow = true;
if (!arena.mem) {
return (t_arena){0};
}
return (arena);
}
t_arena arena_create(size_t size) {
t_arena arena = {0};
arena.cap = size;
arena.used = 0;
arena.mem = calloc(1, size);
arena.grow = false;
if (!arena.mem) {
return (t_arena){0};
}
return (arena);
}
void arena_grow(t_arena *arena, size_t size) {
void *new_mem;
uint new_size;
new_size = arena->cap * (size / arena->cap + 1);
new_mem = realloc(arena->mem, new_size);
arena->mem = new_mem;
assert(new_mem != NULL);
arena->cap = new_size;
// size = 58, cap = 8
}
void *arena_alloc(t_arena *arena, size_t size) {
void *ptr = NULL;
if (arena->used + size > arena->cap) {
if (arena->grow == true) {
arena_grow(arena, size);
} else {
assert(false && "Arena overflow detected");
}
}
ptr = arena->mem + arena->used;
arena->used += size;
return ptr;
}
void arena_clear(t_arena *arena) {
arena->used = 0;
}
void arena_reset(t_arena *arena) {
arena_clear(arena);
memset(arena->mem, 0, arena->cap);
}
void arena_destroy(t_arena *arena) {
free(arena->mem);
memset(arena, 0, sizeof(*arena));
}
void arena_print(t_arena *arena) {
printf("---- Arena Start ----\n");
for (uint i = 0; i < arena->cap; i += DISPLAY_WIDTH) {
printf("%p\t", arena->mem + i);
for (uint j = 0; j < DISPLAY_WIDTH; ++j) {
printf("%02x ", *(char *)(arena->mem + i + j));
}
printf("\t");
printf("|");
for (uint j = 0; j < DISPLAY_WIDTH; ++j) {
uint8_t value = *(char *)(arena->mem + i + j);
if (isalnum(value)) {
printf("%c", value);
} else {
printf(".");
}
}
printf("|");
printf("\n");
}
printf("---- Arena End ----\n");
}
# endif // !ARENA_IMPLEMENTATION
#endif // !ARENA_H

View File

@ -0,0 +1,57 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: vvobis <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/11/10 15:02:31 by vvobis #+# #+# #
# Updated: 2024/12/10 21:23:05 by vvobis ### ########.fr #
# #
# **************************************************************************** #
CC := gcc
CFLAGS = -Werror -Wextra -Wall
SRC := memory.c list.c ft_free.c
OBJDIR := obj
OBJ := $(SRC:%.c=$(OBJDIR)/%.o)
LIBDIR := lib
TARGET := $(LIBDIR)/memory.a
ifdef RELEASE
CFLAGS -= -g3
endif
MKDIR = mkdir
ifeq ($(OS), Windows_NT)
RM = del /S /Q
else
RM = rm -rf
MKDIR += -p
endif
all: $(TARGET)
$(TARGET): $(OBJ) | $(LIBDIR)
ar rcs $@ $(OBJ)
$(OBJDIR)/%.o: %.c | $(OBJDIR)
$(CC) $(CFLAGS) -c $< -o $@
$(LIBDIR): $(OBJDIR)
$(MKDIR) $(LIBDIR)
$(OBJDIR):
$(MKDIR) $(OBJDIR)
clean:
$(RM) $(OBJDIR)
fclean: clean
$(RM) $(LIBDIR)
re: fclean all

View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_free.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/10 12:16:39 by vvobis #+# #+# */
/* Updated: 2024/12/10 21:23:56 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "memory.h"
void ft_free(void *ptr_ptr)
{
void **ptr;
ptr = ptr_ptr;
free(*ptr);
*ptr = NULL;
}

View File

@ -0,0 +1,96 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: anarama <anarama@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/27 11:44:46 by vvobis #+# #+# */
/* Updated: 2024/11/10 15:09:54 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "list.h"
#include "memory.h"
t_clean *lst_node_new(void *content, void (*del)(void *))
{
t_clean *new;
if (!content || !del)
return (NULL);
new = malloc(sizeof(*new));
if (!new)
return (NULL);
new->content = content;
new->clean = del;
new->next = NULL;
return (new);
}
void lst_node_del(t_clean **lst)
{
(*lst)->clean((*lst)->content);
ft_free(&*lst);
}
void lst_node_del_clean(t_clean **lst, void *mem)
{
t_clean *tmp;
t_clean *head;
if (!lst || !*lst)
return ;
head = *lst;
tmp = *lst;
while ((*lst) && (*lst)->next->content != mem)
(*lst) = (*lst)->next;
if (!*lst)
return ;
tmp = *lst;
if ((*lst)->next && (*lst)->next->next)
{
tmp = (*lst)->next->next;
lst_node_del(&(*lst)->next);
(*lst)->next = tmp;
}
else if ((*lst)->next)
lst_node_del(&(*lst)->next);
else
lst_node_del(lst);
*lst = head;
}
void lst_list_clean(t_clean **head)
{
t_clean *tmp;
while (*head)
{
tmp = (*head)->next;
lst_node_del(head);
*head = tmp;
}
free(*head);
}
int lst_add_back(t_clean **node, t_clean *new)
{
t_clean *tmp;
if (!new)
{
perror("malloc");
return (0);
}
if (*node)
{
tmp = *node;
while (tmp->next)
tmp = tmp->next;
tmp->next = new;
}
else
*node = new;
return (1);
}

View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/10 14:48:59 by vvobis #+# #+# */
/* Updated: 2024/11/10 15:08:38 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIST_C
# define LIST_C
# include <stdlib.h>
# include <stdio.h>
typedef struct s_clean
{
void *content;
void (*clean)(void *del);
struct s_clean *next;
} t_clean;
/* List */
t_clean *lst_node_new(void *content, void (*del)(void *));
void lst_node_del(t_clean **lst);
void lst_node_del_clean(t_clean **lst, void *mem);
void lst_list_clean(t_clean **head);
int lst_add_back(t_clean **node, t_clean *node_new);
#endif

View File

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lst_memory.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/10 14:47:33 by vvobis #+# #+# */
/* Updated: 2024/11/11 11:53:06 by marvin ### ########.fr */
/* */
/* ************************************************************************** */
# include "list.h"
# include "memory.h"
# include <stdlib.h>
# include <stdio.h>
void lst_memory(void *mem, void (*del)(void *c), int mode)
{
static t_clean *list;
t_clean *new;
if (mode == FAIL)
return (lst_list_clean(&list), exit(EXIT_FAILURE));
else if (mode == END)
return (lst_list_clean(&list));
else if (mode == FREE)
return (lst_node_del_clean(&list, mem));
else if (!mem && mode != AT_EXIT)
return (lst_list_clean(&list), perror("malloc"), exit(EXIT_FAILURE));
new = lst_node_new(mem, del);
if (!new)
return (del(mem), lst_list_clean(&list), \
perror(""), exit(EXIT_FAILURE));
lst_add_back(&list, new);
}

View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* memory.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/10 14:46:26 by vvobis #+# #+# */
/* Updated: 2024/12/10 21:22:01 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MEMORY_H
# define MEMORY_H
# include <stdlib.h>
# include "../libft/libft.h"
enum e_alloc
{
ADD,
FAIL,
END,
FREE,
AT_EXIT,
};
void lst_memory(void *mem, void (*del)(void *c), int mode);
#endif

View File

@ -0,0 +1,50 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: victor </var/spool/mail/victor> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/09/14 09:21:46 by victor #+# #+# #
# Updated: 2024/11/11 11:52:05 by marvin ### ########.fr #
# #
# **************************************************************************** #
LIBDIR := lib
NAME := $(LIBDIR)/libftprintf.a
CC := gcc
CFLAGS = -Wall -Werror -Wextra -g3
SRC := ft_fprintf.c ft_printf.c ft_putascii.c ft_puthex.c ft_putptr.c ft_strlen.c ft_putfloat.c
OBJ := $(SRC:.c=.o)
ifdef RELEASE
CFLAGS -= -g3
endif
MKDIR = mkdir
ifeq ($(OS), Windows_NT)
RM = del /Q
else
RM = rm -rf
MKDIR += -p
endif
all: $(NAME)
$(NAME): $(OBJ)
ar rsc $@ $(OBJ)
$(OBJ): $(SRC) $(LIBDIR)
$(CC) $(CFLAGS) -c $(SRC)
$(LIBDIR):
$(MKDIR) $@
clean:
$(RM) $(OBJ)
fclean: clean
$(RM) $(LIBDIR)
re: fclean all

View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_fprintf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/17 12:04:30 by vvobis #+# #+# */
/* Updated: 2024/09/17 12:09:29 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "./ft_printf.h"
int ft_fprintf(int fd, const char *format, ...)
{
va_list args;
int count;
if (!format || fd < 0)
return (-1);
va_start(args, format);
count = 0;
while (1)
{
while (*format != '%' && *format)
ft_putchar(*format++, &count, fd);
if (!*format)
break ;
else
format++;
if (!*format || !handle_arg(args, *format, &count, fd))
return (-1);
format++;
}
va_end(args);
return (count);
}

View File

@ -0,0 +1,63 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/18 17:50:35 by vvobis #+# #+# */
/* Updated: 2024/09/17 14:11:57 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int handle_arg(va_list args, char format, int *count, int fd)
{
if (format == 'd' || format == 'i')
ft_putnbr(va_arg(args, int), count, fd);
else if (format == 'u')
ft_putnbr(va_arg(args, unsigned int), count, fd);
else if (format == 's')
ft_putstr(va_arg(args, char *), count, fd);
else if (format == 'X')
ft_puthex_upper(va_arg(args, unsigned int), count, fd);
else if (format == 'x')
ft_puthex_lower(va_arg(args, unsigned int), count, fd);
else if (format == 'p')
ft_putptr(va_arg(args, void *), count, fd);
else if (format == 'c')
ft_putchar(va_arg(args, int), count, fd);
else if (format == '%')
ft_putchar('%', count, fd);
else if (format == 'f')
ft_putnbrf(va_arg(args, double), count, 5, fd);
else
return (0);
return (1);
}
int ft_printf(const char *format, ...)
{
va_list args;
int count;
if (!format)
return (-1);
va_start(args, format);
count = 0;
while (1)
{
while (*format != '%' && *format)
ft_putchar(*format++, &count, STDOUT_FILENO);
if (!*format)
break ;
else
format++;
if (!*format || !handle_arg(args, *format, &count, STDOUT_FILENO))
return (-1);
format++;
}
va_end(args);
return (count);
}

View File

@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/18 12:50:35 by vvobis #+# #+# */
/* Updated: 2024/11/11 11:42:50 by marvin ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_PRINTF_H
# define FT_PRINTF_H
# include <stdarg.h>
# include <stdlib.h>
# include <stdio.h>
# ifdef _WIN32
# include <io.h>
# undef STDIN_FILENO
# define STDIN_FILENO _fileno(stdin)
# undef STDOUT_FILENO
# define STDOUT_FILENO _fileno(stdout)
# define write _write
# else
# include <unistd.h>
# endif
int ft_fprintf(int fd, const char *format, ...);
int ft_printf(const char *format, ...);
int handle_arg(va_list args, char format, int *count, int fd);
void ft_puthex_lower(unsigned long nbr, int *count, int fd);
void ft_puthex_upper(unsigned long nbr, int *count, int fd);
void ft_putchar(int c, int *count, int fd);
void ft_putnbr(long n, int *count, int fd);
void ft_putnbrf(double f, int *count, int precision, int fd);
void ft_putstr(const char *str, int *count, int fd);
void ft_putptr(void *ptr, int *count, int fd);
size_t ft_strlen(const char *s);
#endif

View File

@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putascii.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/18 17:54:53 by vvobis #+# #+# */
/* Updated: 2024/09/17 16:35:10 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_putchar(int c, int *count, int fd)
{
write(fd, &c, 1);
*count += 1;
}
void ft_putnbr(long n, int *count, int fd)
{
if (n < 0)
ft_putchar(0x2d, count, fd);
if (n <= -10)
ft_putnbr(n / -10, count, fd);
if (n >= 10)
ft_putnbr(n / 10, count, fd);
if (n >= 0)
ft_putchar(n % 10 + 0x30, count, fd);
if (n < 0)
ft_putchar(-(n % -10) + 0x30, count, fd);
}
void ft_putstr(const char *str, int *count, int fd)
{
if (!str)
{
*count += ft_fprintf(fd, "(null)");
return ;
}
*count += ft_fprintf(fd, str);
}

View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putfloat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/17 11:50:45 by vvobis #+# #+# */
/* Updated: 2024/09/17 12:07:47 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_putnbrf(double f, int *count, int precision, int fd)
{
int number;
double fraction;
unsigned int mult;
mult = 1;
while (precision-- > 0)
{
mult *= 10;
}
number = (int)f;
fraction = f - (double)number;
if (fraction < 0)
fraction = -fraction;
ft_putnbr(number, count, fd);
ft_putchar('.', count, fd);
number = fraction * mult;
ft_putnbr(number, count, fd);
}

View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_puthex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/18 17:52:38 by vvobis #+# #+# */
/* Updated: 2024/09/17 12:09:29 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_puthex_upper(unsigned long nbr, int *count, int fd)
{
char *base_str;
base_str = "0123456789ABCDEF";
if (nbr >= 16)
ft_puthex_upper(nbr / 16, count, fd);
ft_putchar(base_str[(nbr % 16)], count, fd);
}
void ft_puthex_lower(unsigned long nbr, int *count, int fd)
{
char *base_str;
base_str = "0123456789abcdef";
if (nbr >= 16)
ft_puthex_lower(nbr / 16, count, fd);
ft_putchar(base_str[(nbr % 16)], count, fd);
}

View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putptr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/18 17:53:53 by vvobis #+# #+# */
/* Updated: 2024/11/10 20:42:58 by marvin ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_putptr(void *ptr, int *count, int fd)
{
void **to_print;
if (!ptr)
{
*count += ft_fprintf(fd, "(nil)");
return ;
}
to_print = &ptr;
*count += ft_printf("0x");
ft_puthex_lower((unsigned long long)*to_print, count, fd);
}

View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/02 16:01:03 by vvobis #+# #+# */
/* Updated: 2024/09/14 09:24:02 by victor ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
#include "limits.h"
size_t ft_strlen(char const *str)
{
size_t i;
i = 0;
while (*str++)
{
i++;
if (i == ULONG_MAX)
break ;
}
return (i);
}

View File

@ -0,0 +1,86 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: anarama <anarama@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/07/05 12:24:47 by victor #+# #+# #
# Updated: 2024/12/10 21:20:03 by vvobis ### ########.fr #
# #
# **************************************************************************** #
# COMPILER AND FLAGS
CC := cc
CFLAGS = -Werror -Wall -Wextra -g3
RM = rm
ifdef release
CFLAGS -= -g3
endif
MKDIR = mkdir
ifeq ($(OS), Windows_NT)
RM = del /S /Q
else
RM = rm -rf
MKDIR += -p
endif
# DIRECTORIES
SRCDIR := src
OBJDIR := obj
# SOURCES AND OBJECTS
SRC := prompt_input.c prompt_string_management.c \
prompt_utils.c tab_completion.c \
escape_sequences.c arrowkeys.c \
prompt_print.c tab_get_word.c \
non_blocking_mode.c prompt_handle_chars.c \
utils.c terminal.c
OBJ := $(SRC:%.c=$(OBJDIR)/%.o)
SRCS := $(addprefix $(SRCDIR)/, $(SRC))
# TARGETS
MEMORY := ../memory/lib/memory.a
LIBFT := ../libft/lib/libft.a
LIBS := $(MEMORY) $(LIBFT)
LIBDIR := lib
NAME := $(LIBDIR)/prompt.a
all: $(NAME)
test: $(NAME)
$(CC) $(CFLAGS) main.c $(NAME) $(LIBS)
$(NAME): $(OBJ) prompt.h | $(LIBS)
ar rcs $@ $(OBJ) $(LIBS)
$(OBJDIR)/%.o: $(SRCDIR)/%.c | $(OBJDIR)
$(CC) $(CFLAGS) -c $< -o $@
$(OBJDIR): $(LIBDIR)
$(MKDIR) $@
$(LIBDIR):
$(MKDIR) $@
$(LIBS):
make -C ../memory
make -C ../libft
clean:
make fclean -C ../memory
make fclean -C ../libft
$(RM) $(OBJDIR)
fclean: clean
$(RM) $(LIBDIR)
re: fclean all

View File

@ -0,0 +1,141 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* prompt.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/10 14:40:41 by vvobis #+# #+# */
/* Updated: 2024/12/10 21:19:14 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PROMPT_H
# define PROMPT_H
# include <stdlib.h>
# include <sys/types.h>
# include <stdint.h>
# include <stdio.h>
# include "../memory/memory.h"
# include <errno.h>
# include "../printf/ft_printf.h"
# include "../libft/libft.h"
# include <fcntl.h>
# include <stdbool.h>
# ifdef _WIN32
# include <windows.h>
# include <io.h>
# define NL "\r\n"
# undef STDOUT_FILENO
# undef STDIN_FILENO
# define STDIN_FILENO _fileno(stdin)
# define STDOUT_FILENO _fileno(stdout)
# else
# include <unistd.h>
# include <termios.h>
# define NL "\n"
# endif
# define EOT 4
# define ESC 27
# define DEL 127
# define SCREEN_DISBLE_WRAPPING "\033[?7l"
# define SCREEN_ENABLE_WRAPPING "\033[?7h"
# define SCREEN_CLEAR_TO_EOL "\033[K"
# define SCREEN_CLEAR "\033[2J"
# define SCREEN_CLEAR_TO_EOF "\033[J"
# define PROMPT_COMMAND_STACK_SIZE 16
# define PROMPT_INPUT_BUFFER_SIZE 4096
# define CURSOR_MOVE_HOME "\033[H"
# define GREEN "\033[0;32m"
# define RESET "\033[0m"
# define RED "\033[0;31m"
# define CURSOR_MOVE_UP "\033[A"
# define CURSOR_MOVE_DOWN "\033[B"
# define CURSOR_MOVE_RIGHT "\033[C"
# define CURSOR_MOVE_LEFT "\033[D"
# define CURSOR_POSITION_GET "\033[6n"
# define CURSOR_POSITION_SET "\033[%d;%dH"
# define CURSOR_POSITION_SAVE "\033[s"
# define CURSOR_POSITION_RESTORE "\033[u"
# define BUFFER_CAPACITY 64
typedef struct s_history_buffer
{
uint32_t write;
uint32_t read;
uint32_t buffer_capacity;
char *buffer[BUFFER_CAPACITY];
} t_history_buffer;
typedef struct s_prompt
{
bool exists;
char *prompt;
t_history_buffer history;
uint32_t cursor_position[2];
void (*prompt_display_func)(char *);
uint32_t prompt_length;
char *command;
} t_prompt;
/* The Main function, all other ones shouldnt be used outside this module */
/* Returns string of input that has to be freed. Internally, this stores a static prompt struct */
/* that stores the current state and history of the input buffer */
char *prompt_get(const char *prompt);
void prompt_destroy(void *prompt);
int64_t ft_read(int fd, char *character, uint32_t size_to_read);
t_prompt prompt_create_internal(const char *prompt);
char *prompt_get_input_internal(t_prompt *prompt, uint32_t prompt_initial_size, const char *delimiter);
uint32_t prompt_display_string_set_internal(t_prompt *prompt, const char *prompt_string);
void prompt_print_custom_string_internal(char *string);
/* Cursor Manipulation */
void cursor_position_get(uint32_t cursor_position[2]);
void cursor_position_save(void);
void cursor_position_restore(void);
void cursor_position_set(uint32_t row, uint32_t column);
/* Prompt Buffer Management */
bool prompt_handle_escape_sequence_internal(t_prompt *prompt, char buffer[], char **input, uint32_t cursor_position[2]);
bool prompt_handle_new_character_to_input_internal(char **input, char character, uint32_t *cursor_position, uint32_t prompt_length);
uint8_t prompt_handle_single_char_input_internal(char **input, char buffer[], uint32_t cursor_position[2], bool *do_refresh);
bool prompt_handle_multiple_character_internal(char **input, char buffer[], uint32_t cursor_position[2], uint32_t prompt_length);
void prompt_handle_rapid_input_internal(char buffer[], uint32_t cursor_position[2], char **input, uint32_t cursor_position_base);
void prompt_handle_backspace_internal( char *input, uint32_t *cursor_position_current, uint32_t input_length_current);
void prompt_refresh_line_internal(char *input, uint32_t cursor_position_base, uint32_t cursor_position[2]);
char *buffer_size_manage(char **input, uint32_t old_size, uint32_t size_to_add, uint32_t scalar);
void prompt_src_string_insert_internal(char *string_to_insert, char **current_input, char *position_to_insert, uint32_t current_word_length);
/* Non Blocking mode */
void blocking_mode_toggle(int fd, int flag);
/* Tab Completion */
void prompt_handle_tab_internal(char **input, t_prompt *prompt);
void prompt_handle_tab_no_match_internal(const char *input_path, uint32_t cursor_position_current[2], t_prompt *prompt);
void prompt_handle_tab_yes_match_internal(t_prompt *prompt, const char *next_word_match, char **input, uint32_t current_word_length);
char *prompt_determine_word_internal(char *input, char **input_path, uint32_t cursor_position_current);
void prompt_get_next_word_match_internal(char **input, t_prompt *prompt, char *input_path, bool *is_directory);
/* Termios */
void terminal_raw_mode_enable_internal(void);
void terminal_raw_mode_disable_internal(void);
#endif

View File

@ -0,0 +1,75 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* arrowkeys.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: anarama <anarama@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/08 11:02:39 by victor #+# #+# */
/* Updated: 2024/11/11 15:25:23 by victor ### ########.fr */
/* */
/* ************************************************************************** */
#include "../prompt.h"
static void prompt_handle_arrow_key_up_internal( t_history_buffer *history,
char **input,
uint32_t cursor_position_current[2])
{
if (cursor_position_current[0] > 0)
ft_putstr_fd(CURSOR_MOVE_UP, 1);
if (history->read == 0)
history->read = history->buffer_capacity - 1;
else
history->read -= 1;
if (history->buffer[history->read] == 0)
{
history->read = (history->read + 1) % history->buffer_capacity;
return ;
}
if (history->buffer[history->write] == 0)
history->buffer[history->write] = *input;
*input = history->buffer[history->read];
cursor_position_current[1] = ft_strlen(*input);
}
static void prompt_handle_arrow_key_down_internal(t_history_buffer *history, uint32_t cursor_position[2], char **input)
{
if (history->read == history->write)
return ;
history->read = (history->read + 1) % history->buffer_capacity;
*input = history->buffer[history->read];
cursor_position[1] = ft_strlen(*input);
}
static void prompt_handle_arrow_key_right_internal(uint32_t *cursor_position_current, uint32_t prompt_length_current, uint32_t prompt_string_length)
{
if (cursor_position_current[1] < prompt_length_current)
cursor_position_current[1]++;
cursor_position_set(cursor_position_current[0], cursor_position_current[1] + prompt_string_length);
}
static void prompt_handle_arrow_key_left_internal( uint32_t *cursor_position_current, uint32_t prompt_string_length)
{
if (cursor_position_current[1] > 0)
cursor_position_current[1]--;
cursor_position_set(cursor_position_current[0], cursor_position_current[1] + prompt_string_length);
}
bool prompt_handle_escape_sequence_internal(t_prompt *prompt, char buffer[], char **input, uint32_t cursor_position_current[2])
{
uint32_t prompt_length_current;
prompt_length_current = ft_strlen(*input);
if (buffer[0] == 91 && buffer[1] == 65)
return (prompt_handle_arrow_key_up_internal(&prompt->history, \
input, cursor_position_current), 1);
else if (buffer[0] == 91 && buffer[1] == 66)
return (prompt_handle_arrow_key_down_internal(&prompt->history, \
cursor_position_current, input), 1);
else if (buffer[0] == 91 && buffer[1] == 67)
prompt_handle_arrow_key_right_internal(cursor_position_current, prompt_length_current, prompt->prompt_length);
else if (buffer[0] == 91 && buffer[1] == 68)
prompt_handle_arrow_key_left_internal(cursor_position_current, prompt->prompt_length);
return (0);
}

View File

@ -0,0 +1,63 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* escape_sequences.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: anarama <anarama@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/07 19:29:29 by vvobis #+# #+# */
/* Updated: 2024/11/11 15:17:42 by victor ### ########.fr */
/* */
/* ************************************************************************** */
#include "../prompt.h"
#ifdef _WIN32
void cursor_position_get(uint32_t cursor_position[2])
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO info;
if (!GetConsoleScreenBufferInfo(hConsole, &info))
{
ft_printf("Failed to get Cursor Positon\n");
lst_memory(NULL, NULL, FAIL);
}
else
{
cursor_position[0] = info.dwCursorPosition.Y;
cursor_position[1] = info.dwCursorPosition.X;
}
}
#else
void cursor_position_get(uint32_t cursor_position[2])
{
char cursor_position_str[32];
char *cursor_position_str_ptr;
char *cursor_position_str_ptr2;
int bytes_read;
ft_bzero(&cursor_position_str, 32);
write(1, CURSOR_POSITION_GET, ft_strlen(CURSOR_POSITION_GET));
bytes_read = read(1, cursor_position_str, 32);
if (bytes_read == -1)
return (perror("read"));
cursor_position_str_ptr = cursor_position_str;
cursor_position_str_ptr2 = ft_strchr(cursor_position_str, ';');
if (!cursor_position_str_ptr2)
return ;
*cursor_position_str_ptr2 = 0;
while (!(*cursor_position_str_ptr >= '0'
&& *cursor_position_str_ptr <= '9'))
cursor_position_str_ptr++;
cursor_position[0] = ft_atoi(cursor_position_str_ptr);
cursor_position_str_ptr = cursor_position_str_ptr2 + 1;
cursor_position_str_ptr2 = ft_strchr(cursor_position_str_ptr, 'R');
*cursor_position_str_ptr2 = 0;
cursor_position[1] = ft_atoi(cursor_position_str_ptr);
}
#endif

View File

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/10 16:15:22 by vvobis #+# #+# */
/* Updated: 2024/11/11 11:11:34 by marvin ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
#ifdef _WIN32
# include <io.h>
# include <stdio.h>
void ft_putstr_fd(char *buf, int fd)
{
_write(fd, buf, strlen(buf));
fflush(stdout);
}
#else
#include <unistd.h>
void ft_putstr_fd(char *buf, int fd)
{
write(fd, buf, strlen(buf));
}
#endif

View File

@ -0,0 +1,96 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_read.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/10 18:06:24 by vvobis #+# #+# */
/* Updated: 2024/11/11 12:22:24 by marvin ### ########.fr */
/* */
/* ************************************************************************** */
#include "../prompt.h"
#ifdef _WIN32
# include <io.h>
# include <fcntl.h>
int64_t ft_read(int fd, char *character, uint32_t size_to_read)
{
int64_t bytes_read;
char *tmp;
if (fd == 0)
fd = _fileno(stdin);
else if (fd == 1)
fd = _fileno(stdout);
bytes_read = _read(fd, character, size_to_read);
if (bytes_read == -1)
{
if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
return (0);
perror("read");
lst_memory(NULL, NULL, FAIL);
}
if ((tmp = ft_strrchr(character, '\r')))
{
*tmp = '\n';
}
return (bytes_read);
}
// int64_t ft_read(int fd, char *character, uint32_t size_to_read)
// {
// int64_t bytes_read;
// char *tmp;
//
// if (fd == 0)
// fd = _fileno(stdin);
// else if (fd == 1)
// fd = _fileno(stdout);
// bytes_read = _read(fd, character, size_to_read);
// if (bytes_read > 0)
// {
// character[bytes_read] = '\0';
// tmp = ft_strrchr(character, '\r');
// if (tmp && (tmp - character < bytes_read - 1) && *(tmp + 1) == '\n')
// {
// ft_memmove(tmp, tmp + 1, bytes_read - (tmp - character) - 1);
// bytes_read--;
// }
// }
// if (bytes_read == -1)
// {
// if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
// return (0);
// perror("read");
// lst_memory(NULL, NULL, FAIL);
// }
// return (bytes_read);
// }
//
# else
# include <unistd.h>
int64_t ft_read(int fd, char *character, uint32_t size_to_read)
{
int64_t bytes_read;
bytes_read = read(fd, character, size_to_read);
if (bytes_read == -1)
{
if (errno == EINTR \
|| errno == EAGAIN \
|| errno == EWOULDBLOCK)
return (0);
perror("read");
lst_memory(NULL, NULL, FAIL);
}
return (bytes_read);
}
#endif

View File

@ -0,0 +1,76 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* non_blocking_mode.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/02 17:14:46 by vvobis #+# #+# */
/* Updated: 2024/11/10 21:21:20 by marvin ### ########.fr */
/* */
/* ************************************************************************** */
#include "../prompt.h"
#ifdef _WIN32
#include <windows.h>
void blocking_mode_toggle(int fd, int enable)
{
HANDLE hStd;
DWORD mode;
if (fd == 0)
{
hStd = GetStdHandle(STD_INPUT_HANDLE);
}
else if (fd == 1)
{
hStd = GetStdHandle(STD_OUTPUT_HANDLE);
}
else
{
return ;
}
if (hStd == INVALID_HANDLE_VALUE)
{
fprintf(stderr, "Error getting standard input handle." NL);
exit(1);
}
if (!GetConsoleMode(hStd, &mode))
{
fprintf(stderr, "Error getting console mode." NL);
exit(1);
}
if (enable)
{
mode &= ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
}
else
{
mode |= (ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
}
if (!SetConsoleMode(hStd, mode))
{
fprintf(stderr, "Error setting console mode." NL);
exit(1);
}
}
#else
# include <sys/ioctl.h>
void blocking_mode_toggle(int fd, int flag)
{
if (ioctl(fd, FIONBIO, &flag) == -1)
{
perror("ioctl");
exit(1);
}
}
#endif

View File

@ -0,0 +1,105 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* prompt_handle_chars.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/02 17:11:28 by vvobis #+# #+# */
/* Updated: 2024/11/11 13:13:36 by marvin ### ########.fr */
/* */
/* ************************************************************************** */
#include "../prompt.h"
uint8_t prompt_handle_single_char_input_internal( char **input, char buffer[], uint32_t cursor_position_current[2], bool *do_refresh)
{
uint32_t input_length_current;
uint32_t new_buffer_length;
input_length_current = ft_strlen(*input);
new_buffer_length = ft_strlen(buffer);
if (new_buffer_length == 1)
{
if (ft_isprint(buffer[0]) || buffer[0] == '\n')
return (*do_refresh = prompt_handle_new_character_to_input_internal(input, buffer[0],
cursor_position_current, input_length_current), 1);
else if (buffer[0] == EOT \
&& (input_length_current == 0 \
|| (*input)[input_length_current - 1] == '\n'))
return (ft_putstr_fd(NL, STDIN_FILENO), \
terminal_raw_mode_disable_internal(), \
1);
}
else
{
*do_refresh = prompt_handle_multiple_character_internal(input, buffer, \
cursor_position_current, input_length_current);
return (1);
}
return (0);
}
bool prompt_handle_new_character_to_input_internal(char **input, char character, uint32_t *cursor_position_current, uint32_t prompt_length_current)
{
bool do_refresh;
do_refresh = false;
*input = buffer_size_manage(input, prompt_length_current, prompt_length_current + 1, PROMPT_INPUT_BUFFER_SIZE);
if (cursor_position_current[1] < prompt_length_current)
{
ft_memmove(&(*input)[cursor_position_current[1] + 1], &(*input)[cursor_position_current[1]], \
prompt_length_current - cursor_position_current[1]);
}
(*input)[cursor_position_current[1]] = character;
cursor_position_current[1]++;
do_refresh = true;
return (do_refresh);
}
bool prompt_handle_multiple_character_internal(char **input, char buffer[], uint32_t cursor_position_current[2], uint32_t prompt_length_current)
{
bool do_refresh;
uint32_t buffer_length;
do_refresh = false;
buffer_length = ft_strlen(buffer);
buffer_size_manage(input, prompt_length_current, prompt_length_current + buffer_length + 1, PROMPT_INPUT_BUFFER_SIZE);
if (cursor_position_current[1] < prompt_length_current - 1)
{
ft_memmove(&(*input)[cursor_position_current[1] + buffer_length], &(*input)[cursor_position_current[1]], \
prompt_length_current - cursor_position_current[1] + buffer_length);
do_refresh = true;
}
ft_memcpy(&(*input)[cursor_position_current[1]], buffer, buffer_length);
(cursor_position_current[1]) += buffer_length;
return (do_refresh);
}
void prompt_handle_backspace_internal(char *input, uint32_t *cursor_position_current, uint32_t input_length_current)
{
if (cursor_position_current[1] <= 0)
return ;
cursor_position_current[1]--;
ft_memmove(&input[cursor_position_current[1]], &input[cursor_position_current[1] + 1], input_length_current - cursor_position_current[1]);
ft_putstr_fd(CURSOR_MOVE_LEFT, STDOUT_FILENO);
}
void prompt_handle_rapid_input_internal(char buffer[], uint32_t cursor_position[2], char **input, uint32_t cursor_position_base)
{
int32_t bytes_read;
bool do_refresh;
blocking_mode_toggle(STDIN_FILENO, 1);
bytes_read = 1;
do_refresh = true;
while (bytes_read > 0)
{
prompt_handle_multiple_character_internal(input, buffer, cursor_position, ft_strlen(*input));
ft_bzero(buffer, 100);
bytes_read = ft_read(STDIN_FILENO, buffer, 99);
}
blocking_mode_toggle(STDIN_FILENO, 1);
if (do_refresh)
prompt_refresh_line_internal(*input, cursor_position_base, cursor_position);
}

View File

@ -0,0 +1,127 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* prompt_input.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: anarama <anarama@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/07 19:40:20 by vvobis #+# #+# */
/* Updated: 2024/11/11 14:10:50 by marvin ### ########.fr */
/* */
/* ************************************************************************** */
#include "../prompt.h"
static uint8_t prompt_handle_accepted_input_internal(t_prompt *prompt, uint32_t cursor_position[2], char **input, char buffer[])
{
bool do_refresh;
do_refresh = true;
switch (buffer[0])
{
case ESC:
do_refresh = prompt_handle_escape_sequence_internal(prompt, &buffer[1], input, cursor_position);
break ;
case '\t':
prompt_handle_tab_internal(input, prompt);
break ;
case DEL:
prompt_handle_backspace_internal(*input, cursor_position, \
ft_strlen(*input));
break ;
case 3:
return (1);
break ;
case 4:
return (1);
break ;
case '\n':
prompt_handle_new_character_to_input_internal(input, buffer[0], cursor_position, ft_strlen(*input));
prompt->prompt_display_func(prompt->prompt);
cursor_position[0]++;
break ;
default:
prompt_handle_single_char_input_internal(input, buffer, cursor_position, &do_refresh);
break ;
}
if (do_refresh == true)
prompt_refresh_line_internal(*input, prompt->prompt_length, cursor_position);
return (0);
}
static bool prompt_is_delimiter(char *input, const char *delimiter)
{
char *tmp;
uint32_t delimiter_length;
if (*delimiter == '\n')
return (true);
delimiter_length = ft_strlen(delimiter);
tmp = ft_strrchr(input - (*input == '\n' && ft_strlen(input) > 0), '\n');
if (tmp)
if (tmp++ && ((*tmp == *delimiter && delimiter_length == 1) \
|| ft_strncmp(tmp, delimiter, delimiter_length) == 0))
return (*tmp = 0, true);
if ((*input == *delimiter && delimiter_length == 1) \
|| ft_strncmp(input, delimiter, delimiter_length) == 0)
return (*input = 0, true);
return (false);
}
static char *prompt_handle_input(t_prompt *prompt, char *input, uint32_t cursor_position[2], const char *delimiter)
{
char buffer[100];
int64_t bytes_read;
bytes_read = sizeof(buffer);
while (1)
{
ft_bzero(buffer, bytes_read);
bytes_read = ft_read(STDIN_FILENO, buffer, 20);
if (bytes_read > 3)
prompt_handle_rapid_input_internal(buffer, cursor_position, &input, prompt->prompt_length);
else if (bytes_read >= 1)
{
if (buffer[bytes_read - (bytes_read > 0)] == '\n')
if (prompt_is_delimiter(input, delimiter))
break ;
if (prompt_handle_accepted_input_internal(prompt, cursor_position, &input, buffer) == 1)
break ;
}
}
return (input);
}
static void prompt_handle_history(t_history_buffer *buffer, char *input)
{
buffer->buffer[buffer->write++] = input;
buffer->write %= buffer->buffer_capacity;
buffer->buffer[buffer->write] = 0;
buffer->read = buffer->write;
}
char *prompt_get_input_internal( t_prompt *prompt, uint32_t prompt_initial_size, const char *delimiter)
{
char *input;
input = ft_calloc(prompt_initial_size, sizeof(*input));
if (!input)
return (perror("malloc"), NULL);
lst_memory(input, free, ADD);
terminal_raw_mode_enable_internal();
prompt->prompt_display_func(prompt->prompt);
cursor_position_get(prompt->cursor_position);
prompt->cursor_position[1] = 0;
if (!delimiter)
delimiter = "\n";
input = prompt_handle_input(prompt, input, prompt->cursor_position, delimiter);
if (!input)
{
ft_printf("prompt_handle_input returned NULL pointer" NL);
return (NULL);
}
prompt_handle_history(&prompt->history, input);
terminal_raw_mode_disable_internal();
prompt->command = input;
return (input);
}

View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* prompt_print.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/02 16:11:25 by vvobis #+# #+# */
/* Updated: 2024/11/11 11:40:31 by marvin ### ########.fr */
/* */
/* ************************************************************************** */
#include "../prompt.h"
void prompt_print_custom_string_internal(char *string)
{
ft_putstr_fd(string, STDOUT_FILENO);
}

View File

@ -0,0 +1,73 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* prompt_string_management.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: anarama <anarama@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/09 08:16:45 by victor #+# #+# */
/* Updated: 2024/11/11 15:23:36 by victor ### ########.fr */
/* */
/* ************************************************************************** */
#include "../prompt.h"
void cursor_position_save(void)
{
ft_putstr_fd(CURSOR_POSITION_SAVE, STDOUT_FILENO);
}
void cursor_position_restore(void)
{
ft_putstr_fd(CURSOR_POSITION_RESTORE, STDOUT_FILENO);
}
#ifdef _WIN32
void cursor_position_set(uint32_t row, uint32_t column)
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsole, (COORD){.Y = row, .X = column});
}
#else
void cursor_position_set(uint32_t row, uint32_t column)
{
ft_printf(CURSOR_POSITION_SET, row, column);
}
#endif
void prompt_refresh_line_internal(char *input, uint32_t cursor_position_base, uint32_t cursor_position_current[2])
{
ft_putstr_fd(SCREEN_CLEAR_TO_EOF, STDOUT_FILENO);
cursor_position_set(cursor_position_current[0], cursor_position_base + cursor_position_current[1] - (cursor_position_current[1] > 0));
ft_putstr_fd(&input[cursor_position_current[1] - (cursor_position_current[1] > 0)], STDOUT_FILENO);
cursor_position_set(cursor_position_current[0], cursor_position_current[1] + cursor_position_base);
}
char *buffer_size_manage(char **input, uint32_t old_size, uint32_t size_to_add, uint32_t scalar)
{
char *input_free_ptr;
uint32_t size_multiplier;
uint32_t new_size;
if (scalar == 0)
return (*input);
if ((old_size + size_to_add) > (scalar * ((old_size / scalar) + 1)))
{
new_size = old_size + size_to_add;
new_size += new_size % scalar;
size_multiplier = (old_size / scalar) + 2;
input_free_ptr = *input;
*input = ft_calloc(1, new_size * size_multiplier + 1);
if (!*input)
return (perror("malloc"), lst_memory(NULL, NULL, FAIL), NULL);
ft_memcpy(*input, input_free_ptr, old_size);
lst_memory(input_free_ptr, NULL, FREE);
lst_memory(*input, free, ADD);
}
return (*input);
}

View File

@ -0,0 +1,78 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* prompt_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: anarama <anarama@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/07 19:23:40 by anarama #+# #+# */
/* Updated: 2024/11/11 15:20:32 by victor ### ########.fr */
/* */
/* ************************************************************************** */
#include "../prompt.h"
void prompt_destroy(void *prompt)
{
t_prompt *prompt_ptr;
prompt_ptr = (t_prompt *)prompt;
ft_free(&prompt_ptr);
}
#ifdef _WIN32
uint32_t prompt_display_string_set_internal(t_prompt *prompt, const char *prompt_string)
{
uint32_t prompt_string_length;
prompt_string_length = ft_strlen(prompt_string);
prompt->prompt = (char *)prompt_string;
return (prompt_string_length);
}
#else
uint32_t prompt_display_string_set_internal(t_prompt *prompt, const char *prompt_string)
{
uint32_t prompt_string_length;
prompt_string_length = ft_strlen(prompt_string) + 1;
prompt->prompt = (char *)prompt_string;
return (prompt_string_length);
}
#endif
t_prompt prompt_create_internal(char const *prompt)
{
t_prompt tmp;
tmp = (t_prompt){0};
tmp.history = (t_history_buffer){0};
tmp.history.buffer_capacity = BUFFER_CAPACITY;
tmp.exists = true;
if (!prompt)
tmp.prompt = "> ";
tmp.prompt_display_func = prompt_print_custom_string_internal;
return (tmp);
}
char *prompt_get(const char *prompt)
{
char *input;
static t_prompt _prompt = {0};
if (!_prompt.exists)
_prompt = prompt_create_internal(prompt);
_prompt.prompt = NULL;
lst_memory(&_prompt, prompt_destroy, ADD);
_prompt.prompt_length = prompt_display_string_set_internal(&_prompt, prompt);
prompt_get_input_internal(&_prompt, PROMPT_INPUT_BUFFER_SIZE, "\n");
if (!_prompt.command || !*_prompt.command)
return (NULL);
ft_putstr_fd(SCREEN_CLEAR_TO_EOF, 1);
input = ft_strdup(_prompt.command);
if (!input)
return (perror("malloc"), lst_memory(NULL, NULL, FAIL), NULL);
return (input);
}

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