Advertisement
Guest User

dw

a guest
Mar 19th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.25 KB | None | 0 0
  1. t_list  *ft_lstnew(void const *content, size_t content_size)
  2. {
  3.     t_list *new;
  4.  
  5.     if (!(new = (t_list *)malloc(sizeof(t_list))))
  6.         return (NULL);
  7.     if (content == NULL)
  8.     {
  9.         new->content = NULL;
  10.         new->content_size = 0;
  11.     }
  12.     else
  13.     {
  14.         new->content = malloc(content_size);
  15.         if (new->content == NULL)
  16.             return (NULL);
  17.         ft_memmove(new->content, content, content_size);
  18.         new->content_size = content_size;
  19.     }
  20.     new->next = NULL;
  21.     return (new);
  22. }
  23.  
  24. t_tetr *create_teatr(char *buf, char a, int ret)
  25. {
  26.     t_tetr *new;
  27.     char *newbuf;
  28.     char **shape;
  29.     int i;
  30.     int j;
  31.     int c;
  32.     int bytes;
  33.     char *newb;
  34.     char ptr;
  35.     char **test;
  36.  
  37.     bytes = 0;
  38.     i = 0;
  39.     j = 0;
  40.     c = 4;
  41.     //printf("%d", ret);
  42.     newbuf = (char *)malloc(sizeof(char) * ret);
  43.     newbuf[ret] = '\0';
  44.     ft_memcpy(newbuf, buf, ret);
  45.     //printf("%s", newbuf);
  46.     newb = trimBuf(newbuf, ret);
  47.    
  48.     free(newb);
  49.     newb = shapeToLetter(newb, a);
  50.     //printf("%s", newb);
  51.     new = (t_tetr *)malloc(sizeof(t_tetr));
  52.     shape = (char **)malloc(sizeof(char *) * 4);
  53.     shape[4] = NULL;
  54.     //printf("%s", newb);
  55.     while (i < 4)
  56.         shape[i++] = malloc(sizeof(char) * 5);
  57.     i = 0;
  58.     while (i < 4)
  59.     {
  60.         shape[i] = ft_strndup(newb, 4);
  61.         c = 4;
  62.         while (c-- > 0)
  63.         {
  64.             ptr = *newb++;
  65.         }
  66.         i++;
  67.     }
  68.     new ->shape = shape;
  69.     new ->height = height(shape, a);
  70.     new -> width = width(shape, a);
  71.     new ->letter = a;
  72.     test = new ->shape;
  73.     i = 0;
  74.     printf("%p\n\n", new->height);
  75.     return (new);
  76. }
  77. char *trimBuf(char *newbuf, int ret)
  78. {
  79.     char *res;
  80.     int i;
  81.     int j;
  82.     int c;
  83.  
  84.     i = 0;
  85.     c = 0;
  86.     j = 16;
  87.     res = (char *)malloc(sizeof(char) * 16);
  88.     while (ret--)
  89.     {
  90.         if (newbuf[i] == '.')
  91.             res[c++] = '.';
  92.         else if (newbuf[i] == '#')
  93.             res[c++] = '#';
  94.         i++;
  95.     }
  96.     res[16] = '\0';
  97.  
  98.     return (res);
  99. }
  100. char *shapeToLetter(char *buf, char a)
  101. {
  102.     int i;
  103.  
  104.     i = 0;
  105.     while (buf[i] != '\0')
  106.     {
  107.         if (buf[i] == '#')
  108.             buf[i] = a;
  109.         i++;
  110.     }
  111.     return (buf);
  112. }
  113.  
  114. int height(char **shape, char a)
  115. {
  116.     int i;
  117.     int j;
  118.     int height;
  119.  
  120.     i = 0;
  121.     j = 0;
  122.     height = 0;
  123.     while (i < 4)
  124.     {
  125.         j = 0;
  126.         while (j < 5)
  127.         {
  128.             if (shape[i][j] == a)
  129.                 {
  130.                     height++;
  131.                     j = 5;
  132.                 }
  133.                 j++;
  134.         }
  135.         i++;
  136.     }
  137.     return (height);
  138. }
  139.  
  140. int width(char **shape, char a)
  141. {
  142.     int i;
  143.     int j;
  144.     int width;
  145.     int temp;
  146.     int res;
  147.  
  148.     res = 0;
  149.     i = 0;
  150.     temp = 0;
  151.     j = 0;
  152.     width = 0;
  153.     while (i < 4)
  154.     {
  155.         j = 0;
  156.         width = 0;
  157.         while (j < 5)
  158.         {
  159.             if (shape[i][j] == a)
  160.                 width++;
  161.             j++;
  162.         }
  163.         if (width > temp)
  164.             temp = width;
  165.         i++;
  166.     }
  167.     width > temp ? (res = width) : (res = temp);
  168.     return (res);
  169. }
  170.  
  171. t_list  *readtetr(const int fd)
  172. {
  173.     int ret;
  174.     char buf[21];
  175.     char a;
  176.     t_list *list;
  177.     t_list **tet;
  178.     tet = &list;
  179.     int bytes;
  180.  
  181.     bytes = 0;
  182.     ret = 0;
  183.     a = 'A';
  184.     while ((ret = read(fd, buf, 21)))
  185.     {
  186.         buf[ret] = '\0';
  187.         *tet = ft_lstnew(create_teatr(buf, a++, ret), ret);
  188.         tet = &((*tet)->next);
  189.     }
  190.     *tet = NULL;
  191.     return (list);
  192.  
  193.  
  194. int main()
  195. {
  196.     int i;
  197.     int j;
  198.  
  199.     i = 0;
  200.     j = 0;
  201.     int fd;
  202.     t_list *a;
  203.     t_tetr *b;
  204.     char **figure;
  205.     char *line;
  206.  
  207.     fd = open("sample", O_RDONLY);
  208.     a = readtetr(fd);
  209.     b = a ->content;
  210.     //i = a ->content_size
  211.     figure = b -> shape;
  212.     i = b ->height;
  213.     printf("main %p", b->height);
  214.     //printf("%zu\n", a ->content_size);
  215.     //printf("%s\n\n", (char *)b->shape[0]);
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement