Bonjour

J'ai besoin de vos conseils,

Certains le savent deja, j'ai eu pour projet de re-coder(dur torture) un grep en c, avec un maximum d'option.
Bon grace à vos conseils j'ai pu develloper un parseur de commade en utilisant mon propre my_get_opt();
maintenant que tout sa marche ... il faut bien que j'appel les fonctions correnspondant au options .... le probleme, c'est que la je vois pas du tout comment je dois faire ... surtout que je me dit qu 'il ya forcement des options qui seront imbrique dans dautre ....

je vous met tout mon code(oui il doit manque quelque close file ... im aware about that)

main.c :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
 
#include"my_grep.h"
 
int main(int argc,char** argv)
{
 
	t_parametre parametre; // cette structure est le resultat du parseur.
 
	parseur_parametre(argc,argv,&parametre);
	load_file(&parametre);
 
	return 0;
}
grep.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define OptMax 4
 
#define TRUE 1
#define FALSE 0
typedef int BOOL;
typedef struct s_parametre
{
	char **str_cmd;		// save argv
	BOOL option[OptMax]; // save the different option extracted of argv
}t_parametre;
 
 
 
void parseur_parametre(int c_argc, char ** c_argv,t_parametre *c_parametre);
int My_Error();
int load_file(t_parametre *c_parametre);
int read_file(t_parametre *c_parametre,FILE*);
int get_opt(char *str_opt,t_parametre *c_parametre);
int grep_standard(char *line,int taille,t_parametre *c_parametre);
int grep_whith_upper_case(char *line,int taille,t_parametre *c_parametre);// 
                                   //cette fonction sera p-e à changer ou à  
                                  //disparaitre
grep.c
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
 
 
 
#include "my_grep.h"
 
int My_Error()
{
	printf("Error: veuillez verifier que vous utiliser grep avec cette syntaxe :\ngrep [OPTION]... PATTERN [FILE]\n");
	//exit(1);	
	return 1;
}
int load_file(t_parametre *c_parametre)
{
	int i = 0;
	FILE* fichier = NULL;
	BOOL not_found = FALSE;
	// the name file(pattern) cannot to be in argv[0] 
	//c_parametre->str_cmd[2] = "C:\\Dev\\a_geter\\grep\\Debug\\toto.txt";
	for(i=1;c_parametre->str_cmd[i] != "\0";i+=1)
	{
 
		fichier = fopen(c_parametre->str_cmd[i],"r");
		if(!fichier)//if don't open or not found the pattern
		{
			//pattern don't match
			continue;
			//not_found = FALSE;
		}
		else
		{
			// partern founded
			read_file(c_parametre,fichier);
			not_found = TRUE;
		}
 
	}
	if(not_found == FALSE)
	{
		My_Error();
		exit(1);
	}
	fclose(fichier);
 
	return(0);
}
 
int read_file(t_parametre *c_parametre,FILE *file)
{
	char temp[1000];
	while(fgets(temp,1000,file)!= NULL)
	{
		if(c_parametre->option[3] == TRUE) // exemple d'appel d'option
		{
			grep_standard(temp,strlen(temp),c_parametre) ;
		}
	}
}
 
void parseur_parametre(int c_argc,char ** c_argv,t_parametre *c_parametre)
{
	int i= 0;
	int it_str_cmd = 0;
	c_parametre->str_cmd = (char**)malloc((c_argc)*sizeof(char*));
 
	//init********************************************************
	for(i;i<=c_argc;i+=1)
	{
		c_parametre->str_cmd[i] = "\0";
	}
	for(i = 0;i<OptMax;i+=1)
	{
		c_parametre->option[i]=FALSE;
	}
	//***********************************************************
 
	if(c_argc<2)
	{
		My_Error();//print Error on screen
		exit(1);
	}
	for(i=0;i<c_argc;i+=1)
	{
		if(c_argv[i][0] == '-') // check if there are option(s) ('-' is only on the first line of c_argv)
		{
			//get_opt() permet de determiner le nbre d'option et quel option appliquee
			get_opt(&c_argv[i][1],c_parametre); //take the next caracteres after'-'
		}
		else
		{
			c_parametre->str_cmd[it_str_cmd] = c_argv[i];
			it_str_cmd+=1;
		}
	}
	if(it_str_cmd == 1)
	{
		My_Error();
		printf("Aide: verifier la syntaxe apres [OPTION]\n");
		exit(1);
	}
 
 
}
 
int get_opt(char *str_opt,t_parametre *c_parametre)
{
	char option[]={'E','F','G','i',0}; // liste d'option diponible du grep
	int i = 0,j = 0;
	BOOL option_non_repertorie = FALSE;
 
	for(i;str_opt[i];i+=1)
	{
		for(j;option[j];j+=1)
		{
			if(str_opt[i]==option[j])
			{
				c_parametre->option[j] = TRUE;
				option_non_repertorie = TRUE;
			}
		}
		if(option_non_repertorie == FALSE )
		{
			My_Error();
			printf("Aide: verifier la syntaxe apres [OPTION] ou option non repertorié\n");
			//exit(1);
		}
	}
	return 0;
}
 
int grep_standard(char *line,const int taille,t_parametre *c_parametre)
{
 
	int i = 0,j = 0;
 
	for(i = 0;line[i];i++)
	{
		if(line[i] == c_parametre->str_cmd[1][0])
		{
			for(j = 0; c_parametre->str_cmd[1][j];j++)
			{
				if(line[i+j] == c_parametre->str_cmd[1][j]&&c_parametre->str_cmd[1][j])
				{
					if(c_parametre->str_cmd[1][strlen(c_parametre->str_cmd[1])-1] ==line[i+j])
					{
						printf("%s\n",line);
						break;
					}
 
				}
			}
		}
	}
}
 
int grep_whith_upper_case(char *line,const int taille,t_parametre *c_parametre)
{
 
	int i = 0,j = 0;
	for(i;line[i];i+=1)
	{
		if(line[i] >= 'a' && line [i]<='z')
		{
			line[i] -=32;
		}
		if(c_parametre->str_cmd[1][i]>= 'a' &&c_parametre->str_cmd[1][i]<='z' )
		{
			c_parametre->str_cmd[1][i] -=32;
		}
	}
 
	for(i = 0;line[i];i++)
	{
		if(line[i] == c_parametre->str_cmd[1][0])
		{
			for(j = 0; c_parametre->str_cmd[1][j];j++)
			{
				if(line[i+j] == c_parametre->str_cmd[1][j]&&c_parametre->str_cmd[1][j])
				{
					if(c_parametre->str_cmd[1][strlen(c_parametre->str_cmd[1])-1] ==line[i+j])
					{
						printf("%s\n",line);
						break;
					}
 
				}
			}
		}
	}
}
c'est dans
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
int read_file(t_parametre *c_parametre,FILE *file)
{
	char temp[1000];
	while(fgets(temp,1000,file)!= NULL)
	{
		if(c_parametre->option[3] == TRUE) // exemple d'appel d'option
		{
			grep_standard(temp,strlen(temp),c_parametre) ;
		}
	}
}
que j'essaie d'appeler les fonctions ... comment les oprganiser proprements ....