Skip to content

Commit 1110a57

Browse files
FindingMiGoclaude
andcommitted
cub3dから汎用ユーティリティ(exit_error, x_malloc, color.h, error.h)を移植
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 0abcb3f commit 1110a57

6 files changed

Lines changed: 129 additions & 1 deletion

File tree

‎Makefile‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ SRCS = ft_isalpha.c ft_isdigit.c ft_isalnum.c ft_isascii.c ft_isprint.c \
77
ft_substr.c ft_strjoin.c ft_strtrim.c ft_split.c ft_itoa.c \
88
ft_strmapi.c ft_striteri.c ft_putchar_fd.c ft_putstr_fd.c ft_putendl_fd.c \
99
ft_putnbr_fd.c \
10-
gnl.c
10+
gnl.c \
11+
exit_error.c x_malloc.c
1112

1213
SRCSB = ft_lstnew.c ft_lstadd_front.c ft_lstsize.c ft_lstlast.c ft_lstadd_back.c \
1314
ft_lstclear.c ft_lstdelone.c ft_lstiter.c ft_lstmap.c

‎color.h‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* color.h :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: user <user@student.42tokyo.jp> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2023/04/29 15:30:00 by user #+# #+# */
9+
/* Updated: 2023/04/29 15:30:00 by user ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#ifndef COLOR_H
14+
# define COLOR_H
15+
16+
/* Foreground color */
17+
# define F_RED "\x1b[31m"
18+
# define F_GREEN "\x1b[32m"
19+
# define F_YELLOW "\x1b[33m"
20+
# define F_BLUE "\x1b[34m"
21+
# define F_PINK "\x1b[35m"
22+
# define F_LIGHT_BLUE "\x1b[36m"
23+
# define F_WHITE "\x1b[37m"
24+
# define F_RESET "\x1b[39m"
25+
26+
/* Background color */
27+
# define B_RED "\x1b[41m"
28+
# define B_GREEN "\x1b[42m"
29+
# define B_YELLOW "\x1b[43m"
30+
# define B_BLUE "\x1b[44m"
31+
# define B_PINK "\x1b[45m"
32+
# define B_LIGHT_BLUE "\x1b[46m"
33+
# define B_GRAY "\x1b[47m"
34+
# define B_RESET "\x1b[49m"
35+
36+
/* Other decorations */
37+
# define UNDERLINE "\x1b[4m"
38+
# define BOLD "\x1b[1m"
39+
# define O_RESET "\x1b[0m"
40+
41+
#endif

‎error.h‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* error.h :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: user <user@student.42tokyo.jp> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2023/04/29 15:30:00 by user #+# #+# */
9+
/* Updated: 2023/04/29 15:30:00 by user ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#ifndef ERROR_H
14+
# define ERROR_H
15+
16+
# define MALLOC_ERROR "Malloc failed"
17+
# define ARG_ERROR "Number of arguments is different"
18+
# define CUB_EXTENSION_ERROR "Cubfile could not be loaded"
19+
# define CUB_OPEN_ERROR "Cubfile could not be open"
20+
# define CUB_FORMAT_ERROR "Cubfile is not the correct format"
21+
# define MAP_INIT_ERROR "Map is uninitialized"
22+
# define PATH_INIT_ERROR "Path is uninitialized"
23+
# define PATH_READ_ERROR "Path could not be read"
24+
# define PATH_OPEN_ERROR "Path could not be open"
25+
# define RGB_INIT_ERROR "RGB is uninitialized"
26+
# define RGB_INVALID_ERROR "RGB is invalid value"
27+
# define WALL_ERROR "Map was not walled off"
28+
# define MAP_INVALID_ERROR "Map is invalid value"
29+
30+
# define INIT_ERROR "Initialization failed"
31+
# define MAPSIZE_ERROR "The map is too big for the screen"
32+
33+
#endif

‎exit_error.c‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* exit_error.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: user <user@student.42tokyo.jp> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2023/04/29 15:30:00 by user #+# #+# */
9+
/* Updated: 2023/04/29 15:30:00 by user ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "libft.h"
14+
15+
/* Function to output an error message and exit the program */
16+
void exit_error(char *error_message)
17+
{
18+
ft_putstr_fd(F_RED, STDERR_FILENO);
19+
ft_putstr_fd("Error: ", STDERR_FILENO);
20+
ft_putstr_fd(F_RESET, STDERR_FILENO);
21+
ft_putendl_fd(error_message, STDERR_FILENO);
22+
exit(1);
23+
}

‎libft.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
# include <stdint.h>
1919
# include <limits.h>
2020

21+
# include "error.h"
22+
# include "color.h"
23+
2124
# ifndef BUFFER_SIZE
2225
# define BUFFER_SIZE 42
2326
# endif
@@ -91,4 +94,8 @@ char *get_next_line(int fd);
9194
void ft_gnl_free(char **p1, char **p2);
9295
char *ft_gnl_trim_until_end(char **line, ssize_t rc);
9396

97+
//utils
98+
void exit_error(char *error_message);
99+
void *x_malloc(size_t size);
100+
94101
#endif

‎x_malloc.c‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* x_malloc.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: user <user@student.42tokyo.jp> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2023/04/29 15:30:00 by user #+# #+# */
9+
/* Updated: 2023/04/29 15:30:00 by user ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "libft.h"
14+
15+
void *x_malloc(size_t size)
16+
{
17+
void *content;
18+
19+
content = malloc(size);
20+
if (content == NULL)
21+
exit_error(MALLOC_ERROR);
22+
return (content);
23+
}

0 commit comments

Comments
 (0)