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
|
/******************************************************************************************************/
/* Nom Projet : my_grep */
/* Nom fichier :my_grep.c */
/* Date dernière mise à jour: 8 nov 2012 */
/* Nom : */
/* Commentaires : contient definition des fct de my_grep. */
/* Fonctionne de paire avec main.c et my_grep.h */
/******************************************************************************************************/
#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;
// the name file(pattern) cannot to be in argv[0]
for(i=1;c_parametre->str_cmd[i];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
}
else
{
// partern founded
read_file(c_parametre,*fichier);
}
fclose(fichier);
}
return(0);
}
int read_file(t_parametre c_parametre,FILE *file)
{
char temp[1000];
while(fgets(temp,1000,file))
{
}
}
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','P',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] = FALSE;
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);
}
}
} |
Partager