Added functions abs, ctoa and itoa_base and changed header and Makefile accordingly.
authorLukas Jiriste <ljiriste@student.42prague.com>
Tue, 5 Sep 2023 09:48:50 +0000 (11:48 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Tue, 5 Sep 2023 09:48:50 +0000 (11:48 +0200)
Makefile
ft_abs.c [new file with mode: 0644]
ft_ctoa.c [new file with mode: 0644]
ft_itoa_base.c [new file with mode: 0644]
libft.h

index 982546fa007f5ab9a15c88c5de4d844ab307b2e7..1867830e5f6d6e8c70220e0f44ef1320f38c33b7 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -7,7 +7,7 @@ RM = rm -f
 INCDIR = .
 SRCDIR = .
 # SOURCES = $(addprefix $(SRCDIR)/, ft_putchar.c ft_putstr.c ft_strcmp.c ft_strlen.c ft_swap.c)
-SOURCES = $(shell find $(SRCDIR) -name "*.c")
+SOURCES = $(shell find $(SRCDIR) -maxdepth 1 -name "*.c")
 OBJECTS = $(SOURCES:.c=.o)
 
 NAME = libft.a
diff --git a/ft_abs.c b/ft_abs.c
new file mode 100644 (file)
index 0000000..c277682
--- /dev/null
+++ b/ft_abs.c
@@ -0,0 +1,21 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_abs.c                                           :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2023/09/05 09:48:21 by ljiriste          #+#    #+#             */
+/*   Updated: 2023/09/05 11:47:52 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+int    ft_abs(int n)
+{
+       if (n < 0)
+               return (-n);
+       else
+               return (n);
+}
diff --git a/ft_ctoa.c b/ft_ctoa.c
new file mode 100644 (file)
index 0000000..1574aaf
--- /dev/null
+++ b/ft_ctoa.c
@@ -0,0 +1,26 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_ctoa.c                                          :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2023/09/05 09:55:17 by ljiriste          #+#    #+#             */
+/*   Updated: 2023/09/05 09:56:02 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include <stdlib.h>    //malloc
+#include "libft.h"
+
+char   *ft_ctoa(char c)
+{
+       char    *res;
+
+       res = malloc(2 * sizeof(char));
+       if (res == NULL)
+               return (res);
+       res[0] = c;
+       res[1] = '\0';
+       return (res);
+}
diff --git a/ft_itoa_base.c b/ft_itoa_base.c
new file mode 100644 (file)
index 0000000..7e0b901
--- /dev/null
@@ -0,0 +1,83 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_itoa_base.c                                     :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2023/09/05 09:46:11 by ljiriste          #+#    #+#             */
+/*   Updated: 2023/09/05 11:46:44 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include <stdlib.h>    //malloc
+#include <stdint.h>    //intmax_t
+#include "libft.h"
+
+static intmax_t        abs_max(intmax_t n)
+{
+       if (n < 0)
+               return (-n);
+       else
+               return (n);
+}
+
+static int     size_needed(intmax_t n, size_t base_len)
+{
+       int     res;
+
+       if (n == 0)
+               return (2);
+       res = 1;
+       if (n < 0)
+               ++res;
+       while (n != 0)
+       {
+               ++res;
+               n /= base_len;
+       }
+       return (res);
+}
+
+static int     valid_base(const char *base)
+{
+       int     i;
+       int     j;
+
+       if (ft_strlen(base) < 2)
+               return (0);
+       i = 0;
+       while (base[i])
+       {
+               j = i + 1;
+               while (base[j])
+                       if (base[i] == base[j++])
+                               return (0);
+               ++i;
+       }
+       return (1);
+}
+
+char   *ft_itoa_base(intmax_t n, const char *base)
+{
+       int             size;
+       int             base_len;
+       char    *res;
+
+       if (!valid_base(base))
+               return (NULL);
+       base_len = ft_strlen(base);
+       size = size_needed(n, base_len);
+       res = malloc(size);
+       if (res == NULL)
+               return (res);
+       if (n < 0)
+               res[0] = '-';
+       res[size--] = '\0';
+       while (n != 0)
+       {
+               res[size--] = base[abs_max(n % base_len)];
+               n /= base_len;
+       }
+       return (res);
+}
diff --git a/libft.h b/libft.h
index 61cc5cbb49840c3a4ceb263beb54489a6ce77ac9..9b8d3b255256f0b3c7cf156cbb3830e519177141 100644 (file)
--- a/libft.h
+++ b/libft.h
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/08/15 12:58:15 by ljiriste          #+#    #+#             */
-/*   Updated: 2023/08/24 09:24:32 by ljiriste         ###   ########.fr       */
+/*   Updated: 2023/09/05 11:47:43 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -14,6 +14,7 @@
 # define LIBFT_H
 
 # include <sys/types.h>
+# include <stdint.h>
 
 int            ft_isalnum(int c);
 int            ft_isalpha(int c);
@@ -24,6 +25,8 @@ int           ft_isspace(int c);
 int            ft_isupper(int c);
 int            ft_isascii(int c);
 
+int            ft_abs(int n);
+
 void   *ft_memset(void *s, int c, size_t n);
 void   *ft_memcpy(void *dest, const void *src, size_t n);
 void   *ft_memmove(void *dest, const void *src, size_t n);
@@ -53,6 +56,7 @@ char  *ft_strtrim(const char *s1, const char *set);
 char   **ft_split(const char *s, char c);
 
 char   *ft_itoa(int n);
+char   *ft_itoa_base(intmax_t n, const char *base);
 
 char   *ft_strmapi(const char *s, char (*f)(unsigned int, char));
 void   ft_striteri(char *s, void (*f)(unsigned int, char *));