-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflags.c
More file actions
37 lines (35 loc) · 1006 Bytes
/
flags.c
File metadata and controls
37 lines (35 loc) · 1006 Bytes
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
#include "main.h"
/**
* get_flags - Calculates active flags
* @format: Formatted string in which to print the arguments
* @i: take a parameter.
* Return: Flags:
*/
int get_flags(const char *format, int *i)
{
/* - + 0 # ' ' */
/* 1 2 4 8 16 */
int j, curr_i;
int flags = 0;
const char FLAGS_CH[] = {'-', '+', '0', '#', ' ', '\0'};
const int FLAGS_ARR[] = {F_MINUS, F_PLUS, F_ZERO, F_HASH, F_SPACE, 0};
/* Loop through the format string, starting from *i+1 */
for (curr_i = *i + 1; format[curr_i] != '\0'; curr_i++)
{
/* Loop through the flag characters */
for (j = 0; FLAGS_CH[j] != '\0'; j++)
{
/* If current character match flag character, set corresponding flag bit */
if (format[curr_i] == FLAGS_CH[j])
{
flags |= FLAGS_ARR[j]; /* Set flag bit using bitwise OR */
break;
}
}
/* If current character does not match any flag character, break loop */
if (FLAGS_CH[j] == 0)
break;
}
*i = curr_i - 1; /* Set the updated index value */
return (flags);
}