main.c 3.94 KB
#include "dico.h"






FILE* getFile(string mode){
  if(strcmp(mode,"r"))//mode!"=r", gestion de l'affichage suivant si mode= "r" ou "w"
    printf("Tapez un nom du fichier avec son extension pour diriger la sortie des tests vers ce fichier.\n");
  else printf("Tapez \"terminal\" pour entrer des mots depuis le terminal.\nTapez le nom du fichier avec son extension pour charger des mots depuis ce fichier\n");
  FILE*file=NULL;
  char fileName[30]={0};

  //get the fileName
  scanf("%30s",fileName);
  if(!strcmp(fileName,"terminal")){
    if(!strcmp(mode,"r"))
      file=stdin;}
  else file=fopen(fileName,mode);

  //while there is a problem about it
  while(file==NULL){
    printf("desole, impossible d'ouvrir ce fichier.\nVeuillez essayer un autre fichier");
    scanf("%30s",fileName);
    if(!strcmp(fileName,"terminal")){
      if(!strcmp(mode,"r"))
	file=stdin;}
    else file=fopen(fileName,mode);
  }
  
  return file;
}



int main(void){
  int choice=0;
  printf("Bienvenue dans le dictonnaire !\n\n");
  dico monDico;
  make_empty_dico(monDico);
  
  //initialisation
  printf("Pour commencer, veuillez charger un dictionnaire.\n"); 
  FILE*fileIn=getFile("r"),*fileOut=stdout;
  if(fileIn==stdin)
    printf("Entrez vos mots (ctrl-D pour terminer le chargement):\n");
  loadfrom_file(monDico,fileIn);
  if(fileIn!=stdin)
    fclose(fileIn);


  
  //boucle
  while(1){
    
    clear();
    //menu
    printf("La sortie est branchee sur %s.\n",fileOut==stdout?"le terminal":"un fichier");
    printf("Que voulez-vous faire ?\n");
    
    //end programme
    printf("0) Fermer le programme.\n\n");
    
    //just about the dictionnary
    printf("1) Lire le dictionnaire.\n");
    printf("2) Rajouter des mots au dictionnaire\n");
    printf("3) Effacer le dictionnaire\n");
    printf("4) Remplacer le dictionnaire actuel par un autre dictionnaire\n\n");
    
    //word correction
    printf("5) Tester si un mot, un ensemble de mot, une phrase ou un paragraphe appartient au dictionnaire.\n\n");
    
    //out stream
    printf("6) Définir un fichier de sortie.\n");
    printf("7) Définir la sortie sur le terminal.\n");



    
    //saisie du choix
    choice=getchar();
    choice=ctoi(choice);



    
    switch(choice){
      
    case 0: {//sortie du programme
      if(fileOut!=stdout)
	fclose(fileOut);
      fileOut=stdout;
      delete_dico(monDico);
      printf("Le dictionnaire vous dit aurevoir.\n\n\n");
      return EXIT_SUCCESS;
    }

      
    case 1: {//Lire le dictionnaire
      printto_file(monDico,fileOut);
      printf("dictionnaire imprimme\n");
      break;}

      
      //manip sur le dictionnaire
    case 2: {//ajout
      fileIn=getFile("r");
      if(fileIn==stdin)
	printf("Entrez vos mots (ctrl-D pour terminer le chargement):\n");
      loadfrom_file(monDico,fileIn);
      if(fileIn!=stdin)
	fclose(fileIn);
      fileIn=stdin;
      break;}
    case 3: {//effacer
      delete_dico(monDico);
      printf("Le dictionnaire est maintenant vide.\n");
      break;}
    case 4: {//remplacer
      delete_dico(monDico);
      printf("Le dictionnaire est maintenant vide.\n");
      fileIn=getFile("r");
      loadfrom_file(monDico,fileIn);
      if(fileIn!=stdin)
	fclose(fileIn);
      fileIn=stdin;
      break;}

      
      
    case 5: {//faire des tests
      fileIn=getFile("r");
      if(fileIn==stdin)
	printf("Entrez vos mots (ctrl-D pour terminer les tests):\n");
      test_words(monDico,fileIn,fileOut);
      if(fileIn!=stdin)
	fclose(fileIn);
      fileIn=stdin;
      break;}


      
      //définir la sortie des tests et de "Lire" le dictionnaire
    case 6: {
      if(fileOut!=stdout)
	fclose(fileOut);
      fileOut=getFile("a");
      break;}
    case 7: {
      if(fileOut!=stdout)
	fclose(fileOut);
      fileOut=stdout;
      break;}


      
      //si le choix n'est pas parmi les précédents
    default: {break;}
    }

    
    //protection, si le scanf pour le choix a un problème
    choice=-1;
    pause();
  }//end of while
}