-
Notifications
You must be signed in to change notification settings - Fork 0
/
xergStrike.c
172 lines (124 loc) · 2.81 KB
/
xergStrike.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
#include "xergStrike.h"
#include <string.h>
static void afficherAide()
{
printf(MESSAGE_AIDE);
}
static bool usage(int argc ,char **argv)
{
if(argc!=4&&argc!=5){
afficherAide();
return false;
}
return true;
}
static bool estMinuscule(char a)
{
return (a>='a'&&a<='z')?true:false;
}
static bool estMajuscule(char a)
{
return (a>='A'&&a<='Z')?true:false;
}
static bool estLettre(char a)
{
return (estMajuscule(a)||estMinuscule(a))?true:false;
}
static void afficherMessageMasquageTexte(int nbrMasque)
{
printf("\n\n______________________\n\nMasquage de texte *%d\n______________________\n\n",nbrMasque>26?26:nbrMasque);
}
static void afficherResultatMasquageMultiple(char *masque,char *original,char *resultat,int rang){
printf("\nmasque : %20s \ntexte : %21s \nresultat : %18s (rang %d)\n_____________\n",masque,original,resultat,rang);
}
int main(int argc,char **argv)
{
if(!usage(argc,argv))
return EXIT_FAILURE;
launcher(argc,argv);
return EXIT_SUCCESS;
}
int alphabetToInt(char a)
{
int i;
for(i=0;i<26&&(i+'a')!=a;i++);
return i;
}
char integerToAlpha(int a)
{
assert(a>=0);
while(a>=26)
a-=26;
return 'a'+a;
}
void masquage(char *masque,char *texte,char *final)
{
for(int i=0,y=0;i<strlen(texte)+1;i++,y=y%strlen(masque)){
if(estLettre(texte[i]))
final[i]=integerToAlpha(alphabetToInt(masque[y++])+alphabetToInt(texte[i])+1);
else
final[i]=texte[i];
}
}
static char* copierChaine(char *a)
{
char *retour=malloc(sizeof(char)*strlen(a)+1);
char *b=retour;
for(;*a;a++,b++)
*b=*a;
*b=*a;
return retour;
}
void tabToTab(char *a,char *b)
{
for(;*b;b++,a++)
*a=*b;
*a=*b;
}
void affichageMasquageMultiple(int argc,char **argv)
{
int nbrMasque=atoi(argv[4]);
if(nbrMasque<1)
{
printf(ERREUR_NB_MAX_MASQUE);
assert(0);
}
int tailleFinal=strlen(argv[3])+1;
char *final=copierChaine(argv[3]);
char save[tailleFinal];
char *masque=argv[2];
afficherMessageMasquageTexte(nbrMasque);
for(int i=0;i<nbrMasque&&i<26;i++)
{
tabToTab(save,final);
masquage(masque,final,final);
afficherResultatMasquageMultiple(masque,save,final,i+1);
}
if(nbrMasque>26)
printf(MESSAGE_NB_MAX_MASQUAGE);
free(final);
}
void launcher (int argc,char **argv)
{
if(strcmp(argv[1],"help")==0)
return afficherAide();
else if(strcmp(argv[1],"masque")==0)
{
if(argc==4)
{
char final[strlen(argv[3])+1];
masquage(argv[2],argv[3],final);
printf("\n\n%s\n\n",final);
}
else if(argc==5)
{
affichageMasquageMultiple(argc,argv);
}
else
printf("Erreur , arguments invalide \nTaper help pour obtenir de l'aide sur les commande\n\n");
}
}