tgrekov-fdf
HIVE fdf May 2024
Loading...
Searching...
No Matches
read_map.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* read_map.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: tgrekov <tgrekov@student.hive.fi> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/05/20 07:22:24 by tgrekov #+# #+# */
9/* Updated: 2024/06/24 08:55:47 by tgrekov ### ########.fr */
10/* */
11/* ************************************************************************** */
12
20#include <stdlib.h>
21#include <stdio.h>
22#include <errno.h>
23#include <libft.h>
24#include <ft_printf.h>
25#include <get_next_line_bonus.h>
26#include "utils/utils.h"
27#include "fdf/map.h"
28
35static void lst_arr_free(void *arr)
36{
37 arr_free((void **) arr);
38}
39
48static char *prep_row(char *row, t_list **lst)
49{
50 int row_len;
51
52 if (!row)
53 {
54 if (!*lst)
55 {
56 if (!errno)
57 ft_printf("%>File is empty\n", 2);
58 else
59 perror("get_next_line()");
60 }
61 return (0);
62 }
63 row_len = ft_strlen(row);
64 if (row[row_len - 1] == '\n')
65 row[row_len - 1] = 0;
66 return (row);
67}
68
76static void split_rows(int fd, t_list **lst)
77{
78 char *row;
79 char **split;
80 t_list *new_row;
81
82 *lst = 0;
83 while (1)
84 {
85 row = prep_row(get_next_line(fd), lst);
86 if (!row && !*lst)
87 return ;
88 if (!row)
89 break ;
90 split = ft_split(row, ' ');
91 free(row);
92 if (!split)
93 return (perror("malloc()"));
94 new_row = ft_lstnew(split);
95 if (!new_row)
96 {
98 return (perror("malloc()"));
99 }
100 ft_lstadd_back(lst, new_row);
101 }
102}
103
112static int fill_points(t_map *map, t_list *lst)
113{
114 int x;
115 int y;
116
117 y = 0;
118 while (lst)
119 {
120 x = arr_len(lst->content);
121 if (map->width != x)
122 {
123 ft_printf("%>Inconsistent width on row %d\n", 2, y + 1);
124 arr_free((void **) map->point);
125 return (1);
126 }
127 map->point[y] = malloc(map->width * sizeof(t_point));
128 if (!map->point[y])
129 {
130 arr_free((void **) map->point);
131 return (*(int *) err("malloc() failed", &x));
132 }
133 while (x--)
134 map->point[y][x].height = ft_atoi(((char **) lst->content)[x]);
135 y++;
136 lst = lst->next;
137 }
138 return (0);
139}
140
148{
149 t_map map;
150 t_list *lst;
151
152 map.point = 0;
153 split_rows(fd, &lst);
154 if (!lst)
155 return (map);
156 map.height = ft_lstsize(lst);
157 map.width = arr_len(lst->content);
158 if (map.height == 1 && map.width == 1)
159 {
161 return (*(t_map *) err("Cannot draw line from 1 point", (void *) &map));
162 }
163 map.point = ft_calloc(map.height + 1, sizeof(t_point **));
164 if (!map.point)
165 {
167 return (*(t_map *) err("malloc()", (void *) &map));
168 }
169 if (fill_points(&map, lst))
170 map.point = 0;
172 return (map);
173}
void ** arr_free(void **arr)
Free each element in a null-terminated array, and then the array itself.
Definition arr_free.c:30
int arr_len(void **arr)
Count number of elements in a null-terminated array.
Definition arr_len.c:26
int ft_printf(const char *format,...)
Prints and formats a variable set of arguments.
Definition ft_printf.c:252
void * err(const char *str, void *retval)
Wrapper around perror() that always returns retval.
Definition err.c:29
int ft_atoi(const char *str)
Converts ASCII string str to integer representation.
Definition ft_atoi.c:42
void * ft_calloc(size_t count, size_t size)
Allocates count * size bytes with malloc and returns a pointer to the result.
Definition ft_calloc.c:34
void ft_lstadd_back(t_list **lst, t_list *new)
Adds node at pointer new to the end of the list starting at pointer pointed to by lst.
void ft_lstclear(t_list **lst, void(*del)(void *))
Delete every node in a list along with it's content.
t_list * ft_lstnew(void *content)
Allocates a new node, set it's content, and return it's address.
int ft_lstsize(t_list *lst)
Counts the number of nodes in a list.
char ** ft_split(char const *s, char c)
Split string s by characer c into allocated array of strings, with NULL as the last element.
Definition ft_split.c:72
size_t ft_strlen(const char *str)
Get length of str.
Definition ft_strlen.c:28
char * get_next_line(int fd)
Return the next segment of text ending in a newline or EOF from file descriptor fd.
static char * prep_row(char *row, t_list **lst)
Remove trailing newline from row or print appropriate error message.
Definition read_map.c:48
t_map read_map(int fd)
Initialize map from an fdf map file pointed to by fd.
Definition read_map.c:147
static int fill_points(t_map *map, t_list *lst)
Allocate map points and set height values from lst.
Definition read_map.c:112
static void lst_arr_free(void *arr)
Wrapper around arr_free for use with ft_lstclear.
Definition read_map.c:35
static void split_rows(int fd, t_list **lst)
Read rows from fd and place them into lst nodes, split on spaces.
Definition read_map.c:76
Linked list node.
Definition libft.h:44
struct s_list * next
Pointer to the next node.
Definition libft.h:46
void * content
void* to the node's contents.
Definition libft.h:45
Map data.
Definition map.h:54
int width
Number of columns in the map.
Definition map.h:55
int height
Number of rows in the map.
Definition map.h:56
t_point ** point
2d array of points in [y][x] order
Definition map.h:57
Map point.
Definition map.h:36
int height
Height extracted from map file.
Definition map.h:37