ft_take_args.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* ft_take_args.c :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: bchanot <bchanot@student.42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2018/01/04 20:23:47 by bchanot #+# #+# */
  9. /* Updated: 2018/10/14 04:25:02 by bchanot ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12. #include "libft.h"
  13. static char **ft_take_arg_begin2(size_t size, int ac, char **av)
  14. {
  15. t_bool check;
  16. size_t index;
  17. char **args;
  18. int i;
  19. i = -1;
  20. index = 0;
  21. check = false;
  22. if (!(args = (char **)ft_memalloc(sizeof(char *) * (size + 1))))
  23. return (NULL);
  24. while (++i < ac)
  25. {
  26. if (check)
  27. args[index++] = ft_strdup(av[i]);
  28. else if (av[i][0] != '-')
  29. {
  30. args[index++] = ft_strdup(av[i]);
  31. check = true;
  32. }
  33. else if ((av[i][0] == '-' && av[i][1] && av[i][1] == '-' &&
  34. !av[i][2]))
  35. check = true;
  36. }
  37. args[index] = 0;
  38. return (args);
  39. }
  40. char **ft_take_args_begin(int ac, char **av)
  41. {
  42. t_bool check;
  43. size_t index;
  44. int i;
  45. i = -1;
  46. check = false;
  47. index = 0;
  48. if (ac < 1)
  49. return (NULL);
  50. while (++i < ac)
  51. {
  52. if (check)
  53. index++;
  54. else if (av[i][0] != '-')
  55. {
  56. check = true;
  57. index++;
  58. }
  59. if (av[i][0] == '-' && av[i][1] && av[i][1] == '-' &&
  60. !av[i][2])
  61. check = true;
  62. }
  63. return (index > 0 ? ft_take_arg_begin2(index, ac, av) : NULL);
  64. }