changed to file output
This commit is contained in:
parent
390dd10dca
commit
95d7ef4212
17
Makefile
17
Makefile
@ -75,7 +75,7 @@ MATHSRC := $(addprefix $(MATHDIR)/, $(addsuffix .s, \
|
||||
))
|
||||
|
||||
STRSRC := $(addprefix $(STRDIR)/, $(addsuffix .s, \
|
||||
strlen split strcpy substr is_num strcmp is_alpha \
|
||||
strlen split strdup substr is_num strcmp is_alpha \
|
||||
))
|
||||
|
||||
MEMSRC := $(addprefix $(MEMDIR)/, $(addsuffix .s, \
|
||||
@ -120,7 +120,7 @@ GLOBALSRC := $(addprefix $(GLOBALDIR)/, $(addsuffix .s, \
|
||||
))
|
||||
|
||||
SBSRC := $(addprefix $(SBDIR)/, $(addsuffix .s, \
|
||||
string_builder sb_append \
|
||||
string_builder sb_append sb_dump_to_file \
|
||||
))
|
||||
|
||||
# Collect all sources and objects
|
||||
@ -131,7 +131,9 @@ ALL_SRC := $(MAIN_SRC) $(MATHSRC) $(STRSRC) $(SBSRC) $(PRINTSRC) $(FILESRC) $(VA
|
||||
ALL_OBJ := $(patsubst $(SRCDIR)/%.s,$(OBJDIR)/%.o,$(ALL_SRC))
|
||||
|
||||
# Library settings
|
||||
LIBNAME := $(LIBDIR)/core.a
|
||||
LIBNAME := $(LIBDIR)/core/core.a
|
||||
LIBFTDIR := $(LIBDIR)/libft
|
||||
LIBFT := $(LIBFTDIR)/lib/libft.a
|
||||
LIB_OBJ := $(filter-out $(OBJDIR)/start.o, $(ALL_OBJ))
|
||||
|
||||
# Module-specific object files for staged compilation
|
||||
@ -206,8 +208,8 @@ build-main: $(MAIN_OBJ)
|
||||
# Stage 7: Link executable
|
||||
link-executable: $(TARGET)
|
||||
|
||||
$(TARGET): $(ALL_OBJ)
|
||||
ld -g -o $@ $(ALL_OBJ) -nostdlib -static
|
||||
$(TARGET): $(ALL_OBJ) $(LIBFT) $(LIBNAME)
|
||||
ld -g -o $@ $(ALL_OBJ) $(LIBFT) $(LIBNAME) -nostdlib -static
|
||||
|
||||
# Stage 8: Create library
|
||||
create-library: $(LIBNAME)
|
||||
@ -215,6 +217,9 @@ create-library: $(LIBNAME)
|
||||
$(LIBNAME): $(LIB_OBJ) | $(LIBDIR)
|
||||
ar rcs $@ $(LIB_OBJ)
|
||||
|
||||
$(LIBFT):
|
||||
make -C $(LIBFTDIR)
|
||||
|
||||
# Individual file compilation rule
|
||||
$(OBJDIR)/%.o: $(SRCDIR)/%.s | $(OBJDIRS)
|
||||
nasm $(AFLAGS) $< -o $@
|
||||
@ -222,6 +227,7 @@ $(OBJDIR)/%.o: $(SRCDIR)/%.s | $(OBJDIRS)
|
||||
# Utility targets
|
||||
clean:
|
||||
rm -rf $(OBJDIR)
|
||||
make clean -C $(LIBFTDIR)
|
||||
|
||||
clean-library:
|
||||
rm -f $(LIBNAME)
|
||||
@ -230,6 +236,7 @@ clean-executable:
|
||||
rm -f $(TARGET)
|
||||
|
||||
fclean: clean clean-library clean-executable
|
||||
make fclean -C $(LIBFTDIR)
|
||||
|
||||
re: fclean all
|
||||
|
||||
|
||||
18
compile
Executable file
18
compile
Executable file
@ -0,0 +1,18 @@
|
||||
#!/bin/sh
|
||||
|
||||
set -e
|
||||
|
||||
LD_ARGS="-nostdlib -static"
|
||||
NASM_FLAG="-felf64 -g"
|
||||
LIBS="./lib/core/core.a ./lib/libft/lib/libft.a"
|
||||
PROGNAME="langproc"
|
||||
|
||||
filename=$1
|
||||
comp_out=${filename%.lang}.s
|
||||
nasm_out=${comp_output%.s}.o
|
||||
|
||||
./langc ${filename}
|
||||
|
||||
nasm ${NASM_FLAG} ${comp_out} -o ${nasm_out}
|
||||
ld ${nasm_out} ${LD_ARGS} ${LIBS} -o ${PROGNAME}
|
||||
rm -rf ${comp_out} ${nasm_out}
|
||||
BIN
lib/core.a
BIN
lib/core.a
Binary file not shown.
109
lib/gnl/get_next_line.c
Normal file
109
lib/gnl/get_next_line.c
Normal 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));
|
||||
}
|
||||
38
lib/gnl/get_next_line.h
Normal file
38
lib/gnl/get_next_line.h
Normal 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
|
||||
118
lib/gnl/get_next_line_utils.c
Normal file
118
lib/gnl/get_next_line_utils.c
Normal 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);
|
||||
}
|
||||
78
lib/libft/Makefile
Normal file
78
lib/libft/Makefile
Normal file
@ -0,0 +1,78 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: vvobis <marvin@42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/09/17 14:23:19 by vvobis #+# #+# #
|
||||
# Updated: 2025/05/14 15:16:25 by vvobis ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
NAME := libft.a
|
||||
|
||||
CC := cc
|
||||
|
||||
CFLAGS := -Wall -Wextra -Werror -mabi=sysv
|
||||
|
||||
ifdef DEBUG
|
||||
CFLAGS += -g3
|
||||
endif
|
||||
|
||||
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)
|
||||
|
||||
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)
|
||||
57
lib/libft/ft_atod.c
Normal file
57
lib/libft/ft_atod.c
Normal 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);
|
||||
}
|
||||
60
lib/libft/ft_atoi.c
Normal file
60
lib/libft/ft_atoi.c
Normal 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);
|
||||
}
|
||||
19
lib/libft/ft_bzero.c
Normal file
19
lib/libft/ft_bzero.c
Normal 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;
|
||||
}
|
||||
30
lib/libft/ft_calloc.c
Normal file
30
lib/libft/ft_calloc.c
Normal 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);
|
||||
}
|
||||
22
lib/libft/ft_free.c
Normal file
22
lib/libft/ft_free.c
Normal 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;
|
||||
}
|
||||
20
lib/libft/ft_isalnum.c
Normal file
20
lib/libft/ft_isalnum.c
Normal 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);
|
||||
}
|
||||
20
lib/libft/ft_isalpha.c
Normal file
20
lib/libft/ft_isalpha.c
Normal 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);
|
||||
}
|
||||
20
lib/libft/ft_isascii.c
Normal file
20
lib/libft/ft_isascii.c
Normal 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);
|
||||
}
|
||||
18
lib/libft/ft_isdigit.c
Normal file
18
lib/libft/ft_isdigit.c
Normal 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);
|
||||
}
|
||||
18
lib/libft/ft_isprint.c
Normal file
18
lib/libft/ft_isprint.c
Normal 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);
|
||||
}
|
||||
18
lib/libft/ft_isspace.c
Normal file
18
lib/libft/ft_isspace.c
Normal 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));
|
||||
}
|
||||
75
lib/libft/ft_itoa.c
Normal file
75
lib/libft/ft_itoa.c
Normal 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));
|
||||
}
|
||||
30
lib/libft/ft_lstadd_back_bonus.c
Normal file
30
lib/libft/ft_lstadd_back_bonus.c
Normal 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;
|
||||
}
|
||||
26
lib/libft/ft_lstadd_front_bonus.c
Normal file
26
lib/libft/ft_lstadd_front_bonus.c
Normal 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;
|
||||
}
|
||||
}
|
||||
28
lib/libft/ft_lstclear_bonus.c
Normal file
28
lib/libft/ft_lstclear_bonus.c
Normal 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;
|
||||
}
|
||||
}
|
||||
21
lib/libft/ft_lstdelone_bonus.c
Normal file
21
lib/libft/ft_lstdelone_bonus.c
Normal 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);
|
||||
}
|
||||
24
lib/libft/ft_lstiter_bonus.c
Normal file
24
lib/libft/ft_lstiter_bonus.c
Normal 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;
|
||||
}
|
||||
}
|
||||
22
lib/libft/ft_lstlast_bonus.c
Normal file
22
lib/libft/ft_lstlast_bonus.c
Normal 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);
|
||||
}
|
||||
44
lib/libft/ft_lstmap_bonus.c
Normal file
44
lib/libft/ft_lstmap_bonus.c
Normal 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);
|
||||
}
|
||||
25
lib/libft/ft_lstnew_bonus.c
Normal file
25
lib/libft/ft_lstnew_bonus.c
Normal 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);
|
||||
}
|
||||
28
lib/libft/ft_lstsize_bonus.c
Normal file
28
lib/libft/ft_lstsize_bonus.c
Normal 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);
|
||||
}
|
||||
31
lib/libft/ft_memchr.c
Normal file
31
lib/libft/ft_memchr.c
Normal 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);
|
||||
}
|
||||
31
lib/libft/ft_memcmp.c
Normal file
31
lib/libft/ft_memcmp.c
Normal 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);
|
||||
}
|
||||
32
lib/libft/ft_memcpy.c
Normal file
32
lib/libft/ft_memcpy.c
Normal 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);
|
||||
}
|
||||
30
lib/libft/ft_memmove.c
Normal file
30
lib/libft/ft_memmove.c
Normal 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);
|
||||
}
|
||||
23
lib/libft/ft_memset.c
Normal file
23
lib/libft/ft_memset.c
Normal 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);
|
||||
}
|
||||
18
lib/libft/ft_putchar_fd.c
Normal file
18
lib/libft/ft_putchar_fd.c
Normal 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);
|
||||
}
|
||||
21
lib/libft/ft_putendl_fd.c
Normal file
21
lib/libft/ft_putendl_fd.c
Normal 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);
|
||||
}
|
||||
27
lib/libft/ft_putnbr_fd.c
Normal file
27
lib/libft/ft_putnbr_fd.c
Normal 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);
|
||||
}
|
||||
21
lib/libft/ft_putstr_fd.c
Normal file
21
lib/libft/ft_putstr_fd.c
Normal 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);
|
||||
}
|
||||
25
lib/libft/ft_read.c
Normal file
25
lib/libft/ft_read.c
Normal 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);
|
||||
}
|
||||
84
lib/libft/ft_split.c
Normal file
84
lib/libft/ft_split.c
Normal 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);
|
||||
}
|
||||
29
lib/libft/ft_strchr.c
Normal file
29
lib/libft/ft_strchr.c
Normal 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);
|
||||
}
|
||||
31
lib/libft/ft_strdup.c
Normal file
31
lib/libft/ft_strdup.c
Normal 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);
|
||||
}
|
||||
27
lib/libft/ft_striteri.c
Normal file
27
lib/libft/ft_striteri.c
Normal 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++;
|
||||
}
|
||||
}
|
||||
41
lib/libft/ft_strjoin.c
Normal file
41
lib/libft/ft_strjoin.c
Normal 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);
|
||||
}
|
||||
37
lib/libft/ft_strlcat.c
Normal file
37
lib/libft/ft_strlcat.c
Normal 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);
|
||||
}
|
||||
32
lib/libft/ft_strlcpy.c
Normal file
32
lib/libft/ft_strlcpy.c
Normal 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);
|
||||
}
|
||||
27
lib/libft/ft_strlen.c
Normal file
27
lib/libft/ft_strlen.c
Normal 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);
|
||||
}
|
||||
33
lib/libft/ft_strmapi.c
Normal file
33
lib/libft/ft_strmapi.c
Normal 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);
|
||||
}
|
||||
31
lib/libft/ft_strncmp.c
Normal file
31
lib/libft/ft_strncmp.c
Normal 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);
|
||||
}
|
||||
36
lib/libft/ft_strnstr.c
Normal file
36
lib/libft/ft_strnstr.c
Normal 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);
|
||||
}
|
||||
33
lib/libft/ft_strrchr.c
Normal file
33
lib/libft/ft_strrchr.c
Normal 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);
|
||||
}
|
||||
47
lib/libft/ft_strtrim.c
Normal file
47
lib/libft/ft_strtrim.c
Normal 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));
|
||||
}
|
||||
41
lib/libft/ft_substr.c
Normal file
41
lib/libft/ft_substr.c
Normal 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);
|
||||
}
|
||||
20
lib/libft/ft_tolower.c
Normal file
20
lib/libft/ft_tolower.c
Normal 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);
|
||||
}
|
||||
20
lib/libft/ft_toupper.c
Normal file
20
lib/libft/ft_toupper.c
Normal 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
lib/libft/libft.h
Normal file
92
lib/libft/libft.h
Normal 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
|
||||
50
lib/libft/printf/Makefile
Normal file
50
lib/libft/printf/Makefile
Normal 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
|
||||
38
lib/libft/printf/ft_fprintf.c
Normal file
38
lib/libft/printf/ft_fprintf.c
Normal 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);
|
||||
}
|
||||
63
lib/libft/printf/ft_printf.c
Normal file
63
lib/libft/printf/ft_printf.c
Normal 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);
|
||||
}
|
||||
52
lib/libft/printf/ft_printf.h
Normal file
52
lib/libft/printf/ft_printf.h
Normal 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
|
||||
49
lib/libft/printf/ft_putascii.c
Normal file
49
lib/libft/printf/ft_putascii.c
Normal file
@ -0,0 +1,49 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putascii.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/04/18 17:54:53 by vvobis #+# #+# */
|
||||
/* Updated: 2025/05/14 17:11:37 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);
|
||||
}
|
||||
|
||||
int putstring(char *str, int fd) {
|
||||
|
||||
return (write(fd, str, ft_strlen(str)));
|
||||
|
||||
}
|
||||
|
||||
void ft_putstr(const char *str, int *count, int fd)
|
||||
{
|
||||
if (!str)
|
||||
{
|
||||
*count += putstring("(null)", fd);
|
||||
return ;
|
||||
}
|
||||
*count += putstring((char *)str, fd);
|
||||
}
|
||||
34
lib/libft/printf/ft_putfloat.c
Normal file
34
lib/libft/printf/ft_putfloat.c
Normal 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);
|
||||
}
|
||||
33
lib/libft/printf/ft_puthex.c
Normal file
33
lib/libft/printf/ft_puthex.c
Normal 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);
|
||||
}
|
||||
27
lib/libft/printf/ft_putptr.c
Normal file
27
lib/libft/printf/ft_putptr.c
Normal file
@ -0,0 +1,27 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putptr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/04/18 17:53:53 by vvobis #+# #+# */
|
||||
/* Updated: 2025/05/14 17:13:56 by vvobis ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_printf.h"
|
||||
|
||||
void ft_putptr(void *ptr, int *count, int fd)
|
||||
{
|
||||
void **to_print;
|
||||
|
||||
if (!ptr)
|
||||
{
|
||||
ft_putstr("(nil)", count, fd);
|
||||
return ;
|
||||
}
|
||||
to_print = &ptr;
|
||||
*count += write(fd, "0x", 2);
|
||||
ft_puthex_lower((unsigned long long)*to_print, count, fd);
|
||||
}
|
||||
28
lib/libft/printf/ft_strlen.c
Normal file
28
lib/libft/printf/ft_strlen.c
Normal 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);
|
||||
}
|
||||
@ -4,7 +4,7 @@ section .text
|
||||
extern malloc
|
||||
extern putstr
|
||||
extern putnumber
|
||||
extern strcpy
|
||||
extern strdup
|
||||
extern memchr
|
||||
extern substr
|
||||
extern strlen
|
||||
@ -121,7 +121,7 @@ split: ; RAX: char ** split(RDI: char *, RSI: int)
|
||||
jmp .cleanup
|
||||
.no_match:
|
||||
push rcx
|
||||
call strcpy
|
||||
call strdup
|
||||
pop rcx
|
||||
mov [rcx], rax
|
||||
.done:
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
global strcpy
|
||||
global strdup
|
||||
|
||||
section .text
|
||||
extern malloc
|
||||
extern strlen
|
||||
|
||||
strcpy: ; rax: char *(rdi: char *)
|
||||
strdup: ; rax: char *(rdi: char *)
|
||||
push rbx
|
||||
push rcx
|
||||
call strlen
|
||||
@ -5,6 +5,8 @@ section .text
|
||||
extern memcpy
|
||||
extern malloc
|
||||
extern err_malloc
|
||||
extern ft_strlcpy
|
||||
extern ft_strlcat
|
||||
|
||||
%define SB [rbp - 16]
|
||||
%define SB_LEN dword [rbp - 4]
|
||||
@ -60,7 +62,7 @@ sb_append: ; (rdi: *sb, rsi: char*)
|
||||
mov rsi, SB
|
||||
mov rsi, [rsi + STR_DATA]
|
||||
mov edx, SB_LEN
|
||||
call memcpy
|
||||
call ft_strlcpy
|
||||
pop rax
|
||||
mov rsi, SB
|
||||
pop r9
|
||||
@ -72,10 +74,9 @@ sb_append: ; (rdi: *sb, rsi: char*)
|
||||
mov dword [r9 + STR_LEN], eax
|
||||
mov rdi, [r9 + STR_DATA]
|
||||
mov eax, dword [rbp - 4]
|
||||
lea rdi, [rdi + rax]
|
||||
mov rsi, APPENDIX
|
||||
mov edx, APP_LEN
|
||||
call memcpy
|
||||
mov edx, SB_CAP
|
||||
call ft_strlcat
|
||||
|
||||
.done:
|
||||
mov rsp, rbp
|
||||
|
||||
25
src/core/string_builder/sb_dump_to_file.s
Normal file
25
src/core/string_builder/sb_dump_to_file.s
Normal file
@ -0,0 +1,25 @@
|
||||
%include "./src/core/string_builder/sb.s"
|
||||
|
||||
%define FD dword [rbp - 4]
|
||||
|
||||
section .text
|
||||
extern write
|
||||
|
||||
global sb_dump_to_file
|
||||
sb_dump_to_file: ; (rdi: *sb, rsi: int fd)
|
||||
push rbp
|
||||
mov rbp, rsp
|
||||
sub rsp, 16
|
||||
|
||||
; load data, len and fd into regs to prepare for write syscall
|
||||
mov FD, esi
|
||||
mov rsi, [rdi + STR_DATA]
|
||||
mov rdx, [rdi + STR_LEN]
|
||||
mov edi, FD
|
||||
|
||||
; dump to file
|
||||
call write
|
||||
|
||||
mov rsp, rbp
|
||||
pop rbp
|
||||
ret
|
||||
@ -5,18 +5,24 @@ section .text
|
||||
global read
|
||||
global get_file_size
|
||||
global lseek
|
||||
global write
|
||||
|
||||
open: ; rax: int fd (rdi: char *name, rsi: int flags)
|
||||
mov rax, 2 ; sys_open
|
||||
write: ; write(RDI: int fd, RSI: char *buffer, RDX: size_t size)
|
||||
mov rax, 1
|
||||
syscall
|
||||
ret
|
||||
|
||||
open: ; rax: int fd (rdi: char *name, rsi: int flags)
|
||||
mov rax, 2 ; sys_open
|
||||
syscall
|
||||
ret
|
||||
|
||||
close:
|
||||
mov rax, 3 ; sys_close
|
||||
mov rax, 3 ; sys_close
|
||||
syscall
|
||||
ret
|
||||
|
||||
read: ; read(RDI: int fd, RSI: char *buffer, RDX: size_t size)
|
||||
read: ; read(RDI: int fd, RSI: char *buffer, RDX: size_t size)
|
||||
mov rax, 0 ; sys_read
|
||||
syscall ;
|
||||
ret
|
||||
|
||||
@ -8,10 +8,10 @@ section .data
|
||||
exit_func
|
||||
|
||||
global func_call_prologue
|
||||
func_call_prologue: db " push rdi", 0
|
||||
func_call_prologue: db " push rdi", 0x0a, 0
|
||||
|
||||
global func_call
|
||||
func_call: db " call ", 0
|
||||
func_call: db " call %s", 0x0a, 0
|
||||
|
||||
global func_call_epilogue
|
||||
func_call_epilogue: db " pop rdi", 0
|
||||
func_call_epilogue: db " pop rdi", 0x0a, 0
|
||||
|
||||
39
src/inc/c_alignment.s
Normal file
39
src/inc/c_alignment.s
Normal file
@ -0,0 +1,39 @@
|
||||
%macro c_call 1-*
|
||||
push r12
|
||||
; Save current stack pointer in a scratch register
|
||||
mov r12, rsp
|
||||
mov al, 0
|
||||
|
||||
; Align stack to 16 bytes
|
||||
and rsp, -16
|
||||
|
||||
; Set up arguments if provided
|
||||
%if %0 > 1
|
||||
mov rdi, %2
|
||||
%endif
|
||||
%if %0 > 2
|
||||
mov rsi, %3
|
||||
%endif
|
||||
%if %0 > 3
|
||||
mov rdx, %4
|
||||
%endif
|
||||
%if %0 > 4
|
||||
mov rcx, %5
|
||||
%endif
|
||||
%if %0 > 5
|
||||
mov r8, %6
|
||||
%endif
|
||||
%if %0 > 6
|
||||
mov r9, %7
|
||||
%endif
|
||||
|
||||
; Set al to 0 for variadic functions
|
||||
mov al, 0
|
||||
|
||||
; Make the call
|
||||
call %1
|
||||
|
||||
; Restore original stack pointer
|
||||
mov rsp, r12
|
||||
pop r12
|
||||
%endmacro
|
||||
@ -1,26 +1,35 @@
|
||||
%include "./src/inc/c_alignment.s"
|
||||
|
||||
section .text
|
||||
extern putstr
|
||||
extern putnumberendl
|
||||
extern fd_out
|
||||
extern ft_fprintf
|
||||
|
||||
func_stack_alloc: db " push rbp", 0xa, " mov rbp, rsp", 0xa, " sub rsp, ", 0
|
||||
func_stack_alloc: db " push rbp", 0xa, " mov rbp, rsp", 0xa, " sub rsp, %d", 0x0a, 0
|
||||
func_stack_dealloc: db " mov rsp, rbp", 0xa, " pop rbp", 0xa, 0
|
||||
ret_inst: db " ret", 0xa, 0
|
||||
|
||||
global func_prologue
|
||||
func_prologue: ; (rdi: var_count)
|
||||
; calculate var offsets
|
||||
mov rax, 8
|
||||
imul rax, rdi
|
||||
push rax
|
||||
mov rdi, func_stack_alloc
|
||||
call putstr
|
||||
pop rdi
|
||||
call putnumberendl
|
||||
|
||||
; prepare func call
|
||||
mov rdi, [fd_out]
|
||||
mov rsi, func_stack_alloc
|
||||
mov rdx, rax
|
||||
|
||||
c_call ft_fprintf
|
||||
|
||||
ret
|
||||
|
||||
global func_epilogue
|
||||
func_epilogue: ;
|
||||
mov rdi, func_stack_dealloc
|
||||
call putstr
|
||||
mov rdi, ret_inst
|
||||
call putstr
|
||||
mov rdi, [fd_out]
|
||||
mov rsi, func_stack_dealloc
|
||||
|
||||
c_call ft_fprintf
|
||||
|
||||
ret
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
%include "./src/inc/expression.s"
|
||||
%include "./src/inc/function_table.s"
|
||||
%include "./src/inc/token.s"
|
||||
%include "./src/inc/c_alignment.s"
|
||||
|
||||
%define FUNC_ERR "[FUNC_ERROR]"
|
||||
|
||||
@ -11,6 +12,8 @@ section .data
|
||||
extern func_call_prologue
|
||||
extern func_call_epilogue
|
||||
extern REG_RDI
|
||||
extern REG_RAX
|
||||
extern fd_out
|
||||
|
||||
section .text
|
||||
global lex_func_call
|
||||
@ -25,6 +28,7 @@ section .text
|
||||
extern insert_mov
|
||||
extern load_var_reg
|
||||
extern load_const_reg
|
||||
extern ft_fprintf
|
||||
|
||||
insert_func_with_const: ; (rdi: name*, rsi: arg*)
|
||||
push rbp
|
||||
@ -34,18 +38,22 @@ insert_func_with_const: ; (rdi: name*, rsi: arg*)
|
||||
mov [rbp - 8], rdi ; store name
|
||||
mov [rbp - 16], rsi ; store arg
|
||||
|
||||
mov rdi, func_call_prologue
|
||||
call putendl
|
||||
mov rdi, [fd_out]
|
||||
mov rsi, func_call_prologue
|
||||
call ft_fprintf
|
||||
|
||||
mov rdi, [rbp - 16]
|
||||
mov rsi, REG_RDI
|
||||
mov rdx, REG_RAX
|
||||
call load_const_reg
|
||||
mov rdi, func_call
|
||||
call putstr
|
||||
mov rdi, [rbp - 8]
|
||||
call putendl
|
||||
mov rdi, [fd_out]
|
||||
mov rsi, func_call
|
||||
mov rdx, [rbp - 8]
|
||||
c_call ft_fprintf
|
||||
|
||||
mov rdi, [fd_out]
|
||||
mov rdi, func_call_epilogue
|
||||
call putendl
|
||||
c_call ft_fprintf
|
||||
|
||||
mov rsp, rbp
|
||||
pop rbp
|
||||
@ -59,18 +67,21 @@ insert_func_with_var: ; (rdi: name*, rsi: arg*)
|
||||
mov [rbp - 8], rdi ; store name
|
||||
mov [rbp - 16], rsi ; store arg
|
||||
|
||||
mov rdi, func_call_prologue
|
||||
call putendl
|
||||
mov rdi, [fd_out]
|
||||
mov rsi, func_call_prologue
|
||||
call ft_fprintf
|
||||
|
||||
mov rdi, [rbp - 16]
|
||||
mov rsi, REG_RDI
|
||||
call load_var_reg
|
||||
mov rdi, func_call
|
||||
call putstr
|
||||
mov rdi, [rbp - 8]
|
||||
call putendl
|
||||
mov rdi, [fd_out]
|
||||
mov rsi, func_call
|
||||
mov rdx, [rbp - 8]
|
||||
c_call ft_fprintf
|
||||
|
||||
mov rdi, [fd_out]
|
||||
mov rdi, func_call_epilogue
|
||||
call putendl
|
||||
c_call ft_fprintf
|
||||
|
||||
mov rsp, rbp
|
||||
pop rbp
|
||||
|
||||
@ -20,12 +20,13 @@ section .text
|
||||
extern insert_var
|
||||
extern insert_mov
|
||||
extern load_var_rax
|
||||
extern load_rax_var
|
||||
extern op_const_rax
|
||||
extern op_var_rax
|
||||
extern load_reg_var
|
||||
extern op_const_reg
|
||||
extern op_var_reg
|
||||
extern xor_reg
|
||||
extern look_up_var
|
||||
|
||||
|
||||
extern REG_RAX
|
||||
|
||||
extern lex_eexpect
|
||||
@ -50,7 +51,8 @@ process_var: ; (rdi: lex*, rsi: name*, rdx: tok_op)
|
||||
|
||||
mov rdi, rax
|
||||
mov rsi, rdx
|
||||
call op_var_rax
|
||||
mov rdx, REG_RAX
|
||||
call op_var_reg
|
||||
|
||||
mov rsp, rbp
|
||||
pop rbp
|
||||
@ -85,7 +87,9 @@ process_token: ; rax: new_last_tok_type (rdi: lex*, rsi: *tok, rdx: last_to
|
||||
mov rdi, [rsi + TOK_VALUE]
|
||||
mov rsi, rdx
|
||||
|
||||
call op_const_rax
|
||||
mov rdx, REG_RAX
|
||||
|
||||
call op_const_reg
|
||||
|
||||
pop rdi
|
||||
mov rax, TOK_CONST
|
||||
@ -199,7 +203,8 @@ lex_load: ; rax: bool (rdi: lex*)
|
||||
|
||||
.done_true:
|
||||
mov rdi, [rbp - 40]
|
||||
call load_rax_var
|
||||
mov rsi, REG_RAX
|
||||
call load_reg_var
|
||||
mov rax, 1
|
||||
mov rsp, rbp
|
||||
pop rbp
|
||||
|
||||
@ -20,6 +20,7 @@ section .text
|
||||
extern func_prologue
|
||||
extern func_epilogue
|
||||
extern memset
|
||||
extern sb_new
|
||||
|
||||
extern program_prologue
|
||||
|
||||
@ -45,6 +46,10 @@ lex: ; rax: lex* (rdi: char *file_content)
|
||||
mov rdx, LEX_SIZE
|
||||
call memset
|
||||
|
||||
mov rdi, 0
|
||||
lea rsi, [rax + LEX_OUT]
|
||||
call sb_new
|
||||
|
||||
pop rax
|
||||
mov [rbp - 24], rax ; store lex on stack
|
||||
|
||||
|
||||
@ -1,35 +1,40 @@
|
||||
%include "./src/inc/c_alignment.s"
|
||||
%include "./src/inc/lexer.s"
|
||||
%include "./src/inc/token.s"
|
||||
%include "./src/inc/expression.s"
|
||||
|
||||
section .data
|
||||
section_text: db "section .text", 0xa, 0
|
||||
program_start: db "global _start", 0xa, "_start:", 0xa, 0
|
||||
extern_str: db "extern ", 0
|
||||
prologue: db "section .text", 0xa, 0
|
||||
program_entry: db "global _start", 0xa, "_start:", 0xa, 0
|
||||
extern_str: db "extern %s", 0x0a, 0
|
||||
|
||||
|
||||
section .text
|
||||
extern putendl
|
||||
extern putstr
|
||||
extern fd_out
|
||||
extern ft_fprintf
|
||||
|
||||
global program_prologue
|
||||
|
||||
print_section_text:
|
||||
mov rdi, section_text
|
||||
call putstr
|
||||
print_program_entry:
|
||||
mov rdi, [fd_out]
|
||||
mov rsi, program_entry
|
||||
c_call ft_fprintf
|
||||
ret
|
||||
|
||||
print_program_start:
|
||||
mov rdi, program_start
|
||||
call putendl
|
||||
print_section_text:
|
||||
mov rdi, [fd_out]
|
||||
mov rsi, prologue
|
||||
c_call ft_fprintf
|
||||
ret
|
||||
|
||||
declare_extern: ; (rdi: func_name*)
|
||||
push rdi
|
||||
mov rdi, extern_str
|
||||
call putstr
|
||||
pop rdi
|
||||
call putendl
|
||||
mov rdi, [fd_out]
|
||||
mov rsi, extern_str
|
||||
pop rdx
|
||||
c_call ft_fprintf
|
||||
ret
|
||||
|
||||
declare_extern_func: ;(rdi: lex*)
|
||||
@ -88,9 +93,8 @@ declare_extern_func: ;(rdi: lex*)
|
||||
|
||||
program_prologue: ;(rdi: lex*)
|
||||
push rdi
|
||||
mov rdi, section_text
|
||||
call putendl
|
||||
call print_section_text
|
||||
pop rdi
|
||||
call declare_extern_func
|
||||
call print_program_start
|
||||
call print_program_entry
|
||||
ret
|
||||
|
||||
@ -1,15 +1,22 @@
|
||||
%include "./src/inc/token.s"
|
||||
%include "./src/inc/c_alignment.s"
|
||||
|
||||
|
||||
section .data
|
||||
|
||||
INST_MOV: db " mov ", 0
|
||||
INST_ADD: db " add ", 0
|
||||
INST_SUB: db " sub ", 0
|
||||
INST_XOR: db " xor ", 0
|
||||
INST_XOR: db " xor %s, %s", 0x0a, 0
|
||||
|
||||
OPEN_STACK_VAR: db "[rbp-", 0
|
||||
CLOSE_STACK_VAR: db "]", 0
|
||||
SEP_INST: db ", ", 0
|
||||
OPERAND_PAIR_CONST_REG: db "%s, %s", 0x0a, 0
|
||||
OPERAND_PAIR_CONST: db "%s, %s", 0x0a, 0
|
||||
OPERAND_PAIR_REG_VAR: db "[rbp-%d], %s", 0x0a, 0
|
||||
OPERAND_PAIR_VAR_REG: db "%s, [rbp-%d]", 0x0a, 0
|
||||
|
||||
LOAD_REG_VAR: db " mov [rbp-%d], %s", 0x0a, 0
|
||||
LOAD_VAR_REG: db " mov %s, [rbp-%d]", 0x0a, 0
|
||||
LOAD_CONST_REG: db " mov %s, %s", 0x0a, 0
|
||||
|
||||
section .text
|
||||
extern putstr
|
||||
@ -26,28 +33,26 @@ section .text
|
||||
extern REG_RAX
|
||||
extern REG_RDI
|
||||
|
||||
global insert_xor
|
||||
insert_xor:
|
||||
mov rdi, INST_XOR
|
||||
call putstr
|
||||
ret
|
||||
extern ft_fprintf
|
||||
|
||||
global insert_mov
|
||||
insert_mov:
|
||||
mov rdi, INST_MOV
|
||||
call putstr
|
||||
ret
|
||||
extern fd_out
|
||||
|
||||
global insert_add
|
||||
insert_add:
|
||||
mov rdi, INST_ADD
|
||||
call putstr
|
||||
mov rdi, [fd_out]
|
||||
mov rsi, INST_ADD
|
||||
|
||||
c_call ft_fprintf
|
||||
|
||||
ret
|
||||
|
||||
global insert_sub
|
||||
insert_sub:
|
||||
mov rdi, INST_SUB
|
||||
call putstr
|
||||
mov rdi, [fd_out]
|
||||
mov rsi, INST_SUB
|
||||
|
||||
c_call ft_fprintf
|
||||
|
||||
ret
|
||||
|
||||
global xor_reg
|
||||
@ -55,35 +60,37 @@ xor_reg: ; rdi: char*
|
||||
push rbx
|
||||
mov rbx, rdi
|
||||
|
||||
call insert_xor
|
||||
mov rdi, [fd_out]
|
||||
mov rsi, INST_XOR
|
||||
mov rdx, rbx
|
||||
mov rcx, rbx
|
||||
|
||||
mov rdi, rbx
|
||||
call putstr
|
||||
|
||||
mov rdi, SEP_INST
|
||||
call putstr
|
||||
|
||||
mov rdi, rbx
|
||||
call putendl
|
||||
c_call ft_fprintf
|
||||
|
||||
pop rbx
|
||||
ret
|
||||
|
||||
global load_var_reg
|
||||
load_var_reg: ; (rdi: OFF_S, rsi: REG*)
|
||||
push rsi
|
||||
push rdi
|
||||
mov rdi, [fd_out]
|
||||
mov rsi, LOAD_VAR_REG
|
||||
pop rcx
|
||||
pop rdx
|
||||
|
||||
c_call ft_fprintf
|
||||
ret
|
||||
|
||||
global load_reg_var
|
||||
load_reg_var: ; (rdi: OFF_S, rsi: REG*)
|
||||
push rsi
|
||||
push rdi
|
||||
call insert_mov
|
||||
|
||||
pop rdi
|
||||
call insert_var
|
||||
|
||||
mov rdi, SEP_INST
|
||||
call putstr
|
||||
|
||||
pop rdi
|
||||
call putendl
|
||||
|
||||
mov rdi, [fd_out]
|
||||
mov rsi, LOAD_REG_VAR
|
||||
pop rdx
|
||||
pop rcx
|
||||
c_call ft_fprintf
|
||||
ret
|
||||
|
||||
global load_const_reg
|
||||
@ -91,75 +98,19 @@ load_const_reg: ; (rdi: const*, rsi: REG*)
|
||||
push rdi
|
||||
push rsi
|
||||
|
||||
call insert_mov
|
||||
mov rdi, [fd_out]
|
||||
mov rsi, LOAD_CONST_REG
|
||||
pop rdx
|
||||
pop rcx
|
||||
|
||||
pop rdi
|
||||
call putstr
|
||||
|
||||
mov rdi, SEP_INST
|
||||
call putstr
|
||||
|
||||
pop rdi
|
||||
call insert_const_endl
|
||||
c_call ft_fprintf
|
||||
|
||||
ret
|
||||
|
||||
global load_var_reg
|
||||
load_var_reg: ; (rdi: OFF_S, rsi: REG*)
|
||||
push rdi
|
||||
push rsi
|
||||
|
||||
call insert_mov
|
||||
|
||||
pop rdi
|
||||
call putstr
|
||||
|
||||
mov rdi, SEP_INST
|
||||
call putstr
|
||||
|
||||
pop rdi
|
||||
call insert_var_endl
|
||||
|
||||
ret
|
||||
|
||||
global load_rax_var
|
||||
load_rax_var: ; (rdi: OFF_S)
|
||||
push rdi
|
||||
call insert_mov
|
||||
|
||||
pop rdi
|
||||
call insert_var
|
||||
|
||||
mov rdi, SEP_INST
|
||||
call putstr
|
||||
|
||||
mov rdi, REG_RAX
|
||||
call putendl
|
||||
|
||||
ret
|
||||
|
||||
global load_var_rax
|
||||
load_var_rax: ; (rdi: OFF_S)
|
||||
push rdi
|
||||
call insert_mov
|
||||
|
||||
mov rdi, REG_RAX
|
||||
call putstr
|
||||
|
||||
mov rdi , SEP_INST
|
||||
call putstr
|
||||
|
||||
pop rdi
|
||||
call insert_var_endl
|
||||
|
||||
mov rdi, '\n'
|
||||
call putchar
|
||||
|
||||
ret
|
||||
|
||||
global op_const_rax
|
||||
op_const_rax: ; (rdi: char *, rsi: op)
|
||||
global op_const_reg
|
||||
op_const_reg: ; (rdi: char *, rsi: op, rdx: reg*)
|
||||
push rdi
|
||||
push rdx
|
||||
|
||||
cmp rsi, TOK_SUB
|
||||
je .sub
|
||||
@ -171,20 +122,20 @@ op_const_rax: ; (rdi: char *, rsi: op)
|
||||
call insert_add
|
||||
|
||||
.done:
|
||||
mov rdi, REG_RAX
|
||||
call putstr
|
||||
pop rdx
|
||||
pop rcx
|
||||
|
||||
mov rdi, SEP_INST
|
||||
call putstr
|
||||
mov rdi, [fd_out]
|
||||
mov rsi, OPERAND_PAIR_CONST_REG
|
||||
|
||||
pop rdi
|
||||
call putendl
|
||||
c_call ft_fprintf
|
||||
|
||||
ret
|
||||
|
||||
global op_var_rax
|
||||
op_var_rax: ; (rdi: OFF_S, rsi: op)
|
||||
global op_var_reg
|
||||
op_var_reg: ; (rdi: OFF_S, rsi: op, rdx: reg)
|
||||
push rdi
|
||||
push rdx
|
||||
|
||||
cmp rsi, TOK_SUB
|
||||
je .sub
|
||||
@ -196,95 +147,12 @@ op_var_rax: ; (rdi: OFF_S, rsi: op)
|
||||
call insert_add
|
||||
.done:
|
||||
|
||||
mov rdi, REG_RAX
|
||||
call putstr
|
||||
pop rdx
|
||||
pop rcx
|
||||
|
||||
mov rdi, SEP_INST
|
||||
call putstr
|
||||
mov rdi, [fd_out]
|
||||
mov rsi, OPERAND_PAIR_VAR_REG
|
||||
|
||||
pop rdi
|
||||
call insert_var_endl
|
||||
|
||||
ret
|
||||
|
||||
global sub_var_rax
|
||||
sub_var_rax: ; (rdi: OFF_S)
|
||||
push rdi
|
||||
call insert_sub
|
||||
|
||||
mov rdi, REG_RAX
|
||||
call putstr
|
||||
|
||||
mov rdi , SEP_INST
|
||||
call putstr
|
||||
|
||||
pop rdi
|
||||
call insert_var_endl
|
||||
|
||||
ret
|
||||
|
||||
global add_rax_var
|
||||
add_rax_var: ; (rdi: OFF_S)
|
||||
push rdi
|
||||
call insert_add
|
||||
|
||||
pop rdi
|
||||
call insert_var
|
||||
|
||||
mov rdi , SEP_INST
|
||||
call putstr
|
||||
|
||||
mov rdi, REG_RAX
|
||||
call putendl
|
||||
|
||||
ret
|
||||
|
||||
global sub_rax_var
|
||||
sub_rax_var: ; (rdi: OFF_S)
|
||||
push rdi
|
||||
call insert_sub
|
||||
|
||||
pop rdi
|
||||
call insert_var
|
||||
|
||||
mov rdi , SEP_INST
|
||||
call putstr
|
||||
|
||||
mov rdi, REG_RAX
|
||||
call putendl
|
||||
|
||||
ret
|
||||
|
||||
insert_const_endl: ; (rdi: const*)
|
||||
push rdi
|
||||
call putendl
|
||||
pop rdi
|
||||
|
||||
ret
|
||||
|
||||
insert_var_endl: ; (rdi: OFF_S)
|
||||
push rdi
|
||||
mov rdi, OPEN_STACK_VAR
|
||||
call putstr
|
||||
|
||||
pop rdi
|
||||
call putnumber
|
||||
|
||||
mov rdi, CLOSE_STACK_VAR
|
||||
call putendl
|
||||
|
||||
ret
|
||||
|
||||
global insert_var
|
||||
insert_var: ; (rdi: OFF_S)
|
||||
push rdi
|
||||
mov rdi, OPEN_STACK_VAR
|
||||
call putstr
|
||||
|
||||
pop rdi
|
||||
call putnumber
|
||||
|
||||
mov rdi, CLOSE_STACK_VAR
|
||||
call putstr
|
||||
c_call ft_fprintf
|
||||
|
||||
ret
|
||||
|
||||
87
src/start.s
87
src/start.s
@ -2,10 +2,14 @@
|
||||
|
||||
section .data
|
||||
usage: db "Usage: ./debug <file>.lang", 0xa, 0
|
||||
example_data: db "Do you know Ligma?"
|
||||
fd_out: dd 0
|
||||
dot_s: db ".s", 0
|
||||
|
||||
global fd_out
|
||||
|
||||
section .text
|
||||
global _start
|
||||
|
||||
extern exit
|
||||
extern err_args
|
||||
extern get_file_content
|
||||
@ -18,46 +22,69 @@ section .text
|
||||
extern vec_pop
|
||||
extern sb_new
|
||||
extern sb_append
|
||||
extern ft_fprintf
|
||||
extern ft_printf
|
||||
extern split
|
||||
extern ft_strjoin
|
||||
extern open
|
||||
|
||||
print_usage:
|
||||
mov rdi, usage
|
||||
call putstr
|
||||
|
||||
|
||||
_start:
|
||||
;push rbp
|
||||
;mov rbp, rsp
|
||||
;sub rsp, 16
|
||||
;mov rdi, usage
|
||||
;lea rsi, [rbp - 16]
|
||||
;call sb_new
|
||||
;
|
||||
;lea rdi, [rbp - 16]
|
||||
;mov rsi, example_data
|
||||
;call sb_append
|
||||
;mov al, 1
|
||||
;mov rsi, 1
|
||||
;mov rdi, qword [rbp - 16 + STR_DATA]
|
||||
;call ft_printf
|
||||
|
||||
pop rdi
|
||||
cmp rdi, 2
|
||||
jne err_args
|
||||
mov rdi, [rsp + 8] ; argv[1]
|
||||
|
||||
push rbp
|
||||
mov rbp, rsp
|
||||
|
||||
sub rsp, 16
|
||||
mov rdi, usage
|
||||
lea rsi, [rbp - 16]
|
||||
call sb_new
|
||||
|
||||
mov rdi, [rbp - 16 + STR_DATA]
|
||||
call putstr
|
||||
lea rdi, [rbp - 16]
|
||||
mov rsi, example_data
|
||||
call sb_append
|
||||
mov rdi, [rbp - 16 + STR_DATA]
|
||||
call putstr
|
||||
; store filename on stack
|
||||
mov [rbp - 16], rdi
|
||||
|
||||
; pop rdi
|
||||
; cmp rdi, 2
|
||||
; jne err_args
|
||||
; mov rdi, [rsp + 8] ; argv[1]
|
||||
; push rbp
|
||||
; mov rbp, rsp
|
||||
;
|
||||
; sub rsp, 16
|
||||
;
|
||||
; call get_file_content
|
||||
; mov rdi, rax
|
||||
; mov [rbp - 8], rax
|
||||
;
|
||||
; mov rdi, rax
|
||||
; call lex
|
||||
;
|
||||
mov rsp, rbp
|
||||
pop rbp
|
||||
mov rsi, '.'
|
||||
call split
|
||||
|
||||
mov rdi, rax
|
||||
mov rdi, [rdi]
|
||||
mov rsi, dot_s
|
||||
call ft_strjoin
|
||||
mov rdi, rax
|
||||
mov rsi, 0x241
|
||||
mov rdx, 0o644
|
||||
call open
|
||||
mov [fd_out], rax
|
||||
|
||||
mov rdi, [rbp - 16]
|
||||
|
||||
call get_file_content
|
||||
mov rdi, rax
|
||||
mov [rbp - 8], rax
|
||||
|
||||
mov rdi, rax
|
||||
call lex
|
||||
|
||||
mov rsp, rbp
|
||||
pop rbp
|
||||
|
||||
done:
|
||||
xor rdi, rdi
|
||||
|
||||
24
test.s
24
test.s
@ -1,24 +0,0 @@
|
||||
section .text
|
||||
global _start
|
||||
extern putnumber
|
||||
|
||||
_start:
|
||||
push rbp
|
||||
mov rbp, rsp
|
||||
sub rsp, 16
|
||||
xor rax, rax
|
||||
add rax, 6
|
||||
mov [rbp-8], rax
|
||||
xor rax, rax
|
||||
add rax, 5
|
||||
sub rax, 2
|
||||
add rax, 6
|
||||
add rax, [rbp-8]
|
||||
mov [rbp-16], rax
|
||||
push rdi
|
||||
mov rdi, [rbp-16]
|
||||
call putnumber
|
||||
pop rdi
|
||||
mov rsp, rbp
|
||||
pop rbp
|
||||
ret
|
||||
Loading…
x
Reference in New Issue
Block a user