@@ -0,0 +1,96 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akhramut <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/01 09:53:43 by akhramut #+# #+# */
/* Updated: 2017/02/01 19:15:54 by nmoiseye ### ########.fr */
/* */
/* ************************************************************************** */

#include "bsq.h"

#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

void output_map(t_map map)
{
write(1, map.data, (map.w + 1) * map.h);
}

char *read_fd(int fd, int *size)
{
char *data;
int bytes_read;
int data_size;
int offset;

data_size = 20000 * 20000;
data = malloc(data_size);
offset = 0;
while ((bytes_read = read(fd,
data + offset, data_size - offset)) > 0)
{
offset += bytes_read;
if (offset == data_size)
{
data = my_realloc(data, data_size, data_size * 2);
data_size *= 2;
}
}
*size = offset;
return (data);
}

void parse_fd(int fd)
{
char *data;
int size;
t_square box;
t_map map;

data = read_fd(fd, &size);
map = parse_legend(data, size);
if (is_valid_map(map))
{
box = parse_bsq(map);
mark_box(map, box);
output_map(map);
}
else
write(2, "map error\n", 10);
free(data);
}

void parse_file(char *filename)
{
int fd;

fd = open(filename, O_RDONLY);
if (fd > 0)
{
parse_fd(fd);
close(fd);
}
}

int main(int argc, char **argv)
{
int i;

if (argc >= 2)
{
i = 1;
while (i < argc)
{
parse_file(argv[i]);
++i;
}
}
else
parse_fd(0);
return (0);
}
@@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* bsq.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akhramut <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/01 09:47:09 by akhramut #+# #+# */
/* Updated: 2017/02/01 14:24:08 by nmoiseye ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef BSQ_H
# define BSQ_H

# include "damn_util.h"
# include "map_stuff.h"

char *g_filler[] = {
"Richter: Die monster. You don’t belong in this world!",
"Dracula: It was not by my hand that I am once again given flesh.",
"I was called here by humans who wish to pay me tribute.",
"Richter: Tribute!?!",
"You steal men’s souls and make them your slaves!",
"Dracula: Perhaps the same could be said of all religions...",
"Richter: Your words are as empty as your soul!",
"Mankind ill needs a savior such as you!",
"Dracula: What is a man? A miserable little pile of secrets.",
"But enough talk... Have at you!"
};

#endif
@@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* damn_util.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akhramut <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/01 11:33:41 by akhramut #+# #+# */
/* Updated: 2017/02/01 14:23:31 by nmoiseye ### ########.fr */
/* */
/* ************************************************************************** */

#include "damn_util.h"

#include <stdlib.h>

int index_of_free(char *s, char c, int n)
{
int i;

i = 0;
while (i < n && s[i] != '\n' && s[i] != c)
i++;
return (i);
}

char *my_realloc(char *data, int size, int new_size)
{
char *new_data;
int i;

new_data = malloc(new_size);
i = 0;
while (i < size && i < new_size)
{
new_data[i] = data[i];
i++;
}
free(data);
return (new_data);
}

int my_atoi(char *s)
{
int res;

res = 0;
while (*s >= '0' && *s <= '9')
res = res * 10 + *s++ - '0';
return (res);
}
@@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* damn_util.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nmoiseye <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/01 13:31:34 by nmoiseye #+# #+# */
/* Updated: 2017/02/01 14:08:43 by nmoiseye ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef DAMN_UTIL_H
# define DAMN_UTIL_H

char *my_realloc(char *data, int size, int new_size);
int index_of_free(char *s, char c, int n);
int my_atoi(char *s);

#endif
@@ -0,0 +1,122 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map_stuff.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akhramut <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/01 10:48:58 by akhramut #+# #+# */
/* Updated: 2017/02/01 19:26:12 by nmoiseye ### ########.fr */
/* */
/* ************************************************************************** */

#include "damn_util.h"
#include "map_stuff.h"

t_map parse_legend(char *data, int size)
{
t_map desc;
int i;

desc.w = 0;
i = 0;
desc.h = my_atoi(data);
while (data[i] >= '0' && data[i] <= '9')
i++;
desc.c_empty = data[i++];
desc.c_obstacle = data[i++];
desc.c_full = data[i++];
if (data[i++] != '\n')
return (desc);
desc.data = data + i;
if ((size - i) % desc.h)
return (desc);
desc.w = (size - i) / (desc.h) - 1;
return (desc);
}

int is_valid_map(t_map map)
{
int x;
int y;
char *pos;

if (map.c_empty == map.c_obstacle || map.c_obstacle == map.c_full
|| map.c_full == map.c_empty)
return (0);
y = 0;
while (y < map.h)
{
pos = map.data + y * (map.w + 1);
x = 0;
while (x < map.w)
if (pos[x] != map.c_empty && pos[x] != map.c_obstacle)
return (0);
else
x++;
if (pos[x] != '\n')
return (0);
y++;
}
return (1);
}

void mark_box(t_map map, t_square box)
{
int x;
int y;

y = 0;
while (y < box.w)
{
x = 0;
while (x < box.w)
{
map.data[(y + box.y) * (map.w + 1) + box.x + x] = map.c_full;
x++;
}
y++;
}
}

int find_square(t_map map, int x, int y, t_square *bsq)
{
int i;
int f;
int w;

i = 0;
w = bsq->w + 1;
while (i < w)
{
f = index_of_free(map.data + x + (y + i) *
(map.w + 1), map.c_obstacle, w);
if (f < w)
return (f + 1);
i++;
}
bsq->x = x;
bsq->y = y;
bsq->w = w;
return (0);
}

t_square parse_bsq(t_map map)
{
int y;
int x;
t_square bs;

bs.w = 0;
bs.x = 0;
bs.y = 0;
y = 0;
while (y < map.h - bs.w)
{
x = 0;
while (x < map.w - bs.w && y < map.h - bs.w)
x += find_square(map, x, y, &bs);
y++;
}
return (bs);
}
@@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map_stuff.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nmoiseye <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/01 13:24:08 by nmoiseye #+# #+# */
/* Updated: 2017/02/01 14:00:46 by nmoiseye ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef MAP_STUFF_H
# define MAP_STUFF_H

typedef struct s_map_descriptor {
int w;
int h;
char c_empty;
char c_obstacle;
char c_full;
char *data;
} t_map;
typedef struct s_square {
int w;
int x;
int y;
} t_square;

t_map parse_legend(char *data, int size);
int is_valid_map(t_map map);
t_square parse_bsq(t_map map);
void mark_box(t_map map, t_square box);

#endif