fork download
  1. #include <unistd.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. #define BUF_SIZE 1
  9.  
  10. void ft_putchar(char c)
  11. {
  12. write(1, &c, 1);
  13. }
  14.  
  15. int count_cols(char *filename)
  16. {
  17. int cols;
  18. char buf[1];
  19. int fd;
  20. int size;
  21. int cont;
  22.  
  23. fd = 0;
  24. cols = 0;
  25. cont = 1;
  26. fd = open(filename, O_RDONLY);
  27. if (fd)
  28. {
  29. while((size = read(fd, buf, BUF_SIZE)) && (cont == 1))
  30. {
  31. if ((buf[0] != '\n') && (buf[0] != ' '))
  32. {
  33. cols++;
  34. }
  35. else
  36. cont = 0;
  37. }
  38. }
  39. return (cols);
  40. }
  41.  
  42. int count_rows(char *filename)
  43. {
  44. int rows;
  45. char buf[1];
  46. int fd;
  47. int size;
  48. int cont;
  49.  
  50. fd = 0;
  51. rows = 0;
  52. cont = 1;
  53. fd = open(filename, O_RDONLY);
  54. if (fd)
  55. {
  56. while((size = read(fd, buf, BUF_SIZE)))
  57. {
  58. if (buf[0] == '\n')
  59. {
  60. rows++;
  61. }
  62. }
  63. }
  64. return (rows + 1);
  65. }
  66.  
  67. void fill_map(int **map,int fd, int row, int col)
  68. {
  69. ssize_t size;
  70. char buf[BUF_SIZE];
  71.  
  72. size = 0;
  73. while ((size = read(fd, buf, BUF_SIZE)))
  74. {
  75. if (size < 1)
  76. return (0);
  77. buf[size] = '\0';
  78. if (buf[0] == '\n')
  79. {
  80. map[row] = malloc(sizeof(int) * (col + 1));
  81. row += 1;
  82. col = 0;
  83. }
  84. else
  85. {
  86. if (buf[0] == '.')
  87. map[row][col] = 1;
  88. else if (buf[0] == 'o')
  89. map[row][col] = 0;
  90. col++;
  91. }
  92. }
  93. }
  94.  
  95. int **ft_create_map(char *filename, int nb_cols, int nb_rows)
  96. {
  97. int **map;
  98. int fd;
  99. int row;
  100. int col;
  101. ssize_t size;
  102. // char buf[BUF_SIZE];
  103.  
  104. row = 0;
  105. size = 0;
  106. col = 0;
  107. map = (int **)malloc(nb_rows * sizeof(int *));
  108. if (!map)
  109. return (map);
  110. map[0] = malloc(sizeof(int) * nb_cols);
  111. fd = open(filename, O_RDONLY);
  112. if (!fd)
  113. return (NULL);
  114. fill_map(map, fd, row, col);
  115. return (map);
  116. }
  117.  
  118. int min(int a, int b, int c)
  119. {
  120. int m;
  121.  
  122. m = a;
  123. if (m > b)
  124. m = b;
  125. if (m > c)
  126. m = c;
  127. return m;
  128. }
  129.  
  130. void printf_max_square(int **map, int rows, int cols)
  131. {
  132. int i;
  133. int S[rows][cols];
  134. int max_of_s;
  135. int max_i;
  136. int max_j;
  137. int j;
  138.  
  139. i = 0;
  140. j = 0;
  141. while (i < rows)
  142. {
  143. S[i][0] = map[i][0];
  144. i++;
  145. }
  146. while (j < cols)
  147. {
  148. S[0][j] = map[0][j];
  149. j++;
  150. }
  151.  
  152. i = 1;
  153. while (i < rows)
  154. {
  155. j = 1;
  156. while (j < cols)
  157. {
  158. if (map[i][j] == 1)
  159. S[i][j] = min(S[i][j - 1], S[i - 1][j], S[i - 1][j - 1]) + 1;
  160. else
  161. S[i][j] = 0;
  162. j++;
  163. }
  164. i++;
  165. }
  166. max_of_s = S[0][0];
  167. max_i = 0;
  168. max_j = 0;
  169. i = 0;
  170. while (i < rows)
  171. {
  172. j = 0;
  173. while (j < cols)
  174. {
  175. if (max_of_s < S[i][j])
  176. {
  177. max_of_s = S[i][j];
  178. max_i = i;
  179. max_j = j;
  180. }
  181. j++;
  182. }
  183. i++;
  184. }
  185. i = max_i;
  186. while (i > (max_i - max_of_s))
  187. {
  188. j = max_j;
  189. while (j > (max_j - max_of_s))
  190. {
  191. map[i][j] = '*';
  192. j--;
  193. }
  194. i--;
  195. }
  196. }
  197.  
  198. void ft_read_file(char *filename)
  199. {
  200. int rows;
  201. char buf[1];
  202. int fd;
  203. int size;
  204. int cont;
  205.  
  206. fd = 0;
  207. rows = 0;
  208. cont = 1;
  209. fd = open(filename, O_RDONLY);
  210. if (fd)
  211. {
  212. while((size = read(fd, buf, BUF_SIZE)))
  213. {
  214. buf[size] = '\0';
  215. ft_putchar(buf[0]);
  216. }
  217. }
  218. }
  219.  
  220. int main(int argc, char **argv)
  221. {
  222. if (argc <= 1)
  223. return (1);
  224. int cols = count_cols(argv[1]);
  225. int rows = count_rows(argv[1]);
  226. int **arr;
  227.  
  228. arr = ft_create_map(argv[1], cols, rows);
  229. printf_max_square(arr, rows, cols);
  230. int i = 0;
  231. int j;
  232. while (i < rows)
  233. {
  234. j = 0;
  235. while (j < cols)
  236. {
  237. if (arr[i][j] == 42)
  238. printf("%c", 'X');
  239. else if (arr[i][j] == 0)
  240. printf("%c", 'o');
  241. else
  242. printf("%c", '.');
  243. j++;
  244. }
  245. printf("\n");
  246. i++;
  247. }
  248. return 0;
  249. }
  250.  
Runtime error #stdin #stdout 0s 2152KB
stdin
Standard input is empty
stdout
Standard output is empty