-
Notifications
You must be signed in to change notification settings - Fork 0
/
open_map.c
65 lines (60 loc) · 1.24 KB
/
open_map.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
** EPITECH PROJECT, 2021
** open_map.c
** File description:
** open_map
*/
#include "my.h"
#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int count_game_objects(char *map)
{
int count = 0;
for (int i = 0; map[i] != '\0'; i++) {
if (map[i] != '\n' && map[i] != '\0' && map[i] != ' ')
count++;
}
return count;
}
int open_file(char const *filepath)
{
int fd = open(filepath, O_RDONLY);
return fd;
}
int get_size(char const *filepath)
{
char *buf = NULL;
int size = 0;
size_t buf_size = 0;
ssize_t line_size = 0;
FILE *fp = fopen(filepath, "r");
while (line_size >= 0) {
line_size = getline(&buf, &buf_size, fp);
size += line_size;
}
size += 2;
free(buf);
fclose(fp);
return size;
}
char *load_file(char const *filepath)
{
int output = 0;
char *buffer = NULL;
int size = get_size(filepath);
int fd = open_file(filepath);
if (fd == -1)
return NULL;
buffer = malloc(size * sizeof(char));
output = read(fd, buffer, size);
if (output == -1)
return NULL;
buffer[size - 1] = '\0';
close(fd);
return buffer;
}