aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-07-29 16:56:27 +0200
committerCharles <sircharlesaze@gmail.com>2019-07-29 16:56:27 +0200
commit9a2b208985ac7d4644c718ada74770b98eeb4598 (patch)
treef190f9dc0bc1228a28c5901842d2f30738a865f8
parent009780065032aa23c6d72dd7ab02adb481a7c76d (diff)
downloadlibft-9a2b208985ac7d4644c718ada74770b98eeb4598.tar.gz
libft-9a2b208985ac7d4644c718ada74770b98eeb4598.tar.bz2
libft-9a2b208985ac7d4644c718ada74770b98eeb4598.zip
part 2 done (except putchar and putstr unicode)
-rw-r--r--Makefile14
-rw-r--r--ft_itoa.c53
-rw-r--r--ft_memalloc.c13
-rw-r--r--ft_memdel.c7
-rw-r--r--ft_putchar.c8
-rw-r--r--ft_putchar_fd.c6
-rw-r--r--ft_putendl.c8
-rw-r--r--ft_putendl_fd.c7
-rw-r--r--ft_putnbr.c17
-rw-r--r--ft_putnbr_fd.c17
-rw-r--r--ft_putstr.c7
-rw-r--r--ft_putstr_fd.c7
-rw-r--r--ft_strclr.c5
-rw-r--r--ft_strdel.c6
-rw-r--r--ft_strequ.c6
-rw-r--r--ft_striter.c5
-rw-r--r--ft_striteri.c11
-rw-r--r--ft_strjoin.c13
-rw-r--r--ft_strmap.c21
-rw-r--r--ft_strmapi.c21
-rw-r--r--ft_strnequ.c6
-rw-r--r--ft_strnew.c14
-rw-r--r--ft_strsplit.c54
-rw-r--r--ft_strsub.c18
-rw-r--r--ft_strtrim.c27
-rw-r--r--libft.h22
26 files changed, 388 insertions, 5 deletions
diff --git a/Makefile b/Makefile
index 5389a10..9013acc 100644
--- a/Makefile
+++ b/Makefile
@@ -3,9 +3,13 @@ CCFLAGS = -Wall -Wextra -Werror
NAME = libft.a
SRC = ft_atoi.c ft_bzero.c ft_isalnum.c ft_isalpha.c ft_isascii.c ft_isdigit.c ft_isprint.c \
- ft_memccpy.c ft_memchr.c ft_memcmp.c ft_memcpy.c ft_memmove.c ft_memset.c ft_strcat.c \
- ft_strchr.c ft_strcmp.c ft_strcpy.c ft_strdup.c ft_strlcat.c ft_strlen.c ft_strncat.c \
- ft_strncmp.c ft_strncpy.c ft_strnstr.c ft_strrchr.c ft_strstr.c ft_tolower.c \
+ ft_itoa.c ft_memalloc.c ft_memccpy.c ft_memchr.c ft_memcmp.c ft_memcpy.c ft_memdel.c \
+ ft_memmove.c ft_memset.c ft_putchar.c ft_putchar_fd.c ft_putendl.c ft_putendl_fd.c \
+ ft_putnbr.c ft_putnbr_fd.c ft_putstr.c ft_putstr_fd.c ft_strcat.c ft_strchr.c \
+ ft_strclr.c ft_strcmp.c ft_strcpy.c ft_strdel.c ft_strdup.c ft_strequ.c ft_striter.c \
+ ft_striteri.c ft_strjoin.c ft_strlcat.c ft_strlen.c ft_strmap.c ft_strmapi.c \
+ ft_strncat.c ft_strncmp.c ft_strncpy.c ft_strnequ.c ft_strnew.c ft_strnstr.c \
+ ft_strrchr.c ft_strsplit.c ft_strstr.c ft_strsub.c ft_strtrim.c ft_tolower.c \
ft_toupper.c
OBJ = $(SRC:.c=.o)
@@ -15,7 +19,7 @@ $(NAME): $(OBJ) libft.h
ar rc $(NAME) $(OBJ)
%.o: %.c
- $(CC) $(CCFLAGS) -c -o $@ $<
+ $(CC) $(CCFLAGS) -fPIC -c -o $@ $< # /!\ fPIC
clean:
rm -f $(OBJ)
@@ -26,4 +30,4 @@ fclean: clean
re: fclean all
so: $(OBJ) libft.h
- $(CC) -shared -Wl,-soname,libft.so -o libft.so $(OBJ)
+ $(CC) -shared -fPIC -Wl,-soname,libft.so -o libft.so $(OBJ)
diff --git a/ft_itoa.c b/ft_itoa.c
new file mode 100644
index 0000000..3dc4396
--- /dev/null
+++ b/ft_itoa.c
@@ -0,0 +1,53 @@
+#include <stdlib.h>
+
+
+static int count_len(int nbr)
+{
+ int counter;
+ 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 /= 10;
+ counter++;
+ }
+ return (counter);
+}
+
+char *ft_itoa(int n)
+{
+ char *str;
+ int len;
+ int is_negative;
+ unsigned int u_nbr;
+
+ len = count_len(n);
+ if ((str = (char*)malloc(sizeof(char) * (len + 1))) == NULL)
+ return (NULL);
+ str[len] = '\0';
+ is_negative = 0;
+ u_nbr = n;
+ if (n < 0)
+ {
+ is_negative = 1;
+ str[0] = '-';
+ u_nbr = -n;
+ }
+ len--;
+ while (len >= (is_negative ? 1 : 0))
+ {
+ str[len] = u_nbr % 10 + '0';
+ u_nbr /= 10;
+ len--;
+ }
+ return (str);
+}
diff --git a/ft_memalloc.c b/ft_memalloc.c
new file mode 100644
index 0000000..3c81f10
--- /dev/null
+++ b/ft_memalloc.c
@@ -0,0 +1,13 @@
+#include <stdlib.h>
+#include <string.h>
+
+void *ft_memalloc(size_t size)
+{
+ void *ptr;
+
+ if ((ptr = malloc(size)) == NULL)
+ return (NULL);
+ while (size-- > 0)
+ ((unsigned char*)ptr)[size] = 0;
+ return (ptr);
+}
diff --git a/ft_memdel.c b/ft_memdel.c
new file mode 100644
index 0000000..6d4cd6f
--- /dev/null
+++ b/ft_memdel.c
@@ -0,0 +1,7 @@
+#include <stdlib.h>
+
+void ft_memdel(void **ap)
+{
+ free(*ap);
+ *ap = NULL;
+}
diff --git a/ft_putchar.c b/ft_putchar.c
new file mode 100644
index 0000000..5718181
--- /dev/null
+++ b/ft_putchar.c
@@ -0,0 +1,8 @@
+#include <unistd.h>
+
+void ft_putchar(char c)
+{
+ /* if (c < 0) */
+ /* write(1, "", 1); */
+ write(STDOUT_FILENO, &c, 1);
+}
diff --git a/ft_putchar_fd.c b/ft_putchar_fd.c
new file mode 100644
index 0000000..405bbe4
--- /dev/null
+++ b/ft_putchar_fd.c
@@ -0,0 +1,6 @@
+#include <unistd.h>
+
+void ft_putchar_fd(char c, int fd)
+{
+ write(fd, &c, 1);
+}
diff --git a/ft_putendl.c b/ft_putendl.c
new file mode 100644
index 0000000..0e9eaed
--- /dev/null
+++ b/ft_putendl.c
@@ -0,0 +1,8 @@
+#include <unistd.h>
+#include "libft.h"
+
+void ft_putendl(char const *s)
+{
+ ft_putstr(s);
+ ft_putchar('\n');
+}
diff --git a/ft_putendl_fd.c b/ft_putendl_fd.c
new file mode 100644
index 0000000..58ca452
--- /dev/null
+++ b/ft_putendl_fd.c
@@ -0,0 +1,7 @@
+#include "libft.h"
+
+void ft_putendl_fd(char const *s, int fd)
+{
+ ft_putstr_fd(s, fd);
+ ft_putchar_fd('\n', fd);
+}
diff --git a/ft_putnbr.c b/ft_putnbr.c
new file mode 100644
index 0000000..856c429
--- /dev/null
+++ b/ft_putnbr.c
@@ -0,0 +1,17 @@
+#include <unistd.h>
+#include "libft.h"
+
+void ft_putnbr(int n)
+{
+ unsigned int p_n;
+
+ p_n = n;
+ if (n < 0)
+ {
+ ft_putchar('-');
+ p_n = -n;
+ }
+ if (p_n > 9)
+ ft_putnbr(p_n / 10);
+ ft_putchar(p_n % 10 + '0');
+}
diff --git a/ft_putnbr_fd.c b/ft_putnbr_fd.c
new file mode 100644
index 0000000..c49de56
--- /dev/null
+++ b/ft_putnbr_fd.c
@@ -0,0 +1,17 @@
+#include <unistd.h>
+#include "libft.h"
+
+void ft_putnbr_fd(int n, int fd)
+{
+ unsigned int p_n;
+
+ p_n = n;
+ if (n < 0)
+ {
+ ft_putchar_fd('-', fd);
+ p_n = -n;
+ }
+ if (p_n > 9)
+ ft_putnbr_fd(p_n / 10, fd);
+ ft_putchar_fd(p_n % 10 + '0', fd);
+}
diff --git a/ft_putstr.c b/ft_putstr.c
new file mode 100644
index 0000000..1bfe06c
--- /dev/null
+++ b/ft_putstr.c
@@ -0,0 +1,7 @@
+#include <unistd.h>
+
+void ft_putstr(char const *s)
+{
+ while (*s)
+ write(STDOUT_FILENO, s++, 1);
+}
diff --git a/ft_putstr_fd.c b/ft_putstr_fd.c
new file mode 100644
index 0000000..d7c2756
--- /dev/null
+++ b/ft_putstr_fd.c
@@ -0,0 +1,7 @@
+#include <unistd.h>
+
+void ft_putstr_fd(char const *s, int fd)
+{
+ while (*s)
+ write(fd, s++, 1);
+}
diff --git a/ft_strclr.c b/ft_strclr.c
new file mode 100644
index 0000000..17fdc1f
--- /dev/null
+++ b/ft_strclr.c
@@ -0,0 +1,5 @@
+void ft_strclr(char *s)
+{
+ while (*s)
+ *s++ = '\0';
+}
diff --git a/ft_strdel.c b/ft_strdel.c
new file mode 100644
index 0000000..62debcc
--- /dev/null
+++ b/ft_strdel.c
@@ -0,0 +1,6 @@
+#include "libft.h"
+
+void ft_strdel(char **as)
+{
+ ft_memdel((void*)as);
+}
diff --git a/ft_strequ.c b/ft_strequ.c
new file mode 100644
index 0000000..bd2469c
--- /dev/null
+++ b/ft_strequ.c
@@ -0,0 +1,6 @@
+#include "libft.h"
+
+int ft_strequ(char const *s1, char const *s2)
+{
+ return (ft_strcmp(s1, s2) == 0);
+}
diff --git a/ft_striter.c b/ft_striter.c
new file mode 100644
index 0000000..a4e69bc
--- /dev/null
+++ b/ft_striter.c
@@ -0,0 +1,5 @@
+void ft_striter(char *s, void (*f)(char *))
+{
+ while (*s)
+ (*f)(s++);
+}
diff --git a/ft_striteri.c b/ft_striteri.c
new file mode 100644
index 0000000..305138f
--- /dev/null
+++ b/ft_striteri.c
@@ -0,0 +1,11 @@
+void ft_striteri(char *s, void (*f)(unsigned int, char *))
+{
+ unsigned int i;
+
+ i = 0;
+ while (s[i])
+ {
+ (*f)(i, &s[i]);
+ i++;
+ }
+}
diff --git a/ft_strjoin.c b/ft_strjoin.c
new file mode 100644
index 0000000..3befb18
--- /dev/null
+++ b/ft_strjoin.c
@@ -0,0 +1,13 @@
+#include <stdlib.h>
+#include "libft.h"
+
+char * ft_strjoin(char const *s1, char const *s2)
+{
+ char *joined;
+
+ if ((joined = (char*)malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1)))
+ == NULL)
+ return (NULL);
+ joined = ft_strcpy(joined, s1);
+ return (ft_strcat(joined, s2));
+}
diff --git a/ft_strmap.c b/ft_strmap.c
new file mode 100644
index 0000000..7fc3051
--- /dev/null
+++ b/ft_strmap.c
@@ -0,0 +1,21 @@
+#include <stdlib.h>
+#include "libft.h"
+
+char *ft_strmap(char const *s, char (*f)(char))
+{
+ size_t i;
+ size_t len;
+ char *mapped;
+
+ len = ft_strlen(s);
+ if ((mapped = (char*)malloc(sizeof(char) * (len + 1))) == NULL)
+ return (NULL);
+ i = 0;
+ while (i < len)
+ {
+ mapped[i] = (*f)(s[i]);
+ i++;
+ }
+ mapped[i] = '\0';
+ return (mapped);
+}
diff --git a/ft_strmapi.c b/ft_strmapi.c
new file mode 100644
index 0000000..da75c98
--- /dev/null
+++ b/ft_strmapi.c
@@ -0,0 +1,21 @@
+#include <stdlib.h>
+#include "libft.h"
+
+char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
+{
+ size_t i;
+ size_t len;
+ char *mapped;
+
+ len = ft_strlen(s);
+ if ((mapped = (char*)malloc(sizeof(char) * (len + 1))) == NULL)
+ return (NULL);
+ i = 0;
+ while (i < len)
+ {
+ mapped[i] = (*f)((unsigned int)i, s[i]);
+ i++;
+ }
+ mapped[i] = '\0';
+ return (mapped);
+}
diff --git a/ft_strnequ.c b/ft_strnequ.c
new file mode 100644
index 0000000..6eb58bf
--- /dev/null
+++ b/ft_strnequ.c
@@ -0,0 +1,6 @@
+#include "libft.h"
+
+int ft_strnequ(char const *s1, char const *s2, size_t n)
+{
+ return (ft_strncmp(s1, s2, n) == 0);
+}
diff --git a/ft_strnew.c b/ft_strnew.c
new file mode 100644
index 0000000..defe2e0
--- /dev/null
+++ b/ft_strnew.c
@@ -0,0 +1,14 @@
+#include <stdlib.h>
+#include <string.h>
+
+char *ft_strnew(size_t size)
+{
+ char *str;
+
+ if ((str = (char*)malloc(sizeof(char) * (size + 1))) == NULL)
+ return (NULL);
+ str[size] = '\0';
+ while (size-- > 0)
+ str[size] = '\0';
+ return (str);
+}
diff --git a/ft_strsplit.c b/ft_strsplit.c
new file mode 100644
index 0000000..2002add
--- /dev/null
+++ b/ft_strsplit.c
@@ -0,0 +1,54 @@
+#include <stdlib.h>
+
+static size_t count_segment(char const *s, char c)
+{
+ size_t counter;
+
+ counter = 0;
+ while (*s)
+ {
+ if (*s == c)
+ {
+ s++;
+ continue ;
+ }
+ counter++;
+ while (*s && *s != c)
+ s++;
+ }
+ return (counter);
+}
+
+char **ft_strsplit(char const *s, char c)
+{
+ char **strs;
+ char *tmp;
+ size_t size;
+ size_t i;
+ size_t j;
+
+ size = count_segment(s, c);
+ if ((strs = (char**)malloc(sizeof(char*) * (size + 1))) == NULL)
+ return (NULL);
+ j = 0;
+ while (*s)
+ {
+ if (*s == c)
+ {
+ s++;
+ continue ;
+ }
+ i = 0;
+ while (s[i] && s[i] != c)
+ i++;
+ if ((tmp = (char*)malloc(sizeof(char) * (i + 1))) == NULL)
+ return (NULL);
+ i = 0;
+ while (*s && *s != c)
+ tmp[i++] = *s++;
+ tmp[i] = '\0';
+ strs[j++] = tmp;
+ }
+ strs[j] = 0;
+ return (strs);
+}
diff --git a/ft_strsub.c b/ft_strsub.c
new file mode 100644
index 0000000..11be9c3
--- /dev/null
+++ b/ft_strsub.c
@@ -0,0 +1,18 @@
+#include <stdlib.h>
+
+char *ft_strsub(char const *s, unsigned int start, size_t len)
+{
+ unsigned int i;
+ char *sub;
+
+ if ((sub = (char*)malloc(sizeof(char) * (len + 1))) == NULL)
+ return (NULL);
+ i = 0;
+ while (i < len)
+ {
+ sub[i] = s[start + i];
+ i++;
+ }
+ sub[i] = '\0';
+ return (sub);
+}
diff --git a/ft_strtrim.c b/ft_strtrim.c
new file mode 100644
index 0000000..7b93eae
--- /dev/null
+++ b/ft_strtrim.c
@@ -0,0 +1,27 @@
+#include <stdlib.h>
+#include "libft.h"
+
+static int is_space(char c)
+{
+ return (c == ' ' || c == '\n' || c == '\t');
+}
+
+char *ft_strtrim(char const *s)
+{
+ size_t start;
+ size_t len;
+ char *trimed;
+
+ start = 0;
+ while (s[start] && is_space(s[start]))
+ start++;
+ len = ft_strlen(&s[start]);
+ if (len != 0)
+ while (s[start + len - 1] && is_space(s[start + len - 1]))
+ len--;
+ if ((trimed = (char*)malloc(sizeof(char) * (len + 1))) == NULL)
+ return (NULL);
+ trimed = ft_strncpy(trimed, &s[start], len);
+ trimed[len] = '\0';
+ return (trimed);
+}
diff --git a/libft.h b/libft.h
index 0b110e9..4766b2f 100644
--- a/libft.h
+++ b/libft.h
@@ -30,5 +30,27 @@ int ft_isalnum(int c);
int ft_isascii(int c);
int ft_isprint(int c);
int ft_toupper(int c);
+void *ft_memalloc(size_t size);
+void ft_memdel(void **ap);
+char *ft_strnew(size_t size);
+void ft_strdel(char **as);
+void ft_strclr(char *s);
+void ft_striter(char *s, void (*f)(char *));
+void ft_striteri(char *s, void (*f)(unsigned int, char *));
+char *ft_strmap(char const *s, char (*f)(char));
+char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
+int ft_strequ(char const *s1, char const *s2);
+int ft_strnequ(char const *s1, char const *s2, size_t n);
+char *ft_strsub(char const *s, unsigned int start, size_t len);
+char *ft_strjoin(char const *s1, char const *s2);
+char *ft_strtrim(char const *s);
+char **ft_strsplit(char const *s, char c);
+char *ft_itoa(int n);
+void ft_putchar(char c);
+void ft_putstr(char const *s);
+void ft_putnbr(int n);
+void ft_putchar_fd(char c, int fd);
+void ft_putstr_fd(char const *s, int fd);
+void ft_putendl_fd(char const *s, int fd);
#endif