part 2, needs testing

This commit is contained in:
Jack Halford 2016-08-28 16:33:44 +02:00
parent 85c77f916c
commit 481c8641ed
26 changed files with 471 additions and 4 deletions

View file

@ -8,7 +8,7 @@
/* Created: 2016/08/03 16:17:21 by jhalford #+# #+# */ /* Created: 2016/08/03 16:17:21 by jhalford #+# #+# */
/* Updated: 2016/08/07 18:10:10 by jhalford ### ########.fr */ /* Updated: 2016/08/07 18:10:10 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
static int ft_iswhitespace(char c) static int ft_iswhitespace(char c)
{ {

55
libftasm/ft_itoa.c Normal file
View file

@ -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));
}

15
libftasm/ft_memalloc.c Normal file
View file

@ -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);
}

7
libftasm/ft_memdel.c Normal file
View file

@ -0,0 +1,7 @@
#include "libft.h"
void ft_memdel(void **ap)
{
free(*ap);
*ap = NULL;
}

6
libftasm/ft_putchar.c Normal file
View file

@ -0,0 +1,6 @@
#include "libft.h"
void ft_putchar(char c)
{
write(1, &c, 1);
}

6
libftasm/ft_putchar_fd.c Normal file
View file

@ -0,0 +1,6 @@
#include "libft.h"
void ft_putchar_fd(char c, int fd)
{
write(fd, &c, 1);
}

10
libftasm/ft_putendl.c Normal file
View file

@ -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);
}

10
libftasm/ft_putendl_fd.c Normal file
View file

@ -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);
}

30
libftasm/ft_putnbr.c Normal file
View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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');
}

30
libftasm/ft_putnbr_fd.c Normal file
View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

18
libftasm/ft_putstr.c Normal file
View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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));
}

6
libftasm/ft_putstr_fd.c Normal file
View file

@ -0,0 +1,6 @@
#include "libft.h"
void ft_putstr_fd(char const *s, int fd)
{
write(fd, s, ft_strlen(s));
}

12
libftasm/ft_strclr.c Normal file
View file

@ -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;
}

7
libftasm/ft_strdel.c Normal file
View file

@ -0,0 +1,7 @@
#include "libft.h"
void ft_strdel(char **as)
{
free(*as);
*as = NULL;
}

6
libftasm/ft_strequ.c Normal file
View file

@ -0,0 +1,6 @@
#include "libft.h"
int ft_strequ(char const *s1, char const *s2)
{
return(ft_strcmp(s1, s2) == 0);
}

12
libftasm/ft_striter.c Normal file
View file

@ -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);
}

12
libftasm/ft_striteri.c Normal file
View file

@ -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);
}

11
libftasm/ft_strjoin.c Normal file
View file

@ -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);
}

17
libftasm/ft_strmap.c Normal file
View file

@ -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);
}

17
libftasm/ft_strmapi.c Normal file
View file

@ -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);
}

6
libftasm/ft_strnequ.c Normal file
View file

@ -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);
}

15
libftasm/ft_strnew.c Normal file
View file

@ -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);
}

93
libftasm/ft_strsplit.c Normal file
View file

@ -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);
}

16
libftasm/ft_strsub.c Normal file
View file

@ -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);
}

18
libftasm/ft_strtrim.c Normal file
View file

@ -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);
}

View file

@ -1,6 +1,12 @@
#include <string.h> #ifndef LIBFT_H
#include <unistd.h> #define LIBFT_H
#include <stdlib.h> # include <string.h>
# include <unistd.h>
# include <stdlib.h>
# 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_memset(void *b, int c, size_t len);
void ft_bzero(void *s, size_t n); void ft_bzero(void *s, size_t n);
@ -30,3 +36,29 @@ int ft_isascii(int c);
int ft_isprint(int c); int ft_isprint(int c);
int ft_toupper(int c); int ft_toupper(int c);
int ft_tolower(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