aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-01-18 11:54:47 +0100
committerCharles <sircharlesaze@gmail.com>2020-02-04 05:33:59 +0100
commitf078f191515d0fc65340a8722fad14f3de679d7b (patch)
treed6881ef24f6cc062332f3aa17709f693cf783b20
parent014d876b691b5c0e84faa98bd308a855269e4ae4 (diff)
downloadlibft-f078f191515d0fc65340a8722fad14f3de679d7b.tar.gz
libft-f078f191515d0fc65340a8722fad14f3de679d7b.tar.bz2
libft-f078f191515d0fc65340a8722fad14f3de679d7b.zip
Added ft_getchar and fixing strtol
-rw-r--r--ft_split_strict.c83
-rw-r--r--ft_strtol.c14
-rw-r--r--libft.h3
-rw-r--r--src/io/ft_getchar.c22
-rw-r--r--src/lst/ft_lstreverse_ret.c27
-rw-r--r--src/str/ft_atoi_strict.c38
6 files changed, 179 insertions, 8 deletions
diff --git a/ft_split_strict.c b/ft_split_strict.c
new file mode 100644
index 0000000..9964f4c
--- /dev/null
+++ b/ft_split_strict.c
@@ -0,0 +1,83 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_split_strict.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/04 05:20:26 by cacharle #+# #+# */
+/* Updated: 2020/02/04 05:21:29 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+static size_t count_segment(char const *s, char c)
+{
+ size_t counter;
+ int i;
+
+ counter = 0;
+ i = -1;
+ while (s[++i])
+ if (s[i] == c)
+ counter++;
+ return (counter);
+}
+
+static char *ft_strndup(const char *s1, size_t n)
+{
+ char *clone;
+ size_t i;
+
+ if ((clone = (char*)malloc(sizeof(char) * (n + 1))) == NULL)
+ return (NULL);
+ i = 0;
+ while (i < n)
+ {
+ clone[i] = s1[i];
+ i++;
+ }
+ clone[i] = '\0';
+ return (clone);
+}
+
+static void *destroy_strs(char **strs)
+{
+ int i;
+
+ i = 0;
+ while (strs[i] != NULL)
+ free(strs[i++]);
+ free(strs);
+ return (NULL);
+}
+
+char **ft_split(char const *s, char c)
+{
+ char **strs;
+ size_t tab_counter;
+ size_t i;
+ size_t j;
+
+ if (s == NULL)
+ return (NULL);
+ tab_counter = count_segment(s, c);
+ if ((strs = (char**)malloc(sizeof(char*) * (tab_counter + 1))) == NULL)
+ return (NULL);
+ tab_counter = 0;
+ j = -1;
+ while (s[++j])
+ {
+ if (s[j] == c)
+ continue ;
+ i = 0;
+ while (s[j + i] && s[j + i] != c)
+ i++;
+ if ((strs[tab_counter++] = ft_strndup(&s[j], i)) == NULL)
+ return (destroy_strs(strs));
+ j += i - 1;
+ }
+ strs[tab_counter] = NULL;
+ return (strs);
+}
diff --git a/ft_strtol.c b/ft_strtol.c
index 447f1af..94fc114 100644
--- a/ft_strtol.c
+++ b/ft_strtol.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/15 10:26:45 by cacharle #+# #+# */
-/* Updated: 2020/01/15 11:39:34 by cacharle ### ########.fr */
+/* Updated: 2020/02/04 04:40:45 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -16,6 +16,8 @@
static int strtol_handle_base(int base, const char **str)
{
+ if (base > 36)
+ return (-1);
if (base != 16 && base != 0)
return (base);
if (base == 16 && **str == '0' && (*str)[1] == 'x')
@@ -55,20 +57,20 @@ long ft_strtol(const char *str, char **endptr, int base)
long long nb;
char base_str[37];
- if (base > 36)
- return (errno_return(EINVAL));
while (ft_isspace(*str))
str++;
is_negative = *str == '-' ? TRUE : FALSE;
if (*str == '-' || *str == '+')
str++;
- base = strtol_handle_base(base, &str);
+ if ((base = strtol_handle_base(base, &str)) == -1)
+ return (errno_return(EINVAL));
ft_strncpy(base_str, STRTOL_STD_BASE, base);
+ base_str[base] = '\0';
nb = 0;
- while (ft_strchr(base_str, *str) != NULL)
+ while (*str != '\0' && ft_strchr(base_str, *str) != NULL)
{
nb *= base;
- nb += ft_strchr(base_str, ft_tolower(*str)) - base_str;
+ nb += ft_strchr(base_str, ft_tolower(*str++)) - base_str;
if (((long)nb ^ (long)(nb / base)) < 0)
return (errno_return(ERANGE));
}
diff --git a/libft.h b/libft.h
index bb11c5b..49996c3 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: 2020/02/04 04:08:15 by cacharle ### ########.fr */
+/* Updated: 2020/02/04 04:41:08 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -94,7 +94,6 @@ void ft_putnbr_fd(int n, int fd);
long ft_strtol(const char *str, char **endptr, int base);
-
/*
** bonus
*/
diff --git a/src/io/ft_getchar.c b/src/io/ft_getchar.c
new file mode 100644
index 0000000..54b44d4
--- /dev/null
+++ b/src/io/ft_getchar.c
@@ -0,0 +1,22 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_getchar.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/01/18 10:29:54 by cacharle #+# #+# */
+/* Updated: 2020/01/18 10:46:56 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+char ft_getchar(void)
+{
+ char c;
+
+ if (read(STDIN_FILENO, &c, 1) < 0)
+ return (-1);
+ return (c);
+}
diff --git a/src/lst/ft_lstreverse_ret.c b/src/lst/ft_lstreverse_ret.c
new file mode 100644
index 0000000..caf6ec1
--- /dev/null
+++ b/src/lst/ft_lstreverse_ret.c
@@ -0,0 +1,27 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_lstreverse_ret.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/01/15 12:51:15 by cacharle #+# #+# */
+/* Updated: 2020/01/18 11:52:50 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+t_list *ft_lstreverse_ret(t_list *lst)
+{
+ t_list *tmp;
+
+ if (lst == NULL)
+ return (NULL);
+ if (lst->next == NULL)
+ return (lst);
+ tmp = ft_lstreverse_ret(lst->next);
+ lst->next->next = lst;
+ lst->next = NULL;
+ return (tmp);
+}
diff --git a/src/str/ft_atoi_strict.c b/src/str/ft_atoi_strict.c
new file mode 100644
index 0000000..f12e8db
--- /dev/null
+++ b/src/str/ft_atoi_strict.c
@@ -0,0 +1,38 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_strict_atoi.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/01/15 10:06:29 by cacharle #+# #+# */
+/* Updated: 2020/01/18 11:53:07 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+int ft_strict_atoi(const char *s)
+{
+ char *end;
+ long ret;
+
+ if (*s != '-' && !ft_isdigit(*s))
+ {
+ errno = EINVAL;
+ return (0);
+ }
+ errno = 0;
+ ret = ft_strtol(s, &end, 10);
+ if (errno == ERANGE || ret > INT_MAX || ret < INT_MIN)
+ {
+ errno = ERANGE;
+ return (0);
+ }
+ if (*end != '\0')
+ {
+ errno = EINVAL;
+ return (0);
+ }
+ return (ret);
+}