Skip to content

Commit

Permalink
Merge pull request #17 from Savapitech/s_add_width
Browse files Browse the repository at this point in the history
add: width
  • Loading branch information
savalet authored Oct 28, 2024
2 parents 14cf17f + 0a6e306 commit 50497ce
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 74 deletions.
File renamed without changes.
64 changes: 0 additions & 64 deletions include/lib.h

This file was deleted.

9 changes: 4 additions & 5 deletions include/my.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ typedef struct {
int width;
int precision;
int length;
char specifier;
int count;
char flags;
int written;
Expand All @@ -47,11 +46,11 @@ typedef struct {
} handler_t;

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

char *baby_revstr(char *);
Expand Down
9 changes: 8 additions & 1 deletion src/handler/put_oct.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,12 @@ int baby_put_oct(int nb, flags_t *flags)

void printf_put_oct(flags_t *flags)
{
baby_put_oct(va_arg(flags->args, int), flags);
int nb = va_arg(flags->args, int);

if (nb == 0) {
flags->spec_buff.str = "0";
flags->spec_buff.count = 1;
return;
}
baby_put_oct(nb, flags);
}
6 changes: 3 additions & 3 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ bool handle_flags(flags_t *flags)
flags->spec_buff = (buff_t){ spec_buff, 0 };
flags->prefix_buff = (buff_t){ prefix_buff, 0 };
handle_spec(flags);
if (write(STDOUT_FILENO, flags->spec_buff.str, flags->spec_buff.count) !=
flags->spec_buff.count)
return false;
if (!(flags->flags & FLAGS_PAD_RIGHT))
flags->count += width_printer(flags, flags->spec_buff.count);
write(STDOUT_FILENO, flags->spec_buff.str, flags->spec_buff.count);
if (flags->flags & FLAGS_PAD_RIGHT)
flags->count += width_printer(flags, flags->spec_buff.count);
return true;
Expand Down
7 changes: 6 additions & 1 deletion src/str_builder.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@
int width_printer(flags_t *flags, int printed_count)
{
int i = 0;
char pad_char = ' ';

if ((flags->flags & FLAGS_PAD_RIGHT) && (flags->flags & FLAGS_PAD_ZERO))
flags->flags &= ~FLAGS_PAD_ZERO;
if (flags->flags & FLAGS_PAD_ZERO && flags->spec != 'c')
pad_char = '0';
for (; i < flags->width - printed_count; i++)
write(STDOUT_FILENO, " ", sizeof(char));
write(STDOUT_FILENO, &pad_char, sizeof(char));
return i;
}

0 comments on commit 50497ce

Please sign in to comment.