fix: parsing work
This commit is contained in:
		
							
								
								
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,3 @@ | |||||||
|  | *.o | ||||||
|  | cub3D | ||||||
|  | *.swp | ||||||
							
								
								
									
										9
									
								
								main.c
									
									
									
									
									
								
							
							
						
						
									
										9
									
								
								main.c
									
									
									
									
									
								
							| @ -6,11 +6,12 @@ | |||||||
| /*   By: erey-bet <marvin@42.fr>                    +#+  +:+       +#+        */ | /*   By: erey-bet <marvin@42.fr>                    +#+  +:+       +#+        */ | ||||||
| /*                                                +#+#+#+#+#+   +#+           */ | /*                                                +#+#+#+#+#+   +#+           */ | ||||||
| /*   Created: 2023/04/26 12:44:55 by erey-bet          #+#    #+#             */ | /*   Created: 2023/04/26 12:44:55 by erey-bet          #+#    #+#             */ | ||||||
| /*   Updated: 2023/04/27 14:43:10 by erey-bet         ###   ########.fr       */ | /*   Updated: 2023/05/03 12:50:25 by cchauvet         ###   ########.fr       */ | ||||||
| /*                                                                            */ | /*                                                                            */ | ||||||
| /* ************************************************************************** */ | /* ************************************************************************** */ | ||||||
|  |  | ||||||
| #include "cube3D.h" | #include "cube3D.h" | ||||||
|  | #include "libftx/libftx.h" | ||||||
|  |  | ||||||
| int	main(int argc, char **argv) | int	main(int argc, char **argv) | ||||||
| { | { | ||||||
| @ -21,11 +22,11 @@ int	main(int argc, char **argv) | |||||||
| 		printf("No argument"); | 		printf("No argument"); | ||||||
| 		return (1); | 		return (1); | ||||||
| 	} | 	} | ||||||
| 	/*map = map_parsing(argv[1]); | 	map = map_parsing(argv[1]); | ||||||
| 	if (!map) | 	if (!map) | ||||||
| 	{ |  | ||||||
| 		return (2); | 		return (2); | ||||||
| 	}*/ | 	ft_printf("%S", map->map); | ||||||
|  | 	return (2); | ||||||
| 	(void)argv; | 	(void)argv; | ||||||
| 	map = malloc(sizeof(t_map)); | 	map = malloc(sizeof(t_map)); | ||||||
| 	map->map = malloc(sizeof(char *) * 5); | 	map->map = malloc(sizeof(char *) * 5); | ||||||
|  | |||||||
| @ -1,12 +1,83 @@ | |||||||
| #include "./parsing_private.h" | #include "./parsing_private.h" | ||||||
|  | #include "map.h" | ||||||
|  | #include <stddef.h> | ||||||
|  |  | ||||||
|  | static ssize_t get_nb_line(const char *path) | ||||||
|  | { | ||||||
|  | 	int		fd; | ||||||
|  | 	char	readed[1]; | ||||||
|  | 	size_t	i; | ||||||
|  |  | ||||||
|  | 	fd = open(path, O_RDONLY); | ||||||
|  | 	if (fd == -1) | ||||||
|  | 		return (-1); | ||||||
|  | 	i = 1; | ||||||
|  | 	while (read(fd, readed, 1)) | ||||||
|  | 	{ | ||||||
|  | 		if (readed[0] == '\n') | ||||||
|  | 			i++; | ||||||
|  | 	} | ||||||
|  | 	close(fd); | ||||||
|  | 	return (i); | ||||||
|  | } | ||||||
|  | static char	**read_map(const char *path) | ||||||
|  | { | ||||||
|  | 	int		fd; | ||||||
|  | 	size_t	i; | ||||||
|  | 	char	**map; | ||||||
|  |  | ||||||
|  | 	map = malloc(sizeof(char *) * (get_nb_line(path) + 1)); | ||||||
|  | 	if (map == NULL) | ||||||
|  | 		return (NULL); | ||||||
|  | 	fd = open(path, O_RDONLY); | ||||||
|  | 	if (fd == -1) | ||||||
|  | 	{ | ||||||
|  | 		free(map); | ||||||
|  | 		return (NULL); | ||||||
|  | 	} | ||||||
|  | 	i = 0; | ||||||
|  | 	map[i] = get_next_line(fd); | ||||||
|  | 	while (map[i] != NULL) | ||||||
|  | 	{ | ||||||
|  | 		map[i][ft_strlen(map[i]) - 1] = '\0'; | ||||||
|  | 		i++; | ||||||
|  | 		map[i] = get_next_line(fd); | ||||||
|  | 	} | ||||||
|  | 	return (map); | ||||||
|  | } | ||||||
|  |  | ||||||
| t_map	*map_parsing(const char *path) | t_map	*map_parsing(const char *path) | ||||||
| { | { | ||||||
|  | 	char	**file_content; | ||||||
|  | 	char	***header; | ||||||
|  | 	char	**body; | ||||||
|  | 	size_t	header_size; | ||||||
| 	t_map	*map; | 	t_map	*map; | ||||||
|  |  | ||||||
|  | 	if (parsing_meta(path)) | ||||||
|  | 		return (NULL); | ||||||
| 	map = malloc(sizeof(t_map)); | 	map = malloc(sizeof(t_map)); | ||||||
| 	if (map == NULL) | 	if (map == NULL) | ||||||
| 		return (NULL); | 		return (NULL); | ||||||
| 	(void)path; | 	file_content = read_map(path); | ||||||
|  | 	if (file_content == NULL) | ||||||
|  | 		return (NULL); | ||||||
|  | 	header = get_header((const char **) file_content, &header_size); | ||||||
|  | 	if (header_is_valid(header, map) == 0) | ||||||
|  | 	{ | ||||||
|  | 		ft_freer_tab_ultimate(1, file_content); | ||||||
|  | 		ft_freer_ultimate(1, map); | ||||||
|  | 		return (NULL); | ||||||
|  | 	} | ||||||
|  | 	//ici aussi header; | ||||||
|  | 	body = get_body((const char **) file_content, header_size); | ||||||
|  | 	ft_freer_tab_ultimate(1, file_content); | ||||||
|  | 	if (body_is_valid((const char **) body, map) == 0) | ||||||
|  | 	{ | ||||||
|  | 		ft_freer_ultimate(1, map); | ||||||
|  | 		// faudra free | ||||||
|  | 		return (NULL); | ||||||
|  | 	} | ||||||
|  | 	map->map = body; | ||||||
| 	return (map); | 	return (map); | ||||||
| } | } | ||||||
|  | |||||||
| @ -63,7 +63,7 @@ static int	map_is_in_one_part(const char **body) | |||||||
| { | { | ||||||
| 	size_t	y; | 	size_t	y; | ||||||
|  |  | ||||||
| 	y = ft_tablen((const void **) body); | 	y = ft_tablen((const void **) body) - 1; | ||||||
| 	while (body[y][0] == '\0' || ft_contain_only(body[y], ' ')) | 	while (body[y][0] == '\0' || ft_contain_only(body[y], ' ')) | ||||||
| 		y--; | 		y--; | ||||||
| 	while (y > 0) | 	while (y > 0) | ||||||
| @ -77,14 +77,15 @@ static int	map_is_in_one_part(const char **body) | |||||||
|  |  | ||||||
| static int	map_surround_check(char **body, size_t x, size_t y) | static int	map_surround_check(char **body, size_t x, size_t y) | ||||||
| { | { | ||||||
| 	if (ft_is_in(";1", body[y][x])) |  | ||||||
| 		return (0); |  | ||||||
| 	if (ft_is_in(" ", body[y][x])) |  | ||||||
| 		return (1); |  | ||||||
| 	if (y == 0 || body[y] == NULL) | 	if (y == 0 || body[y] == NULL) | ||||||
| 		return (1); | 		return (1); | ||||||
| 	if (x == 0 || body[y][x] == '\0') | 	if (x == 0 || body[y][x] == '\0') | ||||||
| 		return (1); | 		return (1); | ||||||
|  | 	if (ft_is_in("21", body[y][x])) | ||||||
|  | 		return (0); | ||||||
|  | 	if (ft_is_in(" ", body[y][x])) | ||||||
|  | 		return (1); | ||||||
|  | 	body[y][x] = '2'; | ||||||
| 	if (map_surround_check(body, x + 1, y)) | 	if (map_surround_check(body, x + 1, y)) | ||||||
| 		return (1); | 		return (1); | ||||||
| 	if (map_surround_check(body, x - 1, y)) | 	if (map_surround_check(body, x - 1, y)) | ||||||
| @ -129,28 +130,30 @@ int	body_is_valid(const char **body, t_map *map) | |||||||
| { | { | ||||||
| 	int	error; | 	int	error; | ||||||
|  |  | ||||||
|  | 	if (body == NULL) | ||||||
|  | 		return (0); | ||||||
| 	error = get_spawn_position(body, &map->spawn_x, &map->spawn_y); | 	error = get_spawn_position(body, &map->spawn_x, &map->spawn_y); | ||||||
| 	if (error == 1) | 	if (error == 1) | ||||||
| 	{ | 	{ | ||||||
| 		ft_eprintf("map: spawn position: multiple definition"); | 		ft_eprintf("map: spawn position: multiple definition"); | ||||||
| 		return (1); | 		return (0); | ||||||
| 	} | 	} | ||||||
| 	else if (error == 2) | 	else if (error == 2) | ||||||
| 	{ | 	{ | ||||||
| 		ft_eprintf("map: spawn position: not define"); | 		ft_eprintf("map: spawn position: not define"); | ||||||
| 		return (1); | 		return (0); | ||||||
| 	} | 	} | ||||||
| 	error = map_surround(body, map); | 	error = map_surround(body, map); | ||||||
| 	if (error) | 	if (error) | ||||||
| 	{ | 	{ | ||||||
| 		ft_eprintf("map: not surrounded"); | 		ft_eprintf("map: not surrounded"); | ||||||
| 		return (1); | 		return (0); | ||||||
| 	} | 	} | ||||||
| 	error = map_is_in_one_part(body); | 	error = map_is_in_one_part(body); | ||||||
| 	if (error) | 	if (error) | ||||||
| 	{ | 	{ | ||||||
| 		ft_eprintf("map: splitted"); | 		ft_eprintf("map: splitted"); | ||||||
| 		return (1); | 		return (0); | ||||||
| 	} | 	} | ||||||
| 	return (0); | 	return (1); | ||||||
| } | } | ||||||
|  | |||||||
| @ -33,6 +33,8 @@ int	header_is_valid(char ***header, t_map *map) | |||||||
| 	int		token; | 	int		token; | ||||||
|  |  | ||||||
|  |  | ||||||
|  | 	if (header == NULL) | ||||||
|  | 		return (1); | ||||||
| 	map->color_bot = -1; | 	map->color_bot = -1; | ||||||
| 	map->color_top = -1; | 	map->color_top = -1; | ||||||
| 	i = 0; | 	i = 0; | ||||||
|  | |||||||
| @ -5,17 +5,13 @@ static int	name_check(const char *path) | |||||||
| 	size_t	len; | 	size_t	len; | ||||||
|  |  | ||||||
| 	len = ft_strlen(path); | 	len = ft_strlen(path); | ||||||
| 	if (len < 4) | 	if (len < 4 | ||||||
|  | 		|| (ft_strcmp(".cub", path + len - 4) != 0)) | ||||||
| 	{ | 	{ | ||||||
| 		ft_eprintf("map error: name doesn't finished by .cub"); | 		ft_eprintf("map: name doesn't finished by .cub"); | ||||||
| 		return (1); | 		return (1); | ||||||
| 	} | 	} | ||||||
| 	if (ft_strcmp(".cub", path + len - 3) != 0) | 	return (0); | ||||||
| 	{ |  | ||||||
| 		ft_eprintf("map error: name doesn't finished by .cub"); |  | ||||||
| 		return (1); |  | ||||||
| 	} |  | ||||||
| 	return (1); |  | ||||||
| } | } | ||||||
|  |  | ||||||
| int	parsing_meta(const char *path) | int	parsing_meta(const char *path) | ||||||
|  | |||||||
							
								
								
									
										22
									
								
								maps/test1.cub
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								maps/test1.cub
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,22 @@ | |||||||
|  | NO ./path_to_the_north_texture | ||||||
|  | SO ./path_to_the_south_texture | ||||||
|  | WE ./path_to_the_west_texture | ||||||
|  | EA ./path_to_the_east_texture | ||||||
|  |  | ||||||
|  | F 220,100,0 | ||||||
|  | C 225,30,0 | ||||||
|  |  | ||||||
|  |         1111111111111111111111111 | ||||||
|  |         1000000000110000000000001 | ||||||
|  |         1011000001110000000000001 | ||||||
|  |         1001000000000000000000001 | ||||||
|  | 111111111011000001110000000000001 | ||||||
|  | 100000000011000001110111111111111 | ||||||
|  | 11110111111111011100000010001 | ||||||
|  | 11110111111111011101010010001 | ||||||
|  | 11000000110101011100000010001 | ||||||
|  | 10000000000000001100000010001 | ||||||
|  | 10000000000000001101010010001 | ||||||
|  | 11000001110101011111011110N0111 | ||||||
|  | 11110111 1110101 101111010001 | ||||||
|  | 11111111 1111111 111111111111 | ||||||
							
								
								
									
										22
									
								
								maps/test_not_surround.cub
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								maps/test_not_surround.cub
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,22 @@ | |||||||
|  | NO ./path_to_the_north_texture | ||||||
|  | SO ./path_to_the_south_texture | ||||||
|  | WE ./path_to_the_west_texture | ||||||
|  | EA ./path_to_the_east_texture | ||||||
|  |  | ||||||
|  | F 220,100,0 | ||||||
|  | C 225,30,0 | ||||||
|  |  | ||||||
|  |         1111111111111111111111111 | ||||||
|  |         1000000000110000000000001 | ||||||
|  |         1011000001110000000000001 | ||||||
|  |         1001000000000000000000001 | ||||||
|  | 111111111011000001110000000000001 | ||||||
|  | 100000000011000001110111111111111 | ||||||
|  | 11110111111111011100000010001 | ||||||
|  | 11110111111111011101010010001 | ||||||
|  | 11000000110101011100000010001 | ||||||
|  | 10000000000000001100000010001 | ||||||
|  | 10000000000000001101010010001 | ||||||
|  | 11000001110101011111011110N00 | ||||||
|  | 11110111 1110101 101111010001 | ||||||
|  | 11111111 1111111 111111111111 | ||||||
							
								
								
									
										23
									
								
								maps/test_splitted.cub
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								maps/test_splitted.cub
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,23 @@ | |||||||
|  | NO ./path_to_the_north_texture | ||||||
|  | SO ./path_to_the_south_texture | ||||||
|  | WE ./path_to_the_west_texture | ||||||
|  | EA ./path_to_the_east_texture | ||||||
|  |  | ||||||
|  | F 220,100,0 | ||||||
|  | C 225,30,0 | ||||||
|  |  | ||||||
|  |         1111111111111111111111111 | ||||||
|  |         1000000000110000000000001 | ||||||
|  |         1011000001110000000000001 | ||||||
|  |         1001000000000000000000001 | ||||||
|  | 111111111011000001110000000000001 | ||||||
|  |  | ||||||
|  | 100000000011000001110111111111111 | ||||||
|  | 11110111111111011100000010001 | ||||||
|  | 11110111111111011101010010001 | ||||||
|  | 11000000110101011100000010001 | ||||||
|  | 10000000000000001100000010001 | ||||||
|  | 10000000000000001101010010001 | ||||||
|  | 11000001110101011111011110N0111 | ||||||
|  | 11110111 1110101 101111010001 | ||||||
|  | 11111111 1111111 111111111111 | ||||||
		Reference in New Issue
	
	Block a user