Skip to content

Commit

Permalink
Merge pull request #31 from Savapitech/put_science
Browse files Browse the repository at this point in the history
fix: put hex and put pointer
  • Loading branch information
savalet authored Oct 31, 2024
2 parents fe9bc2f + ed19697 commit 672d62e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
37 changes: 27 additions & 10 deletions src/handler/put_hex.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,33 @@

#include "my.h"

static
void put_alt_form(flags_t *flags)
{
if (isupper(flags->spec)) {
if (flags->flags & FLAGS_ALT_FORM) {
flags->spec_buff.str[0] = '0';
flags->spec_buff.str[1] = 'X';
flags->spec_buff.str += 2;
flags->spec_buff.count += 2;
}
} else {
if (flags->flags & FLAGS_ALT_FORM) {
flags->spec_buff.str[0] = '0';
flags->spec_buff.str[1] = 'x';
flags->spec_buff.str += 2;
flags->spec_buff.count += 2;
}
}
}

int baby_put_hex(size_t nb, flags_t *flags, int i)
{
size_t result = (size_t)nb;
char str[64];
char *old_spec_buff = flags->spec_buff.str;

put_alt_form(flags);
for (; nb != 0; i++) {
result = nb % 16;
if (result < 10)
Expand All @@ -23,7 +45,8 @@ int baby_put_hex(size_t nb, flags_t *flags, int i)
str[i] = '\0';
baby_revstr(str);
baby_strcpy(flags->spec_buff.str, str);
flags->spec_buff.count = i;
flags->spec_buff.count += i;
flags->spec_buff.str = old_spec_buff;
return i;
}

Expand All @@ -33,6 +56,7 @@ int baby_put_hex_upc(size_t nb, flags_t *flags, int i)
size_t result = (size_t)nb;
char str[64];

put_alt_form(flags);
for (; nb != 0; i++) {
result = nb % 16;
if (result < 10)
Expand All @@ -50,7 +74,6 @@ int baby_put_hex_upc(size_t nb, flags_t *flags, int i)

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

if (flags->precision == 0 && nb == 0)
Expand All @@ -60,14 +83,8 @@ void printf_put_hex(flags_t *flags)
flags->spec_buff.count = 1;
return;
}
if (flags->precision > 0) {
if (flags->flags & FLAGS_PAD_RIGHT)
flags->flags &= ~FLAGS_PAD_RIGHT;
flags->flags |= FLAGS_PAD_ZERO;
flags->width = flags->precision;
}
if (isupper(flags->spec))
baby_put_hex_upc(nb, flags, i);
baby_put_hex_upc(nb, flags, 0);
else
baby_put_hex(nb, flags, i);
baby_put_hex(nb, flags, 0);
}
11 changes: 3 additions & 8 deletions src/handler/put_pointer.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,14 @@
** display the adress given as parameter
** in the hexadecimal format
*/

#include <stdint.h>

#include "my.h"

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

old_spec_buff_str = flags->spec_buff.str;
flags->spec_buff.str[0] = '0';
flags->spec_buff.str[1] = 'x';
baby_put_hex(src, flags, 3);
flags->spec_buff.str = old_spec_buff_str;
flags->flags |= FLAGS_ALT_FORM;
baby_put_hex(src, flags, 0);
}
4 changes: 4 additions & 0 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const handler_t HANDLERS[] = {
{ 'b', &printf_put_bin },
{ 'B', &printf_put_bin },
{ 'F', &printf_put_float},
{ 'p', &printf_put_pointer},
{ 'e', &printf_put_scientific },
{ 'E', &printf_put_scientific },
{ '\0', NULL }
Expand Down Expand Up @@ -96,6 +97,9 @@ bool handle_flags(flags_t *flags)
static char prefix_buff[4];
static char spec_buff[64];

flags->flags = 0;
flags->width = 0;
flags->precision = -1;
flags->fmt++;
if (*flags->fmt == '\0')
return true;
Expand Down

0 comments on commit 672d62e

Please sign in to comment.