Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- t_list *ft_lstnew(void const *content, size_t content_size)
- {
- t_list *new;
- if (!(new = (t_list *)malloc(sizeof(t_list))))
- return (NULL);
- if (content == NULL)
- {
- new->content = NULL;
- new->content_size = 0;
- }
- else
- {
- new->content = malloc(content_size);
- if (new->content == NULL)
- return (NULL);
- ft_memmove(new->content, content, content_size);
- new->content_size = content_size;
- }
- new->next = NULL;
- return (new);
- }
- t_tetr *create_teatr(char *buf, char a, int ret)
- {
- t_tetr *new;
- char *newbuf;
- char **shape;
- int i;
- int j;
- int c;
- int bytes;
- char *newb;
- char ptr;
- char **test;
- bytes = 0;
- i = 0;
- j = 0;
- c = 4;
- //printf("%d", ret);
- newbuf = (char *)malloc(sizeof(char) * ret);
- newbuf[ret] = '\0';
- ft_memcpy(newbuf, buf, ret);
- //printf("%s", newbuf);
- newb = trimBuf(newbuf, ret);
- free(newb);
- newb = shapeToLetter(newb, a);
- //printf("%s", newb);
- new = (t_tetr *)malloc(sizeof(t_tetr));
- shape = (char **)malloc(sizeof(char *) * 4);
- shape[4] = NULL;
- //printf("%s", newb);
- while (i < 4)
- shape[i++] = malloc(sizeof(char) * 5);
- i = 0;
- while (i < 4)
- {
- shape[i] = ft_strndup(newb, 4);
- c = 4;
- while (c-- > 0)
- {
- ptr = *newb++;
- }
- i++;
- }
- new ->shape = shape;
- new ->height = height(shape, a);
- new -> width = width(shape, a);
- new ->letter = a;
- test = new ->shape;
- i = 0;
- printf("%p\n\n", new->height);
- return (new);
- }
- char *trimBuf(char *newbuf, int ret)
- {
- char *res;
- int i;
- int j;
- int c;
- i = 0;
- c = 0;
- j = 16;
- res = (char *)malloc(sizeof(char) * 16);
- while (ret--)
- {
- if (newbuf[i] == '.')
- res[c++] = '.';
- else if (newbuf[i] == '#')
- res[c++] = '#';
- i++;
- }
- res[16] = '\0';
- return (res);
- }
- char *shapeToLetter(char *buf, char a)
- {
- int i;
- i = 0;
- while (buf[i] != '\0')
- {
- if (buf[i] == '#')
- buf[i] = a;
- i++;
- }
- return (buf);
- }
- int height(char **shape, char a)
- {
- int i;
- int j;
- int height;
- i = 0;
- j = 0;
- height = 0;
- while (i < 4)
- {
- j = 0;
- while (j < 5)
- {
- if (shape[i][j] == a)
- {
- height++;
- j = 5;
- }
- j++;
- }
- i++;
- }
- return (height);
- }
- int width(char **shape, char a)
- {
- int i;
- int j;
- int width;
- int temp;
- int res;
- res = 0;
- i = 0;
- temp = 0;
- j = 0;
- width = 0;
- while (i < 4)
- {
- j = 0;
- width = 0;
- while (j < 5)
- {
- if (shape[i][j] == a)
- width++;
- j++;
- }
- if (width > temp)
- temp = width;
- i++;
- }
- width > temp ? (res = width) : (res = temp);
- return (res);
- }
- t_list *readtetr(const int fd)
- {
- int ret;
- char buf[21];
- char a;
- t_list *list;
- t_list **tet;
- tet = &list;
- int bytes;
- bytes = 0;
- ret = 0;
- a = 'A';
- while ((ret = read(fd, buf, 21)))
- {
- buf[ret] = '\0';
- *tet = ft_lstnew(create_teatr(buf, a++, ret), ret);
- tet = &((*tet)->next);
- }
- *tet = NULL;
- return (list);
- int main()
- {
- int i;
- int j;
- i = 0;
- j = 0;
- int fd;
- t_list *a;
- t_tetr *b;
- char **figure;
- char *line;
- fd = open("sample", O_RDONLY);
- a = readtetr(fd);
- b = a ->content;
- //i = a ->content_size
- figure = b -> shape;
- i = b ->height;
- printf("main %p", b->height);
- //printf("%zu\n", a ->content_size);
- //printf("%s\n\n", (char *)b->shape[0]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement