Skip to content

Commit

Permalink
M
Browse files Browse the repository at this point in the history
  • Loading branch information
andreyvdl committed Jun 27, 2024
1 parent 976dade commit fac7ddd
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
25 changes: 25 additions & 0 deletions RANK03/PRINTF/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# PRINTF

## INFORMATION/INFORMAÇÃO

Write a function named `ft_printf` that will mimic the real printf but will
manage only the following conversions:

Esccreva uma função chamada `ft_printf` que vai imitar a printf real mas só vai
tratar as seguintes conversões:

```text
s (string)
d (decimal)
x (lowercase hexadecimal)
```

## FUNCTIONS/FUNÇÕES

`malloc, free, write, va_start, var_arg, va_copy and va_end`

## PROTOTYPE

```c
int ft_printf(const char *, ...)
```
129 changes: 129 additions & 0 deletions RANK03/PRINTF/ft_printf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adantas- <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/25 14:43:17 by adantas- #+# #+# */
/* Updated: 2024/06/27 11:28:00 by adantas- ### ########.fr */
/* */
/* ************************************************************************** */

#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>

static int putstr(char **start, char *end);
static int expand_args(va_list args, char **fmt, char **walk);
static int print_str(char *str);

int ft_printf(const char *fmt, ...)
{
int printed;
char *walk;
va_list args;

if (fmt == 0)
return (-1);
va_start(args, fmt);
printed = 0;
for (walk = (char *)fmt; *walk != 0; walk++) {
if (*walk == '%') {
printed += putstr(&fmt, walk);
walk++;
if (*walk == 0)
break;
printed += expand_args(args, &fmt, &walk);
}
}
if (walk != fmt)
printed += putstr(&fmt, walk);
va_end(args);
return (printed);
}

static int putstr(char **start, char *end)
{
size_t size;

size = *start - end;
size = write(STDOUT_FILENO, *start, size);
*start = end;
return ((int)size);
}



























// TODO LATER















static int expand_args(va_list args, char **fmt, char **walk)
{
int size = 0;

if (**walk == 's') {
(*walk)++;
size = print_str(va_arg(args, char *));
}
else if (**walk == 'd') {
(*walk)++;
size = print_digit(va_arg(args, int));
}
else if (**walk == 'x') {
(*walk)++;
size = print_hexadecimal(va_arg(args, unsigned int));
}
else {
(*walk)++;
size = putstr(fmt, *walk);
}
*fmt = *walk;
return (size);
}

static int print_str(char *str)
{
int i = 0;

while (str[i] != 0)
++i;
return (write(STDOUT_FILENO, str, i));
}
4 changes: 4 additions & 0 deletions RANK03/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# CHOICES/ESCOLHAS

⭕ ft_printf
❌ get_next_line

0 comments on commit fac7ddd

Please sign in to comment.