forked from kbtisuu/printf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
godswillubah
committed
Mar 28, 2023
1 parent
52348f8
commit 664e114
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include "main.h" | ||
/** | ||
* handle_print - Prints an argument based on its type | ||
* @fmt: Formatted string in which to print the arguments. | ||
* @list: List of arguments to be printed. | ||
* @ind: ind. | ||
* @buffer: Buffer array to handle print. | ||
* @flags: Calculates active flags | ||
* @width: get width. | ||
* @precision: Precision specification | ||
* @size: Size specifier | ||
* Return: 1 or 2; | ||
* Written by Kebron Araya and Godswill Ubah | ||
*/ | ||
int handle_print(const char *fmt, int *ind, va_list list, char buffer[], | ||
int flags, int width, int precision, int size) | ||
{ | ||
int i, unknow_len = 0, printed_chars = -1; | ||
fmt_t fmt_types[] = { | ||
{'c', print_char}, {'s', print_string}, {'%', print_percent}, | ||
{'i', print_int}, {'d', print_int}, {'b', print_binary}, | ||
{'u', print_unsigned}, {'o', print_octal}, {'x', print_hexadecimal}, | ||
{'X', print_hexa_upper}, {'p', print_pointer}, {'S', print_non_printable}, | ||
{'r', print_reverse}, {'R', print_rot13string}, {'\0', NULL} | ||
}; | ||
for (i = 0; fmt_types[i].fmt != '\0'; i++) | ||
if (fmt[*ind] == fmt_types[i].fmt) | ||
return (fmt_types[i].fn(list, buffer, flags, width, precision, size)); | ||
|
||
if (fmt_types[i].fmt == '\0') | ||
{ | ||
if (fmt[*ind] == '\0') | ||
return (-1); | ||
unknow_len += write(1, "%%", 1); | ||
if (fmt[*ind - 1] == ' ') | ||
unknow_len += write(1, " ", 1); | ||
else if (width) | ||
{ | ||
--(*ind); | ||
while (fmt[*ind] != ' ' && fmt[*ind] != '%') | ||
--(*ind); | ||
if (fmt[*ind] == ' ') | ||
--(*ind); | ||
return (1); | ||
} | ||
unknow_len += write(1, &fmt[*ind], 1); | ||
return (unknow_len); | ||
} | ||
return (printed_chars); | ||
} |