Merge pull request #3 from LinlyBoi/printf-smol

Shrunk printf
This commit is contained in:
2023-08-22 19:15:20 +03:00
committed by GitHub
4 changed files with 48 additions and 45 deletions

4
main.h
View File

@@ -1,5 +1,6 @@
#ifndef MAIN_H_ #ifndef MAIN_H_
#define MAIN_H_ #define MAIN_H_
#include <stdarg.h>
int _printf(const char *format, ...); int _printf(const char *format, ...);
int _contains(const char *str, char c); int _contains(const char *str, char c);
int _strlen(const char *str); int _strlen(const char *str);
@@ -7,6 +8,7 @@ char *_strcpy(char *dest, char *src);
char *append(char *str, char c); char *append(char *str, char c);
int _puts(char *str); int _puts(char *str);
int _putchar(char c); int _putchar(char c);
char* _memset(char *str, int bval); char *_memset(char *adr, int bval);
int fmt(char c, va_list args);
#endif #endif

View File

@@ -4,19 +4,19 @@
* _memset - sets values of bytes to specific value * _memset - sets values of bytes to specific value
* *
* @adr: head address * @adr: head address
* @n: number of bytes * @bval: number of bytes
* *
* Return: pointer to place * Return: pointer to place
*/ */
char* _memset(char *str, int bval) char *_memset(char *adr, int bval)
{ {
int i; int i;
for (i = 0; i < _strlen(str); i++) for (i = 0; i < _strlen(adr); i++)
{ {
*(str + i) = bval; *(adr + i) = bval;
} }
return (str); return (adr);
} }

View File

@@ -1,5 +1,4 @@
#include "main.h" #include "main.h"
#include <stdarg.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
/** /**
@@ -10,61 +9,63 @@
**/ **/
int _printf(const char *format, ...) int _printf(const char *format, ...)
{ {
int buff_idx, fmt_idx; /* Indexes */ unsigned int buff_idx, fmt_idx, buff_size, printed;
unsigned int identifiers, BUFF_SIZE, printed;
char *buffer; /*where non formated things are stored*/ char *buffer; /*where non formated things are stored*/
va_list args; va_list args;
va_start(args, format); va_start(args, format);
buff_size = _strlen(format) - _contains(format, '%');
buffer = (char *) malloc(buff_size); /* sized of the non % instances only*/
identifiers = _contains(format, '%'); /* instances of %s, %c etc */ if (!format && !buffer) /* No string. No laundry */
BUFF_SIZE = _strlen(format) - identifiers;
buffer = malloc(BUFF_SIZE); /* sized of the non % instances only*/
if (!format) /* No string. No laundry */
return (0); return (0);
buff_idx = 0; /* was there a way to squish these together? */ buff_idx = fmt_idx = printed = 0; /*chain assignment*/
fmt_idx = 0;
printed = 0;
while (*(format + fmt_idx)) while (*(format + fmt_idx))
{ {
if ((*(format + fmt_idx) == '%') && (*(format + fmt_idx + 1))) if ((*(format + fmt_idx) == '%') && (*(format + fmt_idx + 1)))
{ {
if (*buffer) /* printing and clearing buffer on formatted things */ if (buffer) /* printing and clearing buffer on formatted things */
{ {
_puts(buffer); printed += _puts(buffer);
BUFF_SIZE -= _strlen(buffer); buff_size -= _strlen(buffer);
printed += _strlen(buffer); _memset(buffer, 0);
free(buffer); buffer = (char *) malloc(buff_size);
buffer = malloc(BUFF_SIZE); if (!buffer)
return (-1);
buff_idx = 0; buff_idx = 0;
} }
switch (*(format + fmt_idx + 1)) /*this needs to shrink*/ printed += fmt(*(format + fmt_idx + 1), args);
{ fmt_idx += 2;
case 's':
printed += _puts(va_arg(args, char*));
break;
case 'c':
printed += _putchar(va_arg(args, int));
break;
case '%': /*add 1 byte*/
printed += _putchar('%');
break;
}
fmt_idx += 2;
} }
else else
{ *(buffer + buff_idx++) = *(format + fmt_idx++); /* filling up buffer */
*(buffer + buff_idx) = *(format + fmt_idx); /* filling up buffer */
buff_idx++;
fmt_idx++;
}
} }
if (*buffer) /*final buffer check*/ if (buffer)
{ {
printed += _puts(buffer); _puts(buffer);
free(buffer); free(buffer);
} }
return (printed); return (printed);
}
/**
* fmt - format because its too chonky for printf
* @c: format character (for now)
* @args: the arguments to pop from
* Return: bytes written to stdout
*/
int fmt(char c, va_list args)
{
switch (c) /*this needs to shrink*/
{
case 's':
return (_puts(va_arg(args, char*)));
case 'c':
return (_putchar(va_arg(args, int)));
case '%': /*add 1 byte*/
return (_putchar('%'));
default:
return (0);
}
} }

View File

@@ -10,6 +10,6 @@ int main(void)
{ {
char c = 97; char c = 97;
_printf("cat: %css\n", c); _printf("cat: %css\noh wait is that...a %%\n", c);
return (0); return (0);
} }