-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy path1.4_parsing.c
executable file
·87 lines (77 loc) · 2.47 KB
/
1.4_parsing.c
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
/**********************************************************************
** 1.4 : Parsing flag using bitwise operations whatever the project is
**********************************************************************/
#include "bitwise.h"
/*
** First we build the ft_strchr_index function which is similar to libft strchr
** but instead returns the character index in a string
*/
static int ft_strchr_index(char *s, int c)
{
int i;
i = 0;
while (s[i])
{
if (s[i] == c)
return (i);
++i;
}
return (-1);
}
/*
** Then we create the function parse_options which receive av[1]
** and our variable pointer address.
**
** a) Here we light the nth bit according to the parsed flag
** Firstly define flags as power of 2 in the header.h :
** # define FLAG_A 1 # define FLAG_B 2
** # define FLAG_C 4 # define FLAG_D 8
** etc. if more flags necessary.
**
** b) Then we send input from user after we find a '-' character
** hence the while (*(++s)) in order to skip it.
** if the string does not contain abcd it will return the USAGE
**
** c) Each time the function str_chr_index, we will light the bit corresponding.
** for example if we use /a.out -c we will get n equal to 2 (a starting at 0).
*/
int parse_options(char *s, int *flags)
{
int n;
while (*(++s)) // b)
{
if ((n = ft_strchr_index("abcd", *s)) == -1)
error(s, USAGE); // b)
*flags |= (1 << n); // c)
}
return (1);
}
/*
** Lastly a small example on how to use it.
**
** a) We check that exactly one parameter was sent starting with '-'.
**
** b) Remember that FLAG_C is defined as 4 in the header.h file
** if (flag & 4) also means (flag % 5)
**
** c) If flag A was ON it will toggle flag B OFF by clearing bit FLAG_B (2).
*/
int main(int ac, char **av)
{
int flags;
if (ac == 2 && *av[1] == '-')
parse_options(av[1], &flags); // a)
printf("value : %d\n", flags); // our variable raw value
printf("binary value : %bd\n", flags); // displayed as a binary number
if (flags & FLAG_C) // b) ,
printf("c flag ON");
if (flags & FLAG_B && flags & FLAG_D) // replace 2 by FLAG_B and 8 by FLAG_D
printf("Both flags b and d are together ON");
if (flags & FLAG_A && flag & FLAG_B) // replace 1 by FLAG_A and 2 by FLAG_B
{
*flags &= ~FLAG_B; // c)
printf("As flag A was ON, flag B is switched OFF");
printf("new binary value : %bd\n", flags);
}
return (0);
}