Advertisement
Guest User

BSQ

a guest
Aug 27th, 2015
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.70 KB | None | 0 0
  1. #ifndef FT_BSQ_H
  2. # define FT_BSQ_H
  3. # include <unistd.h>
  4. # include <stdlib.h>
  5. # include <sys/types.h>
  6. # include <sys/uio.h>
  7.  
  8. typedef struct s_lstring t_lstring;
  9. typedef struct s_square
  10. {
  11. int x;
  12. int y;
  13. int size;
  14. } t_square;
  15. typedef struct s_grid
  16. {
  17. int width;
  18. int height;
  19. char empty;
  20. char full;
  21. char obstacle;
  22. } t_grid;
  23. typedef struct s_linttab
  24. {
  25. int line;
  26. int next;
  27. } t_linttab;
  28. struct s_lstring
  29. {
  30. t_lstring *next;
  31. char c;
  32. int lstring;
  33. int *preview;
  34. int *line;
  35. };
  36. char *ft_readline(int fd, int len);
  37. int ft_strlen(char *str);
  38. char *ft_readline(int fd, int len);
  39. char *ft_readfirstline(int fd);
  40. void ft_putstr(char *str);
  41. t_lstring *ft_pushlstring(char c, t_lstring *prev);
  42. char *ft_lstringtostring(t_lstring *begin);
  43. int ft_lenlstring(t_lstring *begin);
  44. t_grid ft_getinfos(int fd, char *line);
  45. void ft_freelstring(t_lstring *begin);
  46. void ft_bsq(int fd, char *filename);
  47. int ft_atoi(char *str);
  48. void ft_affgrid(t_grid *grid, int fd, char *file);
  49.  
  50. #endif
  51.  
  52. #include "includes/bsq.h"
  53.  
  54. int main(int ac, int **av)
  55. {
  56. int fd;
  57. int i;
  58.  
  59. if (ac == 0)
  60. ft_bsq(0, "stdin");
  61. else
  62. {
  63. i = 1;
  64. while (i < ac)
  65. {
  66. fd = open(av[i], O_RDONLY);
  67. if (fd != -1)
  68. ft_bsq(fd, av[i]);
  69. else
  70. ft_error(av[i]);
  71. i++;
  72. }
  73. }
  74. return (0);
  75. }
  76. /* ************************************************************************** */
  77. /* */
  78. /* ::: :::::::: */
  79. /* ft_affgrid.c :+: :+: :+: */
  80. /* +:+ +:+ +:+ */
  81. /* By: mcathala <mcathala@student.42.fr> +#+ +:+ +#+ */
  82. /* +#+#+#+#+#+ +#+ */
  83. /* Created: 2015/08/27 15:05:41 by mcathala #+# #+# */
  84. /* Updated: 2015/08/27 20:08:13 by tplessis ### ########.fr */
  85. /* */
  86. /* ************************************************************************** */
  87.  
  88. #include "../includes/bsq.h"
  89.  
  90. void ft_affgrid(t_grid *grid, int fd, char *file)
  91. {
  92. int i;
  93. char *str;
  94.  
  95. i = 0;
  96. ft_putstr(file);
  97. while (i < grid->height)
  98. {
  99. str = ft_readline(fd, grid->width);
  100. ft_putstr(str);
  101. i++;
  102. }
  103. }
  104. /* ************************************************************************** */
  105. /* */
  106. /* ::: :::::::: */
  107. /* ft_atoi.c :+: :+: :+: */
  108. /* +:+ +:+ +:+ */
  109. /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
  110. /* +#+#+#+#+#+ +#+ */
  111. /* Created: 2015/08/27 12:22:57 by tplessis #+# #+# */
  112. /* Updated: 2015/08/27 17:02:47 by tplessis ### ########.fr */
  113. /* */
  114. /* ************************************************************************** */
  115.  
  116. int ft_atoi(char *str)
  117. {
  118. int i;
  119. int neg;
  120. int nb;
  121.  
  122. i = 0;
  123. neg = 0;
  124. while (str[i] == '+' || str[i] == '-')
  125. {
  126. if (str[i] == '-')
  127. neg++;
  128. i++;
  129. }
  130. nb = 0;
  131. while (str[i] && str[i] <= '9' && str[i] >= '0')
  132. {
  133. nb = nb * 10;
  134. nb = nb + str[i] - '0';
  135. i++;
  136. }
  137. if (neg % 2)
  138. return (-nb);
  139. return (nb);
  140. }
  141. /* ************************************************************************** */
  142. /* */
  143. /* ::: :::::::: */
  144. /* ft_bsq.c :+: :+: :+: */
  145. /* +:+ +:+ +:+ */
  146. /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
  147. /* +#+#+#+#+#+ +#+ */
  148. /* Created: 2015/08/27 05:00:59 by tplessis #+# #+# */
  149. /* Updated: 2015/08/27 20:19:56 by tplessis ### ########.fr */
  150. /* */
  151. /* ************************************************************************** */
  152.  
  153. #include "../includes/bsq.h"
  154.  
  155. void ft_bsq(int fd, char *filename)
  156. {
  157. t_grid *grid;
  158. char *line;
  159.  
  160. grid = ft_getinfos(fd, line);
  161. filename = NULL;
  162. }
  163. /* ************************************************************************** */
  164. /* */
  165. /* ::: :::::::: */
  166. /* ft_createlinttab.c :+: :+: :+: */
  167. /* +:+ +:+ +:+ */
  168. /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
  169. /* +#+#+#+#+#+ +#+ */
  170. /* Created: 2015/08/27 16:59:14 by tplessis #+# #+# */
  171. /* Updated: 2015/08/27 20:18:32 by tplessis ### ########.fr */
  172. /* */
  173. /* ************************************************************************** */
  174.  
  175. #include "../includes/bsq.h"
  176.  
  177. t_linttab *ft_createlinttab(int *tab)
  178. {
  179. t_linttab *elmt;
  180.  
  181. elmt = (t_linttab*)malloc(sizeof(t_linttab));
  182. elmt->next = NULL;
  183. elmt->prev = NULL;
  184. elmt->tab = tab;
  185. return (elmt);
  186. }
  187. /* ************************************************************************** */
  188. /* */
  189. /* ::: :::::::: */
  190. /* ft_createlstring.c :+: :+: :+: */
  191. /* +:+ +:+ +:+ */
  192. /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
  193. /* +#+#+#+#+#+ +#+ */
  194. /* Created: 2015/08/27 17:01:20 by tplessis #+# #+# */
  195. /* Updated: 2015/08/27 18:51:21 by tplessis ### ########.fr */
  196. /* */
  197. /* ************************************************************************** */
  198.  
  199. #include "../includes/bsq.h"
  200.  
  201. t_lstring *ft_createlstring(char c)
  202. {
  203. t_lstring *elmt;
  204.  
  205. elmt = (t_lstring*)malloc(sizeof(t_lstring));
  206. elmt->next = NULL;
  207. elmt->c = c;
  208. return (elmt);
  209. }
  210. /* ************************************************************************** */
  211. /* */
  212. /* ::: :::::::: */
  213. /* ft_error.c :+: :+: :+: */
  214. /* +:+ +:+ +:+ */
  215. /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
  216. /* +#+#+#+#+#+ +#+ */
  217. /* Created: 2015/08/27 18:41:40 by tplessis #+# #+# */
  218. /* Updated: 2015/08/27 18:41:43 by tplessis ### ########.fr */
  219. /* */
  220. /* ************************************************************************** */
  221.  
  222. #include "../includes/bsq.h"
  223.  
  224. void ft_error(char *file)
  225. {
  226. ft_putstr(file);
  227. ft_putstr(":\n");
  228. write(2, "map error\n", 10);
  229. }
  230. /* ************************************************************************** */
  231. /* */
  232. /* ::: :::::::: */
  233. /* ft_freelstring.c :+: :+: :+: */
  234. /* +:+ +:+ +:+ */
  235. /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
  236. /* +#+#+#+#+#+ +#+ */
  237. /* Created: 2015/08/27 14:38:59 by tplessis #+# #+# */
  238. /* Updated: 2015/08/27 14:41:57 by tplessis ### ########.fr */
  239. /* */
  240. /* ************************************************************************** */
  241.  
  242. #include "../includes/bsq.h"
  243.  
  244. void ft_freelstring(t_lstring *begin)
  245. {
  246. t_lstring *tmp;
  247.  
  248. while (begin)
  249. {
  250. tmp = begin->next;
  251. free(begin);
  252. begin = tmp;
  253. }
  254. }
  255. /* ************************************************************************** */
  256. /* */
  257. /* ::: :::::::: */
  258. /* ft_getinfos.c :+: :+: :+: */
  259. /* +:+ +:+ +:+ */
  260. /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
  261. /* +#+#+#+#+#+ +#+ */
  262. /* Created: 2015/08/27 11:30:37 by tplessis #+# #+# */
  263. /* Updated: 2015/08/27 20:22:46 by tplessis ### ########.fr */
  264. /* */
  265. /* ************************************************************************** */
  266.  
  267. #include "../includes/bsq.h"
  268.  
  269. t_grid *ft_getinfos(int fd, char *line)
  270. {
  271. char infos[50];
  272. int i;
  273. t_grid *grid;
  274.  
  275. grid = (t_grid*)malloc(sizeof(t_grid));
  276. i = 0;
  277. while (infos[i] != '\n')
  278. {
  279. read(fd, &infos[i], 1);
  280. i++;
  281. }
  282. infos[i] = '\0';
  283. i = ft_strlen(infos);
  284. grid->full = infos[i];
  285. grid->obstacle = infos[i - 1];
  286. grid->empty = infos[i - 2];
  287. infos[i - 2] = '\0';
  288. grid->height = ft_atoi(infos);
  289. line = ft_readfirstline(fd);
  290. grid->width = ft_strlen(line);
  291. return (grid);
  292. }
  293. /* ************************************************************************** */
  294. /* */
  295. /* ::: :::::::: */
  296. /* ft_lenlstring.c :+: :+: :+: */
  297. /* +:+ +:+ +:+ */
  298. /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
  299. /* +#+#+#+#+#+ +#+ */
  300. /* Created: 2015/08/27 14:26:49 by tplessis #+# #+# */
  301. /* Updated: 2015/08/27 14:58:20 by tplessis ### ########.fr */
  302. /* */
  303. /* ************************************************************************** */
  304.  
  305. #include "../includes/bsq.h"
  306.  
  307. int ft_lenlstring(t_lstring *begin)
  308. {
  309. int len;
  310.  
  311. len = 0;
  312. while (begin)
  313. {
  314. begin = begin->next;
  315. len++;
  316. }
  317. return (len);
  318. }
  319. /* ************************************************************************** */
  320. /* */
  321. /* ::: :::::::: */
  322. /* ft_lstringtostring.c :+: :+: :+: */
  323. /* +:+ +:+ +:+ */
  324. /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
  325. /* +#+#+#+#+#+ +#+ */
  326. /* Created: 2015/08/27 14:42:53 by tplessis #+# #+# */
  327. /* Updated: 2015/08/27 14:48:06 by tplessis ### ########.fr */
  328. /* */
  329. /* ************************************************************************** */
  330.  
  331. #include "../includes/bsq.h"
  332.  
  333. char *ft_lstringtostring(t_lstring *begin)
  334. {
  335. char *str;
  336. int i;
  337.  
  338. str = (char *)malloc(sizeof(char) * ft_lenlstring(begin));
  339. i = 0;
  340. while (begin)
  341. {
  342. str[i] = begin->c;
  343. begin = begin->next;
  344. i++;
  345. }
  346. return (str);
  347. }
  348. /* ************************************************************************** */
  349. /* */
  350. /* ::: :::::::: */
  351. /* ft_parseline.c :+: :+: :+: */
  352. /* +:+ +:+ +:+ */
  353. /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
  354. /* +#+#+#+#+#+ +#+ */
  355. /* Created: 2015/08/27 19:30:53 by tplessis #+# #+# */
  356. /* Updated: 2015/08/27 20:05:38 by tplessis ### ########.fr */
  357. /* */
  358. /* ************************************************************************** */
  359.  
  360. #include "../includes/bsq.h"
  361.  
  362. t_square *ft_parseline(t_grid *grid, char *line, int fd, t_linttab *tab)
  363. {
  364. int i;
  365. int tmp;
  366.  
  367. i = 0;
  368. while (line[i])
  369. {
  370. tmp = (int*)malloc(sizeof(int) * grid->width);
  371.  
  372. if (line[i] == grid->obstacle)
  373. tmp[i] = 0;
  374. else if (line[i] == grid->empty)
  375. tmp[i] = ft_emptycell(i, tab);
  376. else
  377. return (NULL);
  378. i++;
  379. }
  380. }
  381. /* ************************************************************************** */
  382. /* */
  383. /* ::: :::::::: */
  384. /* ft_pushlinttab.c :+: :+: :+: */
  385. /* +:+ +:+ +:+ */
  386. /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
  387. /* +#+#+#+#+#+ +#+ */
  388. /* Created: 2015/08/27 18:57:47 by tplessis #+# #+# */
  389. /* Updated: 2015/08/27 19:02:07 by tplessis ### ########.fr */
  390. /* */
  391. /* ************************************************************************** */
  392.  
  393. #include "../includes/bsq.h"
  394.  
  395. t_linttab *ft_pushlinttab(int *tab, t_linttab *prev)
  396. {
  397. t_linttab *line;
  398.  
  399. line = ft_createlinttab(tab);
  400. if (prev)
  401. {
  402. prev->next = line;
  403. line->prev = prev;
  404. }
  405. else
  406. prev = line;
  407. return (line);
  408. }
  409. /* ************************************************************************** */
  410. /* */
  411. /* ::: :::::::: */
  412. /* ft_pushlstring.c :+: :+: :+: */
  413. /* +:+ +:+ +:+ */
  414. /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
  415. /* +#+#+#+#+#+ +#+ */
  416. /* Created: 2015/08/27 12:52:57 by tplessis #+# #+# */
  417. /* Updated: 2015/08/27 20:54:11 by mcathala ### ########.fr */
  418. /* */
  419. /* ************************************************************************** */
  420.  
  421. #include "../includes/bsq.h"
  422.  
  423. t_lstring *ft_pushlstring(char c, lstring *prev)
  424. {
  425. t_lstring *line;
  426.  
  427. line = ft_createlstring(c);
  428. if (prev)
  429. prev->next = line;
  430. else
  431. prev = line;
  432. return (line);
  433. }
  434. /* ************************************************************************** */
  435. /* */
  436. /* ::: :::::::: */
  437. /* ft_putstr.c :+: :+: :+: */
  438. /* +:+ +:+ +:+ */
  439. /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
  440. /* +#+#+#+#+#+ +#+ */
  441. /* Created: 2015/08/27 14:36:16 by tplessis #+# #+# */
  442. /* Updated: 2015/08/27 14:38:02 by tplessis ### ########.fr */
  443. /* */
  444. /* ************************************************************************** */
  445.  
  446. #include "../includes/bsq.h"
  447.  
  448. void ft_putstr(char *str)
  449. {
  450. int i;
  451.  
  452. while (str[i])
  453. {
  454. write(1, &str[i], 1);
  455. i++;
  456. }
  457. }
  458. /* ************************************************************************** */
  459. /* */
  460. /* ::: :::::::: */
  461. /* ft_readfirstline.c :+: :+: :+: */
  462. /* +:+ +:+ +:+ */
  463. /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
  464. /* +#+#+#+#+#+ +#+ */
  465. /* Created: 2015/08/27 12:42:55 by tplessis #+# #+# */
  466. /* Updated: 2015/08/27 18:42:39 by tplessis ### ########.fr */
  467. /* */
  468. /* ************************************************************************** */
  469.  
  470. #include "../includes/bsq.h"
  471.  
  472. char *ft_readfirstline(int fd)
  473. {
  474. int ok;
  475. char c;
  476. t_lstring *string;
  477. char *line;
  478.  
  479. string = NULL;
  480. ok = read(fd, &c, 1);
  481. while (c != '\n' && ok)
  482. {
  483. ft_pushlstring(c, string);
  484. ok = read(fd, &c, 1);
  485. }
  486. line = ft_lstringtostring(string);
  487. ft_freelstring(string);
  488. return (line);
  489. }
  490. /* ************************************************************************** */
  491. /* */
  492. /* ::: :::::::: */
  493. /* ft_readline.c :+: :+: :+: */
  494. /* +:+ +:+ +:+ */
  495. /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
  496. /* +#+#+#+#+#+ +#+ */
  497. /* Created: 2015/08/25 12:36:17 by tplessis #+# #+# */
  498. /* Updated: 2015/08/27 15:17:47 by tplessis ### ########.fr */
  499. /* */
  500. /* ************************************************************************** */
  501.  
  502. #include "../includes/bsq.h"
  503.  
  504. char *ft_readline(int fd, int len)
  505. {
  506. char *line;
  507.  
  508. line = (char *)malloc(sizeof(char) * len);
  509. read(fd, line, len);
  510. return (line);
  511. }
  512. /* ************************************************************************** */
  513. /* */
  514. /* ::: :::::::: */
  515. /* ft_shiftlinttab.c :+: :+: :+: */
  516. /* +:+ +:+ +:+ */
  517. /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
  518. /* +#+#+#+#+#+ +#+ */
  519. /* Created: 2015/08/27 18:50:36 by tplessis #+# #+# */
  520. /* Updated: 2015/08/27 18:57:17 by tplessis ### ########.fr */
  521. /* */
  522. /* ************************************************************************** */
  523.  
  524. #include "../includes/bsq.h"
  525.  
  526. void ft_shiftlinttab(t_linttab *begin)
  527. {
  528. begin = begin->next;
  529. free(begin);
  530. }
  531. /* ************************************************************************** */
  532. /* */
  533. /* ::: :::::::: */
  534. /* ft_strlen.c :+: :+: :+: */
  535. /* +:+ +:+ +:+ */
  536. /* By: tplessis <tplessis@student.42.fr> +#+ +:+ +#+ */
  537. /* +#+#+#+#+#+ +#+ */
  538. /* Created: 2015/08/27 12:20:50 by tplessis #+# #+# */
  539. /* Updated: 2015/08/27 12:28:31 by tplessis ### ########.fr */
  540. /* */
  541. /* ************************************************************************** */
  542.  
  543. int ft_strlen(char *str)
  544. {
  545. int len;
  546.  
  547. len = 0;
  548. while (str[len])
  549. len++;
  550. return (len);
  551. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement