Skip to content

Commit b408529

Browse files
committed
Parse format string
1 parent ad2abf0 commit b408529

File tree

5 files changed

+55
-33
lines changed

5 files changed

+55
-33
lines changed

_printf.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,33 @@
1616

1717
int _printf(const char *format, ...)
1818
{
19-
int char_count = 0;
19+
int char_count = 0, fmt_status;
2020
va_list ap;
2121

22-
va_start(ap, format);
2322

2423
if (format == NULL)
2524
{
2625
write(2, "Cannot print null\n", 19);
27-
va_end(ap);
28-
return (-1);
26+
return (0);
2927
}
28+
fmt_status = parse_format(format);
3029

31-
while (*format)
30+
if (fmt_status == 1)
3231
{
33-
if (*format == '%')
34-
char_count += p_func(ap, *++format);
35-
else
36-
char_count += write(1, format, 1);
32+
va_start(ap, format);
33+
while (*format)
34+
{
35+
if (*format == '%')
36+
char_count += p_func(ap, *++format);
37+
else
38+
char_count += write(1, format, 1);
3739

38-
++format;
40+
++format;
41+
}
42+
va_end(ap);
3943
}
40-
va_end(ap);
44+
else
45+
write(1, "Invalid format specifier\n", 26);
4146
return (char_count);
4247
}
4348

@@ -74,8 +79,6 @@ int p_func(va_list ap, char specifier)
7479
case '%':
7580
char_count += write(1, "%", 1);
7681
break;
77-
default:
78-
break;
7982
}
8083
va_end(ap);
8184
return (char_count);

main.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef MAIN_H
22
#define MAIN_H
33
#include <stdarg.h>
4+
#include <string.h>
45
#define ABS(x) ((x) < 0 ? -(x) : (x))
56
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
67
#define MAX_BYTE_SIZE 30
@@ -10,7 +11,7 @@ int print_char(va_list);
1011
int print_string(va_list);
1112
int p_func(va_list, char);
1213
void print_error(char *);
13-
14+
int parse_format(const char *);
1415

1516
void *alloc(size_t);
1617
char *int_to_string(int);

parser.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "main.h"
2+
3+
/**
4+
* parse_format - parse format string
5+
* @fmt: Format string to parse
6+
* Description: Determines if the format string
7+
* contains valid format specifiers.
8+
* Return: 1 (valid) or 0 (invalid).
9+
*/
10+
int parse_format(const char *fmt)
11+
{ int i = 0, ret_val = 1, len = strlen(fmt) - 1;
12+
13+
while (i <= len)
14+
{
15+
if (fmt[i] == '%')
16+
{
17+
if (i + 1 > len) /* fmt[i] is the last char of *fmt */
18+
{
19+
ret_val = 0;
20+
break;
21+
}
22+
/* char fmt[i] does not denote allowed specifier */
23+
if (fmt[i + 1] != 'c' && fmt[i + 1] != 's' && fmt[i +
24+
1] != '%' && fmt[i + 1] != 'd' && fmt[i
25+
+ 1] != 'i')
26+
{
27+
ret_val = 0;
28+
break;
29+
}
30+
i += 2; /* skip determined fmt specifier */
31+
}
32+
else
33+
i++;
34+
}
35+
return (ret_val);
36+
}

print_digits.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <unistd.h>
33
#include "main.h"
44
#include <stdlib.h>
5+
#include <limits.h>
56

67
/**
78
* print_decimal - Auxilliary Function
@@ -19,11 +20,6 @@ int print_decimal(va_list ap)
1920
int num = va_arg(ap, int);
2021
char *number_string;
2122

22-
if (!num)
23-
{
24-
write(2, "Error", 6);
25-
return (-1);
26-
}
2723
if (num < 0)
2824
{
2925
write(1, "-", 1);

print_error.c

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)