Main Page | File List

main.c

00001 #include <stdio.h>
00002 #include <unistd.h>
00003 #include <dirent.h>
00004 #include <stdlib.h>
00005 #include <ctype.h>
00006 #include <string.h>
00007 #include "main.h"
00008 #include "lex.yy.h"
00009 #include "store.h"
00010 #include "score.h"
00011 
00017 int main (int argc, char **argv)
00018 {
00019   int qflag = 0;
00020   int lflag = 0;
00021   int sflag = 0;
00022   int pflag = 0;
00023   int nflag = 0;
00024   int iflag = 0;
00025   int fflag = 0;
00026   int dflag = 0;
00027   int bflag = 0;
00028   int vflag = 0;
00029 
00030   char *fvalue = NULL,*dvalue = NULL, *bvalue = NULL;
00031   int c;
00032 
00033   opterr = 0;
00034 
00035   while ((c = getopt (argc, argv, "qhlsnpivef:d:b:")) != -1)
00036     switch (c)
00037       {
00038       case 'h':
00039         display_help();
00040         exit(0);
00041         break;
00042       case 'q':
00043         qflag = 1;
00044         break;
00045       case 'l':
00046         /* -l: option for learning database */
00047         lflag = 1;
00048         break;
00049       case 's':
00050         /* -s option for calculating the score of a page */
00051         sflag = 1;
00052         break;
00053       case 'n':
00054         /* -n option for non porn page(s) */
00055         nflag = 1;
00056         break;
00057       case 'p':
00058         /* -p option for porn page(s) */
00059         pflag = 1;
00060         break;
00061       case 'i':
00062         /* -i option to parse the standard input */
00063         iflag = 1;
00064         break;
00065       case 'v':
00066         /* -v option for simple verbose mode (for scoring) */
00067         vflag = 1;
00068         break;
00069       case 'e':
00070         /* -e option for complex verbose mode (for scoring) */
00071         vflag = 2;
00072         break;
00073       case 'f':
00074         /* -f option to parse one page */
00075         fflag = 1;
00076         fvalue = optarg;
00077         break;
00078       case 'd':
00079         /* -d option to parse a directory */
00080         dflag = 1;
00081         dvalue = optarg;
00082         break;
00083       case 'b':
00084         /* -b option to indicate database name and location */
00085         bflag = 1;
00086         bvalue = optarg;
00087         break;
00088       case '?':
00089         if (isprint (optopt))
00090           fprintf (stderr, "Unknown option `-%c'.\n", optopt);
00091         else
00092           fprintf (stderr,
00093                    "Unknown option character `\\x%x'.\n",
00094                    optopt);
00095         return 1;
00096       default:
00097         abort ();
00098       }
00099 
00100   /* Catching errors */
00101   if(sflag==0 && lflag==0 && qflag==0){
00102     fprintf(stderr, "you have to choose between -s -l or -q options");
00103     exit(0);
00104   }
00105   if(sflag==1 && lflag==1 && qflag==1){
00106     fprintf(stderr, "you can't choose both options -s -l and -q");
00107     exit(0);
00108   }
00109   if(fflag==1 && dflag==1 &&iflag==1){
00110     fprintf(stderr, "you can't choose both options -f -d and -i");
00111     exit(0);
00112   }
00113   if(nflag==1 && pflag==1 && lflag==1){
00114     fprintf(stderr, "you can't choose both options -n and -p for learning");
00115     exit(0);
00116   }
00117   if((nflag == 1 || pflag ==1) && lflag ==0){
00118     fprintf(stderr, "you don't to choose options -n or -p for scoring");
00119     exit(0);
00120   }
00121   if(fflag==0 && dflag==0 && iflag==0 && qflag==0){
00122     fprintf(stderr, "you have to choose between option -f, -d, - or q\n");
00123     exit(0);
00124   }
00125   if(bflag == 0){
00126     fprintf(stderr, "you have to indicate the database name with -b option\n");
00127     exit(0);
00128   }
00129 
00130   /* Dealing with options */
00131   if(qflag==1) {
00132     store_opendb(bvalue);
00133     store_displayTokens();
00134     store_closedb();
00135   }
00136   if(fflag==1) parse_file(fvalue, pflag, sflag, bvalue, vflag);
00137   if(dflag==1) parse_dir(dvalue, pflag, sflag, bvalue, vflag);
00138   if(iflag==1) parse_standard_input(stdin, pflag, sflag, bvalue, vflag);
00139 
00140   return 0;
00141 }
00142 
00151 void parse_file(char *file, int isporn, int isscoring, char *dbname, int verbose)
00152 {
00153   if(!isscoring) {
00154     store_opendbs(dbname);
00155   }else{
00156     store_opendb(dbname);
00157   }
00158   FILE *fp = fopen(file,"r");
00159   init_parsing(fp, isporn, isscoring, dbname, verbose);
00160   fclose(fp);
00161   store_closedb();
00162 }
00163 
00172 void parse_dir(char *directory, int isporn, int isscoring, char *dbname, int verbose)
00173 {
00174   struct dirent **files;
00175   char *nom_fichier;
00176   nom_fichier = (char*)malloc(sizeof(char)*500);
00177   strcpy(nom_fichier, directory);
00178 
00179   int nbpages = scandir(nom_fichier,
00180                         &files,
00181                         0,
00182                         alphasort);
00183   if(!isscoring) {
00184     store_opendbs(dbname);
00185   }else{
00186     store_opendb(dbname);
00187   }
00188  
00189   nbpages--;
00190   while (nbpages != 0){
00191     if(strcmp(files[nbpages]->d_name, ".") !=0 && strcmp(files[nbpages]->d_name, "..") != 0){
00192       strcpy(nom_fichier,directory);
00193       strcat(nom_fichier, files[nbpages]->d_name);
00194       printf("%-50s.....%d\n", nom_fichier, nbpages);
00195       FILE *fp = fopen(nom_fichier,"r");
00196       init_parsing(fp, isporn, isscoring, dbname, verbose);
00197       fclose(fp);
00198     }
00199       nbpages--;
00200   }
00201   
00202   store_closedb();
00203 }
00204 
00213 void parse_standard_input(FILE *stdinput, int isporn, int isscoring, char *dbname, int verbose)
00214 {
00215   if(!isscoring) {
00216     store_opendbs(dbname);
00217   }else{
00218     store_opendb(dbname);
00219   }
00220   init_parsing(stdinput, isporn, isscoring, dbname, verbose);
00221   store_closedb();
00222 }
00223 
00227 void display_help()
00228 {
00229   printf(" choose between -s(scoring) and -l(learning) options\n");
00230   printf(" choose between -f[file] or -d[directory] or -i options (file, directory or standard input\n");
00231   printf(" with -b option indicate the database location and name\n");
00232   printf(" -v will display some information for scoring \n");
00233   printf(" -e will display more information for scoring \n");
00234   printf(" -q will display all the tokens stored in the database");
00235   printf(" -h will display yhis message \n");
00236   printf("\n example : ./main -s -f ./index.html -b database.db -v\n");
00237   printf(" will launch scoring for \"index.html\" using database.db and will display some statistics\n");
00238   printf("\n example : ./main -l -n -f ./index.html -b database.db");
00239   printf(" will tokenize and store the tokens from \"index.html\" in database.db wich will be created if doesn't exist\n");
00240 }
00241 

Generated on Tue May 31 14:22:44 2005 for filterFlex by  doxygen 1.3.9.1