pf_b.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* pf_b.c :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: bchanot <bchanot@students.42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2016/03/18 19:17:43 by bchanot #+# #+# */
  9. /* Updated: 2016/03/28 21:42:33 by bchanot ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12. #include "libftprintf.h"
  13. static int pf_width_b(char *str, t_inf inf, int len, int nb)
  14. {
  15. int i;
  16. i = nb == 0 && inf.prec == -1 ? 0 : (int)ft_strlen(str);
  17. if (inf.prec > i)
  18. {
  19. if (len < inf.width - inf.prec)
  20. return (1);
  21. }
  22. else if (len < inf.width - i)
  23. return (1);
  24. return (0);
  25. }
  26. static int pf_b2(int nb, char *str, t_inf inf, int len)
  27. {
  28. if (nb == 0 && inf.prec == -1)
  29. len += 0;
  30. else
  31. len += pf_putstr(str, inf.fd);
  32. if (inf.width > 0 && inf.min == 1)
  33. while (len < inf.width && len++ >= 0)
  34. ft_putchar_fd(' ', inf.fd);
  35. return (len);
  36. }
  37. int pf_b(va_list ap, t_inf inf)
  38. {
  39. char *str;
  40. int len;
  41. int nb;
  42. int cpt;
  43. nb = va_arg(ap, int);
  44. str = ft_itob(nb);
  45. len = 0;
  46. if (inf.zero && inf.prec != 0)
  47. inf.zero = 0;
  48. if (inf.width > 0 && !inf.min && !inf.zero && inf.width > inf.prec)
  49. while (pf_width_b(str, inf, len, nb) && len++ >= 0)
  50. ft_putchar_fd(' ', inf.fd);
  51. cpt = -1;
  52. if (inf.prec > 0)
  53. while (++cpt < inf.prec - ((int)ft_strlen(str)) && len++ >= 0)
  54. ft_putchar_fd('0', inf.fd);
  55. cpt = -1;
  56. if (inf.width && inf.zero && !inf.min)
  57. while (++cpt < inf.width - ((int)ft_strlen(str)) && len++ >= 0)
  58. ft_putchar_fd('0', inf.fd);
  59. return (pf_b2(nb, str, inf, len));
  60. }