Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
# By: bob <fokrober@student.1337.ma> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/04/15 15:46:07 by bob #+# #+# #
# Updated: 2020/10/16 05:18:33 by fokrober ### ########.fr #
# Updated: 2020/10/17 02:35:31 by fokrober ### ########.fr #
# #
# **************************************************************************** #

NAME = lem-in
SRC = $(addprefix src/, lem_in.c get_data.c)
SRC = $(addprefix src/, lem_in.c get_data.c hashmap.c parser.c \
sort_list.c tools.c)
HEADER = $(addprefix include/, lem_in.h)
OBJ = $(SRC:.c=.o)
LFTDIR = ./libft
Expand Down
13 changes: 9 additions & 4 deletions include/lem_in.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: bob <fokrober@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/22 12:49:48 by bob #+# #+# */
/* Updated: 2020/10/16 05:28:17 by fokrober ### ########.fr */
/* Updated: 2020/10/17 02:36:55 by fokrober ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -30,9 +30,10 @@
# define MAX_LLONG 18446744073709551615
# define SPACE " \t"
# include <stdlib.h>
# include <libft.h>
# include "libft.h"
# include <stdio.h>
# include <unistd.h>
# include <limits.h>

typedef struct s_hash t_hash;
typedef struct s_edge t_edge;
Expand All @@ -42,7 +43,7 @@ typedef struct s_env t_env;

struct s_edge
{
t_node *child;
t_node *to;
int flow;
t_edge *next;
};
Expand Down Expand Up @@ -93,8 +94,12 @@ struct s_env
t_env *farmdata(void);
int parser(t_env *env);
t_hash *create_hash(size_t size);
int save_node(t_env *env, char *name);
t_node *get_node(t_env *env, char *name);
int save_node(t_env *env, char *name);
int save_link(t_env *env, char *link);
int get_data(t_env *env, char *line, int type);
void sort_edges(t_edge **head, t_env *env);
int x_isalpha(char *s, char *end);
int isnumber(char *s, char *end);
int add_edge(t_node *node, t_node *to);
#endif
4 changes: 3 additions & 1 deletion include/libft.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: mzaboub <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/29 23:32:55 by mzaboub #+# #+# */
/* Updated: 2020/10/16 05:29:07 by fokrober ### ########.fr */
/* Updated: 2020/10/17 02:36:10 by fokrober ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -22,6 +22,8 @@ void ft_putstr(char const *str);
void ft_putnbr(int n);
void ft_putendl_fd(char *s, int fd);
int ft_strnequ(char const *s1, char const *s2, size_t n);
int ft_strequ(char const *s1, char const *s2);
char *ft_strchr(char const *s1, int c);
int ft_atoi(const char *str);
void *ft_memalloc(size_t size);
void *ft_memcpy(void *dst, const void *src, size_t n);
Expand Down
9 changes: 5 additions & 4 deletions libft/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
# By: bob <fokrober@student.1337.ma> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/04/15 22:00:03 by bob #+# #+# #
# Updated: 2020/10/16 03:50:00 by fokrober ### ########.fr #
# Updated: 2020/10/17 02:12:27 by fokrober ### ########.fr #
# #
# **************************************************************************** #

NAME = libft.a
SRC = ft_putstr.c ft_putnbr.c ft_putendl_fd.c ft_strnequ.c ft_strsub.c \
ft_memcpy.c ft_memalloc.c ft_memdel.c ft_atoi.c ft_bzero.c
ft_memcpy.c ft_memalloc.c ft_memdel.c ft_atoi.c ft_bzero.c ft_strchr.c \
ft_strequ.c
HEADER = libft.h
OBJ = $(SRC:.c=.o)
ARFLAGS = rcsu
Expand All @@ -31,12 +32,12 @@ $(NAME): prompt $(OBJ) $(HEADER)
@echo "\n\n$(GREEN) > $(NAME) built !\n$(RESET)"

prompt:
@echo -n " > $(NAME) building"
@printf " > $(NAME) building"

%.o: %.c $(HEADER)
@$(CC) $(CFLAGS) $< -o $@
@ar $(ARFLAGS) $(NAME) $@
@echo ".\c"
@printf "."

clean:
@$(RM) $(OBJ)
Expand Down
29 changes: 29 additions & 0 deletions libft/ft_strchr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fokrober <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/12 23:06:57 by fokrober #+# #+# */
/* Updated: 2019/04/23 19:50:24 by fokrober ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"

char *ft_strchr(const char *s, int c)
{
int i;

i = 0;
while (s[i])
{
if (s[i] == (char)c)
return (((char*)s + i));
i++;
}
if (s[i] == (char)c)
return ((char*)s + i);
return (NULL);
}
26 changes: 26 additions & 0 deletions libft/ft_strequ.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strequ.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fokrober <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/11 05:19:54 by fokrober #+# #+# */
/* Updated: 2020/10/17 01:59:01 by fokrober ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"

int ft_strequ(char const *s1, char const *s2)
{
if (!s1 || !s2)
return (0);
if (*s1 && *s2)
{
if (*s1 != *s2)
return (0);
(void)(s1++ && s2++);
}
return (*s1 == *s2);
}
63 changes: 62 additions & 1 deletion src/get_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,73 @@
/* By: bob <fokrober@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/26 16:49:07 by bob #+# #+# */
/* Updated: 2020/10/16 05:29:49 by fokrober ### ########.fr */
/* Updated: 2020/10/17 02:37:24 by fokrober ### ########.fr */
/* */
/* ************************************************************************** */

#include "lem_in.h"

static int save_rooms(t_env *env, char *line, int type)
{
int i;
char *tmp;

i = 0;
while (line[i] && line[i] != ' ')
i++;
if (!(tmp = ft_strsub(line, 0, i)))
return (SYS_ERROR);
type == START ? (env->start = tmp) : 0;
type == END ? (env->end = tmp) : 0;
return (save_node(env, tmp));
}

static int check_rooms(char *s)
{
int res[3];
int pos;
int i;

pos = 0;
i = -1;
ft_bzero(res, 3 * sizeof(int));
while (s[++i] && pos < 5)
{
if (!pos && s[0] != 'L')
res[pos] = x_isalpha(s, SPACE);
if (pos && pos % 2 == 0)
res[pos / 2] = isnumber(&s[i], SPACE);
if (!pos || !(pos % 2))
{
if (!res[pos ? pos / 2 : pos])
return (FALSE);
i += res[pos ? pos / 2 : pos];
pos += 1;
}
if ((pos && pos % 2) && (s[i] == ' ' || s[i] == '\t'))
pos += (s[i + 1] != ' ' && s[i + 1] != '\t');
}
return (res[0] && (res[1] && res[2]));
}

int get_data(t_env *env, char *line, int type)
{
static int prev;
int ret;

if (!env->hash && !(env->hash = create_hash(HASH_SIZE)))
return (SYS_ERROR);
if (!prev)
{
if (check_rooms(line))
return (save_rooms(env, line, type));
ret = save_link(env, line);
prev = !ret;
return (!prev ? INVALID_LINE : ret);
}
return (save_link(env, line));
}

static int pushdata(t_env *env, char *farm, int ret)
{
char *tmp;
Expand Down
117 changes: 117 additions & 0 deletions src/hashmap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hashmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bob <fokrober@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/25 23:35:28 by bob #+# #+# */
/* Updated: 2020/10/17 02:31:07 by fokrober ### ########.fr */
/* */
/* ************************************************************************** */

#include "lem_in.h"

t_hash *create_hash(size_t size)
{
t_hash *hash;
size_t i;

hash = (t_hash*)malloc(sizeof(t_hash));
if (hash)
hash->tab = (t_node**)malloc(sizeof(t_node*) * size);
if (!hash || !hash->tab)
return (NULL);
hash->size = size;
i = -1;
while (++i < size)
hash->tab[i] = NULL;
return (hash);
}

static int hash_calc(t_hash *hash, char *key)
{
size_t result;
size_t tmp;
int prev;

if (!key)
return (-1);
result = 0;
prev = 1;
while (*key)
{
tmp = ((*key * 0x80200802ULL) & 0x0884422110ULL);
result += ((tmp * (key[1] + prev)) % hash->size);
prev += *key;
key++;
}
result = result >= hash->size ? result % hash->size : result;
return (result);
}

t_node *get_node(t_env *env, char *name)
{
int pos;
t_node *tmp;

pos = hash_calc(env->hash, name);
if (pos < 0)
return (NULL);
tmp = env->hash->tab[pos];
while (tmp)
{
if (ft_strequ(tmp->name, name))
return (tmp);
tmp = tmp->next;
}
return (NULL);
}

int save_node(t_env *env, char *name)
{
int pos;
t_node *tmp;
t_node *prev;

if ((pos = hash_calc(env->hash, name)) < 0)
return (pos);
prev = env->hash->tab[pos];
tmp = prev;
while (tmp)
{
if (ft_strequ(tmp->name, name))
return (OK);
tmp = tmp->next;
}
if (!(tmp = (t_node*)malloc(sizeof(t_node))))
return (SYS_ERROR);
tmp->id = 0;
tmp->name = name;
tmp->links = NULL;
tmp->lck = 0;
tmp->next = prev;
env->hash->tab[pos] = tmp;
env->size += 1;
return (OK);
}

int save_link(t_env *env, char *link)
{
t_node *node1;
t_node *node2;
char *room2;
int ret;

if (!(room2 = ft_strchr(link, '-')))
return (INVALID_LINE);
*room2 = '\0';
node1 = get_node(env, link);
node2 = get_node(env, room2 + 1);
if (!node1 || !node2)
return (INVALID_LINE);
ret = OK;
ret = add_edge(node1, node2);
ret == OK ? (ret = add_edge(node2, node1)) : ret;
return (ret);
}
Loading