aboutsummaryrefslogtreecommitdiff
path: root/utils.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-10-12 10:43:01 +0200
committerCharles <sircharlesaze@gmail.com>2019-10-12 11:48:54 +0200
commit6ea4606cd3f74377691d200d69df8398f90cc2ff (patch)
treef354459b9054f5bd23a7508c12ef1589af38e75a /utils.c
parentbddc2d153a4c47257740a0bf0651513058a612d5 (diff)
downloadft_printf-6ea4606cd3f74377691d200d69df8398f90cc2ff.tar.gz
ft_printf-6ea4606cd3f74377691d200d69df8398f90cc2ff.tar.bz2
ft_printf-6ea4606cd3f74377691d200d69df8398f90cc2ff.zip
Basic conversion parsing
Using a list to store each format conversion informations.
Diffstat (limited to 'utils.c')
-rw-r--r--utils.c99
1 files changed, 99 insertions, 0 deletions
diff --git a/utils.c b/utils.c
index 8b13789..9e8d4e9 100644
--- a/utils.c
+++ b/utils.c
@@ -1 +1,100 @@
+#include <stdlib.h>
+#include "header.h"
+#define MIN_INT (1 << 31)
+#define MAX_INT (~(1 << 31))
+
+static int precheck(const char **str)
+{
+ int is_negative;
+
+ while (**str == ' ' || **str == '\t' || **str == '\n'
+ || **str == '\v' || **str == '\f' || **str == '\r')
+ (*str)++;
+ is_negative = 0;
+ if (**str == '-' || **str == '+')
+ {
+ if (**str == '-')
+ is_negative = 1;
+ (*str)++;
+ }
+ return (is_negative);
+}
+
+int ft_atoi(const char *str)
+{
+ unsigned int nb;
+ int i;
+ int is_negative;
+
+ is_negative = precheck(&str);
+ i = 0;
+ nb = 0;
+ while (str[i] >= '0' && str[i] <= '9')
+ {
+ if (!is_negative && nb > (unsigned int)MAX_INT)
+ return (-1);
+ else if (nb > (unsigned int)MIN_INT)
+ return (0);
+ nb *= 10;
+ nb += str[i] - '0';
+ i++;
+ }
+ return ((int)(is_negative ? -nb : nb));
+}
+
+char *ft_strndup(const char *s1, int n)
+{
+ char *clone;
+ int 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);
+}
+
+char *ft_strrchr(const char *s, int c)
+{
+ int i;
+
+ i = ft_strlen(s) - 1;
+ while (s[i] != (char)c)
+ {
+ if (i == 0)
+ return (NULL);
+ i--;
+ }
+ return ((char*)s + i);
+}
+
+int strrchr_index(const char *s, char c)
+{
+ int i;
+
+ i = ft_strlen(s) - 1;
+ while (s[i] != c)
+ {
+ if (i == 0)
+ return (-1);
+ i--;
+ }
+ return (i);
+
+}
+
+int ft_strlen(char *str)
+{
+ int counter;
+
+ counter = 0;
+ while (str[counter])
+ counter++;
+ return (counter);
+}