pf_more_inf.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* pf_more_inf.c :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: bchanot <bchanot@students.42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2016/03/10 02:39:52 by bchanot #+# #+# */
  9. /* Updated: 2016/11/08 11:10:31 by bchanot ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12. #include "libftprintf.h"
  13. int pf_wrong_conv(char **str, const char **format, t_inf inf, int *len)
  14. {
  15. int cpt;
  16. cpt = 0;
  17. if (**str == '\0')
  18. {
  19. *format = *str;
  20. return (1);
  21. }
  22. if (inf.width > 0 && inf.min == 0)
  23. while (++cpt < inf.width)
  24. {
  25. ft_putchar_fd((inf.zero == 1 ? '0' : ' '), inf.fd);
  26. *len += 1;
  27. }
  28. ft_putchar_fd(**str, inf.fd);
  29. if (inf.width > 0 && inf.min == 1)
  30. while (++cpt < inf.width)
  31. {
  32. ft_putchar_fd(' ', inf.fd);
  33. *len += 1;
  34. }
  35. *format = *str + 1;
  36. *len += 1;
  37. return (0);
  38. }
  39. int pf_more_flag(char **s, t_inf *inf)
  40. {
  41. if (**s == 'j')
  42. inf->j = 1;
  43. if (**s == 'z')
  44. inf->z = 1;
  45. if (**s == 'l')
  46. {
  47. *s += 1;
  48. if (**s == 'l')
  49. inf->ll = 1;
  50. else
  51. return (inf->l = 1);
  52. }
  53. if (**s == 'h')
  54. {
  55. *s += 1;
  56. if (**s == 'h')
  57. inf->hh = 1;
  58. else
  59. return (inf->h = 1);
  60. }
  61. return (0);
  62. }
  63. void take_prec(char **s, t_inf *inf, va_list ap, int cpt)
  64. {
  65. inf->prec = 0;
  66. *s += 1;
  67. if (**s == '*' && cpt++ >= 0)
  68. {
  69. inf->prec = va_arg(ap, int);
  70. if (inf->prec < 0)
  71. inf->prec = -1;
  72. *s += 1;
  73. }
  74. else
  75. {
  76. while (ft_isdigit(**s) && cpt++ >= 0)
  77. {
  78. inf->prec = inf->prec * 10 + **s - '0';
  79. *s += 1;
  80. }
  81. }
  82. if (cpt == 0 || inf->prec == 0)
  83. inf->prec = -1;
  84. else if (inf->prec == -1)
  85. inf->prec = 0;
  86. }
  87. static int pf_more_color_please(char tmp[8], int fd)
  88. {
  89. if (ft_strstr(tmp, "red"))
  90. ft_putstr_fd(RED, fd);
  91. else if (ft_strstr(tmp, "black"))
  92. ft_putstr_fd(BLACK, fd);
  93. else if (ft_strstr(tmp, "green"))
  94. ft_putstr_fd(GREEN, fd);
  95. else if (ft_strstr(tmp, "yellow"))
  96. ft_putstr_fd(YELLOW, fd);
  97. else if (ft_strstr(tmp, "blue"))
  98. ft_putstr_fd(BLUE, fd);
  99. else if (ft_strstr(tmp, "purple"))
  100. ft_putstr_fd(PURPLE, fd);
  101. else if (ft_strstr(tmp, "cyan"))
  102. ft_putstr_fd(CYAN, fd);
  103. else if (ft_strstr(tmp, "grey"))
  104. ft_putstr_fd(GREY, fd);
  105. else if (ft_strstr(tmp, "eoc"))
  106. ft_putstr_fd(EOC, fd);
  107. else
  108. return (0);
  109. return (1);
  110. }
  111. int pf_take_color(char **str, const char **format, int *len, int fd)
  112. {
  113. int cpt;
  114. char tmp[8];
  115. cpt = 0;
  116. tmp[7] = '\0';
  117. *len += pf_print_part(*format, *str, fd);
  118. *str += 1;
  119. while (**str != '}' && cpt < 7)
  120. {
  121. tmp[cpt] = **str;
  122. *str += 1;
  123. cpt++;
  124. }
  125. *str += 1;
  126. if (!pf_more_color_please(tmp, fd))
  127. {
  128. *str -= cpt + 2;
  129. *format = *str;
  130. return (0);
  131. }
  132. *format = *str;
  133. return (1);
  134. }