Skip to content

Commit 82f3fde

Browse files
committed
feat: update map parsing
1 parent 382477d commit 82f3fde

File tree

8 files changed

+237
-33
lines changed

8 files changed

+237
-33
lines changed

Makefile

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LIBFT = $(LIBFT_DIR)/libft.a
88
MLX = $(MLX_DIR)/libmlx.a
99
BUILD_DIR = build
1010

11-
SRCS = fdf.c window_management.c read_map.c
11+
SRCS = fdf.c window_management.c read_map.c libft_extra.c
1212
OBJS = $(addprefix $(BUILD_DIR)/,$(SRCS:.c=.o))
1313
CFLAGS = -Wall -Wextra -Werror -I$(INC_DIR) -I$(LIBFT_DIR) -I$(MLX_DIR)
1414
LDFLAGS = -L$(LIBFT_DIR) -L$(MLX_DIR)
@@ -18,6 +18,9 @@ RM = rm -rf
1818

1919
all: $(NAME)
2020

21+
debug: $(OBJS) $(LIBFT) $(MLX)
22+
$(CC) -g $(CFLAGS) $(OBJS) -o $(NAME) $(LDFLAGS) $(LDLIBS)
23+
2124
$(NAME): $(OBJS) $(LIBFT) $(MLX)
2225
$(CC) $(CFLAGS) $(OBJS) -o $(NAME) $(LDFLAGS) $(LDLIBS)
2326

fdf.c

+27-5
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,47 @@
66
/* By: ebabaogl <[email protected] +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/01/02 11:39:32 by ebabaogl #+# #+# */
9-
/* Updated: 2025/01/02 16:47:26 by ebabaogl ### ########.fr */
9+
/* Updated: 2025/01/03 16:55:03 by ebabaogl ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include "fdf.h"
1414
#include "window.h"
1515
#include "libft.h"
1616

17-
#include <stdio.h>
1817
#include <stdlib.h>
18+
#include <limits.h>
19+
#include <stdio.h>
20+
#include <unistd.h>
21+
unsigned int ft_atoi_hex(char *str);
22+
1923
int main()
2024
{
2125
//t_vars vars;
2226
//t_mlx mlx;
2327

24-
char *map = read_file("test_maps/42.fdf");
25-
printf("%s", map);
26-
free(map);
28+
// char *map = read_file("test_maps/42.fdf");
29+
// printf("%s", map);
30+
// free(map);
2731
//vars.mlx = &mlx;
2832
//if (init_win(vars.mlx) == -1)
33+
char *map_str = read_file("test_maps/42.fdf");
34+
unsigned long **map = map_string_to_arr_2d(map_str);
35+
write(1, "helloworld", 10);
36+
size_t i;
37+
size_t j;
38+
i = 0;
39+
j = 0;
40+
while(map[i])
41+
{
42+
while (map[i][j] != ULONG_MAX)
43+
{
44+
printf("i: %lu, ", i);
45+
printf("z: %lu, ", map[i][j] >> 32);
46+
printf("color: %lu\n", map[i][j] & COLOR_MASK);
47+
j++;
48+
}
49+
i++;
50+
}
2951
return (1);
3052
}

inc/fdf.h

+12-7
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,22 @@
66
/* By: ebabaogl <[email protected] +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2025/01/02 11:39:37 by ebabaogl #+# #+# */
9-
/* Updated: 2025/01/02 16:21:30 by ebabaogl ### ########.fr */
9+
/* Updated: 2025/01/03 16:42:07 by ebabaogl ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#ifndef FDF_H
1414
# define FDF_H
1515

1616
#define BUFFER_SIZE 30
17-
18-
# include "window.h"
19-
17+
#define COLOR_MASK 4294967295U
18+
#define DEFAULT_COLOR 0x00FFFFFF
19+
#include "window.h"
20+
#include <stddef.h>
2021
typedef struct s_vars
2122
{
2223
t_mlx *mlx;
24+
size_t line_len;
2325
} t_vars;
2426

2527
typedef struct s_point
@@ -30,7 +32,10 @@ typedef struct s_point
3032
int color;
3133
} t_point;
3234

33-
char *read_file(char *file_name);
34-
int init_win(t_mlx *mlx);
35-
35+
char *read_file(char *file_name);
36+
int init_win(t_mlx *mlx);
37+
unsigned int ft_atoi_hex(char *str);
38+
unsigned long point_to_ulong(char *point);
39+
unsigned long **map_string_to_arr_2d(char *whole_file);
40+
size_t ft_ptrarrlen(unsigned long *arr);
3641
#endif

libft_extra.c

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
11
#include "libft.h"
2+
#include <stdio.h>
23

3-
unsigned int ft_atoi_base(const char *str)
4+
unsigned int ft_atoi_hex(char *str)
5+
{
6+
unsigned int res;
7+
int val;
8+
9+
res = 0;
10+
while (*str)
11+
{
12+
if (ft_isdigit(*str))
13+
val = *str - '0';
14+
else if (*str >= 'a' && *str <= 'f')
15+
val = *str - 'a' + 10;
16+
else if (*str >= 'A' && *str <= 'F')
17+
val = *str - 'A' + 10;
18+
res = res * 16 + val;
19+
str++;
20+
}
21+
return (res);
22+
}
23+
24+
size_t ft_ptrarrlen(unsigned long *arr)
425
{
526
size_t i;
627

728
i = 0;
8-
while ()
29+
while (arr[i])
30+
i++;
31+
return (i);
932
}

read_map.c

+55-15
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "fdf.h"
66

77
#include <stdio.h>
8+
#include <limits.h>
89
char *read_file(char *file_name)
910
{
1011
int fd;
@@ -33,37 +34,76 @@ char *read_file(char *file_name)
3334
return (map);
3435
}
3536

37+
// input format "<z:decimalint>,0x<color:hexint>"
38+
// returns ULONG_MAX on error
39+
unsigned long point_to_ulong(char *point)
40+
{
41+
char **point_attrs;
42+
int z;
43+
unsigned int color;
44+
unsigned long ret;
45+
46+
point_attrs = ft_split(point, ',');
47+
if (!point_attrs)
48+
return (ULONG_MAX);
49+
z = ft_atoi(point_attrs[0]);
50+
if (!*(point_attrs + 1))
51+
color = DEFAULT_COLOR;
52+
else
53+
color = ft_atoi_hex(point_attrs[1] + 2);
54+
ret = (((unsigned long)z) << 32) | (color);
55+
free(point_attrs);
56+
return (ret);
57+
}
58+
59+
// returns NULL on error
3660
unsigned long *map_string_to_ulong_array(char *map_one_line)
3761
{
3862
size_t i;
39-
char *point_array;
63+
char **point_array;
4064
size_t final_len;
41-
unsigned long final_array;
65+
unsigned long *final_array;
4266

43-
i = 0;
4467
point_array = ft_split(map_one_line, ' ');
4568
if (!point_array)
46-
return (write(STDERR_FILENO, "allocation error", 16), NULL);
47-
final_len = ft_strlen(point_array);
48-
final_array = malloc(sizeof(unsigned long) * final_len);
69+
return (NULL);
70+
final_len = ft_ptrarrlen((unsigned long *)point_array);
71+
final_array = malloc(sizeof(unsigned long) * (final_len + 1));
4972
if (!final_array)
50-
return (write(STDERR_FILENO, "allocation error", 16), NULL);
73+
return (free(point_array), NULL);
74+
i = 0;
5175
while (point_array[i])
5276
{
53-
77+
final_array[i] = point_to_ulong(point_array[i]);
78+
i++;
5479
}
80+
final_array[final_len] = ULONG_MAX;
81+
return (free(point_array), final_array);
5582
}
5683

5784
unsigned long **map_string_to_arr_2d(char *whole_file)
5885
{
59-
char **map_2d;
86+
char **map_2d_str;
87+
unsigned long **map_2d_ul;
6088
int i;
61-
map_2d = ft_split(str_map, '\n');
62-
if (!map_2d)
63-
return (write(STDERR_FILENO, "allocation error", 16), NULL);
89+
size_t line_count;
90+
91+
map_2d_str = ft_split(whole_file, '\n');
92+
if (!map_2d_str)
93+
return (NULL);
94+
line_count = ft_ptrarrlen((unsigned long *)map_2d_str);
95+
map_2d_ul = malloc(sizeof(unsigned long *) * (line_count + 1));
96+
if (!map_2d_ul)
97+
return (free(map_2d_str), NULL);
98+
printf("line_count: %lu\n", line_count);
6499
i = 0;
65-
while (map_2d[i])
100+
while (map_2d_str[i])
66101
{
67-
102+
map_2d_ul[i] = map_string_to_ulong_array(map_2d_str[i]);
103+
i++;
68104
}
69-
}
105+
map_2d_ul[line_count] = '\0';
106+
return (free(map_2d_str), map_2d_ul);
107+
}
108+
109+
// 3,0xff 6,0xff00

test_maps/3x3.fdf

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
0 0 0 0 0
2+
0 0 0 0 0
3+
0 0 0 0 0

test_maps/42.fdf

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1-
0 0 0
2-
0 0 0
3-
0 0 0
1+
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2+
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3+
0 0 10 10 0 0 10 10 0 0 0 10 10 10 10 10 0 0 0
4+
0 0 10 10 0 0 10 10 0 0 0 0 0 0 0 10 10 0 0
5+
0 0 10 10 0 0 10 10 0 0 0 0 0 0 0 10 10 0 0
6+
0 0 10 10 10 10 10 10 0 0 0 0 10 10 10 10 0 0 0
7+
0 0 0 10 10 10 10 10 0 0 0 10 10 0 0 0 0 0 0
8+
0 0 0 0 0 0 10 10 0 0 0 10 10 0 0 0 0 0 0
9+
0 0 0 0 0 0 10 10 0 0 0 10 10 10 10 10 10 0 0
10+
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
11+
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 commit comments

Comments
 (0)