-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_printf.c
More file actions
82 lines (74 loc) · 1.58 KB
/
_printf.c
File metadata and controls
82 lines (74 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "holberton.h"
/**
* create_ops - creates dynamically allocated array of op_code structs
* @ops: pointer to op codes
* Return: pointer to array of opcodes, now populated
**/
op_t *create_ops(op_t *ops)
{
int i = 0, len = 0;
op_t pre_ops[] = {
{"c", op_char},
{"i", op_int},
{"d", op_int},
{"%", op_percent},
/* {"f", op_float}, */
{"s", op_string},
{NULL, NULL}
};
while (pre_ops[len].op)
len++;
ops = malloc(sizeof(op_t) * (len + 1));
while (pre_ops[i].op)
{
ops[i].op = _strdup(pre_ops[i].op);
ops[i].f = pre_ops[i].f;
i++;
}
ops[i].op = NULL;
ops[i].f = NULL;
return (ops);
}
/**
* free_ops - free the array of op structs
* @ops: null terminated array of ops
* Return: int, success code
**/
int free_ops(op_t *ops)
{
UNUSED(ops);
return (0);
}
/**
* _printf - simple function duplicates printf functionality
* @format: format string just like the standard printf
* ... followed by variable number of parameters ... just like printf!
* Return: int representing number of printed characters
**/
int _printf(const char *format, ...)
{
params_t p;
va_list valist;
op_t *ops;
ops = create_ops(ops);
va_start(valist, format);
init_params(&p, format, &ops);
while (p.format && p.format[p.dex])
{
if (p.format[p.dex] == '%' && p.format[p.dex + 1])
{
p.dex++;
while (p.format[p.dex] == ' ' && p.format[p.dex + 1])
p.dex++;
p.counter += choose_op(&p, valist);
}
else
{
_putchar(p.format[p.dex]);
p.counter++;
p.dex++;
}
}
va_end(valist);
return (p.counter); /* should return number of printed chars */
}