-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhandle_percent.c
More file actions
145 lines (139 loc) · 5.21 KB
/
handle_percent.c
File metadata and controls
145 lines (139 loc) · 5.21 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* handle_percent.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hiroshiusui <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/01/31 03:00:28 by hiroshius #+# #+# */
/* Updated: 2018/01/31 03:03:06 by hiroshius ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
#define INP in_precision(*format_pointer)
/*
** Source: https://www-user.tu-chemnitz.de/~heha/petzold/ch02c.htm
** A note on capital letter (wide) conversion types: "Nothing about Unicode
** or wide characters alters the meaning of the char data type in C. The char
** continues to indicate 1 byte of storage, and sizeof(char) continues to
** return 1. In theory, a byte in C can be greater than 8 bits, but for most
** of us, a byte (and hence a char) is 8 bits wide.
**
** Wide characters in C are based on the "wchar_t" data type, which is defined
** in several headers files, including "<wchar.h>", like so:
** "typedef unsigned short wchar_t;"
**
** Thus, the "wchar_t" data type is the same as an unsigned short integer:
** 16 bits wide.
**
** Source: The C Programming Language, 2nd Edition (Dennis Ritchie), page 137
** &&
** Source: C Primer Plus 6th Edition, page 113
**
** *******************************Mandatory Part*******************************
** Types of conversions:
**
** s S p d D i o O u U x X c C
** s: Print characters from the string until a '\0' or the number of
** characters given by the precision
**
** S: Equivalent of "%ls"
**
** p: Pointer
**
** d: Signed decimal integer
**
** D: Equivalent of "%ld"
**
** i: Equivalent of 'd'
**
** o: Unsigned octal number (without a leading zero)
**
** u: Unsigned decimal number
**
** U: Equivalent of "%lu"
**
** x: Unsigned hexadecimal number (without a leading 0x), using abcdef
** for 10, ..., 15.
**
** X: Unsigned hexadecimal number (without a leading 0X), using ABCDEF
** for 10, ..., 15.
**
** c: Single character
**
** C: Equivalent of "%lc"
**
** Length modifiers:
** Modifier d, i o, u, x, X n
** hh signed char unsigned char signed char *
** h short unsigned short short *
** l (ell) long unsigned long long *
** ll (ell ell) long long unsigned long long long long *
** j intmax_t uintmax_t intmax_t *
** z (see note) size_t (see note)
**
** Flags:
** #: A "#" character sepcifies that the value should be printed in
** "alternate" form.
** This option has no effect on "b, c, d, s," and "u" formats.
** For the o formats, the precision of the number increases to make the first
** character of the outputted string to be zero.
** For the x/X format, a non-zero string has the string 0x/0X prepended to it.
** For "a, A, e, E, f, F, g, and G formats", the result always has a decimal.
** For "g and G" formats, trailing zeros are not removed from the result as
** they usually would be.
** 0: A "0" character indicates that zero-padding should be used rather than
** blank padding. A "-" overrides a "0" if both are used. If a numeric
** conversion is given (d, i, o, u, i, x, and X), then the '0' flag is ignored.
** -: A "-" sign specifies left adjusment of the output.
** +: A "+" character specifies that there should always be a sign placed
** before the number when using signed formats.
** _: A space specifies that a blank should be left before before a positive
** number for a signed format. A "+" overrides a space if both are used.
*/
static void parse_length_modifier(t_bag *bag, char **format,
char **length_modifiers)
{
char *format_pointer;
char *copy;
int i;
format_pointer = *format;
copy = ft_strdup(format_pointer);
i = 0;
while (copy[i] && !in_format_conversions(copy[i]))
i++;
copy[i] = '\0';
if (in_length_modifiers(length_modifiers, copy))
{
bag->length_modifier = ft_strdup(copy);
free(copy);
*format = &(*format)[i];
}
}
void handle_percent(char **format, va_list args, int *i)
{
char **length_modifiers;
char *format_pointer;
t_bag *bag;
format_pointer = ++(*format);
length_modifiers = set_length_modifiers_array();
bag = malloc(sizeof(t_bag));
initialize_t_bag_variables(bag);
while (in_flags(bag, *format_pointer) && set_flags(bag, *format_pointer))
format_pointer++;
if (in_minimum_field_width(*format_pointer))
bag->width = ft_atoi(format_pointer);
while (in_minimum_field_width(*format_pointer))
format_pointer++;
if (*format_pointer == '.' && (bag->period = 1) && format_pointer++ && INP)
{
bag->precision = ft_atoi(format_pointer);
while (in_precision(*format_pointer))
format_pointer++;
}
parse_length_modifier(bag, &format_pointer, length_modifiers);
if (in_format_conversions(*format_pointer))
bag->format_conversion = *format_pointer++;
print_format_conversion(bag, args, i);
*format = format_pointer;
}