static int copy_buf(char *buf, char **line, int is_reading) { char endbuf; int buffsize; int linesize; buffsize = size_buf(buf); linesize = ft_strlen(*line); if (!(*line = ft_memrealloc(*line, linesize, linesize + buffsize + 1))) return (-1); if (!ft_memcpy(*line + linesize, buf, buffsize)) return (-1); if (buffsize != GNL_BUFF_SIZE) { endbuf = buf[buffsize]; if (!ft_memmove(buf, buf + buffsize + 1, GNL_BUFF_SIZE - buffsize - 1) || !ft_memset(buf + GNL_BUFF_SIZE - buffsize - 1, 0, buffsize + 1)) return (-1); return ((is_reading || *buf || endbuf == '\n') ? 1 : 0); } return ((!ft_memset(buf, 0, buffsize)) ? -1 : 0); }
int ff(char *debug, char *text, char **buf, int *len) { char *tmp; size_t size; if (len[4] == 2) { write(1, "\033[31m", 5); write(1, debug, my_strlen(debug)); write(1, "\033[0m\n\n", 6); } size = my_strlen(text); if (!(tmp = ft_memrealloc(*buf, *len, *len + size))) { if (*buf) free(*buf); write(2, "Error", 5); return (0); } *buf = tmp; ft_memcpy(*buf + *len, text, size); *len += size; return (1); }
int ft_arr_push(t_arr *arr, const void *to_push, int index) { t_arr *tmp; unsigned char *ptr; if (!arr || !to_push) return (EXIT_FAILURE); tmp = arr; index = index < 0 ? (int)tmp->length : index; index = index > (int)tmp->length ? (int)tmp->length : index; if ((tmp->length + 1) * tmp->sizeof_elem > tmp->alloc_len) { if (!(tmp->ptr = ft_memrealloc( &tmp->ptr, tmp->alloc_len, tmp->alloc_len * MULTI_LENGHT_ARRAY))) return (EXIT_FAILURE); tmp->alloc_len *= MULTI_LENGHT_ARRAY; } ptr = (unsigned char *)tmp->ptr + (index * tmp->sizeof_elem); ft_memmove((unsigned char *)ptr + tmp->sizeof_elem, ptr,\ tmp->sizeof_elem * (tmp->length - index)); tmp->f_cpy((ptr), &to_push, tmp->sizeof_elem); tmp->length++; return (EXIT_SUCCESS); }