Skip to content

Commit

Permalink
Print map
Browse files Browse the repository at this point in the history
  • Loading branch information
DeltaEvo committed Apr 27, 2019
1 parent b9b3e19 commit e318cfd
Showing 10 changed files with 212 additions and 54 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
# By: dde-jesu <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2018/11/30 10:00:57 by dde-jesu #+# #+# #
# Updated: 2019/03/20 09:41:21 by dde-jesu ### ########.fr #
# Updated: 2019/04/27 10:42:27 by dde-jesu ### ########.fr #
# #
# **************************************************************************** #

@@ -21,7 +21,7 @@ CFLAGS = -Wall -Wextra -Iinclude -g -fsanitize=address
PRECOMPILE = @mkdir -p $(dir $@)
POSTCOMPILE =

ifdef DEPS
ifndef NODEPS

DEPFLAGS = -MT $@ -MMD -MP -MF $(DEP_DIR)/$*.Td
CFLAGS += $(DEPFLAGS)
24 changes: 17 additions & 7 deletions include/lem_in.h
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
/* By: dde-jesu <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/20 09:03:08 by dde-jesu #+# #+# */
/* Updated: 2019/04/26 14:44:55 by dde-jesu ### ########.fr */
/* Updated: 2019/04/27 10:06:45 by dde-jesu ### ########.fr */
/* */
/* ************************************************************************** */

@@ -34,24 +34,34 @@

struct s_room {
char *name;
char *comments;
int32_t x;
int32_t y;
bool end;
struct s_room_vec *links;
bool mark;
};

struct s_room_ptr {
struct s_room *ptr;
char *comments;
};

struct s_room_vec {
size_t len;
size_t capacity;
struct s_room *rooms[];
size_t len;
size_t capacity;
struct s_room_ptr rooms[];
};

struct s_anthil {
size_t ants;
struct s_room *start;
char *start_comments;
char *end_comments;
size_t ants;
struct s_room *start;
};

struct s_room **add_room(struct s_room_vec **vec);
struct s_room_ptr *add_room(struct s_room_vec **vec);
struct s_room_vec *create_vec(size_t capacity);
void print_anthil(struct s_anthil anthil);

#endif
5 changes: 4 additions & 1 deletion include/mem.h
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
/* By: dde-jesu <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/20 09:16:01 by dde-jesu #+# #+# */
/* Updated: 2019/04/26 14:01:44 by dde-jesu ### ########.fr */
/* Updated: 2019/04/27 09:31:32 by dde-jesu ### ########.fr */
/* */
/* ************************************************************************** */

@@ -18,5 +18,8 @@
void *ft_memcpy(void *dst, const void *src, size_t n);
void *ft_realloc(void *o_ptr, size_t oldsize, size_t newsize);
int ft_strcmp(const char *s1, const char *s2);
size_t ft_strlen(const char *s);
char *ft_strchr(const char *s, int c);
int ft_memcmp(const void *s1, const void *s2, size_t n);

#endif
1 change: 1 addition & 0 deletions src.mk
Original file line number Diff line number Diff line change
@@ -5,3 +5,4 @@ SRCS += src/parser.c
SRCS += src/reader.c
SRCS += src/str.c
SRCS += src/vec.c
SRCS += src/print.c
4 changes: 2 additions & 2 deletions src/lem_in.c
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
/* By: dde-jesu <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/20 08:28:35 by dde-jesu #+# #+# */
/* Updated: 2019/04/26 15:31:39 by dde-jesu ### ########.fr */
/* Updated: 2019/04/27 10:10:40 by dde-jesu ### ########.fr */
/* */
/* ************************************************************************** */

@@ -32,5 +32,5 @@ int main(void)
error("Start is end\n");
return (1);
}
printf("Ants: %zu\n", anthil.ants);
print_anthil(anthil);
}
35 changes: 34 additions & 1 deletion src/mem.c
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
/* By: dde-jesu <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/20 09:14:49 by dde-jesu #+# #+# */
/* Updated: 2019/04/26 14:01:34 by dde-jesu ### ########.fr */
/* Updated: 2019/04/27 09:31:56 by dde-jesu ### ########.fr */
/* */
/* ************************************************************************** */

@@ -50,3 +50,36 @@ int ft_strcmp(const char *s1, const char *s2)
i++;
return ((unsigned char)s1[i] - (unsigned char)s2[i]);
}

int ft_memcmp(const void *s1, const void *s2, size_t n)
{
const unsigned char *c_s1 = s1;
const unsigned char *c_s2 = s2;

if (n != 0)
while (n--)
if (*c_s1++ != *c_s2++)
return (*--c_s1 - *--c_s2);
return (0);
}

char *ft_strchr(const char *s, int c)
{
while (*s)
if (*s++ == c)
return ((char *)s - 1);
if (c)
return (0);
else
return ((char *)s);
}

size_t ft_strlen(const char *s)
{
size_t len;

len = 0;
while (s[len])
len++;
return (len);
}
101 changes: 63 additions & 38 deletions src/parser.c
Original file line number Diff line number Diff line change
@@ -6,25 +6,18 @@
/* By: dde-jesu <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/20 08:57:12 by dde-jesu #+# #+# */
/* Updated: 2019/04/26 16:03:54 by dde-jesu ### ########.fr */
/* Updated: 2019/04/27 10:42:52 by dde-jesu ### ########.fr */
/* */
/* ************************************************************************** */

#include "lem_in.h"
#include "reader.h"
#include "str.h"
#include "mem.h"
#include "hashtable.h"
#include <stdbool.h>
#include <stdlib.h>

enum e_comment
{
COMMENT_NONE,
COMMENT_REAL,
COMMENT_START,
COMMENT_END
};

static bool is_ws(char c)
{
return (c == ' ' || c == '\t');
@@ -59,26 +52,35 @@ char *read_name(t_reader *r)
return (str.inner);
}

enum e_comment read_comment(t_reader *r)
char *read_comments(t_reader *r)
{
int16_t c;
t_str str;

if (io_peek(r) == '#')
{
if (!str_init(&str, 8))
return (NULL);

r->index++;
if (io_peek(r) == '#')
while ((c = io_peek(r)) != -1)
{
if (!str_append(&str, c))
return (NULL);
r->index++;
c = io_peek(r);
if (c == 's' && io_expect(r, "start\n"))
return (COMMENT_START);
if (c == 'e' && io_expect(r, "end\n"))
return (COMMENT_END);
if (c == '\n')
{
if (io_peek(r) == '#')
r->index++;
else
break ;
}
}
io_skip_until(r, '\n');
return (COMMENT_REAL);
str_append(&str, '\0');
return (str.inner);
}
else
return (COMMENT_NONE);
return (NULL);
}

void read_room_coords(t_reader *r, struct s_room *room)
@@ -125,11 +127,11 @@ bool read_object(t_reader *r, struct s_room **room, struct s_link *link)
return (true);
}

bool link_anthil(struct s_hashtable *hashtable, struct s_link *link)
bool link_anthil(struct s_hashtable *hashtable, struct s_link *link, char *comments)
{
struct s_entry *first;
struct s_entry *second;
struct s_room **room;
struct s_room_ptr *room;

if (!(first = hashtable_get(hashtable, link->first)))
warning("Room \"%s\" (first part) not found\n", link->first);
@@ -138,9 +140,15 @@ bool link_anthil(struct s_hashtable *hashtable, struct s_link *link)
if (!first || !second)
return (true);
if ((room = add_room(&((struct s_room *)first->value)->links)))
*room = second->value;
{
room->ptr = second->value;
room->comments = comments;
}
if ((room = add_room(&((struct s_room *)second->value)->links)))
*room = first->value;
{
room->ptr = first->value;
room->comments = comments;
}
return (true);
}

@@ -172,21 +180,38 @@ void free_unused(struct s_anthil *anthil, struct s_hashtable *hashtable)
}
}

bool has_command(const char *comments, const char *command)
{
const size_t command_len = ft_strlen(command);
const char *end;

if (!comments)
return (false);
while (*comments)
{
end = ft_strchr(comments, '\n');
if (!end)
end = comments + ft_strlen(comments);
if ((size_t)(end - comments) == command_len
&& ft_memcmp(comments, command, command_len) == 0)
return (true);
comments = end + 1;
}
return (false);
}

struct s_anthil read_anthil(t_reader *r)
{
struct s_anthil anthil;
enum e_comment comment;
struct s_room *room;
struct s_link link;
struct s_hashtable *hashtable;
struct s_entry *entry;
bool start;
bool end;
int32_t ants;
char *comments;

anthil.start = NULL;
while (read_comment(r) != COMMENT_NONE)
;
anthil.start_comments = read_comments(r);
if (!io_readnum(r, &ants) || ants <= 0 || !io_expect(r, "\n"))
{
error("Invalid ant num\n");
@@ -197,27 +222,26 @@ struct s_anthil read_anthil(t_reader *r)
while (true)
{
skip_nl(r);
start = false;
end = false;
while ((comment = read_comment(r)) != COMMENT_NONE)
if (comment == COMMENT_START)
start = true;
else if (comment == COMMENT_END)
end = true;
comments = read_comments(r);
room = NULL;
if (!read_object(r, &room, &link))
{
anthil.end_comments = comments;
break ;
}
if (room)
{
if (start)
if (has_command(comments, "#start"))
{
if (anthil.start)
warning("Start redefined taking new value \"%s\" old was \"%s\"\n", room->name, anthil.start->name);
anthil.start = room;
}
if (end)
if (has_command(comments, "#end"))
room->end = true;
room->links = create_vec(1);
room->comments = comments;
room->mark = false;
if ((entry = hashtable_insert(&hashtable, create_entry(room->name))))
entry->value = room;
else if (hashtable_get(hashtable, room->name))
@@ -226,7 +250,7 @@ struct s_anthil read_anthil(t_reader *r)
return (anthil);
}
}
else if (!link_anthil(hashtable, &link))
else if (!link_anthil(hashtable, &link, comments))
return (anthil);
if (!io_expect(r, "\n"))
{
@@ -235,5 +259,6 @@ struct s_anthil read_anthil(t_reader *r)
}
}
free_unused(&anthil, hashtable);
free(hashtable);
return (anthil);
}
Loading

0 comments on commit e318cfd

Please sign in to comment.