This commit is contained in:
gbrochar 2020-11-22 17:27:36 +01:00
parent 20203294c5
commit 9665b06df3
85 changed files with 2908 additions and 1 deletions

42
Makefile Normal file
View File

@ -0,0 +1,42 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: gbrochar <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2016/05/10 02:28:41 by gbrochar #+# #+# #
# Updated: 2016/05/30 03:16:28 by gbrochar ### ########.fr #
# #
# **************************************************************************** #
NAME = fractol
SRC = main.c \
draw.c \
event_hook.c \
check_arg.c \
pixel_put.c \
draw_0_3.c \
draw_4_7.c \
compute.c
OBJ = $(SRC:.c=.o)
FLAGS = -Wall -Werror -Wextra
all: $(NAME)
$(NAME):
make -C libft
gcc $(FLAGS) libft/libft.a $(SRC) -o $(NAME) -lpthread -lm -lmlx -framework Appkit -framework OpenGL
clean:
make -C libft clean
rm -f $(OBJ)
fclean: clean
make -C libft fclean
rm -f $(NAME)
re: fclean all

View File

@ -1,3 +1,5 @@
# fractol
Fract'ol : a fractal engine made using minilibx. Features mandelbrot, julia and a third custom fractal.
Fract'ol : a fractal engine made using minilibx. Features mandelbrot, julia and a third custom fractal.
You need to have to minilibx installed to run it. It will not compile on linux systems.

1
auteur Normal file
View File

@ -0,0 +1 @@
gbrochar

57
check_arg.c Normal file
View File

@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_arg.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/05/11 11:00:15 by gbrochar #+# #+# */
/* Updated: 2016/05/26 15:16:23 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "fractol.h"
int check_mandelbrot(char *str)
{
char *mandelbrot;
mandelbrot = ft_strnew(10);
mandelbrot = "mandelbrot";
if (ft_strcmp(mandelbrot, str) == 0)
return (1);
return (0);
}
int check_julia(char *str)
{
char *julia;
julia = ft_strnew(10);
julia = "julia";
if (ft_strcmp(julia, str) == 0)
return (1);
return (0);
}
int check_black_hole(char *str)
{
char *black_hole;
black_hole = ft_strnew(10);
black_hole = "black_hole";
if (ft_strcmp(black_hole, str) == 0)
return (1);
return (0);
}
int check_arg(char *arg)
{
if (check_mandelbrot(arg))
return (1);
if (check_julia(arg))
return (2);
if (check_black_hole(arg))
return (3);
return (0);
}

23
compute.c Normal file
View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* draw.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/05/11 11:06:48 by gbrochar #+# #+# */
/* Updated: 2016/05/30 03:11:43 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "fractol.h"
void compute(int x, int y, t_env *e)
{
if (e->fractal == 1)
mandelbrot_compute(x, y, e);
if (e->fractal == 2)
julia_compute(x, y, e);
if (e->fractal == 3)
black_hole_compute(x, y, e);
}

106
draw.c Normal file
View File

@ -0,0 +1,106 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* mandelbrot.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/05/11 18:14:55 by gbrochar #+# #+# */
/* Updated: 2016/05/30 10:05:16 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "fractol.h"
void black_hole_compute(int x, int y, t_env *e)
{
int iter;
double r;
double i;
double r_save;
iter = 0;
e->map[y / 180].d_r = (double)4 / sqrt(e->zoom);
e->map[y / 180].d_i = (double)-4 / sqrt(e->zoom);
e->map[y / 180].z_r = x / WINX * e->map[y / 180].d_r + e->zd_r + e->os_r;
e->map[y / 180].z_i = y / WINY * e->map[y / 180].d_i + e->zd_i + e->os_i;
r = 0;
i = 0;
while (iter < e->imax && (((r) * (r)) + ((i) * (i))) <= 4)
{
r_save = r;
r = (r * r - i * i) + e->map[y / 180].z_r;
i = ((double)3 * r_save * (double)i) + e->map[y / 180].z_i;
iter++;
}
pixel_put(e, iter, x, y);
}
void mandelbrot_compute(int x, int y, t_env *e)
{
int iter;
double r;
double i;
double r_save;
iter = 0;
e->map[y / 180].d_r = (double)4 / sqrt(e->zoom);
e->map[y / 180].d_i = (double)-4 / sqrt(e->zoom);
e->map[y / 180].z_r = x / WINX * e->map[y / 180].d_r + e->zd_r + e->os_r;
e->map[y / 180].z_i = y / WINY * e->map[y / 180].d_i + e->zd_i + e->os_i;
r = 0;
i = 0;
while (iter < e->imax && ((r * r) + (i * i)) <= 4)
{
r_save = r;
r = (r * r - i * i) + e->map[y / 180].z_r;
i = ((double)2 * (double)r_save * (double)i) + e->map[y / 180].z_i;
iter++;
}
pixel_put(e, iter, x, y);
}
void julia_compute(int x, int y, t_env *e)
{
int iter;
double r;
double i;
double r_save;
iter = 0;
e->map[y / 180].d_r = (double)4 / sqrt(e->zoom);
e->map[y / 180].d_i = (double)-4 / sqrt(e->zoom);
r = (double)x / WINY * e->map[y / 180].d_r + e->zd_r + e->os_r;
i = (double)y / WINX * e->map[y / 180].d_i + e->zd_i + e->os_i;
e->map[y / 180].z_r = e->julia_r;
e->map[y / 180].z_i = e->julia_i;
while (iter < e->imax && ((r * r) + (i * i)) <= 4)
{
r_save = r;
r = (r * r - i * i) + e->map[y / 180].z_r;
i = ((double)2 * (double)r_save * (double)i) + e->map[y / 180].z_i;
iter++;
}
pixel_put(e, iter, x, y);
}
void draw(t_env *e)
{
pthread_t *threads;
void *ret;
int i;
threads = (pthread_t *)malloc(8 * sizeof(pthread_t));
pthread_create(&threads[0], NULL, draw_0, e);
pthread_create(&threads[1], NULL, draw_1, e);
pthread_create(&threads[2], NULL, draw_2, e);
pthread_create(&threads[3], NULL, draw_3, e);
pthread_create(&threads[4], NULL, draw_4, e);
pthread_create(&threads[5], NULL, draw_5, e);
pthread_create(&threads[6], NULL, draw_6, e);
pthread_create(&threads[7], NULL, draw_7, e);
i = 0;
while (i != 8)
pthread_join(threads[i++], &ret);
mlx_put_image_to_window(e->ptr, e->win, e->img, 0, 0);
}

97
draw_0_3.c Normal file
View File

@ -0,0 +1,97 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* draw_0_4.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/05/26 00:36:26 by gbrochar #+# #+# */
/* Updated: 2016/05/26 00:43:18 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "fractol.h"
void *draw_0(void *e_void)
{
int x;
int y;
t_env *e;
e = (t_env *)e_void;
y = 0;
while (y != 180)
{
x = 0;
while (x != WINX)
{
compute(x, y, e);
++x;
}
++y;
}
return (e_void);
}
void *draw_1(void *e_void)
{
int x;
int y;
t_env *e;
e = (t_env *)e_void;
y = 180;
while (y != 360)
{
x = 0;
while (x != WINX)
{
compute(x, y, e);
++x;
}
++y;
}
return (e_void);
}
void *draw_2(void *e_void)
{
int x;
int y;
t_env *e;
e = (t_env *)e_void;
y = 360;
while (y != 540)
{
x = 0;
while (x != WINX)
{
compute(x, y, e);
++x;
}
++y;
}
return (e_void);
}
void *draw_3(void *e_void)
{
int x;
int y;
t_env *e;
e = (t_env *)e_void;
y = 540;
while (y != 720)
{
x = 0;
while (x != WINX)
{
compute(x, y, e);
++x;
}
++y;
}
return (e_void);
}

97
draw_4_7.c Normal file
View File

@ -0,0 +1,97 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* draw_4_7.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/05/26 00:36:52 by gbrochar #+# #+# */
/* Updated: 2016/05/26 00:46:41 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "fractol.h"
void *draw_4(void *e_void)
{
int x;
int y;
t_env *e;
e = (t_env *)e_void;
y = 720;
while (y != 900)
{
x = 0;
while (x != WINX)
{
compute(x, y, e);
++x;
}
++y;
}
return (e_void);
}
void *draw_5(void *e_void)
{
int x;
int y;
t_env *e;
e = (t_env *)e_void;
y = 900;
while (y != 1080)
{
x = 0;
while (x != WINX)
{
compute(x, y, e);
++x;
}
++y;
}
return (e_void);
}
void *draw_6(void *e_void)
{
int x;
int y;
t_env *e;
e = (t_env *)e_void;
y = 1080;
while (y != 1260)
{
x = 0;
while (x != WINX)
{
compute(x, y, e);
++x;
}
++y;
}
return (e_void);
}
void *draw_7(void *e_void)
{
int x;
int y;
t_env *e;
e = (t_env *)e_void;
y = 1260;
while (y != 1440)
{
x = 0;
while (x != WINX)
{
compute(x, y, e);
++x;
}
++y;
}
return (e_void);
}

134
event_hook.c Normal file
View File

@ -0,0 +1,134 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* event_hook.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/05/10 02:38:27 by gbrochar #+# #+# */
/* Updated: 2016/05/30 11:36:32 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "fractol.h"
void color_manage(t_env *e, int keycode)
{
if (keycode == 16)
e->r++;
if (keycode == 32)
e->g++;
if (keycode == 34)
e->b++;
if (keycode == 4)
e->r--;
if (keycode == 38)
e->g--;
if (keycode == 40)
e->b--;
if (keycode == 18 || keycode == 19 || keycode == 20)
{
e->fractal = (keycode == 18) ? 1 : e->fractal;
e->fractal = (keycode == 19) ? 2 : e->fractal;
e->fractal = (keycode == 20) ? 3 : e->fractal;
init_env(e);
}
}
void zoom_manage(t_env *e, int keycode)
{
if (keycode == 69)
{
e->zoom *= 4;
e->map[0].z_r = 720.0 / WINX * e->map[0].d_r + e->zd_r;
e->map[0].z_i = 720.0 / WINY * e->map[0].d_i + e->zd_i;
e->map[0].d_r = 4.0 / sqrt(e->zoom);
e->map[0].d_i = -4.0 / sqrt(e->zoom);
e->zd_r = -((720.0 * (e->map[0].d_r / WINX))) + e->map[0].z_r;
e->zd_i = -((720.0 * ((e->map[0].d_i) / WINY))) + e->map[0].z_i;
draw(e);
}
if (keycode == 78 && e->zoom >= 0.0001)
{
e->zoom /= 4;
e->map[0].z_r = 720.0 / WINX * e->map[0].d_r + e->zd_r;
e->map[0].z_i = 720.0 / WINY * e->map[0].d_i + e->zd_i;
e->map[0].d_r = 4.0 / sqrt(e->zoom);
e->map[0].d_i = -4.0 / sqrt(e->zoom);
e->zd_r = -((720.0 * (e->map[0].d_r / WINX))) + e->map[0].z_r;
e->zd_i = -((720.0 * ((e->map[0].d_i) / WINY))) + e->map[0].z_i;
draw(e);
}
}
int key_hook(int keycode, void *e_void)
{
t_env *e;
e = e_void;
if (keycode == 53)
exit(0);
if (keycode == 15)
e->imax *= 1.2;
if ((keycode == 3) && e->imax > 4)
e->imax /= 1.2;
if (keycode == 13 || keycode == 126)
e->os_i -= (double)0.25 / sqrt(e->zoom);
if (keycode == 1 || keycode == 125)
e->os_i += (double)0.25 / sqrt(e->zoom);
if (keycode == 2 || keycode == 124)
e->os_r -= (double)0.25 / sqrt(e->zoom);
if (keycode == 0 || keycode == 123)
e->os_r += (double)0.25 / sqrt(e->zoom);
if (keycode == 37)
e->lock = (e->lock == 0) ? 1 : 0;
color_manage(e, keycode);
zoom_manage(e, keycode);
draw(e);
return (0);
}
int mouse_hook(int but, int x, int y, void *e_void)
{
t_env *e;
e = (t_env *)e_void;
if ((but == 1 || but == 4) && x > -1 && y > -1 && x < WINX && y < WINY)
{
e->zoom *= 4;
e->map[0].z_r = (double)x / WINX * e->map[0].d_r + e->zd_r;
e->map[0].z_i = (double)y / WINY * e->map[0].d_i + e->zd_i;
e->map[0].d_r = (double)4 / sqrt(e->zoom);
e->map[0].d_i = (double)-4 / sqrt(e->zoom);
e->zd_r = -(((double)x * (e->map[0].d_r / WINX))) + e->map[0].z_r;
e->zd_i = -(((double)y * ((e->map[0].d_i) / WINY))) + e->map[0].z_i;
}
if ((but == 2 || but == 5) && x > -1 && y > -1 && x < WINX && y < WINY)
{
e->zoom /= 4;
e->map[0].z_r = (double)x / WINX * e->map[0].d_r + e->zd_r;
e->map[0].z_i = (double)y / WINY * e->map[0].d_i + e->zd_i;
e->map[0].d_r = (double)4 / sqrt(e->zoom);
e->map[0].d_i = (double)-4 / sqrt(e->zoom);
e->zd_r = -(((double)x * (e->map[0].d_r / WINX))) + e->map[0].z_r;
e->zd_i = -(((double)y * ((e->map[0].d_i) / WINY))) + e->map[0].z_i;
}
draw(e);
return (0);
}
int cursor_hook(int x, int y, void *e_void)
{
t_env *e;
e = (t_env *)e_void;
if (e->lock == 0 && x > -1 && y > -1 && x < WINX && y < WINY)
{
e->map[y / 180].d_r = (double)4 / sqrt(e->zoom);
e->map[y / 180].d_i = (double)-4 / sqrt(e->zoom);
e->julia_r = (double)x / WINX * e->map[y / 180].d_r + e->zd_r;
e->julia_i = (double)y / WINY * e->map[y / 180].d_i + e->zd_i;
draw(e);
}
return (0);
}

84
fractol.h Normal file
View File

@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fractol.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/05/09 23:58:11 by gbrochar #+# #+# */
/* Updated: 2016/05/30 11:36:55 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FRACTOL_H
# define FRACTOL_H
# include "libft/libft.h"
# include "mlx.h"
# include "math.h"
# include <pthread.h>
# define WINX (double)1440
# define WINY (double)1440
typedef struct s_map
{
int y_min;
double d_r;
double d_i;
double z_r;
double z_i;
} t_map;
typedef struct s_env
{
void *ptr;
void *win;
void *img;
char *data;
int bpp;
int sz_l;
int endian;
int sz_i;
int fractal;
t_map *map;
double zd_r;
double zd_i;
double julia_r;
double julia_i;
double os_i;
double os_r;
double zoom;
int r;
int g;
int b;
int lock;
int imax;
int threads_max;
int threads_min;
} t_env;
int key_hook(int keycode, void *e_void);
int mouse_hook(int button, int x, int y, void *e_void);
int cursor_hook(int x, int y, void *e_void);
int check_arg(char *arg);
int check_mandelbrot(char *str);
int check_julia(char *str);
int check_black_hole(char *str);
void init_env(t_env *e);
void draw(t_env *e);
void compute(int x, int y, t_env *e);
void mandelbrot_compute(int x, int y, t_env *e);
void julia_compute(int x, int y, t_env *e);
void black_hole_compute(int x, int y, t_env *e);
void pixel_put(t_env *e, int i, int x, int y);
void *draw_0(void *e_void);
void *draw_1(void *e_void);
void *draw_2(void *e_void);
void *draw_3(void *e_void);
void *draw_4(void *e_void);
void *draw_5(void *e_void);
void *draw_6(void *e_void);
void *draw_7(void *e_void);
#endif

48
libft/Makefile Normal file
View File

@ -0,0 +1,48 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: gbrochar <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2015/11/27 19:26:17 by gbrochar #+# #+# #
# Updated: 2016/05/13 06:37:20 by gbrochar ### ########.fr #
# #
# **************************************************************************** #
NAME = libft.a
SRC = ft_atoi.c ft_bzero.c ft_isalnum.c ft_isalpha.c ft_isascii.c ft_isdigit.c \
ft_isprint.c ft_isspace.c ft_itoa.c ft_lstadd.c ft_lstdel.c ft_lstdelone.c \
ft_lstiter.c ft_lstmap.c ft_lstnew.c ft_memalloc.c ft_memccpy.c ft_memchr.c \
ft_memcmp.c ft_memcpy.c ft_memdel.c ft_memmove.c ft_memset.c ft_power.c \
ft_putchar.c ft_putchar_fd.c ft_putendl.c ft_putendl_fd.c ft_putnbr.c \
ft_putnbr_fd.c ft_putstr.c ft_putstr_fd.c ft_sort_int.c ft_sqrt.c ft_strcat.c \
ft_strchr.c ft_strclr.c ft_strcmp.c ft_strcpy.c ft_strdel.c ft_strdup.c \
ft_strequ.c ft_striter.c ft_striteri.c ft_strjoin.c ft_strlcat.c ft_strlen.c \
ft_strmap.c ft_strmapi.c ft_strncat.c ft_strncmp.c ft_strncpy.c ft_strnequ.c \
ft_strnew.c ft_strnstr.c ft_strrchr.c ft_strsplit.c ft_strstr.c ft_strsub.c \
ft_strtrim.c ft_swap.c ft_tolower.c ft_toupper.c ft_lstadd_preview.c \
ft_get_last_node.c ft_ishexdigit.c ft_atoi_len.c ft_atoi_color.c ft_error.c \
get_next_line.c
OBJ = $(SRC:.c=.o)
CC = gcc
FLAGS = # -Wall -Werror -Wextra -g
all: $(NAME)
$(NAME):
$(CC) $(FLAGS) -c $(SRC)
ar rc $(NAME) $(OBJ)
ranlib $(NAME)
clean:
rm -f $(OBJ)
fclean: clean
rm -f $(NAME)
re: fclean all

32
libft/ft_atoi.c Normal file
View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/27 19:18:43 by gbrochar #+# #+# */
/* Updated: 2015/12/05 15:01:36 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_atoi(const char *str)
{
long result;
long sign;
result = 0;
while (ft_isspace(*str))
str++;
sign = (*str == '-') ? -1 : 1;
if (*str == '-' || *str == '+')
str++;
while (*str >= '0' && *str <= '9')
{
result = (result * 10) + (*str - '0');
str++;
}
return ((int)(result * sign));
}

33
libft/ft_atoi_color.c Normal file
View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi_color.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/27 19:18:43 by gbrochar #+# #+# */
/* Updated: 2016/05/06 10:25:37 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_atoi_color(char *hexa_color)
{
int i;
int result;
result = 0;
i = 5;
while (i >= 0)
{
if (hexa_color[i] >= '0' && hexa_color[i] <= '9')
result += (hexa_color[i] - '0') * ft_power(16, (6 - i));
else if (hexa_color[i] >= 'A' && hexa_color[i] <= 'F')
result += (hexa_color[i] - '0') * ft_power(16, (6 - i));
else
return (-1);
i--;
}
return (result);
}

35
libft/ft_atoi_len.c Normal file
View File

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi_len.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/27 19:18:43 by gbrochar #+# #+# */
/* Updated: 2016/05/06 11:45:55 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_atoi_len(char *str, int *len)
{
int result;
int sign;
result = 0;
*len = 0;
sign = (*str == '-') ? -1 : 1;
if (*str == '-' || *str == '+')
{
str++;
len++;
}
while (*str >= '0' && *str <= '9')
{
result = (result * 10) + (*str - '0');
str++;
len++;
}
return (result * sign);
}

25
libft/ft_bzero.c Normal file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_bzero.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/24 11:39:18 by gbrochar #+# #+# */
/* Updated: 2015/11/24 20:46:21 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_bzero(void *s, size_t n)
{
char *byte;
byte = s;
while (n-- > 0)
{
*byte = 0;
byte++;
}
}

19
libft/ft_error.c Normal file
View File

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_error.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/05/10 02:34:40 by gbrochar #+# #+# */
/* Updated: 2016/05/11 10:55:43 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_error(char *str)
{
ft_putstr_fd(str, 2);
exit(0);
}

23
libft/ft_get_last_node.c Normal file
View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_get_last_node.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/02/28 07:10:21 by gbrochar #+# #+# */
/* Updated: 2016/02/28 07:12:12 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_get_last_node(t_list **alst)
{
t_list *tmp;
tmp = *alst;
while (tmp->next)
tmp = tmp->next;
return (tmp);
}

18
libft/ft_isalnum.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalnum.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/27 18:51:03 by gbrochar #+# #+# */
/* Updated: 2015/11/28 15:22:14 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalnum(int c)
{
return (ft_isalpha(c) || ft_isdigit(c));
}

18
libft/ft_isalpha.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalpha.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/27 18:59:08 by gbrochar #+# #+# */
/* Updated: 2015/11/28 15:42:21 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalpha(int c)
{
return (((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ? 1 : 0));
}

18
libft/ft_isascii.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isascii.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/27 18:48:25 by gbrochar #+# #+# */
/* Updated: 2015/11/28 15:32:54 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isascii(int c)
{
return ((c >= 0 && c <= 127) ? 1 : 0);
}

18
libft/ft_isdigit.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/27 18:53:19 by gbrochar #+# #+# */
/* Updated: 2015/11/28 14:41:12 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isdigit(int c)
{
return ((c >= '0' && c <= '9') ? 1 : 0);
}

20
libft/ft_ishexdigit.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ishexdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/05/05 23:22:51 by gbrochar #+# #+# */
/* Updated: 2016/05/05 23:24:26 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_ishexdigit(int c)
{
if (ft_isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))
return (1);
return (0);
}

18
libft/ft_isprint.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isprint.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/27 18:44:48 by gbrochar #+# #+# */
/* Updated: 2015/11/28 15:33:24 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isprint(int c)
{
return ((c >= 32 && c <= 126) ? 1 : 0);
}

21
libft/ft_isspace.c Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isspace.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/28 14:37:03 by gbrochar #+# #+# */
/* Updated: 2015/11/28 14:39:18 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isspace(int c)
{
if (c == '\t' || c == '\n' || c == '\f' || c == '\v' || c == '\r'
|| c == ' ')
return (1);
return (0);
}

37
libft/ft_itoa.c Normal file
View File

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/29 09:56:59 by gbrochar #+# #+# */
/* Updated: 2015/12/05 16:22:09 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_itoa(int n)
{
char *str;
size_t str_size;
int n_mem;
n_mem = n;
str_size = (n < 0) ? 3 : 2;
while ((n > 9 || n < -9) && str_size++)
n /= 10;
str = (char *)malloc((str_size--) * sizeof(char));
if (!str)
return (NULL);
str[str_size--] = '\0';
while (n_mem > 9 || n_mem < -9)
{
str[str_size--] = (n_mem < 0) ? -(n_mem % 10) + '0' : n_mem % 10 + '0';
n_mem = n_mem / 10;
}
str[0] = (n_mem < 0) ? '-' : (n_mem + '0');
str[1] = (n_mem < 0) ? (-n_mem + '0') : str[1];
return (str);
}

23
libft/ft_lstadd.c Normal file
View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/12/01 14:24:17 by gbrochar #+# #+# */
/* Updated: 2016/02/26 14:02:54 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstadd(t_list **alst, t_list *new)
{
t_list *tmp;
tmp = *alst;
while (tmp->next != NULL)
tmp = tmp->next;
tmp->next = new;
}

19
libft/ft_lstadd_preview.c Normal file
View File

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_preview.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/12/01 14:24:17 by gbrochar #+# #+# */
/* Updated: 2016/02/26 14:01:33 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstadd_preview(t_list **alst, t_list *new)
{
new->next = *alst;
*alst = new;
}

25
libft/ft_lstdel.c Normal file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstdel.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/30 21:34:50 by gbrochar #+# #+# */
/* Updated: 2015/12/05 15:10:37 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstdel(t_list **alst, void (*del)(void *, size_t))
{
t_list *tmp;
while (*alst)
{
tmp = *alst;
ft_lstdelone(alst, del);
*alst = tmp->next;
}
}

19
libft/ft_lstdelone.c Normal file
View File

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstdelone.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/30 21:27:05 by gbrochar #+# #+# */
/* Updated: 2015/12/02 13:28:32 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstdelone(t_list **alst, void (*del)(void *, size_t))
{
del((*alst)->content, (*alst)->content_size);
ft_memdel((void **)alst);
}

29
libft/ft_lstiter.c Normal file
View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstiter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/12/02 13:38:16 by gbrochar #+# #+# */
/* Updated: 2015/12/04 16:41:19 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstiter(t_list *lst, void (*f)(t_list *elem))
{
t_list *start;
t_list *tmp;
start = lst;
while (lst->next)
{
tmp = lst;
f(lst);
lst = tmp->next;
}
f(lst);
lst = start;
}

31
libft/ft_lstmap.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/12/02 14:33:07 by gbrochar #+# #+# */
/* Updated: 2015/12/02 16:17:18 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem))
{
t_list *lst_begin;
t_list *lst_new;
lst_begin = ft_lstnew((f(lst))->content, (f(lst))->content_size);
lst_new = lst_begin;
lst = lst->next;
while (lst->next)
{
lst_new->next = ft_lstnew((f(lst))->content, (f(lst))->content_size);
lst_new = lst_new->next;
lst = lst->next;
}
lst_new->next = ft_lstnew((f(lst))->content, (f(lst))->content_size);
return (lst_begin);
}

40
libft/ft_lstnew.c Normal file
View File

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstnew.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/30 21:21:09 by gbrochar #+# #+# */
/* Updated: 2016/02/27 21:04:51 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstnew(void const *content, size_t content_size)
{
t_list *lst;
lst = (t_list *)malloc(sizeof(t_list));
if (!lst)
return (NULL);
if (!content)
{
lst->content = NULL;
lst->content_size = 0;
}
else
{
lst->content = ft_memalloc(content_size);
if (!lst->content)
{
free(lst);
return (NULL);
}
ft_memmove(lst->content, content, content_size);
lst->content_size = content_size;
}
lst->next = NULL;
return (lst);
}

27
libft/ft_memalloc.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memalloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/28 18:11:36 by gbrochar #+# #+# */
/* Updated: 2016/02/27 21:04:55 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memalloc(size_t size)
{
unsigned char *ptr;
size_t i;
i = 0;
ptr = (unsigned char *)malloc(size);
if (!ptr)
return (NULL);
while (i < size)
ptr[i++] = 0;
return (ptr);
}

37
libft/ft_memccpy.c Normal file
View File

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memccpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/24 14:40:00 by gbrochar #+# #+# */
/* Updated: 2015/12/08 14:37:54 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memccpy(void *dst, const void *src, int c, size_t n)
{
size_t i;
unsigned char to_find;
unsigned char *dst_byte;
unsigned char *src_byte;
to_find = (unsigned char)c;
i = 0;
dst_byte = (unsigned char *)dst;
src_byte = (unsigned char *)src;
while (i != n && src_byte[i] != to_find)
{
dst_byte[i] = src_byte[i];
i++;
}
if (src_byte[i] == to_find)
{
dst_byte[i] = to_find;
return (&dst[i + 1]);
}
return (NULL);
}

31
libft/ft_memchr.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/25 13:03:54 by gbrochar #+# #+# */
/* Updated: 2015/11/25 14:07:56 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memchr(const void *s, int c, size_t n)
{
unsigned char to_find;
unsigned char *str;
size_t i;
i = 0;
to_find = c;
str = (unsigned char *)s;
while (i < n)
{
if (str[i] == to_find)
return ((void *)&s[i]);
i++;
}
return (NULL);
}

33
libft/ft_memcmp.c Normal file
View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/25 14:13:46 by gbrochar #+# #+# */
/* Updated: 2015/11/30 14:46:05 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_memcmp(const void *s1, const void *s2, size_t n)
{
unsigned char *s1_byte;
unsigned char *s2_byte;
size_t i;
s1_byte = (unsigned char *)s1;
s2_byte = (unsigned char *)s2;
i = 0;
if (n <= 0)
return (0);
while (s1_byte[i] == s2_byte[i])
{
if (i + 1 == n)
return (0);
i++;
}
return (s1_byte[i] - s2_byte[i]);
}

30
libft/ft_memcpy.c Normal file
View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/25 14:42:55 by gbrochar #+# #+# */
/* Updated: 2015/11/25 14:43:02 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memcpy(void *dst, const void *src, size_t n)
{
size_t i;
unsigned char *dst_byte;
const unsigned char *src_byte;
i = 0;
dst_byte = dst;
src_byte = src;
while (i < n)
{
dst_byte[i] = src_byte[i];
i++;
}
return (dst);
}

19
libft/ft_memdel.c Normal file
View File

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memdel.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/28 18:23:10 by gbrochar #+# #+# */
/* Updated: 2015/11/28 18:25:10 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_memdel(void **ap)
{
free(*ap);
*ap = NULL;
}

39
libft/ft_memmove.c Normal file
View File

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memmove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/25 12:36:04 by gbrochar #+# #+# */
/* Updated: 2015/11/25 13:03:31 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memmove(void *dst, const void *src, size_t len)
{
unsigned char *dst_byte;
const unsigned char *src_byte;
size_t index;
dst_byte = dst;
src_byte = src;
if (dst_byte > src_byte)
{
index = len;
while (index-- > 0)
dst_byte[index] = src_byte[index];
}
else
{
index = 0;
while (index < len)
{
dst_byte[index] = src_byte[index];
index++;
}
}
return (dst);
}

28
libft/ft_memset.c Normal file
View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/23 17:53:16 by gbrochar #+# #+# */
/* Updated: 2015/11/25 12:18:32 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memset(void *b, int c, size_t len)
{
unsigned char *byte;
size_t i;
byte = b;
i = 0;
while (i++ < len)
{
*byte = c;
byte++;
}
return (b);
}

25
libft/ft_power.c Normal file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_power.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/12/05 15:23:07 by gbrochar #+# #+# */
/* Updated: 2015/12/07 11:03:41 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_power(int nb, int pow)
{
int ret;
ret = 1;
if (pow < 0)
return (-1);
while (pow--)
ret *= nb;
return (ret);
}

18
libft/ft_putchar.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/24 12:01:00 by gbrochar #+# #+# */
/* Updated: 2015/11/29 16:47:24 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putchar(char c)
{
write(1, &c, 1);
}

18
libft/ft_putchar_fd.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/29 09:01:19 by gbrochar #+# #+# */
/* Updated: 2015/11/29 09:02:06 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putchar_fd(char c, int fd)
{
write(fd, &c, 1);
}

19
libft/ft_putendl.c Normal file
View File

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putendl.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/29 09:04:13 by gbrochar #+# #+# */
/* Updated: 2015/11/29 09:04:50 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putendl(char const *s)
{
ft_putstr(s);
ft_putchar('\n');
}

19
libft/ft_putendl_fd.c Normal file
View File

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putendl_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/29 09:04:58 by gbrochar #+# #+# */
/* Updated: 2015/11/29 09:05:51 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putendl_fd(char const *s, int fd)
{
ft_putstr_fd(s, fd);
ft_putchar_fd('\n', fd);
}

31
libft/ft_putnbr.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/29 08:58:30 by gbrochar #+# #+# */
/* Updated: 2015/11/29 10:49:28 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putnbr(int n)
{
if (n == -2147483648)
ft_putstr("-2147483648");
else if (n < 0)
{
ft_putchar('-');
ft_putnbr(-n);
}
else if (n > 9)
{
ft_putnbr(n / 10);
ft_putchar(n % 10 + '0');
}
else
ft_putchar(n + '0');
}

31
libft/ft_putnbr_fd.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/29 09:10:52 by gbrochar #+# #+# */
/* Updated: 2015/11/29 10:49:38 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putnbr_fd(int n, int fd)
{
if (n == -2147483648)
ft_putstr_fd("-2147483648", fd);
else if (n < 0)
{
ft_putchar_fd('-', fd);
ft_putnbr_fd(-n, fd);
}
else if (n > 9)
{
ft_putnbr_fd(n / 10, fd);
ft_putchar_fd(n % 10 + '0', fd);
}
else
ft_putchar_fd(n + '0', fd);
}

19
libft/ft_putstr.c Normal file
View File

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/27 18:23:12 by gbrochar #+# #+# */
/* Updated: 2015/11/29 09:06:37 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putstr(char const *s)
{
while (*s)
write(1, s++, 1);
}

19
libft/ft_putstr_fd.c Normal file
View File

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/29 09:02:48 by gbrochar #+# #+# */
/* Updated: 2015/11/29 09:04:05 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putstr_fd(char const *s, int fd)
{
while (*s)
write(fd, s++, 1);
}

33
libft/ft_sort_int.c Normal file
View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_sort_int.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/12/05 18:36:08 by gbrochar #+# #+# */
/* Updated: 2015/12/07 11:04:19 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_sort_int(int *tab, int size)
{
int i;
int tmp;
i = 0;
while ((i + 1) < size)
{
if (tab[i] > tab[i + 1])
{
tmp = tab[i];
tab[i] = tab[i + 1];
tab[i + 1] = tmp;
i = 0;
}
else
i++;
}
}

23
libft/ft_sqrt.c Normal file
View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_sqrt.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/12/07 08:28:11 by gbrochar #+# #+# */
/* Updated: 2015/12/07 08:50:43 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
long ft_sqrt(long n)
{
long sqrt;
sqrt = 0;
while ((sqrt * sqrt) != n && (sqrt * sqrt) < n)
sqrt++;
return (((sqrt * sqrt) == n) ? (sqrt) : (-1));
}

32
libft/ft_strcat.c Normal file
View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/25 16:26:53 by gbrochar #+# #+# */
/* Updated: 2016/04/09 21:24:58 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strcat(char *s1, const char *s2)
{
char *p_s1;
const char *p_s2;
p_s2 = s2;
p_s1 = s1;
while (*p_s1)
p_s1++;
while (*p_s2)
{
*p_s1 = *p_s2;
p_s1++;
p_s2++;
}
*p_s1 = '\0';
return (s1);
}

23
libft/ft_strchr.c Normal file
View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/27 09:50:37 by gbrochar #+# #+# */
/* Updated: 2016/04/09 21:28:38 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strchr(const char *s, int c)
{
if (s == NULL)
return (0);
while (*s != (char)c)
if (!*s++)
return (0);
return ((char *)s);
}

19
libft/ft_strclr.c Normal file
View File

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strclr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/28 20:22:52 by gbrochar #+# #+# */
/* Updated: 2015/11/28 21:09:55 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_strclr(char *s)
{
while (*s)
*s++ = '\0';
}

21
libft/ft_strcmp.c Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/28 14:52:37 by gbrochar #+# #+# */
/* Updated: 2015/11/28 15:34:12 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_strcmp(const char *s1, const char *s2)
{
while (*s1 || *s2)
if (*s1++ != *s2++)
return ((unsigned char)*--s1 - (unsigned char)*--s2);
return (0);
}

30
libft/ft_strcpy.c Normal file
View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/25 15:10:54 by gbrochar #+# #+# */
/* Updated: 2016/04/09 16:49:30 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strcpy(char *dst, const char *src)
{
char *p_dest;
const char *p_src;
p_src = src;
p_dest = dst;
while (*p_src)
{
*p_dest = *p_src;
p_dest++;
p_src++;
}
*p_dest = '\0';
return (dst);
}

18
libft/ft_strdel.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdel.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/28 20:21:47 by gbrochar #+# #+# */
/* Updated: 2015/12/08 15:21:50 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_strdel(char **as)
{
ft_memdel((void **)as);
}

29
libft/ft_strdup.c Normal file
View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/25 14:47:23 by gbrochar #+# #+# */
/* Updated: 2016/04/09 21:27:37 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strdup(const char *s1)
{
char *dest;
int i;
i = 0;
dest = ft_strnew(ft_strlen(s1));
while (s1[i])
{
dest[i] = s1[i];
i++;
}
dest[i] = '\0';
return (dest);
}

27
libft/ft_strequ.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strequ.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/29 08:35:37 by gbrochar #+# #+# */
/* Updated: 2015/11/29 15:47:45 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_strequ(char const *s1, char const *s2)
{
int i;
i = 0;
while (s1[i] == s2[i])
{
if (s1[i] == '\0' && s2[i] == '\0')
return (1);
i++;
}
return (0);
}

25
libft/ft_striter.c Normal file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_striter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/28 20:37:55 by gbrochar #+# #+# */
/* Updated: 2015/11/29 10:51:54 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_striter(char *s, void (*f)(char *))
{
int i;
i = 0;
while (s[i])
{
f(&s[i]);
i++;
}
}

25
libft/ft_striteri.c Normal file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_striteri.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/28 21:02:16 by gbrochar #+# #+# */
/* Updated: 2015/11/29 10:30:40 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_striteri(char *s, void (*f)(unsigned int, char *s))
{
unsigned int i;
i = 0;
while (s[i])
{
f(i, &s[i]);
i++;
}
}

34
libft/ft_strjoin.c Normal file
View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/29 09:48:11 by gbrochar #+# #+# */
/* Updated: 2016/04/09 16:30:06 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strjoin(char const *s1, const char *s2)
{
size_t i;
char *new;
s1 = (s1 == NULL) ? "" : s1;
s2 = (s2 == NULL) ? "" : s2;
if (s1 && s2)
{
i = ft_strlen(s1) + ft_strlen(s2);
new = ft_strnew(i);
if (new)
{
new = ft_strcpy(new, s1);
new = ft_strcat(new, s2);
return (new);
}
}
return (NULL);
}

32
libft/ft_strlcat.c Normal file
View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/26 08:00:59 by gbrochar #+# #+# */
/* Updated: 2015/11/29 15:48:23 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlcat(char *dst, const char *src, size_t size)
{
size_t dst_len;
size_t src_len;
size_t i_src;
size_t i_dst;
dst_len = ft_strlen(dst);
src_len = ft_strlen(src);
i_src = 0;
i_dst = dst_len;
if (dst_len >= size)
return (size + src_len);
while (src[i_src] && (size-- - (dst_len + 1)) > 0)
dst[i_dst++] = src[i_src++];
dst[i_dst] = '\0';
return (dst_len + src_len);
}

23
libft/ft_strlen.c Normal file
View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/25 14:43:32 by gbrochar #+# #+# */
/* Updated: 2016/04/09 16:57:01 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlen(const char *s)
{
size_t i;
i = 0;
while (s && s[i])
i++;
return (i);
}

33
libft/ft_strmap.c Normal file
View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/28 21:30:34 by gbrochar #+# #+# */
/* Updated: 2015/11/29 16:53:26 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strmap(char const *s, char (*f)(char))
{
char *str;
int i;
int len;
len = ft_strlen(s);
i = 0;
str = (char *)malloc((len + 1) * sizeof(char));
if (!str)
return (NULL);
while (i < len)
{
str[i] = f(s[i]);
i++;
}
str[len] = '\0';
return (str);
}

31
libft/ft_strmapi.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/28 21:39:30 by gbrochar #+# #+# */
/* Updated: 2015/12/02 16:22:05 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
char *str;
unsigned int i;
i = 0;
str = (char *)malloc((ft_strlen(s) + 1) * sizeof(char));
if (!str)
return (NULL);
while (s[i])
{
str[i] = f(i, s[i]);
i++;
}
str[i] = '\0';
return (str);
}

30
libft/ft_strncat.c Normal file
View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/25 19:29:04 by gbrochar #+# #+# */
/* Updated: 2015/11/29 12:33:35 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strncat(char *s1, const char *s2, size_t n)
{
size_t i_dst;
size_t i_src;
i_dst = ft_strlen(s1);
i_src = 0;
while (i_src < n && s2[i_src] != '\0')
{
s1[i_dst] = s2[i_src];
i_dst++;
i_src++;
}
s1[i_dst] = '\0';
return (s1);
}

21
libft/ft_strncmp.c Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/28 14:57:36 by gbrochar #+# #+# */
/* Updated: 2015/11/28 15:34:45 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_strncmp(const char *s1, const char *s2, size_t n)
{
while ((*s1 || *s2) && n--)
if (*s1++ != *s2++)
return ((unsigned char)*--s1 - (unsigned char)*--s2);
return (0);
}

31
libft/ft_strncpy.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/25 16:18:18 by gbrochar #+# #+# */
/* Updated: 2015/11/25 16:21:09 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strncpy(char *dst, const char *src, size_t n)
{
size_t i;
i = 0;
while (i != n && src[i] != '\0')
{
dst[i] = src[i];
i++;
}
while (i != n)
{
dst[i] = '\0';
i++;
}
return (dst);
}

27
libft/ft_strnequ.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnequ.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/29 08:37:07 by gbrochar #+# #+# */
/* Updated: 2015/12/05 16:24:50 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_strnequ(char const *s1, char const *s2, size_t n)
{
size_t i;
i = 0;
while (s1[i] == s2[i] || i == n)
{
if ((s1[i] == '\0' && s2[i] == '\0') || i == n)
return (1);
i++;
}
return (0);
}

23
libft/ft_strnew.c Normal file
View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnew.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/28 18:26:56 by gbrochar #+# #+# */
/* Updated: 2016/04/09 16:25:44 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strnew(size_t size)
{
char *str;
str = (char *)malloc(sizeof(char) * (size + 1));
if (!str)
return (NULL);
return (str);
}

40
libft/ft_strnstr.c Normal file
View File

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/27 12:49:32 by gbrochar #+# #+# */
/* Updated: 2015/11/28 16:39:13 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strnstr(const char *s1, char *s2, size_t n)
{
size_t i;
size_t i_mem;
size_t i_tofind;
i_mem = 0;
if (s2[0] == '\0')
return ((char *)s1);
while (s1[i_mem] != '\0' && i_mem < n)
{
i_tofind = 0;
while (s1[i_mem] != s2[i_tofind] && s1[i_mem] != '\0')
i_mem++;
if (s1[i_mem] == '\0')
return (NULL);
i = i_mem;
while ((s1[i] == s2[i_tofind] || s2[i_tofind] == '\0') && i++ <= n)
if (s2[i_tofind++] == '\0')
return ((char *)&s1[i_mem]);
if (i > n)
return (NULL);
i_mem++;
}
return (NULL);
}

28
libft/ft_strrchr.c Normal file
View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strrchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/27 10:05:56 by gbrochar #+# #+# */
/* Updated: 2015/11/27 10:24:37 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strrchr(const char *s, int c)
{
int i;
i = ft_strlen(s);
if (c)
{
while (i--)
if (s[i] == (char)c)
return ((char *)&s[i]);
return (NULL);
}
return ((char *)&s[i]);
}

87
libft/ft_strsplit.c Normal file
View File

@ -0,0 +1,87 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/29 09:55:10 by gbrochar #+# #+# */
/* Updated: 2015/12/07 11:00:21 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_wordcount(char const *s, char c)
{
int word_count;
int i;
i = 0;
word_count = 0;
while (s[i] != '\0')
{
while (s[i] == c)
i++;
if (s[i] != '\0')
word_count++;
while (s[i] != c && s[i] != '\0')
i++;
}
return (word_count);
}
static void *ft_taballoc(char const *s, char **str_tab, char c)
{
int i;
int i2;
int wl;
i = 0;
i2 = 0;
while (s[i2])
{
wl = 0;
while (s[i2] == c && s[i2] != '\0')
i2++;
while (s[i2] != c && s[i2] != '\0')
{
wl++;
i2++;
}
if (wl != 0)
*str_tab++ = (char *)malloc((wl + 1) * sizeof(char));
if (!(str_tab - 1))
return (NULL);
}
return (*str_tab);
}
char **ft_strsplit(char const *s, char c)
{
char **str_tab;
int i;
int i2;
int i3;
int wc;
i = 0;
i3 = 0;
wc = ft_wordcount(s, c);
str_tab = (char **)malloc((wc + 1) * sizeof(char *));
if (!str_tab)
return (NULL);
ft_taballoc(s, str_tab, c);
while (i != wc)
{
i2 = 0;
while (s[i3] == c && s[i3] != '\0')
i3++;
while (s[i3] != c && s[i3] != '\0')
str_tab[i][i2++] = s[i3++];
str_tab[i][i2] = '\0';
i++;
}
str_tab[i] = NULL;
return (str_tab);
}

38
libft/ft_strstr.c Normal file
View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/27 10:24:53 by gbrochar #+# #+# */
/* Updated: 2015/11/28 16:38:40 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strstr(const char *s1, char *s2)
{
int i;
int i_mem;
int i_tofind;
i_mem = 0;
if (s2[0] == '\0')
return ((char *)s1);
while (s1[i_mem] != '\0')
{
i_tofind = 0;
while (s1[i_mem] != s2[i_tofind] && s1[i_mem] != '\0')
i_mem++;
if (s1[i_mem] == '\0')
return (NULL);
i = i_mem;
while (s1[i++] == s2[i_tofind] || s2[i_tofind] == '\0')
if (s2[i_tofind++] == '\0')
return ((char *)&s1[i_mem]);
i_mem++;
}
return (NULL);
}

28
libft/ft_strsub.c Normal file
View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsub.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/29 09:14:36 by gbrochar #+# #+# */
/* Updated: 2015/12/08 15:34:26 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strsub(char const *s, unsigned int start, size_t len)
{
char *str;
size_t i;
i = 0;
str = (char *)malloc((len + 1) * sizeof(char));
if (!str)
return (NULL);
while (i < len)
str[i++] = s[start++];
str[i] = '\0';
return (str);
}

40
libft/ft_strtrim.c Normal file
View File

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/29 09:54:12 by gbrochar #+# #+# */
/* Updated: 2015/12/07 11:00:33 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strtrim(char const *s)
{
char *str;
size_t i;
size_t i2;
size_t ws;
i = ft_strlen(s) - 1;
i2 = 0;
ws = 0;
while ((s[i] == ' ' || s[i] == '\n' || s[i] == '\t') && i--)
ws++;
i = 0;
while ((s[i] == ' ' || s[i] == '\n' || s[i] == '\t') && ws < ft_strlen(s))
{
i++;
ws++;
}
str = (char *)malloc((ft_strlen(s) + 1 - ws) * sizeof(char));
if (!str)
return (NULL);
while (i2 < (ft_strlen(s) - ws))
str[i2++] = s[i++];
str[i2] = '\0';
return (str);
}

22
libft/ft_swap.c Normal file
View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_swap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/12/07 09:44:07 by gbrochar #+# #+# */
/* Updated: 2015/12/07 11:01:13 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_swap(int *a, int *b)
{
int c;
c = *a;
*a = *b;
*b = c;
}

20
libft/ft_tolower.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_tolower.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/27 18:39:55 by gbrochar #+# #+# */
/* Updated: 2015/11/29 08:40:07 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_tolower(int c)
{
if ((int)c >= 'A' && (int)c <= 'Z')
c += 32;
return (c);
}

20
libft/ft_toupper.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_toupper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/27 18:43:24 by gbrochar #+# #+# */
/* Updated: 2015/11/28 16:01:01 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_toupper(int c)
{
if ((int)c >= 'a' && (int)c <= 'z')
c -= 32;
return (c);
}

95
libft/get_next_line.c Normal file
View File

@ -0,0 +1,95 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/03/22 00:00:51 by gbrochar #+# #+# */
/* Updated: 2016/05/11 14:44:53 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *ft_strjoin2(char *str, char *str2)
{
char *tmp;
tmp = ft_strjoin(str, str2);
free(str);
return (tmp);
}
int gnl_manage_return(char **line, char **stock)
{
char *tmp;
char *tmp2;
if ((tmp = ft_strchr(*stock, '\n')))
{
*tmp = '\0';
tmp2 = ft_strdup(tmp + 1);
*line = ft_strdup(*stock);
ft_memdel((void **)stock);
*stock = ft_strdup(tmp2);
free(tmp2);
tmp2 = NULL;
tmp = NULL;
return (1);
}
if (ft_strlen(*stock) == 0)
{
ft_memdel((void **)stock);
return (0);
}
*line = ft_strdup(*stock);
ft_memdel((void **)stock);
return (1);
}
int gnl_manage_read(char **stock, char **line)
{
char *tmp;
char *tmp2;
if ((tmp = ft_strchr(*stock, '\n')))
{
*tmp = '\0';
tmp2 = ft_strdup(tmp + 1);
*line = ft_strdup(*stock);
ft_memdel((void **)stock);
*stock = ft_strdup(tmp2);
free(tmp2);
tmp2 = NULL;
tmp = NULL;
return (1);
}
return (0);
}
int get_next_line(int fd, char **line)
{
static char *stock = NULL;
char *buffer;
int ret;
if (stock)
if (gnl_manage_read(line, &stock))
return (1);
buffer = ft_strnew(BUFF_SIZE);
while ((ret = read(fd, buffer, BUFF_SIZE)) > 0)
{
buffer[ret] = '\0';
stock = ft_strjoin2(stock, buffer);
ft_memdel((void **)&buffer);
if (gnl_manage_read(&stock, line))
return (1);
buffer = ft_strnew(BUFF_SIZE + 1);
}
if (buffer)
ft_memdel((void **)&buffer);
if (ret == -1)
return (-1);
return (gnl_manage_return(line, &stock));
}

22
libft/get_next_line.h Normal file
View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/02/28 22:19:58 by gbrochar #+# #+# */
/* Updated: 2016/05/11 18:59:32 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef GET_NEXT_LINE_H
# define GET_NEXT_LINE_H
# include "libft.h"
# define BUFF_SIZE 42
int get_next_line(int const fd, char **line);
#endif

97
libft/libft.h Normal file
View File

@ -0,0 +1,97 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/24 12:52:28 by gbrochar #+# #+# */
/* Updated: 2016/05/11 10:56:00 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFT_H
# define LIBFT_H
# include <string.h>
# include <unistd.h>
# include <stdlib.h>
typedef struct s_list
{
void *content;
size_t content_size;
struct s_list *next;
} t_list;
t_list *ft_get_last_node(t_list **alst);
void ft_lstadd(t_list **alst, t_list *new);
void ft_lstadd_preview(t_list **alst, t_list *new);
void ft_lstdel(t_list **alst, void (*del)(void *, size_t));
void ft_lstdelone(t_list **alst, void (*del)(void *, size_t));
t_list *ft_lstnew(void const *content, size_t content_size);
void ft_lstiter(t_list *lst, void (*f)(t_list *elem));
t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem));
int ft_atoi(const char *str);
int ft_atoi_color(char *hexa_color);
int ft_atoi_len(char *str, int *len);
void ft_bzero(void *s, size_t n);
int ft_isalnum(int c);
int ft_isalpha(int c);
int ft_isascii(int c);
int ft_isdigit(int c);
int ft_isprint(int c);
int ft_isspace(int c);
char *ft_itoa(int n);
void *ft_memalloc(size_t size);
void *ft_memccpy(void *dst, const void *src, int c, size_t n);
void *ft_memchr(const void *s, int c, size_t n);
int ft_memcmp(const void *s1, const void *s2, size_t n);
void *ft_memcpy(void *dst, const void *src, size_t n);
void ft_memdel(void **ap);
void *ft_memmove(void *dst, const void *src, size_t len);
void *ft_memset(void *b, int c, size_t len);
int ft_power(int nb, int pow);
void ft_putchar(char c);
void ft_putchar_fd(char c, int fd);
void ft_putendl(char const *s);
void ft_putendl_fd(char const *s, int fd);
void ft_putnbr(int n);
void ft_putnbr_fd(int n, int fd);
void ft_putstr(char const *s);
void ft_putstr_fd(char const *s, int fd);
void ft_sort_int(int *tab, int size);
long ft_sqrt(long n);
char *ft_strcat(char *s1, const char *s2);
char *ft_strchr(const char *s, int c);
void ft_strclr(char *s);
int ft_strcmp(const char *s1, const char *s2);
char *ft_strcpy(char *dst, const char *src);
void ft_strdel(char **as);
char *ft_strdup(const char *s1);
int ft_strequ(char const *s1, char const *s2);
void ft_striter(char *s, void (*f)(char *));
void ft_striteri(char *s, void (*f)(unsigned int, char *s));
char *ft_strjoin(char const *s1, char const *s2);
size_t ft_strlcat(char *dst, const char *src, size_t size);
size_t ft_strlen(const char *s);
char *ft_strmap(char const *s, char (*f)(char));
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
char *ft_strncat(char *s1, const char *s2, size_t n);
int ft_strncmp(const char *s1, const char *s2, size_t n);
char *ft_strncpy(char *dst, const char *src, size_t n);
int ft_strnequ(char const *s1, char const *s2, size_t n);
char *ft_strnew(size_t size);
char *ft_strnstr(const char *s1, char *s2, size_t n);
char *ft_strrchr(const char *s, int c);
char **ft_strsplit(char const *s, char c);
char *ft_strstr(const char *s1, char *s2);
char *ft_strsub(char const *s, unsigned int start, size_t len);
char *ft_strtrim(char const *s);
void ft_swap(int *a, int *b);
int ft_tolower(int c);
int ft_toupper(int c);
int ft_ishexdigit(int c);
void ft_error(char *str);
#endif

90
main.c Normal file
View File

@ -0,0 +1,90 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/05/10 00:01:39 by gbrochar #+# #+# */
/* Updated: 2016/05/30 11:52:04 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "fractol.h"
int init_mlx(t_env *e)
{
if (!(e->ptr = mlx_init()))
return (1);
if (!(e->win = mlx_new_window(e->ptr, WINX, WINY, "fractol")))
{
free(e->ptr);
return (1);
}
if (!(e->img = mlx_new_image(e->ptr, WINX, WINY)))
{
mlx_destroy_window(e->ptr, e->win);
free(e->ptr);
return (1);
}
if (!(e->data = mlx_get_data_addr(e->img, &e->bpp, &e->sz_l, &e->endian)))
{
mlx_destroy_image(e->ptr, e->img);
mlx_destroy_window(e->ptr, e->win);
free(e->ptr);
return (1);
}
e->sz_i = ft_strlen(e->data);
mlx_hook(e->win, 2, 1L << 1, key_hook, e);
mlx_hook(e->win, 6, 1L << 1, cursor_hook, e);
mlx_mouse_hook(e->win, mouse_hook, e);
return (0);
}
void init_env(t_env *e)
{
int i;
i = -1;
e->map = (t_map *)malloc(8 * sizeof(t_map));
while (++i != 8)
e->map[i].y_min = 180 * i;
e->imax = 50;
e->zoom = 1;
e->zd_r = -2;
e->zd_i = 2;
e->lock = 0;
e->julia_i = 0;
e->julia_r = 0;
e->os_i = 0;
e->os_r = 0;
e->r = 0;
e->g = 2;
e->b = 4;
}
int main(int argc, char **argv)
{
t_env e;
if (argc == 2)
{
init_env(&(e));
if (init_mlx(&e))
ft_error("mlx_init failed");
if ((e.fractal = check_arg(argv[1])) == 0)
{
ft_putstr("usage: ./fractol mandelbrot\n");
ft_putstr("./fractol julia\n./fractol black_hole\n");
exit(0);
}
draw(&e);
mlx_loop(e.ptr);
}
else
{
ft_putstr("usage: ./fractol mandelbrot\n");
ft_putstr("./fractol julia\n./fractol black_hole\n");
}
return (0);
}

35
pixel_put.c Normal file
View File

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* putpixel.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/05/11 14:17:03 by gbrochar #+# #+# */
/* Updated: 2016/05/30 09:18:09 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "fractol.h"
void pixel_put(t_env *e, int i, int x, int y)
{
int index;
double ct_index;
ct_index = i + 1 - 2 * log(3) / sqrt((e->map[y / 180].z_r *
e->map[y / 180].z_r) + (e->map[y / 180].z_i * e->map[y / 180].z_i));
index = (x * (e->bpp / 8)) + (y * e->sz_l);
if (i == e->imax)
{
e->data[index] = 0;
e->data[index + 1] = 0;
e->data[index + 2] = 0;
}
else
{
e->data[index] = (sin(0.1 * ct_index + e->r) * 50 + 200);
e->data[index + 1] = (sin(0.1 * ct_index + e->g) * 50 + 200);
e->data[index + 2] = (sin(0.1 * ct_index + e->b) * 50 + 200);
}
}