sl_core.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* sl_core.c :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: bchanot <bchanot@student.42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2018/09/24 00:03:51 by bchanot #+# #+# */
  9. /* Updated: 2018/10/14 04:29:32 by bchanot ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12. #include "ft_ssl.h"
  13. void sl_choose_hash(t_inf *inf)
  14. {
  15. static void (*func[NB_HASH])(const char *str) = {HASH_FUNCTIONS};
  16. const char *(hash[NB_HASH + 1]) = {HASH_TAB, NULL};
  17. size_t i;
  18. i = -1;
  19. while (hash[++i])
  20. {
  21. if (!ft_strcmp(inf->hash_tag, hash[i]))
  22. {
  23. inf->func = func[i];
  24. break ;
  25. }
  26. }
  27. }
  28. static void sl_print_and_launch(t_inf *inf, t_files *file, size_t i)
  29. {
  30. if (!file->name)
  31. {
  32. if (ft_check_opts(inf->opts, "p") && i == 0)
  33. ft_printf(1, "%s%c", file->string, file->string[ft_strlen(
  34. file->string) - 1] == '\n' ? 0 : '\n');
  35. else if (file->s_opt && !ft_check_opts(inf->opts, "r") &&
  36. !ft_check_opts(inf->opts, "q"))
  37. ft_printf(1, "%s (\"%s\")= ", inf->hash_tag, file->string);
  38. }
  39. else if (!ft_check_opts(inf->opts, "r") && !ft_check_opts(inf->opts, "q"))
  40. ft_printf(1, "%s (%s)= ", ft_str_upper(inf->hash_tag), file->name);
  41. inf->func(file->string);
  42. if (!ft_check_opts(inf->opts, "q") && ft_check_opts(inf->opts, "r"))
  43. {
  44. if (file->name)
  45. ft_printf(1, " %s", file->name);
  46. else if (file->s_opt)
  47. ft_printf(1, " \"%s\"", file->string);
  48. }
  49. ft_putchar('\n');
  50. }
  51. void sl_prepare_launch(t_inf *inf)
  52. {
  53. t_files *tmp;
  54. size_t i;
  55. tmp = inf->files;
  56. i = 0;
  57. while (tmp)
  58. {
  59. if (!tmp->string)
  60. ft_printf(2, "ft_ssl: %s: %s: No such file or directory\n",
  61. ft_str_lower(inf->hash_tag), tmp->name);
  62. else
  63. sl_print_and_launch(inf, tmp, i++);
  64. tmp = tmp->next;
  65. }
  66. }