Skip to content

Commit 9e52db8

Browse files
committed
adding only necessary lib files
1 parent 6d2457e commit 9e52db8

23 files changed

+598
-0
lines changed

src/libft/ft_bzero.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* ft_bzero.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: csphilli <csphilli@student.42.fr> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2019/10/21 14:27:59 by cphillip #+# #+# */
9+
/* Updated: 2021/03/02 06:42:18 by csphilli ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "../../includes/ft_printf.h"
14+
15+
void ft_bzero(void *s, size_t n)
16+
{
17+
ft_memset(s, 0, n);
18+
}

src/libft/ft_ftoa.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* ft_ftoa.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: csphilli <csphilli@student.42.fr> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2020/02/27 10:23:50 by cphillip #+# #+# */
9+
/* Updated: 2021/03/02 06:37:41 by csphilli ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "../../includes/ft_printf.h"
14+
15+
static long double rounding(long double nbr, int prec)
16+
{
17+
long double rounding;
18+
int i;
19+
20+
i = 0;
21+
rounding = 0.5;
22+
if (nbr < 0)
23+
rounding *= -1;
24+
while (i++ < prec)
25+
rounding /= 10.0;
26+
return (rounding);
27+
}
28+
29+
static char *joining(char *whole, char *dec_part)
30+
{
31+
char *joint;
32+
33+
joint = ft_strjoin(whole, dec_part);
34+
free(whole);
35+
free(dec_part);
36+
return (joint);
37+
}
38+
39+
char *ft_ftoa(long double nbr, int prec, char dot)
40+
{
41+
char *joint;
42+
char *whole;
43+
char *dec_part;
44+
unsigned long long dec;
45+
int i;
46+
47+
i = 0;
48+
prec = prec == -1 ? 6 : prec;
49+
nbr += prec >= 0 ? rounding(nbr, prec) : 0;
50+
nbr < 0 ? nbr *= -1 : nbr * 1;
51+
whole = ft_itoa_uintmax(nbr);
52+
dec = nbr;
53+
nbr = prec > 0 ? nbr - dec : 0;
54+
dec_part = ft_strnew(prec + 1);
55+
dec_part[i++] = (dot && prec > 0) ? '.' : '\0';
56+
while (prec-- > 0)
57+
{
58+
nbr *= 10;
59+
dec = nbr;
60+
nbr -= dec;
61+
dec_part[i++] = dec + '0';
62+
}
63+
joint = joining(whole, dec_part);
64+
return (joint);
65+
}

src/libft/ft_intlen_max.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* ft_intlen_max.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: csphilli <csphilli@student.42.fr> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2020/02/27 10:30:26 by cphillip #+# #+# */
9+
/* Updated: 2021/03/02 07:33:59 by csphilli ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "../../includes/ft_printf.h"
14+
15+
int ft_intlen_max(uintmax_t nbr)
16+
{
17+
int len;
18+
19+
if (nbr < 0)
20+
nbr *= -1;
21+
len = 1;
22+
while ((nbr /= 10) > 0)
23+
len++;
24+
return (len);
25+
}

src/libft/ft_isalpha.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* ft_isalpha.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: cphillip <cphillip@student.hive.fi> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2019/10/24 12:38:35 by cphillip #+# #+# */
9+
/* Updated: 2019/10/31 09:18:09 by cphillip ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
int ft_isalpha(int c)
14+
{
15+
return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
16+
}

src/libft/ft_isdigit.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* ft_isdigit.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: cphillip <cphillip@student.hive.fi> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2019/10/24 12:34:01 by cphillip #+# #+# */
9+
/* Updated: 2019/10/31 09:21:10 by cphillip ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
int ft_isdigit(int c)
14+
{
15+
return (c >= '0' && c <= '9');
16+
}

src/libft/ft_itoa_base.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* ft_itoa_base.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: csphilli <csphilli@student.42.fr> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2020/03/03 11:40:26 by cphillip #+# #+# */
9+
/* Updated: 2021/03/02 06:31:57 by csphilli ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "../../includes/ft_printf.h"
14+
15+
char *ft_itoa_base(uintmax_t nbr, int base)
16+
{
17+
char *new;
18+
int j;
19+
20+
j = ft_nbr_size_base(nbr, base);
21+
if (!(new = (char*)malloc(sizeof(char) * j + 1)))
22+
return (NULL);
23+
if (nbr < 0)
24+
new[0] = '-';
25+
new[j] = '\0';
26+
while (j--)
27+
{
28+
new[j] = (nbr % base < 10) ? nbr % base + '0' : nbr % base + 'a' - 10;
29+
nbr /= base;
30+
}
31+
return (new);
32+
}

src/libft/ft_itoa_uintmax.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* ft_itoa_uintmax.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: csphilli <csphilli@student.42.fr> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2020/02/27 10:35:30 by cphillip #+# #+# */
9+
/* Updated: 2021/03/02 06:42:29 by csphilli ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "../../includes/ft_printf.h"
14+
15+
char *ft_itoa_uintmax(uintmax_t n)
16+
{
17+
char *new;
18+
int len;
19+
20+
len = ft_intlen_max(n) + 1;
21+
if (!(new = ft_strnew(len + 1)))
22+
return (NULL);
23+
new[len] = '\0';
24+
len--;
25+
while (n >= 10)
26+
{
27+
new[--len] = (char)(n % 10 + '0');
28+
n /= 10;
29+
}
30+
new[--len] = (char)(n + '0');
31+
return (new);
32+
}

src/libft/ft_memalloc.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* ft_memalloc.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: csphilli <csphilli@student.42.fr> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2019/10/24 13:47:26 by cphillip #+# #+# */
9+
/* Updated: 2021/03/02 06:42:33 by csphilli ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "../../includes/ft_printf.h"
14+
15+
void *ft_memalloc(size_t size)
16+
{
17+
void *mem;
18+
19+
if (!(mem = malloc(size)))
20+
{
21+
write(2, "ERROR: Failed to allocate memory.\n", 34);
22+
exit(0);
23+
}
24+
ft_bzero(mem, size);
25+
return (mem);
26+
}

src/libft/ft_memset.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* ft_memset.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: csphilli <csphilli@student.42.fr> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2019/10/21 09:55:41 by cphillip #+# #+# */
9+
/* Updated: 2021/03/02 06:31:57 by csphilli ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "../../includes/ft_printf.h"
14+
15+
void *ft_memset(void *b, int c, size_t len)
16+
{
17+
unsigned char *ptr;
18+
19+
ptr = (unsigned char*)b;
20+
while (len-- > 0)
21+
*(ptr++) = (unsigned char)c;
22+
return (b);
23+
}

src/libft/ft_nbr_size.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* ft_nbr_size.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: csphilli <csphilli@student.42.fr> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2019/10/29 14:27:34 by cphillip #+# #+# */
9+
/* Updated: 2021/03/02 06:31:57 by csphilli ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "../../includes/ft_printf.h"
14+
15+
int ft_nbr_size(intmax_t nbr)
16+
{
17+
int len;
18+
19+
if (nbr < 0)
20+
nbr *= -1;
21+
len = 1;
22+
while ((nbr /= 10) > 0)
23+
len++;
24+
return (len);
25+
}

0 commit comments

Comments
 (0)