Skip to content

Commit

Permalink
add: add buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
savalet committed Oct 27, 2024
1 parent c98ae71 commit 2a7713b
Show file tree
Hide file tree
Showing 16 changed files with 178 additions and 138 deletions.
50 changes: 33 additions & 17 deletions include/my.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,56 @@
#ifndef MY_H_
#define MY_H_

#include <stdarg.h>
#include <unistd.h>
#include <ctype.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <unistd.h>

#define EXIT_ERROR -84
#define ERROR_OVERFLOW -2
#define CHAR_TO_DIGIT(c) ((c) & 0xf)
#define ARRAY_SIZE(array) ((sizeof array) / (sizeof array[0]))
#define TODO(s) // renvoie rin


typedef struct {
char *str;
int count;
} buff_t;

typedef struct {
char *fmt;
char spec;
va_list args;
char *built_string;
int width;
int precision;
int length;
char specifier;
int count;
char flags;
int written;
buff_t spec_buff;
buff_t prefix_buff;
} flags_t;

typedef struct {
char flag;
int (*ptr)(flags_t *flags);
void (*ptr)(flags_t *flags);
} handler_t;

typedef enum {
FLAGS_ALT_FORM = 1 << 0,
FLAGS_PAD_ZERO = 1 << 1,
FLAGS_SET_SPACE = 1 << 2,
FLAGS_PUT_SIGN = 1 << 3,
FLAGS_PAD_RIGHT = 1 << 4
} flags_bit_shifts_t;

char *baby_revstr(char *);
int baby_getnbr(char const *);
int baby_put_nbr(int);
int baby_put_hex(uintptr_t truc);
int baby_put_nbr(int, flags_t *, int);
int baby_put_hex(size_t nb, flags_t *);
int baby_putchar(char);
int baby_putstr(char *);
int baby_stridx(char const *, char);
Expand All @@ -51,15 +66,16 @@ size_t baby_intlen(size_t truc, int);
int baby_strpnum(char **);
int my_printf(char *, ...);
int parser(char *, va_list);
int printf_put_float(flags_t *);
int printf_put_hex(flags_t *);
int printf_put_nbr(flags_t *);
int printf_put_oct(flags_t *);
int printf_put_pointer(flags_t *);
int printf_put_unsigned_nbr(flags_t *);
int printf_putchar(flags_t *);
int printf_putpercentage(flags_t *);
int printf_putstr(flags_t *);
int printf_put_bin(flags_t *);
int printf_put_hex_upc(flags_t *);
void printf_put_float(flags_t *);
void printf_put_hex(flags_t *);
void printf_put_nbr(flags_t *);
void printf_put_oct(flags_t *);
void printf_put_pointer(flags_t *);
void printf_put_unsigned_nbr(flags_t *);
void printf_putchar(flags_t *);
void printf_putpercentage(flags_t *);
void printf_putstr(flags_t *);
void printf_put_bin(flags_t *);
void printf_put_hex_upc(flags_t *);
int width_printer(flags_t *, int);
#endif /* MY_H_ */
19 changes: 11 additions & 8 deletions src/baby/baby_put_nbr.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@
*/
#include "my.h"

int baby_put_nbr(int nb)
int baby_put_nbr(int nb, flags_t *flags, int i)
{
if (nb < 0) {
baby_putchar('-');
flags->spec_buff.str[i] = '-';
nb *= -1;
i++;
}
if (nb < 10) {
baby_putchar(nb + '0');
} else {
baby_put_nbr(nb / 10);
baby_putchar(nb % 10 +'0');
if (nb < 10)
flags->spec_buff.str[i] = nb + '0';
else {
i = baby_put_nbr(nb / 10, flags, i);
flags->spec_buff.str[i] = nb % 10 + '0';
}
return 0;
i++;
flags->spec_buff.count = i;
return i;
}
17 changes: 7 additions & 10 deletions src/handler/put_bin.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
*/
#include "my.h"

int baby_put_bin(int nb)
void printf_put_bin(flags_t *flags)
{
int nb = va_arg(flags->args, int);
int result = nb;
int len = baby_intlen(nb, 2);
char str[baby_intlen(nb, 2)];

if (nb == 0){
baby_putchar('0');
return 1;
flags->spec_buff.str = "0";
flags->spec_buff.count = 1;
return;
}
for (int i = 0; nb != 0; i++) {
result = nb % 2;
Expand All @@ -23,11 +25,6 @@ int baby_put_bin(int nb)
}
str[len] = '\0';
baby_revstr(str);
baby_putstr(str);
return len;
}

int printf_put_bin(flags_t *flags)
{
return baby_put_bin(va_arg(flags->args, int));
flags->spec_buff.count = len;
flags->spec_buff.str = str;
}
4 changes: 2 additions & 2 deletions src/handler/put_count_char.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

#include "my.h"

int printf_count_char(flags_t *flags)
void printf_count_char(flags_t *flags)
{
int *count = va_arg(flags->args, int *);

*count = flags->written;
return *count;
flags->spec_buff.count = *count;
}
39 changes: 2 additions & 37 deletions src/handler/put_float.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,7 @@

#include "my.h"

static
void printf_put_float2(
int *decimal,
int x,
int precision,
int entier
)
void printf_put_float(flags_t *flags)
{
x += baby_put_nbr(entier);
baby_putchar('.');
x++;
for (int i = 0; i < precision; i++)
if (decimal[i] != 0)
x += baby_put_nbr(decimal[i]);
}

int printf_put_float(flags_t *flags)
{
int x = 0;
float nb = va_arg(flags->args, double);
int entier = (int)nb;
int precision = flags->precision;
int decimal[precision];
int b = 10;
int tt;

for (int i = 0; i < precision; i++) {
tt = (int)(nb * b);
decimal[i] = tt % 10;
b = b * 10;
}
if (nb < 0) {
baby_putchar('-');
nb = nb * -1;
entier = nb;
}
printf_put_float2(decimal, x, precision, entier);
return (0);
return;
}
42 changes: 24 additions & 18 deletions src/handler/put_hex.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,15 @@
** File description:
** display an int in hexadecimal format
*/
#include <stdint.h>

#include "my.h"
#include <stdio.h>

int baby_put_hex(size_t nb)
int baby_put_hex(size_t nb, flags_t *flags)
{
size_t result = (size_t)nb;
char str[baby_intlen(nb, 16)];
int len = baby_intlen(nb, 16);

if (nb == 0) {
baby_putchar('0');
return 1;
}
for (int i = 0; nb != 0; i++) {
result = nb % 16;
if (result < 10)
Expand All @@ -28,20 +23,17 @@ int baby_put_hex(size_t nb)
}
str[len] = '\0';
baby_revstr(str);
baby_putstr(str);
flags->spec_buff.str = str;
return len;
}

int baby_put_hex_upc(size_t nb)
static
int baby_put_hex_upc(size_t nb, flags_t *flags)
{
size_t result = (size_t)nb;
char str[baby_intlen(nb, 16)];
int len = baby_intlen(nb, 16);

if (nb == 0) {
baby_putchar('0');
return 1;
}
for (int i = 0; nb != 0; i++) {
result = nb % 16;
if (result < 10)
Expand All @@ -52,16 +44,30 @@ int baby_put_hex_upc(size_t nb)
}
str[len] = '\0';
baby_revstr(str);
baby_putstr(str);
flags->spec_buff.str = str;
return len;
}

int printf_put_hex_upc(flags_t *flags)
void printf_put_hex_upc(flags_t *flags)
{
return baby_put_hex_upc(va_arg(flags->args, void *));
size_t nb = (size_t)va_arg(flags->args, void *);

if (nb == 0) {
flags->spec_buff.str = "0";
flags->spec_buff.count = 1;
return;
}
baby_put_hex_upc(nb, flags);
}

int printf_put_hex(flags_t *flags)
void printf_put_hex(flags_t *flags)
{
return baby_put_hex(va_arg(flags->args, void *));
size_t nb = (size_t)va_arg(flags->args, void *);

if (nb == 0) {
flags->spec_buff.str = "0";
flags->spec_buff.count = 1;
return;
}
baby_put_hex(nb, flags);
}
4 changes: 2 additions & 2 deletions src/handler/put_nbr.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include "my.h"

int printf_put_nbr(flags_t *flags)
void printf_put_nbr(flags_t *flags)
{
return baby_put_nbr(va_arg(flags->args, int));
baby_put_nbr(va_arg(flags->args, int), flags, 0);
}
28 changes: 16 additions & 12 deletions src/handler/put_oct.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,30 @@
** File description:
** display an int given as parameter in octal format
*/

#include <stdio.h>

#include "my.h"

int baby_put_oct(int nb)
int baby_put_oct(int nb, flags_t *flags)
{
int result = nb;
int len = baby_intlen(nb, 8);
char str[baby_intlen(nb, 8)];
size_t result = (size_t)nb;
char str[64];
int i = 0;

for (int i = 0; nb != 0; i++) {
for (; nb != 0; i++) {
result = nb % 8;
str[i] = (result + 48);
nb /= 8;
str[i] = (result + '0');
nb = nb / 8;
}
str[len] = '\0';
str[i] = '\0';
baby_revstr(str);
baby_putstr(str);
return len;
flags->spec_buff.str = str;
flags->spec_buff.count = i;
return 0;
}

int printf_put_oct(flags_t *flags)
void printf_put_oct(flags_t *flags)
{
return baby_put_oct(va_arg(flags->args, int));
baby_put_oct(va_arg(flags->args, int), flags);
}
13 changes: 9 additions & 4 deletions src/handler/put_pointer.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@
** display the adress given as parameter
** in the hexadecimal format
*/

#include <stdint.h>

#include "my.h"

int printf_put_pointer(flags_t *flags)
void printf_put_pointer(flags_t *flags)
{
uintptr_t src = (uintptr_t)va_arg(flags->args, void *);
char *old_spec_buff_str;

baby_putstr("0x");
baby_put_hex(src);
return 0;
old_spec_buff_str = flags->spec_buff.str;
flags->spec_buff.str = "0x";
flags->spec_buff.str += 2;
baby_put_hex(src, flags);
flags->spec_buff.str = old_spec_buff_str;
}
4 changes: 2 additions & 2 deletions src/handler/put_unsigned_nbr.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int baby_put_unsigned_nbr(unsigned int nb)
return baby_intlen(nb, 10);
}

int printf_put_unsigned_nbr(flags_t *flags)
void printf_put_unsigned_nbr(flags_t *flags)
{
return baby_put_unsigned_nbr(va_arg(flags->args, int));
baby_put_unsigned_nbr(va_arg(flags->args, int));
}
6 changes: 3 additions & 3 deletions src/handler/putchar.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

#include "my.h"

int printf_putchar(flags_t *flags)
void printf_putchar(flags_t *flags)
{
int c = va_arg(flags->args, int);

return (write(STDOUT_FILENO, &c, sizeof(char)) == sizeof(char))
? (int)(sizeof(char)) : EXIT_ERROR;
flags->spec_buff.str[0] = c;
flags->spec_buff.count = 1;
}
Loading

0 comments on commit 2a7713b

Please sign in to comment.