Advertisement
Guest User

Untitled

a guest
May 27th, 2018
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* parser.c :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: dcherend <dcherend@student.42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2018/05/09 18:42:27 by dcherend #+# #+# */
  9. /* Updated: 2018/05/27 18:42:12 by dcherend ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12.  
  13. #include "../inc/fdf.h"
  14.  
  15. t_map *alloc_map(int width, int height)
  16. {
  17. t_map *map;
  18. int i;
  19.  
  20. if (!(map = (t_map*)malloc(sizeof(t_map))))
  21. return (NULL);
  22. map->height = height;
  23. map->width = width;
  24. map->cords = (int**)ft_memalloc(sizeof(int*) * height);
  25. i = 0;
  26. while (i < width)
  27. {
  28. map->cords[i] = (int*)ft_memalloc(sizeof(int) * width);
  29. i++;
  30. }
  31. return (map);
  32. }
  33.  
  34. void del_map(t_map *map)
  35. {
  36. int i;
  37. static int huy = 0;
  38.  
  39. i = 0;
  40. if (map)
  41. {
  42. while (i < map->width)
  43. {
  44. free(map->cords[i]);
  45. i++;
  46. }
  47. free(map->cords);
  48. free(map);
  49. }
  50. }
  51.  
  52. t_map *realloc_map(t_map *map, int width, int height)
  53. {
  54. if (!map)
  55. return (NULL);
  56. del_map(map);
  57. map = alloc_map(width, height);
  58. return (map);
  59. }
  60.  
  61.  
  62. void fill_map(t_map *map, char *line)
  63. {
  64. t_map *tmp;
  65. int i;
  66. int j;
  67. char **symbs;
  68.  
  69. tmp = map;
  70. symbs = ft_strsplit(line, ' ');
  71. free(line);
  72. i = 0;
  73. while (i < map->height)
  74. {
  75. j = 0;
  76. while (j < map->width)
  77. {
  78. map->cords[i][j] = ft_atoi(symbs[j]);
  79. j++;
  80. }
  81. i++;
  82. }
  83. }
  84.  
  85. t_map *ft_parser(char *name)
  86. {
  87. t_map *map;
  88. char *line;
  89. int rows;
  90. int fd;
  91.  
  92. rows = 1;
  93. fd = open(name, O_RDONLY);
  94. if (fd < 0)
  95. throw_error("Bad file input.");
  96. map = alloc_map(1, 1);
  97. while ((get_next_line(fd, &line)) > 0)
  98. {
  99. rows++;
  100. map = realloc_map(map, ft_strlen(line), rows);
  101. fill_map(map, line);
  102. }
  103. return (map);
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement