dprintf_buffer.c
1 /* ************************************************************************** */ 2 /* */ 3 /* ::: :::::::: */ 4 /* dprintf_buffer.c :+: :+: :+: */ 5 /* +:+ +:+ +:+ */ 6 /* By: ll-hotel <ll-hotel@student.42lyon.fr> +#+ +:+ +#+ */ 7 /* +#+#+#+#+#+ +#+ */ 8 /* Created: 2024/02/15 12:48:12 by ll-hotel #+# #+# */ 9 /* Updated: 2024/04/24 16:19:05 by ll-hotel ### ########.fr */ 10 /* */ 11 /* ************************************************************************** */ 12 13 #include "dprintf_inner.h" 14 15 int ft_buffer_flush(t_buffer *buffer, int fd) 16 { 17 const int l = buffer->length[fd]; 18 19 buffer->length[fd] = 0; 20 return (write(fd, buffer->string[fd], l)); 21 } 22 23 int ft_buffer_cat(t_buffer *buffer, int fd, const char *s) 24 { 25 int *const length = buffer->length + fd; 26 char *string; 27 int ret_val; 28 char c; 29 30 if (!s) 31 return (0); 32 string = buffer->string[fd]; 33 ret_val = 0; 34 c = *s; 35 while (c) 36 { 37 if (*length >= DPRINTF_BUFFERSIZE) 38 ft_buffer_flush(buffer, fd); 39 string[(*length)++] = c; 40 if (c == '\n') 41 ft_buffer_flush(buffer, fd); 42 ret_val += 1; 43 c = *(++s); 44 } 45 return (ret_val); 46 } 47 48 int ft_buffer_cat_perc(t_buffer *buffer, int fd, const char *s) 49 { 50 int *const length = buffer->length + fd; 51 char *string; 52 int ret_val; 53 char c; 54 55 if (!s) 56 return (0); 57 string = buffer->string[fd]; 58 ret_val = 0; 59 c = *s; 60 while (c && c != '%') 61 { 62 if (*length >= DPRINTF_BUFFERSIZE) 63 ft_buffer_flush(buffer, fd); 64 string[(*length)++] = c; 65 if (c == '\n') 66 ft_buffer_flush(buffer, fd); 67 ret_val += 1; 68 c = *(++s); 69 } 70 return (ret_val); 71 }