Skip to content

Commit d7f730e

Browse files
authored
Funcionando c, s, i, d e u. Makefile otimizado.
Main realiza novos testes. Adicionadas novas funções à libft para fazer funcionar i, d e u. Regra run adicionada ao Makefile. Registro do Makefile no terminal agora não deixa sujeira. Agradecimento ao ruchoa.
1 parent 71835da commit d7f730e

12 files changed

+264
-55
lines changed

Makefile

+19-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
SOURCES = ft_printf.c
1+
SOURCES = ft_printf.c ft_utils.c
22
# .c=.o implicitly compiles the source-code into binary objects.
33
OBJECTS = $(SOURCES:.c=.o)
44
NAME = libftprintf.a
@@ -16,30 +16,35 @@ REMOVE = rm -f
1616
# Calling its name acts as its own makefile. Will compile only if the object
1717
# dependencies are fulfilled. ar creates an archive (here, library) from the
1818
# files member (objects), replacing them as needed. ranlib simply indexes
19-
# every function in the library (equivalent to s in crs)
20-
$(NAME): $(OBJECTS)
21-
cp $(LIBFT) $(NAME)
22-
ar rcs -r $(NAME) $(OBJECTS)
19+
# every function in the library (equivalent to s in crs).
20+
# @ hides the clutter from the terminal.
21+
$(NAME): $(LIBFT) $(OBJECTS)
22+
@cp $(LIBFT) $(NAME)
23+
@ar rcs $(NAME) $(OBJECTS)
2324

2425
$(LIBFT):
25-
make -C $(LIBFT_PATH)
26+
@make -C $(LIBFT_PATH)
2627

27-
all: $(LIBFT) $(NAME)
28+
all: $(NAME)
2829

2930
# Removes only compiled objects.
3031
clean:
31-
$(MAKEC) $(LIBFT_PATH) clean
32-
$(REMOVE) $(OBJECTS)
32+
@$(MAKEC) $(LIBFT_PATH) clean
33+
@$(REMOVE) $(OBJECTS)
3334

3435
# Removes compiled objects first, then proceeds to remove the library itself.
3536
fclean: clean
36-
$(MAKEC) $(LIBFT_PATH) fclean
37-
$(REMOVE) libft.a
38-
$(REMOVE) $(NAME)
37+
@make fclean -C $(LIBFT_PATH)
38+
@$(REMOVE) libft.a
39+
@$(REMOVE) $(NAME)
3940

4041
# Forces the recompilation even if everything is up to date.
4142
re: fclean all
42-
$(MAKEC) $(LIBFT_PATH) re
43+
@$(MAKEC) $(LIBFT_PATH)
44+
45+
run:
46+
# @cc *.c ./libft/*.c && time valgrind -s ./a.out && rm a.out
47+
@cc *.c ./libft/*.c && ./a.out && rm a.out
4348

4449
# Ignores files that could eventually be named after any of these parameters.
45-
.PHONY: libft all clean fclean re
50+
.PHONY: libft all clean fclean re run

ft_printf.c

+18-15
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,35 @@
66
/* By: cnascime <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2022/07/20 21:51:16 by cnascime #+# #+# */
9-
/* Updated: 2022/08/06 16:51:32 by cnascime ### ########.fr */
9+
/* Updated: 2022/08/10 07:25:11 by cnascime ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include "libftprintf.h"
1414
#include "./libft/libft.h"
1515

16-
// write(1, &"0123456789abcdef"[str[i] / 16], 1);
17-
int treatment(char character, va_list listofarguments)
16+
// write(fd, &"0123456789abcdef"[str[i] / 16], 1);
17+
// write(fd, &("0123456789abcdef"[str[i] / 16]), 1); testar segundo ruchoa
18+
int treatment(int fd, char character, va_list listofarguments)
1819
{
1920
if (character == 'c')
20-
return (ft_putchar_fd(va_arg(listofarguments, int), 1));
21-
/*if (character == 's')
22-
return (função(va_arg(listofarguments, char *)));
23-
if (character == 'p')
24-
return (função(va_arg(listofarguments, long long unsigned int)));
21+
return (ft_putchar_fd(fd, va_arg(listofarguments, int)));
22+
if (character == 's')
23+
return (ft_putstr_fd(fd, va_arg(listofarguments, char *)));
24+
/*if (character == 'p')
25+
return (função(va_arg(listofarguments, long long unsigned int)));*/
2526
if (character == 'd' || character == 'i')
26-
return (função(va_arg(listofarguments, int)));
27+
return (ft_putint(va_arg(listofarguments, int)));
2728
if (character == 'u')
28-
return (função(va_arg(listofarguments, unsigned int)));
29-
if (character == 'o')
29+
return (ft_putunsint(va_arg(listofarguments, unsigned int)));
30+
/*if (character == 'o')
3031
return (função(va_arg(listofarguments, unsigned int)));
3132
if (character == 'x')
3233
return (função(va_arg(listofarguments, unsigned int)));
3334
if (character == 'X')
3435
return (função(va_arg(listofarguments, unsigned int)));*/
3536
if (character == '%')
36-
return (ft_putchar_fd(character, 1));
37+
return (ft_putchar_fd(fd, character));
3738
return (0);
3839
}
3940

@@ -42,7 +43,7 @@ int treatment(char character, va_list listofarguments)
4243
0 ~Left-pads~ the number with zeroes (0) instead of spaces.
4344
. precision (displays only .X amount of characters)
4445
# Used with o, x or X specifiers, the value is ~preceded~ with 0, 0x or 0X
45-
respectively for values different than zero.
46+
respectively for values different from zero.
4647
Used with e, E and f, it forces the written output to contain a decimal point
4748
even if no digits would follow.
4849
By default, if no digits follow, no decimal point is written.
@@ -88,20 +89,22 @@ int ft_printf(const char *fixed, ...)
8889
{
8990
int index;
9091
int count;
92+
int fd;
9193
va_list listofarguments;
9294

9395
index = 0;
9496
count = 0;
97+
fd = 1;
9598
va_start(listofarguments, fixed);
9699
while (fixed[index] != '\0')
97100
{
98101
if (fixed[index] == '%' && ft_strchr("cspdiuoxX%", fixed[index + 1]))
99102
{
100-
count += ft_putchar_fd(fixed[index], 1);
103+
count += treatment(fd, fixed[index + 1], listofarguments);
101104
index++;
102105
}
103106
else
104-
count += ft_putchar_fd(fixed[index], 1);
107+
count += ft_putchar_fd(fd, fixed[index]);
105108
index++;
106109
}
107110
va_end(listofarguments);

ft_utils.c

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* ft_utils.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: cnascime <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2022/08/10 05:48:08 by cnascime #+# #+# */
9+
/* Updated: 2022/08/10 07:24:55 by cnascime ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "libftprintf.h"
14+
#include "./libft/libft.h"
15+
16+
// Converts number to string, calculates and returns the size of said string.
17+
int ft_putint(int number)
18+
{
19+
int places;
20+
char *convert;
21+
22+
convert = ft_itoa(number);
23+
places = ft_putstr_fd(1, convert);
24+
free (convert);
25+
return (places);
26+
}
27+
28+
int ft_putunsint(int number)
29+
{
30+
int places;
31+
char *convert;
32+
33+
convert = ft_utoa(number);
34+
places = ft_putstr_fd(1, convert);
35+
free (convert);
36+
return (places);
37+
}

libft/Makefile

+4-5
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,24 @@ FLAGS = -Wall -Wextra -Werror
1818

1919
# Compiles from open-source to binary, but doesn't link. Necessary to run flags.
2020
.c.o: $(OBJECTS)
21-
$(COMPILE) $(FLAGS) -c $< -o $(<:.c=.o)
21+
@$(COMPILE) $(FLAGS) -c $< -o $(<:.c=.o)
2222

2323
# Calling its name acts as its own makefile. Will compile only if the object
2424
# dependencies are fulfilled. ar creates an archive (here, library) from the
2525
# files member (objects), replacing them as needed. ranlib simply indexes
2626
# every function in the library.
2727
$(NAME): $(OBJECTS)
28-
ar cr $(NAME) $(OBJECTS)
29-
ranlib libft.a
28+
@ar rcs $(NAME) $(OBJECTS)
3029

3130
all: $(NAME)
3231

3332
# Removes only compiled objects.
3433
clean:
35-
$(REMOVE) $(OBJECTS)
34+
@$(REMOVE) $(OBJECTS)
3635

3736
# Removes compiled objects first, then proceeds to remove the library itself.
3837
fclean: clean
39-
$(REMOVE) $(NAME)
38+
@$(REMOVE) $(NAME)
4039

4140
# Forces the recompilation even if everything is up to date.
4241
re: fclean all

libft/ft_putchar_fd.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
/* By: cnascime <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2022/06/03 18:02:01 by cnascime #+# #+# */
9-
/* Updated: 2022/08/05 05:06:04 by cnascime ### ########.fr */
9+
/* Updated: 2022/08/09 00:48:33 by cnascime ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include "libft.h"
1414

15-
int ft_putchar_fd(char c, int fd)
15+
int ft_putchar_fd(int fd, char c)
1616
{
1717
write(fd, &c, sizeof(c));
1818
return (1);

libft/ft_putendl_fd.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
/* By: cnascime <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2022/06/04 03:10:42 by cnascime #+# #+# */
9-
/* Updated: 2022/06/09 18:45:45 by cnascime ### ########.fr */
9+
/* Updated: 2022/08/08 18:37:30 by cnascime ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include "libft.h"
1414

15-
void ft_putendl_fd(char *s, int fd)
15+
void ft_putendl_fd(int fd, char *s)
1616
{
1717
int i;
1818

libft/ft_putnbr_fd.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
/* By: cnascime <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2022/05/09 18:45:22 by cnascime #+# #+# */
9-
/* Updated: 2022/06/08 15:13:46 by cnascime ### ########.fr */
9+
/* Updated: 2022/08/10 04:14:02 by cnascime ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include "libft.h"
1414

1515
// Prints a number in a recursive way.
16-
void ft_putnbr_fd(int n, int fd)
16+
void ft_putnbr_fd(int fd, int n)
1717
{
1818
unsigned char c;
1919

libft/ft_putstr_fd.c

+8-4
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,26 @@
66
/* By: cnascime <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2022/06/04 03:08:09 by cnascime #+# #+# */
9-
/* Updated: 2022/06/09 18:46:03 by cnascime ### ########.fr */
9+
/* Updated: 2022/08/09 00:52:19 by cnascime ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include "libft.h"
1414

15-
void ft_putstr_fd(char *s, int fd)
15+
int ft_putstr_fd(int fd, char *s)
1616
{
1717
int i;
1818

19-
if (!s)
20-
return ;
2119
i = 0;
20+
if (!s)
21+
{
22+
ft_putstr_fd(1, "(null)");
23+
return (ft_strlen("(null)"));
24+
}
2225
while (s[i] != '\0')
2326
{
2427
write(fd, &s[i], sizeof(char));
2528
i++;
2629
}
30+
return (ft_strlen(s));
2731
}

libft/ft_utoa.c

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* ft_utoa.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: cnascime <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2022/06/01 03:58:29 by cnascime #+# #+# */
9+
/* Updated: 2022/08/10 07:18:26 by cnascime ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "libft.h"
14+
15+
static int ft_places(unsigned long number);
16+
17+
// Same as itoa, but for positive values only.
18+
char *ft_utoa(unsigned int n)
19+
{
20+
unsigned long number;
21+
size_t places;
22+
char *string;
23+
24+
number = n;
25+
places = ft_places(number);
26+
string = (char *)ft_calloc(sizeof(*string) * (places), 1);
27+
if (!string)
28+
return (NULL);
29+
while (places-- != 0)
30+
{
31+
if (number / 10 < 1)
32+
break ;
33+
string[places - 1] = number % 10 + '0';
34+
number /= 10;
35+
}
36+
string[places - 1] = number + '0';
37+
return (string);
38+
}
39+
40+
// Calculates how many decimal places the string'll need.
41+
static int ft_places(unsigned long number)
42+
{
43+
size_t places;
44+
45+
places = 0;
46+
if (number <= 0)
47+
places++;
48+
while (number > 0)
49+
{
50+
places++;
51+
number /= 10;
52+
}
53+
places++;
54+
return (places);
55+
}

libft/libft.h

+7-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: cnascime <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2022/05/13 20:13:09 by cnascime #+# #+# */
9-
/* Updated: 2022/08/05 05:17:48 by cnascime ### ########.fr */
9+
/* Updated: 2022/08/10 07:20:59 by cnascime ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -15,6 +15,7 @@
1515

1616
# include <stdlib.h>
1717
# include <unistd.h>
18+
# include <stdint.h>
1819

1920
typedef struct s_list
2021
{
@@ -47,10 +48,10 @@ void *ft_calloc(size_t count, size_t size);
4748
char *ft_strdup(const char *s1);
4849
char *ft_substr(char const *s, unsigned int start, size_t len);
4950
char *ft_strjoin(char const *s1, char const *s2);
50-
int ft_putchar_fd(char c, int fd);
51-
void ft_putstr_fd(char *s, int fd);
52-
void ft_putendl_fd(char *s, int fd);
53-
void ft_putnbr_fd(int n, int fd);
51+
int ft_putchar_fd(int fd, char c);
52+
int ft_putstr_fd(int fd, char *s);
53+
void ft_putendl_fd(int fd, char *s);
54+
void ft_putnbr_fd(int fd, int n);
5455
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
5556
void ft_striteri(char *s, void (*f)(unsigned int, char *));
5657
char *ft_strtrim(char const *s1, char const *set);
@@ -65,4 +66,5 @@ void ft_lstdelone(t_list *lst, void (*del)(void *));
6566
void ft_lstclear(t_list **lst, void (*del)(void *));
6667
void ft_lstiter(t_list *lst, void (*f)(void *));
6768
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
69+
char *ft_utoa(unsigned int n);
6870
#endif

0 commit comments

Comments
 (0)