Archived
1
0
This commit is contained in:
Adam Joly
2024-01-04 16:36:32 +01:00
parent 32983af238
commit fb3fae1e1a
27 changed files with 432 additions and 261 deletions

BIN
a.out

Binary file not shown.

BIN
fdf.h.gch

Binary file not shown.

BIN
font.ttf

Binary file not shown.

@ -6,12 +6,11 @@
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 17:11:59 by adjoly #+# #+# */
/* Updated: 2023/12/23 06:51:37 by adjoly ### ########.fr */
/* Updated: 2024/01/04 00:45:17 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include "../libft/libft.h"
char check_line(char *res, char *buf)
{
@ -57,11 +56,11 @@ char *get_next_line(int fd)
if (BUFFER_SIZE <= 0 || fd < 0 || fd > 1023)
return (NULL);
if (!buf)
buf = ft_calloc(sizeof(char), BUFFER_SIZE + 1);
res = ft_calloc(1, 1);
buf = ft_calloc_gnl(sizeof(char), BUFFER_SIZE + 1);
res = ft_calloc_gnl(1, 1);
while (buf)
{
res = ft_strjoinf(res, buf);
res = ft_strjoin_gnl(res, buf);
if (!res)
return (NULL);
if (check_line(res, buf))

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 17:12:00 by adjoly #+# #+# */
/* Updated: 2023/12/23 06:51:07 by adjoly ### ########.fr */
/* Updated: 2024/01/04 00:38:03 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,8 +21,8 @@
# endif
char *get_next_line(int fd);
char *ft_strjoinf(char *s1, char *s2);
// size_t ft_strlen(char *s);
// void *ft_calloc(size_t nmemb, size_t size);
char *ft_strjoin_gnl(char *s1, char *s2);
size_t ft_strlen_gnl(char *s);
void *ft_calloc_gnl(size_t nmemb, size_t size);
#endif

Binary file not shown.

@ -1,74 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 17:11:59 by adjoly #+# #+# */
/* Updated: 2023/12/15 05:51:22 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
char check_line(char *res, char *buf)
{
int i;
int j;
i = 0;
j = 0;
while (res[i] && res[i] != '\n')
i++;
if (res[i] == '\n')
{
i++;
while (res[i])
{
buf[j] = res[i];
i++;
j++;
}
buf[j] = 0;
res[i - j] = 0;
return (1);
}
return (0);
}
char *ft_read_error(char **buf, char *res)
{
free(*buf);
*buf = NULL;
if (res[0] != 0)
return (res);
free(res);
return (NULL);
}
char *get_next_line(int fd)
{
static char *buf[1024];
char *res;
ssize_t bytes_read;
if (BUFFER_SIZE <= 0 || fd < 0 || fd > 1023)
return (NULL);
if (!buf[fd])
buf[fd] = ft_calloc(sizeof(char), BUFFER_SIZE + 1);
res = ft_calloc(1, 1);
while (buf[fd] && res)
{
res = ft_strjoin(res, buf[fd]);
if (!res)
return (NULL);
if (check_line(res, buf[fd]))
return (res);
bytes_read = read(fd, buf[fd], BUFFER_SIZE);
if (bytes_read < 1)
return (ft_read_error(&buf[fd], res));
buf[fd][bytes_read] = 0;
}
return (NULL);
}

Binary file not shown.

@ -6,24 +6,24 @@
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 17:12:02 by adjoly #+# #+# */
/* Updated: 2023/12/23 06:52:24 by adjoly ### ########.fr */
/* Updated: 2024/01/04 00:47:44 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include "../libft/libft.h"
// size_t ft_strlen(char *s)
// size_t ft_strlen_gnl(char *s)
// {
// size_t i;
//
// i = 0;
// while (s[i])
// i++;
// return (i);
// }
char *ft_strjoinf(char *s1, char *s2)
char *ft_strjoin_gnl(char *s1, char *s2)
{
char *result;
size_t i;
@ -33,7 +33,7 @@ char *ft_strjoinf(char *s1, char *s2)
j = 0;
if (!s2)
return (NULL);
result = ft_calloc((ft_strlen(s1) + ft_strlen(s2) + 1), sizeof(char));
result = ft_calloc_gnl((ft_strlen(s1) + ft_strlen(s2) + 1), 1);
if (result == NULL)
return (NULL);
while (s1[i])
@ -52,7 +52,7 @@ char *ft_strjoinf(char *s1, char *s2)
return (result);
}
void *ft_calloc(size_t nmemb, size_t size)
void *ft_calloc_gnl(size_t nmemb, size_t size)
{
char *result;
size_t i;

@ -1,75 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 17:12:02 by adjoly #+# #+# */
/* Updated: 2023/12/15 05:42:57 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
size_t ft_strlen(char *s)
{
size_t i;
i = 0;
while (s[i])
i++;
return (i);
}
char *ft_strjoin(char *s1, char *s2)
{
char *result;
size_t i;
size_t j;
i = 0;
j = 0;
if (!s2)
return (NULL);
result = ft_calloc((ft_strlen(s1) + ft_strlen(s2) + 1), sizeof(char));
if (result == NULL)
return (NULL);
while (s1[i])
{
result[i] = s1[i];
i++;
}
while (s2[j])
{
result[i] = s2[j];
i++;
j++;
}
free(s1);
result[i] = '\0';
return (result);
}
void *ft_calloc(size_t nmemb, size_t size)
{
char *result;
size_t i;
i = 0;
if (nmemb == 0 || size == 0)
return (malloc(1));
if (((unsigned long long)(size * nmemb) > 4294967295))
return (NULL);
if ((int)size < 0 && (int)nmemb < 0)
return (NULL);
result = malloc(size * nmemb);
if (!result)
return (NULL);
while (i < (size * nmemb))
{
*(unsigned char *)(result + i) = '\0';
i++;
}
return (result);
}

@ -6,82 +6,30 @@
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/12 05:19:39 by adjoly #+# #+# */
/* Updated: 2023/12/23 08:29:30 by adjoly ### ########.fr */
/* Updated: 2024/01/04 16:22:01 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
#include "libft/libft.h"
#include <limits.h>
#include <stddef.h>
#include <stdio.h>
#include "printf/ft_printf.h"
void ft_putstr(char *s){int i = 0;while(s[i]){write(1, &s[i], 1);i++;}}
// void ft_putstr(char *s){int i = 0;while(s[i]){write(1, &s[i], 1);i++;}}
size_t ft_countline_fd(int fd)
void ft_free(void **map)
{
size_t i;
ssize_t rd;
size_t count;
char *buf;
int i;
i = 0;
count = 1;
buf = ft_calloc(1, 1);
while (i <= ULONG_MAX)
{
rd = read(fd, buf, 1);
if (rd == -1)
break ;
else if (buf[0] == '\n')
count++;
i++;
}
if (i == 0)
return (-1);
return (count);
}
size_t ft_countline(char **map)
{
size_t i;
while (map[i])
i++;
return (i);
}
int ft_read_map(int fd, char **map)
{
size_t i;
i = 0;
while (map[i] && i <= ULONG_MAX)
{
map[i] = get_next_line(fd);
free(map[i]);
i++;
}
if (i == 0 && map[i] == NULL)
return (-1);
close(fd);
map[i + 1] = NULL;
return (i);
}
char **ft_getmap(const char *file)
{
char **map;
int fd;
int line_nb;
printf("mais çá fait quelque chose au moins\n");
fd = open(file, O_RDONLY);
printf("file opened");
line_nb = ft_countline_fd(fd);
close(fd);
map = ft_calloc(sizeof(char), line_nb);
printf("linenb %d", line_nb);
fd = open(file, O_RDONLY);
ft_read_map(fd, map);
return (map);
free(map);
}
t_map ft_split_height_color(char *tmp)
@ -93,57 +41,130 @@ t_map ft_split_height_color(char *tmp)
while (tmp[i] && tmp[i] != ',')
i++;
if (tmp[i] == ',')
{
i++;
height_color.color = ft_strdup(&tmp[i]);
return (height_color);
}
height_color.color = ft_strdup(&tmp[i + 1]);
else
{
height_color.color = ft_strdup("0xFFFFFF");
return (height_color);
}
return (height_color);
}
t_map **ft_parse_map(char **mapfile)
size_t ft_countline_fd(int fd)
{
size_t i;
ssize_t rd;
size_t count;
char *buf;
i = 0;
count = 1;
buf = ft_calloc(1, 1);
while (i < ULONG_MAX)
{
rd = read(fd, buf, 1);
if (rd == -1 || rd == 0)
break ;
else if (buf[0] == '\n')
count++;
i++;
}
free(buf);
if (i == 0)
return (-1);
return (count);
}
size_t ft_countline(char **map)
{
size_t i;
i = 0;
while (map[i])
i++;
return (i);
}
t_map **ft_parse_map(char **mapfile, size_t line_count)
{
int z;
int x;
int line_count;
char **tmp;
t_map **parsed_map;
size_t i;
z = 0;
line_count = ft_countline(mapfile);
parsed_map = ft_calloc(sizeof(t_map), line_count);
parsed_map = ft_calloc(sizeof(t_map *), line_count);
while (mapfile[z])
{
tmp = ft_split(mapfile[z], 32);
i = ft_countline(tmp);
parsed_map[z] = ft_calloc(i, sizeof(t_map *));
x = 0;
while (tmp[x])
{
parsed_map[z][x] = ft_split_height_color(tmp[x]);
x++;
}
parsed_map[z][x] = NULL;
z++;
}
parsed_map[z] = NULL;
return (parsed_map);
}
char **ft_read_map(int fd, size_t map_size)
{
size_t i;
char **map;
i = 0;
map = ft_calloc(sizeof(char *), map_size + 1);
while (i < map_size)
{
map[i] = get_next_line(fd);
if (map[i] == NULL)
break ;
i++;
}
return (map);
}
t_map **ft_getmap(char *file)
{
t_map **map_parsed;
char **map;
int fd;
size_t line_nb;
fd = open(file, O_RDONLY);
line_nb = ft_countline_fd(fd);
close(fd);
fd = open(file, O_RDONLY);
map = ft_read_map(fd, line_nb);
close(fd);
map_parsed = ft_parse_map(map, line_nb);
ft_free((void **)map);
return (map_parsed);
}
int main(int ac, char **av)
{
char **map;
t_map **map;
int i = 0;
printf("%s", av[1]);
map = ft_getmap(av[1]);
char *file_name;
file_name = ft_strdup(av[1]);
map = ft_getmap(file_name);
(void)ac;
ft_putstr("map read");
ft_putstr("map read\n");
while (map[i])
{
ft_putstr(map[i]);
free(map[i]);
ft_printf("%s", map[i]->color);
ft_putchar('\n');
ft_putnbr(map[i]->y);
ft_putchar('\n');
i++;
}
free(map);
ft_free((void **)map);
return(0);
}
//cc maps_reader.c fdf.h get_next_line/get_next_line.c get_next_line/get_next_line.h get_next_line/get_next_line_utils.c libft/libft.a

1
printf/.gitignore vendored Normal file

@ -0,0 +1 @@
printfTester/

46
printf/Makefile Normal file

@ -0,0 +1,46 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/11/17 12:35:27 by adjoly #+# #+# #
# Updated: 2023/11/20 15:55:52 by adjoly ### ########.fr #
# #
# **************************************************************************** #
NAME = libftprintf.a
CC = cc
SRCS = ft_printf.c \
ft_putchar.c \
ft_putnbr.c \
ft_putnbrbase.c \
ft_putstr.c \
ft_strlen.c \
OBJS = $(SRCS:.c=.o)
FLAGS = -Werror -Wall -Wextra
HEADER = libftprintf.h
$(NAME): $(OBJS)
ar -rcs $(NAME) $(OBJS)
%.o: %.c
$(CC) $(FLAGS) -I $(HEADER) $< -c -o $@
all: $(NAME)
clean:
rm -f $(OBJS)
fclean: clean
rm -f $(NAME)
re: fclean all
.PHONY: clean all re fclean

92
printf/ft_printf.c Normal file

@ -0,0 +1,92 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/17 16:48:37 by adjoly #+# #+# */
/* Updated: 2023/12/06 14:27:05 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_putaddr(void *ptr)
{
int r;
if (ptr == NULL)
return (write(1, "(nil)", 5));
write(1, "0x", 2);
r = ft_putnbrbase_p((long unsigned int)ptr, "0123456789abcdef");
return (2 + r);
}
int ft_putnbrulong(unsigned int n)
{
int len;
len = 0;
if (n < 10)
len += write(1, &(char){n + '0'}, 1);
else
{
len += ft_putnbrulong(n / 10);
len += write(1, &(char){n % 10 + '0'}, 1);
}
return (len);
}
int ft_printconversion(char conversion, va_list args)
{
int count;
count = 0;
if (conversion == '%')
count = ft_putchar('%');
else if (conversion == 's')
count = ft_putstr(va_arg(args, char *));
else if (conversion == 'c')
count = ft_putchar(va_arg(args, int));
else if (conversion == 'i' || conversion == 'd')
count = ft_putnbr(va_arg(args, int));
else if (conversion == 'u')
count = ft_putnbrulong(va_arg(args, unsigned int));
else if (conversion == 'p')
count = ft_putaddr(va_arg(args, void *));
else if (conversion == 'x')
count = ft_putnbrbase(va_arg(args, unsigned long), "0123456789abcdef");
else if (conversion == 'X')
count = ft_putnbrbase(va_arg(args, unsigned long), "0123456789ABCDEF");
return (count);
}
int ft_printf(const char *format, ...)
{
int i;
va_list args;
int count;
va_start(args, format);
count = 0;
i = 0;
if (format == NULL)
return (-1);
while (format[i])
{
if (format[i] == '%')
{
i++;
if (format[i])
count += ft_printconversion(format[i], args);
else
return (-1);
}
else
count += ft_putchar(format[i]);
i++;
}
va_end(args);
return (count);
}

33
printf/ft_printf.h Normal file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/17 16:50:36 by adjoly #+# #+# */
/* Updated: 2023/11/22 14:00:38 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_PRINTF_H
# define FT_PRINTF_H
# include <stdlib.h>
# include <stdarg.h>
# include <unistd.h>
int ft_printf(const char *format, ...);
int ft_printconversion(char conversion, va_list args);
int ft_putnbrulong(unsigned int n);
int ft_putaddr(void *ptr);
int ft_putstr(char *s);
int ft_putnbrbase_p(unsigned long int n, char *base);
int ft_putnbrbase(unsigned int n, char *base);
int ft_putchar(char c);
int ft_putnbr(int n);
size_t ft_strlen(const char *s);
#endif

BIN
printf/ft_printf.o Normal file

Binary file not shown.

19
printf/ft_putchar.c Normal file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/18 10:49:00 by adjoly #+# #+# */
/* Updated: 2023/11/22 11:07:22 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_putchar(char c)
{
write(1, &c, 1);
return (1);
}

BIN
printf/ft_putchar.o Normal file

Binary file not shown.

36
printf/ft_putnbr.c Normal file

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/31 11:52:46 by adjoly #+# #+# */
/* Updated: 2023/11/22 10:51:24 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_putnbr(int n)
{
unsigned int nbr;
int len;
len = 0;
if (n < 0)
{
len += write(1, "-", 1);
nbr = -n;
}
else
nbr = n;
if (nbr < 10)
len += write(1, &(char){nbr + '0'}, 1);
else
{
len += ft_putnbr(nbr / 10);
len += write(1, &(char){nbr % 10 + '0'}, 1);
}
return (len);
}

BIN
printf/ft_putnbr.o Normal file

Binary file not shown.

47
printf/ft_putnbrbase.c Normal file

@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbrbase.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/18 10:57:44 by adjoly #+# #+# */
/* Updated: 2023/11/22 14:02:37 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_putnbrbase(unsigned int n, char *base)
{
unsigned int base_len;
int len;
len = 0;
base_len = (int)ft_strlen(base);
if (n < base_len)
len += write(1, &base[n % base_len], 1);
else
{
len += ft_putnbrbase(n / base_len, base);
len += write(1, &base[n % base_len], 1);
}
return (len);
}
int ft_putnbrbase_p(unsigned long int n, char *base)
{
unsigned long int base_len;
int len;
len = 0;
base_len = (int)ft_strlen(base);
if (n < base_len)
len += write(1, &base[n % base_len], 1);
else
{
len += ft_putnbrbase_p(n / base_len, base);
len += write(1, &base[n % base_len], 1);
}
return (len);
}

BIN
printf/ft_putnbrbase.o Normal file

Binary file not shown.

@ -1,28 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.h :+: :+: :+: */
/* ft_putstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 17:12:00 by adjoly #+# #+# */
/* Updated: 2023/12/15 05:43:09 by adjoly ### ########.fr */
/* Created: 2023/10/31 11:45:55 by adjoly #+# #+# */
/* Updated: 2023/12/06 15:51:38 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef GET_NEXT_LINE_BONUS_H
# define GET_NEXT_LINE_BONUS_H
#include "ft_printf.h"
# include <unistd.h>
# include <stdlib.h>
int ft_putstr(char *s)
{
int i;
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 5
# endif
char *get_next_line(int fd);
char *ft_strjoin(char *s1, char *s2);
size_t ft_strlen(char *s);
void *ft_calloc(size_t nmemb, size_t size);
#endif
i = 0;
if (s == NULL)
{
write(1, "(null)", 6);
return (6);
}
while (s[i])
{
write(1, &s[i], 1);
i++;
}
return (i);
}

BIN
printf/ft_putstr.o Normal file

Binary file not shown.

23
printf/ft_strlen.c Normal file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/30 18:15:57 by adjoly #+# #+# */
/* Updated: 2023/11/20 15:51:51 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
size_t ft_strlen(const char *s)
{
int i;
i = 0;
while (s[i])
i++;
return (i);
}

BIN
printf/ft_strlen.o Normal file

Binary file not shown.

BIN
printf/libftprintf.a Normal file

Binary file not shown.