Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef FT_BSQ_H
- # define FT_BSQ_H
- # include <unistd.h>
- # include <stdlib.h>
- # include <sys/types.h>
- # include <sys/uio.h>
- typedef struct s_lstring t_lstring;
- typedef struct s_square
- {
- int x;
- int y;
- int size;
- } t_square;
- typedef struct s_grid
- {
- int width;
- int height;
- char empty;
- char full;
- char obstacle;
- } t_grid;
- typedef struct s_linttab
- {
- int line;
- int next;
- } t_linttab;
- struct s_lstring
- {
- t_lstring *next;
- char c;
- int lstring;
- int *preview;
- int *line;
- };
- char *ft_readline(int fd, int len);
- int ft_strlen(char *str);
- char *ft_readline(int fd, int len);
- char *ft_readfirstline(int fd);
- void ft_putstr(char *str);
- t_lstring *ft_pushlstring(char c, t_lstring *prev);
- char *ft_lstringtostring(t_lstring *begin);
- int ft_lenlstring(t_lstring *begin);
- t_grid ft_getinfos(int fd, char *line);
- void ft_freelstring(t_lstring *begin);
- void ft_bsq(int fd, char *filename);
- int ft_atoi(char *str);
- void ft_affgrid(t_grid *grid, int fd, char *file);
- #endif
- #include "includes/bsq.h"
- int main(int ac, int **av)
- {
- int fd;
- int i;
- if (ac == 0)
- ft_bsq(0, "stdin");
- else
- {
- i = 1;
- while (i < ac)
- {
- fd = open(av[i], O_RDONLY);
- if (fd != -1)
- ft_bsq(fd, av[i]);
- else
- ft_error(av[i]);
- i++;
- }
- }
- return (0);
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_affgrid.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: mcathala <mcathala@student.42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2015/08/27 15:05:41 by mcathala #+# #+# */
- /* Updated: 2015/08/27 20:08:13 by tplessis ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "../includes/bsq.h"
- void ft_affgrid(t_grid *grid, int fd, char *file)
- {
- int i;
- char *str;
- i = 0;
- ft_putstr(file);
- while (i < grid->height)
- {
- str = ft_readline(fd, grid->width);
- ft_putstr(str);
- i++;
- }
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_atoi.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2015/08/27 12:22:57 by tplessis #+# #+# */
- /* Updated: 2015/08/27 17:02:47 by tplessis ### ########.fr */
- /* */
- /* ************************************************************************** */
- int ft_atoi(char *str)
- {
- int i;
- int neg;
- int nb;
- i = 0;
- neg = 0;
- while (str[i] == '+' || str[i] == '-')
- {
- if (str[i] == '-')
- neg++;
- i++;
- }
- nb = 0;
- while (str[i] && str[i] <= '9' && str[i] >= '0')
- {
- nb = nb * 10;
- nb = nb + str[i] - '0';
- i++;
- }
- if (neg % 2)
- return (-nb);
- return (nb);
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_bsq.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2015/08/27 05:00:59 by tplessis #+# #+# */
- /* Updated: 2015/08/27 20:19:56 by tplessis ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "../includes/bsq.h"
- void ft_bsq(int fd, char *filename)
- {
- t_grid *grid;
- char *line;
- grid = ft_getinfos(fd, line);
- filename = NULL;
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_createlinttab.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2015/08/27 16:59:14 by tplessis #+# #+# */
- /* Updated: 2015/08/27 20:18:32 by tplessis ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "../includes/bsq.h"
- t_linttab *ft_createlinttab(int *tab)
- {
- t_linttab *elmt;
- elmt = (t_linttab*)malloc(sizeof(t_linttab));
- elmt->next = NULL;
- elmt->prev = NULL;
- elmt->tab = tab;
- return (elmt);
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_createlstring.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2015/08/27 17:01:20 by tplessis #+# #+# */
- /* Updated: 2015/08/27 18:51:21 by tplessis ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "../includes/bsq.h"
- t_lstring *ft_createlstring(char c)
- {
- t_lstring *elmt;
- elmt = (t_lstring*)malloc(sizeof(t_lstring));
- elmt->next = NULL;
- elmt->c = c;
- return (elmt);
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_error.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2015/08/27 18:41:40 by tplessis #+# #+# */
- /* Updated: 2015/08/27 18:41:43 by tplessis ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "../includes/bsq.h"
- void ft_error(char *file)
- {
- ft_putstr(file);
- ft_putstr(":\n");
- write(2, "map error\n", 10);
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_freelstring.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2015/08/27 14:38:59 by tplessis #+# #+# */
- /* Updated: 2015/08/27 14:41:57 by tplessis ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "../includes/bsq.h"
- void ft_freelstring(t_lstring *begin)
- {
- t_lstring *tmp;
- while (begin)
- {
- tmp = begin->next;
- free(begin);
- begin = tmp;
- }
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_getinfos.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2015/08/27 11:30:37 by tplessis #+# #+# */
- /* Updated: 2015/08/27 20:22:46 by tplessis ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "../includes/bsq.h"
- t_grid *ft_getinfos(int fd, char *line)
- {
- char infos[50];
- int i;
- t_grid *grid;
- grid = (t_grid*)malloc(sizeof(t_grid));
- i = 0;
- while (infos[i] != '\n')
- {
- read(fd, &infos[i], 1);
- i++;
- }
- infos[i] = '\0';
- i = ft_strlen(infos);
- grid->full = infos[i];
- grid->obstacle = infos[i - 1];
- grid->empty = infos[i - 2];
- infos[i - 2] = '\0';
- grid->height = ft_atoi(infos);
- line = ft_readfirstline(fd);
- grid->width = ft_strlen(line);
- return (grid);
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_lenlstring.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2015/08/27 14:26:49 by tplessis #+# #+# */
- /* Updated: 2015/08/27 14:58:20 by tplessis ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "../includes/bsq.h"
- int ft_lenlstring(t_lstring *begin)
- {
- int len;
- len = 0;
- while (begin)
- {
- begin = begin->next;
- len++;
- }
- return (len);
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_lstringtostring.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2015/08/27 14:42:53 by tplessis #+# #+# */
- /* Updated: 2015/08/27 14:48:06 by tplessis ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "../includes/bsq.h"
- char *ft_lstringtostring(t_lstring *begin)
- {
- char *str;
- int i;
- str = (char *)malloc(sizeof(char) * ft_lenlstring(begin));
- i = 0;
- while (begin)
- {
- str[i] = begin->c;
- begin = begin->next;
- i++;
- }
- return (str);
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_parseline.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2015/08/27 19:30:53 by tplessis #+# #+# */
- /* Updated: 2015/08/27 20:05:38 by tplessis ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "../includes/bsq.h"
- t_square *ft_parseline(t_grid *grid, char *line, int fd, t_linttab *tab)
- {
- int i;
- int tmp;
- i = 0;
- while (line[i])
- {
- tmp = (int*)malloc(sizeof(int) * grid->width);
- if (line[i] == grid->obstacle)
- tmp[i] = 0;
- else if (line[i] == grid->empty)
- tmp[i] = ft_emptycell(i, tab);
- else
- return (NULL);
- i++;
- }
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_pushlinttab.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2015/08/27 18:57:47 by tplessis #+# #+# */
- /* Updated: 2015/08/27 19:02:07 by tplessis ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "../includes/bsq.h"
- t_linttab *ft_pushlinttab(int *tab, t_linttab *prev)
- {
- t_linttab *line;
- line = ft_createlinttab(tab);
- if (prev)
- {
- prev->next = line;
- line->prev = prev;
- }
- else
- prev = line;
- return (line);
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_pushlstring.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2015/08/27 12:52:57 by tplessis #+# #+# */
- /* Updated: 2015/08/27 20:54:11 by mcathala ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "../includes/bsq.h"
- t_lstring *ft_pushlstring(char c, lstring *prev)
- {
- t_lstring *line;
- line = ft_createlstring(c);
- if (prev)
- prev->next = line;
- else
- prev = line;
- return (line);
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_putstr.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2015/08/27 14:36:16 by tplessis #+# #+# */
- /* Updated: 2015/08/27 14:38:02 by tplessis ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "../includes/bsq.h"
- void ft_putstr(char *str)
- {
- int i;
- while (str[i])
- {
- write(1, &str[i], 1);
- i++;
- }
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_readfirstline.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2015/08/27 12:42:55 by tplessis #+# #+# */
- /* Updated: 2015/08/27 18:42:39 by tplessis ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "../includes/bsq.h"
- char *ft_readfirstline(int fd)
- {
- int ok;
- char c;
- t_lstring *string;
- char *line;
- string = NULL;
- ok = read(fd, &c, 1);
- while (c != '\n' && ok)
- {
- ft_pushlstring(c, string);
- ok = read(fd, &c, 1);
- }
- line = ft_lstringtostring(string);
- ft_freelstring(string);
- return (line);
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_readline.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2015/08/25 12:36:17 by tplessis #+# #+# */
- /* Updated: 2015/08/27 15:17:47 by tplessis ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "../includes/bsq.h"
- char *ft_readline(int fd, int len)
- {
- char *line;
- line = (char *)malloc(sizeof(char) * len);
- read(fd, line, len);
- return (line);
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_shiftlinttab.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2015/08/27 18:50:36 by tplessis #+# #+# */
- /* Updated: 2015/08/27 18:57:17 by tplessis ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include "../includes/bsq.h"
- void ft_shiftlinttab(t_linttab *begin)
- {
- begin = begin->next;
- free(begin);
- }
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_strlen.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2015/08/27 12:20:50 by tplessis #+# #+# */
- /* Updated: 2015/08/27 12:28:31 by tplessis ### ########.fr */
- /* */
- /* ************************************************************************** */
- int ft_strlen(char *str)
- {
- int len;
- len = 0;
- while (str[len])
- len++;
- return (len);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement