aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile6
-rw-r--r--README.md2
-rw-r--r--ft_asprintf.c24
-rw-r--r--ft_dprintf.c24
-rw-r--r--ft_printf.c24
-rw-r--r--ft_printf/convert.c122
-rw-r--r--ft_printf/convert_char.c53
-rw-r--r--ft_printf/convert_hex.c34
-rw-r--r--ft_printf/convert_int.c40
-rw-r--r--ft_printf/convert_none.c25
-rw-r--r--ft_printf/convert_percent.c23
-rw-r--r--ft_printf/convert_ptr.c29
-rw-r--r--ft_printf/convert_str.c25
-rw-r--r--ft_printf/convert_uint.c34
-rw-r--r--ft_printf/convert_written.c28
-rw-r--r--ft_printf/extract.c98
-rw-r--r--ft_printf/ft_printf.c92
-rw-r--r--ft_printf/ft_vasprintf.h155
-rw-r--r--ft_printf/length_modifier.c39
-rw-r--r--ft_printf/list.c67
-rw-r--r--ft_printf/parse.c61
-rw-r--r--ft_printf/utils.c115
-rw-r--r--ft_snprintf.c24
-rw-r--r--ft_sprintf.c24
-rw-r--r--ft_strncpy.c5
-rw-r--r--ft_vasprintf.c21
-rw-r--r--ft_vdprintf.c24
-rw-r--r--ft_vprintf.c18
-rw-r--r--ft_vsnprintf.c26
-rw-r--r--ft_vsprintf.c18
-rw-r--r--get_next_line.c114
-rw-r--r--get_next_line.h5
-rw-r--r--libft.h25
33 files changed, 1354 insertions, 70 deletions
diff --git a/Makefile b/Makefile
index 3360001..152e843 100644
--- a/Makefile
+++ b/Makefile
@@ -6,7 +6,7 @@
# By: cacharle <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2019/10/08 15:45:53 by cacharle #+# #+# #
-# Updated: 2019/11/20 04:13:32 by cacharle ### ########.fr #
+# Updated: 2019/11/21 03:43:46 by cacharle ### ########.fr #
# #
# **************************************************************************** #
@@ -28,7 +28,9 @@ SRC = ft_atoi.c ft_bzero.c ft_isalnum.c ft_isalpha.c ft_isascii.c ft_isdigit.c \
ft_strncmp.c ft_strncpy.c ft_strnequ.c ft_strnew.c ft_strnstr.c \
ft_strrchr.c ft_split.c ft_strstr.c ft_substr.c ft_strtrim.c \
ft_tolower.c ft_toupper.c ft_strlcpy.c ft_calloc.c ft_strndup.c \
- ft_strjoin_free.c ft_strjoin_free_snd.c get_next_line.c ft_strcount.c
+ ft_strjoin_free.c ft_strjoin_free_snd.c get_next_line.c ft_strcount.c \
+ ft_printf.c ft_sprintf.c ft_snprintf.c ft_asprintf.c ft_dprintf.c \
+ ft_vprintf.c ft_vsprintf.c ft_vsnprintf.c ft_vasprintf.c ft_vdprintf.c
OBJ = $(SRC:.c=.o)
INCLUDE = libft.h
diff --git a/README.md b/README.md
index 9fe84d4..13ee0f7 100644
--- a/README.md
+++ b/README.md
@@ -2,6 +2,6 @@
The state of this repo when I pass can be checked with the [raw](http://github.com/HappyTramp/libft/tree/raw) tag.
-I have both the pre 2019 and current functions. There is also my [get\_nex\_line](http://github.com/HappyTramp/get_next_line) in the mix.
+I have both the pre 2019 and current functions. There is also my [get\_nex\_line](http://github.com/HappyTramp/get_next_line) and [ft\_printf](http://github.com/HappyTramp/ft_printf) in the mix.
The [rendu](http://github.com/HappyTramp/libft/tree/rendu) branch is the one I use for my other projects, it doesnt contain .gitignore, README.md and subjects.
diff --git a/ft_asprintf.c b/ft_asprintf.c
new file mode 100644
index 0000000..5eb62d9
--- /dev/null
+++ b/ft_asprintf.c
@@ -0,0 +1,24 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_asprintf.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/11/21 02:30:33 by cacharle #+# #+# */
+/* Updated: 2019/11/21 03:43:08 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+int ft_asprintf(char **ret, const char *format, ...)
+{
+ int vret;
+ va_list ap;
+
+ va_start(ap, format);
+ vret = ft_vasprintf(ret, format, ap);
+ va_end(ap);
+ return (vret);
+}
diff --git a/ft_dprintf.c b/ft_dprintf.c
new file mode 100644
index 0000000..8e60970
--- /dev/null
+++ b/ft_dprintf.c
@@ -0,0 +1,24 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_dprintf.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/11/21 02:29:11 by cacharle #+# #+# */
+/* Updated: 2019/11/21 03:42:05 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+int ft_dprintf(int fd, const char *format, ...)
+{
+ int ret;
+ va_list ap;
+
+ va_start(ap, format);
+ ret = ft_vdprintf(fd, format, ap);
+ va_end(ap);
+ return (ret);
+}
diff --git a/ft_printf.c b/ft_printf.c
new file mode 100644
index 0000000..1b92bb2
--- /dev/null
+++ b/ft_printf.c
@@ -0,0 +1,24 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_printf.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/11/21 02:31:32 by cacharle #+# #+# */
+/* Updated: 2019/11/21 03:41:54 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+int ft_printf(const char *format, ...)
+{
+ int ret;
+ va_list ap;
+
+ va_start(ap, format);
+ ret = ft_vprintf(format, ap);
+ va_end(ap);
+ return (ret);
+}
diff --git a/ft_printf/convert.c b/ft_printf/convert.c
new file mode 100644
index 0000000..398c754
--- /dev/null
+++ b/ft_printf/convert.c
@@ -0,0 +1,122 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* printer.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/10/28 23:19:24 by cacharle #+# #+# */
+/* Updated: 2019/11/14 10:22:04 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include "libft.h"
+#include "ft_vasprintf.h"
+
+char *convert(t_pformat *pformat, va_list ap)
+{
+ char *str;
+
+ if (pformat == NULL)
+ return (NULL);
+ if (pformat->flags & FLAG_WIDTH_WILDCARD)
+ {
+ if (pformat->flags & FLAG_WIDTH_OVERWRITE)
+ (void)va_arg(ap, int);
+ else
+ pformat->width = va_arg(ap, int);
+ if (pformat->width < 0)
+ {
+ pformat->flags |= FLAG_MINUS;
+ pformat->width *= -1;
+ }
+ }
+ if (pformat->flags & FLAG_PRECISION_WILDCARD)
+ pformat->precision = va_arg(ap, int);
+ if ((str = convert_specifier(ap, pformat)) == NULL)
+ return (NULL);
+ return (str);
+}
+
+char *convert_specifier(va_list ap, t_pformat *pformat)
+{
+ if (pformat->specifier == 'c')
+ return (convert_char(ap, pformat));
+ if (pformat->specifier == 's')
+ return (convert_str(ap, pformat));
+ if (pformat->specifier == 'p')
+ return (convert_ptr(ap, pformat));
+ if (pformat->specifier == 'd' || pformat->specifier == 'i')
+ return (convert_int(ap, pformat));
+ if (pformat->specifier == 'u')
+ return (convert_uint(ap, pformat));
+ if (pformat->specifier == 'x')
+ return (convert_hex(ap, pformat));
+ if (pformat->specifier == 'X')
+ return (convert_hex(ap, pformat));
+ if (pformat->specifier == '%')
+ return (convert_percent(ap, pformat));
+ if (pformat->specifier == 'n')
+ return (convert_written(ap, pformat));
+ else
+ return (convert_none(ap, pformat));
+ return (NULL);
+}
+
+char *handle_width(t_pformat *pformat, char *str)
+{
+ char *tmp;
+ int len;
+ int i;
+
+ if ((len = ft_strlen(str)) >= pformat->width)
+ return (str);
+ if ((tmp = (char*)malloc(sizeof(char) * (pformat->width + 1))) == NULL)
+ return (NULL);
+ if (pformat->flags & FLAG_MINUS)
+ {
+ i = len;
+ ft_strcpy(tmp, str);
+ while (i < pformat->width)
+ tmp[i++] = ' ';
+ tmp[i] = 0;
+ }
+ else
+ {
+ i = 0;
+ while (i <= pformat->width - len)
+ tmp[i++] = pformat->flags & FLAG_ZERO ? '0' : ' ';
+ ft_strcpy(tmp + i - 1, str);
+ }
+ free(str);
+ return (tmp);
+}
+
+char *handle_precision(t_pformat *pformat, char *str)
+{
+ int len;
+ char *tmp;
+
+ if (pformat == NULL || str == NULL)
+ return (NULL);
+ if (ft_strchr("diuxX", pformat->specifier) && pformat->precision >= 0)
+ pformat->flags &= ~FLAG_ZERO;
+ len = ft_strlen(str);
+ if (pformat->precision == 0 && str[0] == '0')
+ {
+ free(str);
+ return (ft_strdup(""));
+ }
+ if (!ft_strchr("diuxXp", pformat->specifier) || len >= pformat->precision)
+ return (str);
+ if ((tmp = (char*)malloc(sizeof(char) * (pformat->precision + 1))) == NULL)
+ return (NULL);
+ ft_strcpy(tmp + pformat->precision - len, str);
+ while (pformat->precision-- > len)
+ tmp[pformat->precision - len] = '0';
+ free(str);
+ return (tmp);
+}
diff --git a/ft_printf/convert_char.c b/ft_printf/convert_char.c
new file mode 100644
index 0000000..c5f3a93
--- /dev/null
+++ b/ft_printf/convert_char.c
@@ -0,0 +1,53 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* convert_char.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/10/30 23:22:29 by cacharle #+# #+# */
+/* Updated: 2019/11/05 23:44:42 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_vasprintf.h"
+
+static char *handle_width_char(t_pformat *pformat, char *str)
+{
+ char *tmp;
+ int i;
+
+ pformat->size = 1;
+ if (1 >= pformat->width)
+ return (str);
+ if ((tmp = (char*)malloc(sizeof(char) * (pformat->width + 1))) == NULL)
+ return (NULL);
+ if (pformat->flags & FLAG_MINUS)
+ {
+ ft_memcpy(tmp, str, (i = 1) + 1);
+ while (i < pformat->width)
+ tmp[i++] = ' ';
+ tmp[i] = 0;
+ }
+ else
+ {
+ i = 0;
+ while (i <= pformat->width - 1)
+ tmp[i++] = pformat->flags & FLAG_ZERO ? '0' : ' ';
+ ft_memcpy(tmp + i - 1, str, 2);
+ }
+ free(str);
+ pformat->size = pformat->width;
+ return (tmp);
+}
+
+char *convert_char(va_list ap, t_pformat *pformat)
+{
+ char *str;
+
+ if ((str = ft_strnew(2)) == NULL)
+ return (NULL);
+ str[0] = va_arg(ap, int);
+ str[1] = '\0';
+ return (handle_width_char(pformat, str));
+}
diff --git a/ft_printf/convert_hex.c b/ft_printf/convert_hex.c
new file mode 100644
index 0000000..0464dc7
--- /dev/null
+++ b/ft_printf/convert_hex.c
@@ -0,0 +1,34 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* convert_hex.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/10/30 23:23:06 by cacharle #+# #+# */
+/* Updated: 2019/11/05 23:58:59 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_vasprintf.h"
+
+char *convert_hex(va_list ap, t_pformat *pformat)
+{
+ char *str;
+ long long unsigned int n;
+
+ n = length_modifier_unsigned_int(ap, pformat);
+ str = pformat->specifier == 'x' ? ITOA_HEX_LOW(n) : ITOA_HEX_UP(n);
+ str = handle_precision(pformat, str);
+ if (pformat->flags & FLAG_ZERO)
+ {
+ if (pformat->flags & FLAG_ALTERNATE && n != 0)
+ pformat->width -= 2;
+ str = handle_width(pformat, str);
+ }
+ if (pformat->flags & FLAG_ALTERNATE && n != 0)
+ str = ft_strjoin_free_snd(pformat->specifier == 'X' ? "0X" : "0x", str);
+ if (!(pformat->flags & FLAG_ZERO))
+ str = handle_width(pformat, str);
+ return (str);
+}
diff --git a/ft_printf/convert_int.c b/ft_printf/convert_int.c
new file mode 100644
index 0000000..2345f76
--- /dev/null
+++ b/ft_printf/convert_int.c
@@ -0,0 +1,40 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* convert_int.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/10/30 23:29:53 by cacharle #+# #+# */
+/* Updated: 2019/11/06 00:00:09 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_vasprintf.h"
+
+char *convert_int(va_list ap, t_pformat *pformat)
+{
+ int is_neg;
+ long long int n;
+ char *str;
+
+ n = length_modifier_int(ap, pformat);
+ is_neg = n < 0;
+ str = ITOA_DEC(n);
+ if (is_neg)
+ ft_strcpy(str, str + 1);
+ str = handle_precision(pformat, str);
+ if (pformat->flags & FLAG_ZERO)
+ {
+ if (is_neg || pformat->flags & (FLAG_SIGNED | FLAG_SPACE))
+ pformat->width--;
+ str = handle_width(pformat, str);
+ }
+ if (is_neg)
+ str = ft_strjoin_free_snd("-", str);
+ else if (pformat->flags & (FLAG_SIGNED | FLAG_SPACE))
+ str = ft_strjoin_free_snd(pformat->flags & FLAG_SPACE ? " " : "+", str);
+ if (!(pformat->flags & FLAG_ZERO))
+ str = handle_width(pformat, str);
+ return (str);
+}
diff --git a/ft_printf/convert_none.c b/ft_printf/convert_none.c
new file mode 100644
index 0000000..358ef1b
--- /dev/null
+++ b/ft_printf/convert_none.c
@@ -0,0 +1,25 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* convert_none.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/11/04 19:30:25 by cacharle #+# #+# */
+/* Updated: 2019/11/05 23:44:13 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_vasprintf.h"
+
+char *convert_none(va_list ap, t_pformat *pformat)
+{
+ char *str;
+
+ (void)ap;
+ if ((str = ft_strdup("")) == NULL)
+ return (NULL);
+ str = handle_precision(pformat, str);
+ str = handle_width(pformat, str);
+ return (str);
+}
diff --git a/ft_printf/convert_percent.c b/ft_printf/convert_percent.c
new file mode 100644
index 0000000..813bb77
--- /dev/null
+++ b/ft_printf/convert_percent.c
@@ -0,0 +1,23 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* convert_percent.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/10/30 23:23:27 by cacharle #+# #+# */
+/* Updated: 2019/11/05 23:44:07 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_vasprintf.h"
+
+char *convert_percent(va_list ap, t_pformat *pformat)
+{
+ char *str;
+
+ (void)ap;
+ str = ft_strdup("%");
+ str = handle_width(pformat, str);
+ return (str);
+}
diff --git a/ft_printf/convert_ptr.c b/ft_printf/convert_ptr.c
new file mode 100644
index 0000000..63babb9
--- /dev/null
+++ b/ft_printf/convert_ptr.c
@@ -0,0 +1,29 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* convert_ptr.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/10/30 23:24:08 by cacharle #+# #+# */
+/* Updated: 2019/11/05 23:43:45 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_vasprintf.h"
+
+char *convert_ptr(va_list ap, t_pformat *pformat)
+{
+ char *str;
+
+ str = ITOA_HEX_LOW((long long unsigned int)va_arg(ap, void*));
+ str = handle_precision(pformat, str);
+ if (!(pformat->flags & FLAG_ZERO))
+ str = ft_strjoin_free_snd("0x", str);
+ if (pformat->flags & FLAG_ZERO)
+ pformat->width -= 2;
+ str = handle_width(pformat, str);
+ if (pformat->flags & FLAG_ZERO)
+ str = ft_strjoin_free_snd("0x", str);
+ return (str);
+}
diff --git a/ft_printf/convert_str.c b/ft_printf/convert_str.c
new file mode 100644
index 0000000..7d51a5e
--- /dev/null
+++ b/ft_printf/convert_str.c
@@ -0,0 +1,25 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* convert_str.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/10/30 23:22:25 by cacharle #+# #+# */
+/* Updated: 2019/11/09 01:07:24 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_vasprintf.h"
+
+char *convert_str(va_list ap, t_pformat *pformat)
+{
+ char *str;
+
+ str = va_arg(ap, char*);
+ str = str == NULL ? ft_strdup("(null)") : ft_strdup(str);
+ if (pformat->precision >= 0 && pformat->precision < (int)ft_strlen(str))
+ str[pformat->precision] = '\0';
+ str = handle_width(pformat, str);
+ return (str);
+}
diff --git a/ft_printf/convert_uint.c b/ft_printf/convert_uint.c
new file mode 100644
index 0000000..4207165
--- /dev/null
+++ b/ft_printf/convert_uint.c
@@ -0,0 +1,34 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* convert_uint.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/10/30 23:25:40 by cacharle #+# #+# */
+/* Updated: 2019/11/05 23:44:19 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_vasprintf.h"
+
+char *convert_uint(va_list ap, t_pformat *pformat)
+{
+ char *str;
+ long long unsigned int n;
+
+ if (pformat->flags & FLAG_SHORT)
+ n = (unsigned short)va_arg(ap, int);
+ else if (pformat->flags & FLAG_SHORT_SHORT)
+ n = (unsigned char)va_arg(ap, int);
+ else if (pformat->flags & FLAG_LONG)
+ n = va_arg(ap, long unsigned int);
+ else if (pformat->flags & FLAG_LONG_LONG)
+ n = va_arg(ap, long long unsigned int);
+ else
+ n = va_arg(ap, unsigned int);
+ str = ft_itoa_unsigned_base(n, "0123456789");
+ str = handle_precision(pformat, str);
+ str = handle_width(pformat, str);
+ return (str);
+}
diff --git a/ft_printf/convert_written.c b/ft_printf/convert_written.c
new file mode 100644
index 0000000..4beeaef
--- /dev/null
+++ b/ft_printf/convert_written.c
@@ -0,0 +1,28 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* convert_written.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/10/30 23:38:28 by cacharle #+# #+# */
+/* Updated: 2019/11/05 23:59:24 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_vasprintf.h"
+
+char *convert_written(va_list ap, t_pformat *pformat)
+{
+ if (pformat->flags & FLAG_SHORT)
+ pformat->written = (long long int*)va_arg(ap, signed char*);
+ if (pformat->flags & FLAG_SHORT_SHORT)
+ pformat->written = (long long int*)va_arg(ap, short*);
+ if (pformat->flags & FLAG_LONG)
+ pformat->written = (long long int*)va_arg(ap, long int*);
+ if (pformat->flags & FLAG_LONG_LONG)
+ pformat->written = va_arg(ap, long long int*);
+ else
+ pformat->written = (long long int*)va_arg(ap, int*);
+ return (NULL);
+}
diff --git a/ft_printf/extract.c b/ft_printf/extract.c
new file mode 100644
index 0000000..c56a777
--- /dev/null
+++ b/ft_printf/extract.c
@@ -0,0 +1,98 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* extract.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/10/29 00:10:36 by cacharle #+# #+# */
+/* Updated: 2019/11/10 10:33:33 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_vasprintf.h"
+
+const char *extract_flags(t_pformat *pformat, const char *fmt)
+{
+ if (*fmt == '\0')
+ return (fmt);
+ while (ft_strchr(FLAGS_STR, *fmt) != NULL)
+ {
+ if (*fmt == '0')
+ pformat->flags |= FLAG_ZERO;
+ if (*fmt == '-')
+ pformat->flags |= FLAG_MINUS;
+ if (*fmt == '+')
+ pformat->flags |= FLAG_SIGNED;
+ if (*fmt == ' ')
+ pformat->flags |= FLAG_SPACE;
+ if (*fmt == '#')
+ pformat->flags |= FLAG_ALTERNATE;
+ if (*fmt == '\'')
+ ;
+ fmt++;
+ }
+ if (pformat->flags & FLAG_SIGNED)
+ pformat->flags &= ~FLAG_SPACE;
+ return (fmt);
+}
+
+const char *extract_width(t_pformat *pformat, const char *fmt)
+{
+ if (*fmt == '\0')
+ return (fmt);
+ if (*fmt == '*')
+ {
+ pformat->flags |= FLAG_WIDTH_WILDCARD;
+ fmt++;
+ }
+ if (!ft_isdigit(*fmt))
+ return (fmt);
+ pformat->width = ft_atoi(fmt);
+ while (*fmt && ft_isdigit(*fmt))
+ fmt++;
+ if (pformat->flags & FLAG_WIDTH_WILDCARD)
+ pformat->flags |= FLAG_WIDTH_OVERWRITE;
+ return (fmt);
+}
+
+const char *extract_precision(t_pformat *pformat, const char *fmt)
+{
+ if (*fmt == '\0' || *fmt != '.')
+ return (fmt);
+ fmt++;
+ if (*fmt == '*')
+ {
+ pformat->flags |= FLAG_PRECISION_WILDCARD;
+ fmt++;
+ }
+ pformat->precision = ft_atoi(fmt);
+ while (*fmt && ft_isdigit(*fmt))
+ fmt++;
+ return (fmt);
+}
+
+const char *extract_length_modifier(t_pformat *pformat, const char *fmt)
+{
+ if (fmt[0] && fmt[0] == 'l')
+ {
+ if (fmt[1] && fmt[1] == 'l')
+ {
+ pformat->flags |= FLAG_LONG_LONG;
+ return (fmt + 2);
+ }
+ pformat->flags |= FLAG_LONG;
+ return (fmt + 1);
+ }
+ if (fmt[0] && fmt[0] == 'h')
+ {
+ if (fmt[1] && fmt[1] == 'h')
+ {
+ pformat->flags |= FLAG_SHORT_SHORT;
+ return (fmt + 2);
+ }
+ pformat->flags |= FLAG_SHORT;
+ return (fmt + 1);
+ }
+ return (fmt);
+}
diff --git a/ft_printf/ft_printf.c b/ft_printf/ft_printf.c
new file mode 100644
index 0000000..daa0cf2
--- /dev/null
+++ b/ft_printf/ft_printf.c
@@ -0,0 +1,92 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_printf.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/10/29 00:15:28 by cacharle #+# #+# */
+/* Updated: 2019/11/13 08:56:49 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_vasprintf.h"
+
+int ft_printf(const char *format, ...)
+{
+ t_printf_status status;
+
+ if (format == NULL)
+ return (STATUS_ERROR);
+ if (!parse(format, &status.flist))
+ return (STATUS_ERROR);
+ va_start(status.ap, format);
+ status.format = format;
+ status.out = NULL;
+ status.out_size = 0;
+ while (*status.format)
+ {
+ if (*status.format == '%'
+ && (status.format =
+ add_conversion(&status, status.flist->content)) == NULL)
+ return (destroy_status_error(&status));
+ else if ((status.format = add_between(&status)) == NULL)
+ return (destroy_status_error(&status));
+ }
+ va_end(status.ap);
+ list_destroy(&status.flist);
+ write(STDOUT_FILENO, status.out, status.out_size);
+ free(status.out);
+ return (status.out_size);
+}
+
+const char *add_conversion(t_printf_status *status, t_pformat *pformat)
+{
+ char *conversion_str;
+
+ conversion_str = convert(pformat, status->ap);
+ if (pformat->specifier == 'n')
+ {
+ if (pformat->written != NULL)
+ *pformat->written = status->out_size;
+ status->format += pformat->fmt_len;
+ list_pop_front(&status->flist);
+ return (status->format + 1);
+ }
+ if (conversion_str == NULL)
+ return (NULL);
+ if (pformat->specifier != 'c')
+ pformat->size = ft_strlen(conversion_str);
+ if ((status->out = ft_memjoin_free(status->out, status->out_size,
+ conversion_str, pformat->size)) == NULL)
+ return (NULL);
+ status->out_size += pformat->size;
+ free(conversion_str);
+ status->format += pformat->fmt_len;
+ list_pop_front(&status->flist);
+ return (status->format + 1);
+}
+
+const char *add_between(t_printf_status *status)
+{
+ int i;
+
+ i = 0;
+ while (status->format[i] && status->format[i] != '%')
+ i++;
+ if ((status->out = ft_memjoin_free(status->out, status->out_size,
+ (void*)status->format, i)) == NULL)
+ return (NULL);
+ status->out_size += i;
+ return (status->format + i);
+}
+
+int destroy_status_error(t_printf_status *status)
+{
+ if (status == NULL)
+ return (STATUS_ERROR);
+ va_end(status->ap);
+ list_destroy(&status->flist);
+ free(status->out);
+ return (STATUS_ERROR);
+}
diff --git a/ft_printf/ft_vasprintf.h b/ft_printf/ft_vasprintf.h
new file mode 100644
index 0000000..4110557
--- /dev/null
+++ b/ft_printf/ft_vasprintf.h
@@ -0,0 +1,155 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* header.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/10/29 00:06:46 by cacharle #+# #+# */
+/* Updated: 2019/11/13 09:29:23 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#ifndef HEADER_H
+# define HEADER_H
+
+# include <unistd.h>
+# include <stdlib.h>
+# include <stdarg.h>
+# include "libft.h"
+
+# define STATUS_ERROR -1
+
+# define SPECIFIERS_STR "nfcspdiuxX%"
+# define FLAGS_STR "#0- +'"
+
+# define IS_STANDALONE_FLAG(c) (ft_strchr(FLAGS_STR, c) != NULL)
+
+# define FLAG_MINUS (1 << 0)
+# define FLAG_ZERO (1 << 1)
+# define FLAG_SIGNED (1 << 2)
+# define FLAG_SPACE (1 << 3)
+# define FLAG_ALTERNATE (1 << 4)
+# define FLAG_SHORT (1 << 5)
+# define FLAG_SHORT_SHORT (1 << 6)
+# define FLAG_LONG (1 << 7)
+# define FLAG_LONG_LONG (1 << 8)
+# define FLAG_WIDTH_WILDCARD (1 << 9)
+# define FLAG_PRECISION_WILDCARD (1 << 10)
+# define FLAG_WIDTH_OVERWRITE (1 << 11)
+
+# define ITOA_HEX_LOW(x) (ft_itoa_unsigned_base(x, "0123456789abcdef"))
+# define ITOA_HEX_UP(x) (ft_itoa_unsigned_base(x, "0123456789ABCDEF"))
+# define ITOA_DEC(x) (ft_itoa_base(x, "0123456789"))
+
+typedef int t_bool;
+typedef short t_flags;
+typedef long long int t_big_int;
+typedef long long unsigned int t_big_uint;
+
+typedef struct
+{
+ int precision;
+ int width;
+ t_flags flags;
+ char specifier;
+ int fmt_len;
+ int size;
+ long long int *written;
+} t_pformat;
+
+typedef struct s_flist
+{
+ struct s_flist *next;
+ t_pformat *content;
+} t_flist;
+
+typedef struct s_printf_status
+{
+ va_list ap;
+ t_flist *flist;
+ const char *format;
+ char *out;
+ int out_size;
+} t_printf_status;
+
+/*
+** ft_printf.c
+*/
+
+int ft_printf(const char *format, ...);
+const char *add_conversion(t_printf_status *status,
+ t_pformat *pformat);
+const char *add_between(t_printf_status *status);
+int destroy_status_error(t_printf_status *status);
+
+/*
+** parse.c
+*/
+
+int parse(const char *format, t_flist **flist);
+t_pformat *parse_reduced(const char *fmt);
+
+/*
+** printer.c
+*/
+
+char *convert(t_pformat *pformat, va_list ap);
+char *convert_specifier(va_list ap, t_pformat *pformat);
+char *handle_width(t_pformat *pformat, char *str);
+char *handle_precision(t_pformat *pformat, char *str);
+
+/*
+** utils.c
+*/
+
+char *ft_itoa_base(long long int n, char *base);
+char *ft_itoa_unsigned_base(long long unsigned int n,
+ char *base);
+void *ft_memjoin_free(void *dst, int dst_size, void *src,
+ int src_size);
+
+/*
+** extract.c
+*/
+
+const char *extract_flags(t_pformat *pformat, const char *fmt);
+const char *extract_width(t_pformat *pformat, const char *fmt);
+const char *extract_precision(t_pformat *pformat, const char *fmt);
+const char *extract_length_modifier(t_pformat *pformat,
+ const char *fmt);
+
+/*
+** list.c
+*/
+
+t_flist *list_new(t_pformat *content);
+void *list_destroy(t_flist **lst);
+void list_push_front(t_flist **lst, t_flist *new);
+void list_pop_front(t_flist **lst);
+t_flist *list_reverse(t_flist *lst);
+
+/*
+** convert_*.c
+*/
+
+char *convert_char(va_list ap, t_pformat *pformat);
+char *convert_str(va_list ap, t_pformat *pformat);
+char *convert_ptr(va_list ap, t_pformat *pformat);
+char *convert_int(va_list ap, t_pformat *pformat);
+char *convert_uint(va_list ap, t_pformat *pformat);
+char *convert_hex(va_list ap, t_pformat *pformat);
+char *convert_percent(va_list ap, t_pformat *pformat);
+char *convert_written(va_list ap, t_pformat *pformat);
+char *convert_double(va_list ap, t_pformat *pformat);
+char *convert_none(va_list ap, t_pformat *pformat);
+
+/*
+** length_modifier.c
+*/
+
+t_big_uint length_modifier_unsigned_int(
+ va_list ap, t_pformat *pformat);
+t_big_int length_modifier_int(va_list ap, t_pformat *pformat);
+
+#endif
diff --git a/ft_printf/length_modifier.c b/ft_printf/length_modifier.c
new file mode 100644
index 0000000..88226da
--- /dev/null
+++ b/ft_printf/length_modifier.c
@@ -0,0 +1,39 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* length_modifier.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/11/05 23:56:07 by cacharle #+# #+# */
+/* Updated: 2019/11/09 00:50:06 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_vasprintf.h"
+
+t_big_uint length_modifier_unsigned_int(va_list ap, t_pformat *pformat)
+{
+ if (pformat->flags & FLAG_SHORT)
+ return ((unsigned short)va_arg(ap, int));
+ else if (pformat->flags & FLAG_SHORT_SHORT)
+ return ((unsigned char)va_arg(ap, int));
+ else if (pformat->flags & FLAG_LONG)
+ return (va_arg(ap, long unsigned int));
+ else if (pformat->flags & FLAG_LONG_LONG)
+ return (va_arg(ap, long long unsigned int));
+ return (va_arg(ap, unsigned int));
+}
+
+t_big_int length_modifier_int(va_list ap, t_pformat *pformat)
+{
+ if (pformat->flags & FLAG_SHORT)
+ return ((short)va_arg(ap, int));
+ else if (pformat->flags & FLAG_SHORT_SHORT)
+ return ((signed char)va_arg(ap, int));
+ else if (pformat->flags & FLAG_LONG)
+ return (va_arg(ap, long int));
+ else if (pformat->flags & FLAG_LONG_LONG)
+ return (va_arg(ap, long long int));
+ return (va_arg(ap, int));
+}
diff --git a/ft_printf/list.c b/ft_printf/list.c
new file mode 100644
index 0000000..99491f4
--- /dev/null
+++ b/ft_printf/list.c
@@ -0,0 +1,67 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* list.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/10/29 00:14:50 by cacharle #+# #+# */
+/* Updated: 2019/11/05 23:45:42 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_vasprintf.h"
+
+t_flist *list_new(t_pformat *content)
+{
+ t_flist *lst;
+
+ if ((lst = (t_flist*)malloc(sizeof(t_flist))) == NULL)
+ return (NULL);
+ lst->content = content;
+ lst->next = NULL;
+ return (lst);
+}
+
+void *list_destroy(t_flist **lst)
+{
+ if (lst == NULL)
+ return (NULL);
+ while (*lst != NULL)
+ list_pop_front(lst);
+ return (NULL);
+}
+
+void list_push_front(t_flist **lst, t_flist *new)
+{
+ if (lst == NULL || new == NULL)
+ return ;
+ new->next = *lst;
+ *lst = new;
+}
+
+void list_pop_front(t_flist **lst)
+{
+ t_flist *tmp;
+
+ if (lst == NULL || *lst == NULL)
+ return ;
+ tmp = (*lst)->next;
+ free((*lst)->content);
+ free(*lst);
+ *lst = tmp;
+}
+
+t_flist *list_reverse(t_flist *lst)
+{
+ t_flist *tmp;
+
+ if (lst == NULL)
+ return (NULL);
+ if (lst->next == NULL)
+ return (lst);
+ tmp = list_reverse(lst->next);
+ lst->next->next = lst;
+ lst->next = NULL;
+ return (tmp);
+}
diff --git a/ft_printf/parse.c b/ft_printf/parse.c
new file mode 100644
index 0000000..33928a0
--- /dev/null
+++ b/ft_printf/parse.c
@@ -0,0 +1,61 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* parse.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/10/29 00:11:33 by cacharle #+# #+# */
+/* Updated: 2019/11/13 08:13:02 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_vasprintf.h"
+
+int parse(const char *format, t_flist **flist)
+{
+ t_flist *tmp;
+ t_pformat *parsed;
+
+ *flist = NULL;
+ while (*format)
+ {
+ format++;
+ if (format[-1] != '%')
+ continue;
+ if ((parsed = parse_reduced(format)) == NULL)
+ return ((int)list_destroy(flist));
+ if ((tmp = list_new(parsed)) == NULL)
+ return ((int)list_destroy(flist));
+ list_push_front(flist, tmp);
+ format += (*flist)->content->fmt_len;
+ }
+ *flist = list_reverse(*flist);
+ return (1);
+}
+
+t_pformat *parse_reduced(const char *fmt)
+{
+ t_pformat *pformat;
+ const char *start;
+
+ if ((pformat = (t_pformat*)malloc(sizeof(t_pformat))) == NULL)
+ return (NULL);
+ pformat->precision = -1;
+ pformat->width = -1;
+ pformat->flags = 0;
+ start = fmt;
+ fmt = extract_flags(pformat, fmt);
+ fmt = extract_width(pformat, fmt);
+ fmt = extract_precision(pformat, fmt);
+ fmt = extract_length_modifier(pformat, fmt);
+ pformat->fmt_len = fmt - start + 1;
+ if (*fmt == '\0' || ft_strchr(SPECIFIERS_STR, *fmt) == NULL)
+ {
+ pformat->fmt_len--;
+ pformat->specifier = '_';
+ }
+ else
+ pformat->specifier = *ft_strchr(SPECIFIERS_STR, *fmt);
+ return (pformat);
+}
diff --git a/ft_printf/utils.c b/ft_printf/utils.c
new file mode 100644
index 0000000..ad44980
--- /dev/null
+++ b/ft_printf/utils.c
@@ -0,0 +1,115 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* utils.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/10/29 00:12:40 by cacharle #+# #+# */
+/* Updated: 2019/11/13 08:49:58 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_vasprintf.h"
+
+static int nbrlen_radix(long long int nbr, int radix)
+{
+ int counter;
+ long long unsigned int u_nbr;
+
+ if (nbr == 0)
+ return (1);
+ counter = 0;
+ u_nbr = nbr;
+ if (nbr < 0)
+ {
+ counter++;
+ u_nbr = -nbr;
+ }
+ while (u_nbr > 0)
+ {
+ u_nbr /= radix;
+ counter++;
+ }
+ return (counter);
+}
+
+char *ft_itoa_base(long long int n, char *base)
+{
+ char *str;
+ int len;
+ int radix;
+ long long unsigned int u_nbr;
+
+ radix = ft_strlen(base);
+ len = nbrlen_radix(n, radix);
+ if ((str = (char*)malloc(sizeof(char) * (len + 1))) == NULL)
+ return (NULL);
+ str[len] = '\0';
+ u_nbr = n < 0 ? -n : n;
+ if (n < 0)
+ str[0] = '-';
+ while (--len >= (n < 0 ? 1 : 0))
+ {
+ str[len] = base[u_nbr % radix];
+ u_nbr /= radix;
+ }
+ return (str);
+}
+
+static int nbrlen_unsigned_radix(long long unsigned int nbr, int radix)
+{
+ int counter;
+
+ if (nbr == 0)
+ return (1);
+ counter = 0;
+ while (nbr > 0)
+ {
+ nbr /= radix;
+ counter++;
+ }
+ return (counter);
+}
+
+char *ft_itoa_unsigned_base(long long unsigned int n, char *base)
+{
+ char *str;
+ int len;
+ int radix;
+
+ radix = ft_strlen(base);
+ len = nbrlen_unsigned_radix(n, radix);
+ if ((str = (char*)malloc(sizeof(char) * (len + 1))) == NULL)
+ return (NULL);
+ str[len] = '\0';
+ while (--len >= 0)
+ {
+ str[len] = base[n % radix];
+ n /= radix;
+ }
+ return (str);
+}
+
+void *ft_memjoin_free(void *dst, int dst_size, void *src, int src_size)
+{
+ void *clone;
+
+ if (dst == NULL)
+ {
+ if ((dst = malloc(src_size)) == NULL)
+ return (NULL);
+ ft_memcpy(dst, src, src_size);
+ return (dst);
+ }
+ if ((clone = malloc(dst_size)) == NULL)
+ return (NULL);
+ ft_memcpy(clone, dst, dst_size);
+ free(dst);
+ if ((dst = malloc(dst_size + src_size)) == NULL)
+ return (NULL);
+ ft_memcpy(dst, clone, dst_size);
+ free(clone);
+ ft_memcpy(dst + dst_size, src, src_size);
+ return (dst);
+}
diff --git a/ft_snprintf.c b/ft_snprintf.c
new file mode 100644
index 0000000..e1fdfbd
--- /dev/null
+++ b/ft_snprintf.c
@@ -0,0 +1,24 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_snprintf.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/11/21 02:27:55 by cacharle #+# #+# */
+/* Updated: 2019/11/21 03:41:49 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+int ft_snprintf(char *str, size_t size, const char *format, ...)
+{
+ int ret;
+ va_list ap;
+
+ va_start(ap, format);
+ ret = ft_vsnprintf(str, size, format, ap);
+ va_end(ap);
+ return (ret);
+}
diff --git a/ft_sprintf.c b/ft_sprintf.c
new file mode 100644
index 0000000..31da75e
--- /dev/null
+++ b/ft_sprintf.c
@@ -0,0 +1,24 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_sprintf.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/11/21 02:17:21 by cacharle #+# #+# */
+/* Updated: 2019/11/21 03:42:28 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+int ft_sprintf(char *str, const char *format, ...)
+{
+ int ret;
+ va_list ap;
+
+ va_start(ap, format);
+ ret = ft_vsprintf(str, format, ap);
+ va_end(ap);
+ return (ret);
+}
diff --git a/ft_strncpy.c b/ft_strncpy.c
index 3dbe2c9..a0cfb4c 100644
--- a/ft_strncpy.c
+++ b/ft_strncpy.c
@@ -6,13 +6,12 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/07 10:26:59 by cacharle #+# #+# */
-/* Updated: 2019/11/21 02:01:34 by cacharle ### ########.fr */
+/* Updated: 2019/11/21 02:49:48 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
-
char *ft_strncpy(char *dest, const char *src, size_t n)
{
size_t len;
@@ -20,6 +19,6 @@ char *ft_strncpy(char *dest, const char *src, size_t n)
len = ft_strlen(src);
ft_memcpy(dest, src, MIN(n, len));
if (len < n)
- ft_bzero(dest + len , n - len);
+ ft_bzero(dest + len, n - len);
return (dest);
}
diff --git a/ft_vasprintf.c b/ft_vasprintf.c
new file mode 100644
index 0000000..85f66bc
--- /dev/null
+++ b/ft_vasprintf.c
@@ -0,0 +1,21 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_vasprintf.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/11/21 02:49:56 by cacharle #+# #+# */
+/* Updated: 2019/11/21 03:45:39 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+int ft_vasprintf(char **ret, const char *format, va_list ap)
+{
+ (void)ret;
+ (void)format;
+ (void)ap;
+ return (0);
+}
diff --git a/ft_vdprintf.c b/ft_vdprintf.c
new file mode 100644
index 0000000..a5e5ebf
--- /dev/null
+++ b/ft_vdprintf.c
@@ -0,0 +1,24 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_vdprintf.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/11/21 02:40:03 by cacharle #+# #+# */
+/* Updated: 2019/11/21 03:46:00 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+int ft_vdprintf(int fd, const char *format, va_list ap)
+{
+ int out_len;
+ char *out;
+
+ if ((out_len = ft_vasprintf(&out, format, ap)) == -1)
+ return (-1);
+ write(fd, out, out_len);
+ return (out_len);
+}
diff --git a/ft_vprintf.c b/ft_vprintf.c
new file mode 100644
index 0000000..b98670b
--- /dev/null
+++ b/ft_vprintf.c
@@ -0,0 +1,18 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_vprintf.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/11/21 02:32:44 by cacharle #+# #+# */
+/* Updated: 2019/11/21 03:44:11 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+int ft_vprintf(const char *format, va_list ap)
+{
+ return (ft_vdprintf(STDOUT_FILENO, format, ap));
+}
diff --git a/ft_vsnprintf.c b/ft_vsnprintf.c
new file mode 100644
index 0000000..7db988c
--- /dev/null
+++ b/ft_vsnprintf.c
@@ -0,0 +1,26 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_vsnprintf.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/11/21 02:36:32 by cacharle #+# #+# */
+/* Updated: 2019/11/21 03:45:14 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+int ft_vsnprintf(char *str, size_t size, const char *format, va_list ap)
+{
+ int ret;
+ int full_out_len;
+ char *full_out;
+
+ full_out_len = ft_vasprintf(&full_out, format, ap);
+ ft_strncpy(str, full_out, size);
+ ret = MIN((size_t)full_out_len, size);
+ free(full_out);
+ return (ret);
+}
diff --git a/ft_vsprintf.c b/ft_vsprintf.c
new file mode 100644
index 0000000..91b4815
--- /dev/null
+++ b/ft_vsprintf.c
@@ -0,0 +1,18 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_vsprintf.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/11/21 02:34:17 by cacharle #+# #+# */
+/* Updated: 2019/11/21 03:44:24 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+int ft_vsprintf(char *str, const char *format, va_list ap)
+{
+ return (ft_vsnprintf(str, INT_MAX + 1, format, ap));
+}
diff --git a/get_next_line.c b/get_next_line.c
index f7d9c0e..00aca8d 100644
--- a/get_next_line.c
+++ b/get_next_line.c
@@ -6,12 +6,67 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/19 09:08:36 by cacharle #+# #+# */
-/* Updated: 2019/11/04 00:00:16 by cacharle ### ########.fr */
+/* Updated: 2019/11/21 03:39:51 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
+#define HAS_NEWLINE(str, split_at) ((split_at = find_newline(str)) != -1)
+
+static int find_newline(char *str)
+{
+ int i;
+
+ i = -1;
+ while (str[++i])
+ if (str[i] == '\n')
+ return (i);
+ return (-1);
+}
+
+static int free_return(char **ptr, char **ptr2, int ret)
+{
+ if (ptr != NULL)
+ {
+ free(*ptr);
+ *ptr = NULL;
+ }
+ if (ptr2 != NULL)
+ {
+ free(*ptr2);
+ *ptr2 = NULL;
+ }
+ return (ret);
+}
+
+static int read_line(int fd, char **line, char *rest)
+{
+ int ret;
+ int split_at;
+ char *buf;
+
+ if ((buf = malloc(sizeof(char) * (BUFFER_SIZE + 1))) == NULL)
+ return (free_return(line, NULL, STATUS_ERROR));
+ while ((ret = read(fd, buf, BUFFER_SIZE)) > 0)
+ {
+ buf[ret] = '\0';
+ if (HAS_NEWLINE(buf, split_at))
+ {
+ ft_strcpy(rest, buf + split_at + 1);
+ buf[split_at] = '\0';
+ if ((*line = ft_strjoin_free(*line, buf, 1)) == NULL)
+ return (free_return(&buf, NULL, STATUS_ERROR));
+ return (free_return(&buf, NULL, STATUS_LINE));
+ }
+ if ((*line = ft_strjoin_free(*line, buf, 1)) == NULL)
+ return (free_return(&buf, NULL, STATUS_ERROR));
+ }
+ if (ret == -1)
+ return (free_return(&buf, line, STATUS_ERROR));
+ return (free_return(&buf, NULL, ret));
+}
+
/*
** if has rest:
** if rest has newline:
@@ -30,9 +85,7 @@
** return GNL_EOF
*/
-#define HAS_NEWLINE(str, split_at) ((split_at = find_newline(str)) != -1)
-
-int get_next_line(int fd, char **line)
+int get_next_line(int fd, char **line)
{
int split_at;
static char rest[OPEN_MAX][BUFFER_SIZE + 1] = {{0}};
@@ -60,56 +113,3 @@ int get_next_line(int fd, char **line)
rest[fd][0] = '\0';
return (read_line(fd, line, rest[fd]));
}
-
-int read_line(int fd, char **line, char *rest)
-{
- int ret;
- int split_at;
- char *buf;
-
- if ((buf = malloc(sizeof(char) * (BUFFER_SIZE + 1))) == NULL)
- return (free_return(line, NULL, STATUS_ERROR));
- while ((ret = read(fd, buf, BUFFER_SIZE)) > 0)
- {
- buf[ret] = '\0';
- if (HAS_NEWLINE(buf, split_at))
- {
- ft_strcpy(rest, buf + split_at + 1);
- buf[split_at] = '\0';
- if ((*line = ft_strjoin_free(*line, buf, 1)) == NULL)
- return (free_return(&buf, NULL, STATUS_ERROR));
- return (free_return(&buf, NULL, STATUS_LINE));
- }
- if ((*line = ft_strjoin_free(*line, buf, 1)) == NULL)
- return (free_return(&buf, NULL, STATUS_ERROR));
- }
- if (ret == -1)
- return (free_return(&buf, line, STATUS_ERROR));
- return (free_return(&buf, NULL, ret));
-}
-
-int find_newline(char *str)
-{
- int i;
-
- i = -1;
- while (str[++i])
- if (str[i] == '\n')
- return (i);
- return (-1);
-}
-
-int free_return(char **ptr, char **ptr2, int ret)
-{
- if (ptr != NULL)
- {
- free(*ptr);
- *ptr = NULL;
- }
- if (ptr2 != NULL)
- {
- free(*ptr2);
- *ptr2 = NULL;
- }
- return (ret);
-}
diff --git a/get_next_line.h b/get_next_line.h
index c3962e3..36a5116 100644
--- a/get_next_line.h
+++ b/get_next_line.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/09 13:52:59 by cacharle #+# #+# */
-/* Updated: 2019/11/03 22:43:18 by cacharle ### ########.fr */
+/* Updated: 2019/11/21 02:47:49 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -31,8 +31,5 @@
*/
int get_next_line(int fd, char **line);
-int read_line(int fd, char **line, char *rest);
-int find_newline(char *str);
-int free_return(char **ptr, char **rest, int ret);
#endif
diff --git a/libft.h b/libft.h
index 2e1a2f2..0880568 100644
--- a/libft.h
+++ b/libft.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/07 09:45:02 by cacharle #+# #+# */
-/* Updated: 2019/11/21 02:01:31 by cacharle ### ########.fr */
+/* Updated: 2019/11/21 03:39:19 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,6 +14,7 @@
# define LIBFT_H
# include <unistd.h>
+# include <stdarg.h>
# include <stdlib.h>
# include <stddef.h>
# include <limits.h>
@@ -22,8 +23,8 @@
# define TRUE 1
# define FALSE 0
-#define MIN(x, y) ((x) < (y) ? (x) : (y))
-#define MAX(x, y) ((x) > (y) ? (x) : (y))
+# define MIN(x, y) ((x) < (y) ? (x) : (y))
+# define MAX(x, y) ((x) > (y) ? (x) : (y))
typedef unsigned char t_byte;
@@ -137,4 +138,22 @@ t_list *ft_lstmap(t_list *lst, void *(*f)(void *),
void (*del)(void *));
void ft_lstpop_front(t_list **lst, void (*del)(void *));
+/*
+** ft_*printf
+*/
+
+int ft_printf(const char *format, ...);
+int ft_sprintf(char *str, const char *format, ...);
+int ft_snprintf(char *str, size_t size,
+ const char *format, ...);
+int ft_asprintf(char **ret, const char *format, ...);
+int ft_dprintf(int fd, const char *format, ...);
+
+int ft_vprintf(const char *format, va_list ap);
+int ft_vsprintf(char *str, const char *format, va_list ap);
+int ft_vsnprintf(char *str, size_t size, const char *format,
+ va_list ap);
+int ft_vasprintf(char **ret, const char *format, va_list ap);
+int ft_vdprintf(int fd, const char *format, va_list ap);
+
#endif