Skip to content

Parse format string #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 16 additions & 22 deletions _printf.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include <unistd.h>
#include <stdlib.h>
#include "main.h"

/**
Expand All @@ -11,24 +9,26 @@
*/
int _printf(const char *format, ...)
{
int char_count = 0;
int char_count = 0, fmt_status;
va_list ap;

va_start(ap, format);

if (format == NULL)
return (0);
if (!(*format))
return (char_count);
fmt_status = parse_format(format);

while (*format)
if (fmt_status == 1)
{
if (*format == '%')
char_count += p_func(ap, *++format);
else
char_count += write(1, format, 1);

++format;
va_start(ap, format);
while (*format)
{
if (*format == '%')
char_count += p_func(ap, *++format);
else
char_count += write(1, format, 1);
++format;
}
va_end(ap);
}
va_end(ap);
return (char_count);
}

Expand All @@ -54,11 +54,5 @@ int p_func(va_list ap, char specifier)
case '%':
char_count += write(1, "%", 1);
break;
case '\0':
write(2, "Invalid Format\n", 16);
exit(1);
break;
}
va_end(ap);
return (char_count);
}
}
8 changes: 6 additions & 2 deletions main.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#ifndef MAIN_H
#define MAIN_H

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

#define MIN(x, y) (((x) < (y)) ? (x) : (y))

int _printf(const char *format, ...);
int parse_format(const char *);
int handle_percent(char, char);
int p_func(va_list, char);
int print_char(va_list);
int print_string(va_list);
int p_func(va_list, char);
void print_error(char *);

#endif
72 changes: 72 additions & 0 deletions parser.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include "main.h"

/**
* parse_format - parse format string
* @fmt - Format string to parse
* Description: Determines if the format string
* contains valid format specifiers.
* Return: 1 (valid) or 0 (invalid).
*/
int parse_format(const char *fmt)
{
int i = 0, ret_val, len = strlen(fmt) - 1;

while (i <= len)
{
if (fmt[i] == '%')
{
if (i + 1 > len) /* fmt[i] is the last char of *fmt */
{
ret_val = 0;
break;
}
/* char fmt[i] does not denote allowed specifier */
if (fmt[i + 1] != 'c' && fmt[i + 1] != 's' && fmt[i + 1]
!= '%')
{
ret_val = 0;
break;
}
if (fmt[i + 1] == '%') /* % was escaped */
{
/* Returns (0) when last char is % */
if ((i + 2) == len && fmt[i + 2] == '%')
{
ret_val = 0;
break;
}
/*Fast forward check to determine next-2 chars*/
if ((i + 2) < len)
{
ret_val = (handle_percent(fmt[i + 2], fmt[i
+ 3]));
if (ret_val == 0)
break;
}
}
i += 2; /* skip determined fmt specifier */
}
else
{
ret_val = 1;
i++;
}
}
return (ret_val);
}

/**
* handle_percent - fast forward check
* @nxt2: Third char of *fmt
* @nxt3: Fourth char of *fmt
* Description: Fast forward check the next
* two chars of %.
* Return: 1 (valid) or 0 (invalid)
*/
int handle_percent(char nxt2, char nxt3)
{
if (nxt2 == '%')
if (nxt3 != 'c' && nxt3 != 's' && nxt3 != '%')
return (0);
return (1);
}
1 change: 0 additions & 1 deletion print_literals.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include <unistd.h>
#include "main.h"
#include <string.h>

Expand Down