diff --git a/libftasm/ft_atoi.c b/libftasm/ft_atoi.c index 459e57bf..6fa0bf00 100644 --- a/libftasm/ft_atoi.c +++ b/libftasm/ft_atoi.c @@ -8,7 +8,7 @@ /* Created: 2016/08/03 16:17:21 by jhalford #+# #+# */ /* Updated: 2016/08/07 18:10:10 by jhalford ### ########.fr */ /* */ -/* ************************************************************************** */ +/* ************************************************************************** */ static int ft_iswhitespace(char c) { diff --git a/libftasm/ft_itoa.c b/libftasm/ft_itoa.c new file mode 100644 index 00000000..ea481037 --- /dev/null +++ b/libftasm/ft_itoa.c @@ -0,0 +1,55 @@ +#include "libft.h" + +static char *ft_strrev(char *str) +{ + int len; + char tmp; + int i; + + i = 0; + len = 0; + while (str[len] != '\0') + len++; + while (i < len / 2) + { + tmp = str[len - (i + 1)]; + str[len - (i + 1)] = str[i]; + str[i] = tmp; + i++; + } + return (str); +} + +static size_t ft_size(int n) +{ + size_t i; + + i = 1; + while (n /= 10) + i++; + return (i); +} + +char *ft_itoa(int n) +{ + int i; + char *str; + + i = 0; + str = ft_strnew(ft_size(n) + 1); + if (n == 0) + { + str[i++] = '0'; + str[i] = '\0'; + return (str); + } + while (n) + { + str[i++] = ABS(n % 10) + '0'; + n /= 10; + } + if (NEG(n)) + str[i++] = '-'; + str[i] = '\0'; + return (ft_strrev(str)); +} diff --git a/libftasm/ft_memalloc.c b/libftasm/ft_memalloc.c new file mode 100644 index 00000000..ac781aec --- /dev/null +++ b/libftasm/ft_memalloc.c @@ -0,0 +1,15 @@ +#include "libft.h" + +void *ft_memalloc(size_t size) +{ + void *addr; + size_t i; + + addr = malloc(size); + if (addr == NULL) + return (NULL); + i = -1; + while (++i < size) + ((char *)addr)[i] = 0; + return (addr); +} diff --git a/libftasm/ft_memdel.c b/libftasm/ft_memdel.c new file mode 100644 index 00000000..284d0534 --- /dev/null +++ b/libftasm/ft_memdel.c @@ -0,0 +1,7 @@ +#include "libft.h" + +void ft_memdel(void **ap) +{ + free(*ap); + *ap = NULL; +} diff --git a/libftasm/ft_putchar.c b/libftasm/ft_putchar.c new file mode 100644 index 00000000..70439c66 --- /dev/null +++ b/libftasm/ft_putchar.c @@ -0,0 +1,6 @@ +#include "libft.h" + +void ft_putchar(char c) +{ + write(1, &c, 1); +} diff --git a/libftasm/ft_putchar_fd.c b/libftasm/ft_putchar_fd.c new file mode 100644 index 00000000..094ebd83 --- /dev/null +++ b/libftasm/ft_putchar_fd.c @@ -0,0 +1,6 @@ +#include "libft.h" + +void ft_putchar_fd(char c, int fd) +{ + write(fd, &c, 1); +} diff --git a/libftasm/ft_putendl.c b/libftasm/ft_putendl.c new file mode 100644 index 00000000..43aad0f0 --- /dev/null +++ b/libftasm/ft_putendl.c @@ -0,0 +1,10 @@ +#include "libft.h" + +void ft_putendl(char const *s) +{ + char nl; + + nl = '\n'; + write(1, s, ft_strlen(s)); + write(1, &nl, 1); +} diff --git a/libftasm/ft_putendl_fd.c b/libftasm/ft_putendl_fd.c new file mode 100644 index 00000000..34dea4eb --- /dev/null +++ b/libftasm/ft_putendl_fd.c @@ -0,0 +1,10 @@ +#include "libft.h" + +void ft_putendl_fd(char const *s, int fd) +{ + char nl; + + nl = '\n'; + write(fd, s, ft_strlen(s)); + write(fd, &nl, 1); +} diff --git a/libftasm/ft_putnbr.c b/libftasm/ft_putnbr.c new file mode 100644 index 00000000..08bc2b18 --- /dev/null +++ b/libftasm/ft_putnbr.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/02 21:25:03 by jhalford #+# #+# */ +/* Updated: 2016/08/04 21:28:16 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putnbr(int n) +{ + if (n == -2147483648) + { + ft_putchar('-'); + ft_putchar('2'); + ft_putnbr(147483648); + return ; + } + else if (n < 0) + ft_putchar('-'); + n = ABS(n); + if (n >= 10) + ft_putnbr(n / 10); + ft_putchar(n % 10 + '0'); +} diff --git a/libftasm/ft_putnbr_fd.c b/libftasm/ft_putnbr_fd.c new file mode 100644 index 00000000..4659739e --- /dev/null +++ b/libftasm/ft_putnbr_fd.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/02 21:25:03 by jhalford #+# #+# */ +/* Updated: 2016/08/04 21:28:16 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putnbr_fd(int n, int fd) +{ + if (n == -2147483648) + { + ft_putchar_fd('-', fd); + ft_putchar_fd('2', fd); + ft_putnbr_fd(147483648, fd); + return ; + } + else if (n < 0) + ft_putchar_fd('-', fd); + n = ABS(n); + if (n >= 10) + ft_putnbr_fd(n / 10, fd); + ft_putchar_fd(n % 10 + '0', fd); +} diff --git a/libftasm/ft_putstr.c b/libftasm/ft_putstr.c new file mode 100644 index 00000000..c4e16816 --- /dev/null +++ b/libftasm/ft_putstr.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/03 16:13:07 by jhalford #+# #+# */ +/* Updated: 2016/08/25 17:03:59 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putstr(char const *s) +{ + write(1, s, ft_strlen(s)); +} diff --git a/libftasm/ft_putstr_fd.c b/libftasm/ft_putstr_fd.c new file mode 100644 index 00000000..6c0ad5c4 --- /dev/null +++ b/libftasm/ft_putstr_fd.c @@ -0,0 +1,6 @@ +#include "libft.h" + +void ft_putstr_fd(char const *s, int fd) +{ + write(fd, s, ft_strlen(s)); +} diff --git a/libftasm/ft_strclr.c b/libftasm/ft_strclr.c new file mode 100644 index 00000000..8e3c38b2 --- /dev/null +++ b/libftasm/ft_strclr.c @@ -0,0 +1,12 @@ +#include "libft.h" + +void ft_strclr(char *s) +{ + size_t size; + size_t i; + + size = ft_strlen(s); + i = -1; + while (++i < size) + s[i] = 0; +} diff --git a/libftasm/ft_strdel.c b/libftasm/ft_strdel.c new file mode 100644 index 00000000..a851f878 --- /dev/null +++ b/libftasm/ft_strdel.c @@ -0,0 +1,7 @@ +#include "libft.h" + +void ft_strdel(char **as) +{ + free(*as); + *as = NULL; +} diff --git a/libftasm/ft_strequ.c b/libftasm/ft_strequ.c new file mode 100644 index 00000000..25412e50 --- /dev/null +++ b/libftasm/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/libftasm/ft_striter.c b/libftasm/ft_striter.c new file mode 100644 index 00000000..e1feafaf --- /dev/null +++ b/libftasm/ft_striter.c @@ -0,0 +1,12 @@ +#include "libft.h" + +void ft_striter(char *s, void (*f)(char *)) +{ + size_t size; + size_t i; + + size = ft_strlen(s); + i = -1; + while (++i < size) + (*f)(s + i); +} diff --git a/libftasm/ft_striteri.c b/libftasm/ft_striteri.c new file mode 100644 index 00000000..58773ac3 --- /dev/null +++ b/libftasm/ft_striteri.c @@ -0,0 +1,12 @@ +#include "libft.h" + +void ft_striteri(char *s, void (*f)(unsigned int, char *)) +{ + size_t size; + size_t i; + + size = ft_strlen(s); + i = -1; + while (++i < size) + (*f)(i, s + i); +} diff --git a/libftasm/ft_strjoin.c b/libftasm/ft_strjoin.c new file mode 100644 index 00000000..507fba7f --- /dev/null +++ b/libftasm/ft_strjoin.c @@ -0,0 +1,11 @@ +#include "libft.h" + +char *ft_strjoin(char const *s1, char const *s2) +{ + char *join; + + join = ft_strnew(ft_strlen(s1) + ft_strlen(s2) + 1); + ft_strcpy(join, s1); + ft_strcat(join, s2); + return (join); +} diff --git a/libftasm/ft_strmap.c b/libftasm/ft_strmap.c new file mode 100644 index 00000000..b0a115e7 --- /dev/null +++ b/libftasm/ft_strmap.c @@ -0,0 +1,17 @@ +#include "libft.h" + +char *ft_strmap(char const *s, char (*f)(char)) +{ + size_t size; + size_t i; + char *out; + + size = ft_strlen(s); + out = (char *)malloc(sizeof(char) * (size + 1)); + if (out == NULL) + return (NULL); + i = -1; + while (++i < size) + out[i] = (*f)(s[i]); + return (out); +} diff --git a/libftasm/ft_strmapi.c b/libftasm/ft_strmapi.c new file mode 100644 index 00000000..b3b583c2 --- /dev/null +++ b/libftasm/ft_strmapi.c @@ -0,0 +1,17 @@ +#include "libft.h" + +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) +{ + size_t size; + size_t i; + char *out; + + size = ft_strlen(s); + out = (char *)malloc(sizeof(char) * (size + 1)); + if (out == NULL) + return (NULL); + i = -1; + while (++i < size) + out[i] = (*f)(i, s[i]); + return (out); +} diff --git a/libftasm/ft_strnequ.c b/libftasm/ft_strnequ.c new file mode 100644 index 00000000..8b3b3a06 --- /dev/null +++ b/libftasm/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/libftasm/ft_strnew.c b/libftasm/ft_strnew.c new file mode 100644 index 00000000..21bc4ba7 --- /dev/null +++ b/libftasm/ft_strnew.c @@ -0,0 +1,15 @@ +#include "libft.h" + +char *ft_strnew(size_t size) +{ + char *addr; + size_t i; + + addr = (char *)malloc(size + 1); + if (addr == NULL) + return (NULL); + i = -1; + while (++i <= size) + addr[i] = '\0'; + return (addr); +} diff --git a/libftasm/ft_strsplit.c b/libftasm/ft_strsplit.c new file mode 100644 index 00000000..4eaba8b8 --- /dev/null +++ b/libftasm/ft_strsplit.c @@ -0,0 +1,93 @@ +#include "libft.h" + +static char **alloc_table(char **table, const char *str, char c) +{ + int i; + int n_words; + + i = 0; + n_words = 0; + while (str[i] == c) + i++; + while (str[i] != '\0') + { + i++; + if (str[i] == c) + { + n_words++; + while (str[i] == c) + i++; + } + } + if (str[i - 1] != c) + n_words++; + table = (char**)malloc(sizeof(*table) * (n_words + 10)); + table[n_words] = 0; + return (table); +} + +static char **alloc_words(char **table, const char *str, char c) +{ + int i; + int j; + int k; + + i = 0; + j = 0; + k = 0; + while (str[i] == c) + i++; + while (str[i] != '\0') + { + i++; + if (str[i] == c || !str[i]) + { + table[j] = (char*)malloc(sizeof(**table) * (k + 10)); + j++; + k = 0; + while (str[i] == c) + i++; + } + k++; + } + return (table); +} + +static char **fill_table(char **table, const char *str, char c) +{ + int i; + int j; + int k; + + i = 0; + j = 0; + k = 0; + while (str[i] == c) + i++; + while (str[i] != '\0') + { + table[j][k] = str[i]; + i++; + k++; + if (str[i] == c || !str[i]) + { + table[j][k] = '\0'; + j++; + k = 0; + while (str[i] == c) + i++; + } + } + return (table); +} + +char **ft_strsplit(char const *s, char c) +{ + char **table; + + table = 0; + table = alloc_table(table, s, c); + table = alloc_words(table, s, c); + table = fill_table(table, s, c); + return (table); +} diff --git a/libftasm/ft_strsub.c b/libftasm/ft_strsub.c new file mode 100644 index 00000000..6c5eea09 --- /dev/null +++ b/libftasm/ft_strsub.c @@ -0,0 +1,16 @@ +#include "libft.h" + +char *ft_strsub(char const *s, unsigned int start, size_t len) +{ + char *out; + size_t i; + + out = (char *)malloc(sizeof(char) * (len + 1)); + if (!out) + return (NULL); + i = -1; + while (++i < len) + out[i] = s[i + start]; + out[i] = '\0'; + return (out); +} diff --git a/libftasm/ft_strtrim.c b/libftasm/ft_strtrim.c new file mode 100644 index 00000000..397acd24 --- /dev/null +++ b/libftasm/ft_strtrim.c @@ -0,0 +1,18 @@ +#include "libft.h" + +char *ft_strtrim(char const *s) +{ + char *out; + size_t size; + + out = ft_strdup(s); + while (*out && SEP(*out)) + out++; + size = ft_strlen(out); + while (size - 1 && SEP(out[size - 1])) + { + size--; + out[size] = '\0'; + } + return (out); +} diff --git a/libftasm/libft.h b/libftasm/libft.h index b7e4cc95..105c3e59 100644 --- a/libftasm/libft.h +++ b/libftasm/libft.h @@ -1,6 +1,12 @@ -#include -#include -#include +#ifndef LIBFT_H +#define LIBFT_H +# include +# include +# include +# define SEP(x) (x == ' ' || x == '\t' || x == '\n') +# define ABS(x) (((x) < 0) ? -(x) : (x)) +# define NEG(x) (((x) < 0) ? 1 : 0) +# define POS(x) (((x) > 0) ? 1 : 0) void *ft_memset(void *b, int c, size_t len); void ft_bzero(void *s, size_t n); @@ -30,3 +36,29 @@ int ft_isascii(int c); int ft_isprint(int c); int ft_toupper(int c); int ft_tolower(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_putendl(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); +void ft_putnbr_fd(int n, int fd); + +#endif