Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Création d'un menu pour la partie 2 #31

Merged
merged 6 commits into from
Dec 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/dictionary.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,36 @@ void displayDictionaries(char **dictionaries, size_t count) {
}
}

/**
* \brief Help the user to select a Dictionary
* \return Char* the name of a dictionary without extension
*/
char *menuSelectDictionary(void) {
size_t count;
char **dicos = listDictionaries("resources/dictionaries", &count);
if(dicos == NULL) {
printf("An error has occured.\n");
return NULL;
}

printf("Select a dictionary\n");
displayDictionaries(dicos, count);
printf("\t0. CANCEL - Back to menu\n");
int choice;
do {
printf("Your choice: ");
} while(!getIntRange(&choice, 0, count));
if(choice == 0) {
freeBiChar(dicos, count);
return NULL;
}

char *value = malloc(sizeof(char) * (strlen(dicos[choice - 1]) + 1));
strcpy(value, dicos[choice - 1]);
freeBiChar(dicos, count);
return value;
}

/**
* \fn long positionForWord(Dictionary *dic, char *word)
* \param dic The dictionary
Expand Down
1 change: 1 addition & 0 deletions src/dictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ bool createDictionary(const char *filename);
FILE* openDictionaryFile(const char *filename, const char *rights);
void freeDictionary(Dictionary *dico);
Dictionary* selectDictionary(const char *filename);
char *menuSelectDictionary(void);
bool checkDictionaryPath();
char **listDictionaries(char *dirname, size_t *count);
ssize_t countDictionaries(char *dirname);
Expand Down
31 changes: 0 additions & 31 deletions src/gestbib.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,37 +74,6 @@ void menuOpenDictionary(void) {
}


/**
* \fn char *menuSelectDictionary(void)
* \brief Help the user to select a Dictionary
* \return Char* the name of a dictionary without extension
*/
char *menuSelectDictionary(void) {
size_t count;
char **dicos = listDictionaries("resources/dictionaries", &count);
if(dicos == NULL) {
printf("An error has occured.\n");
return NULL;
}

printf("Select a dictionary\n");
displayDictionaries(dicos, count);
printf("\t0. CANCEL - Back to menu\n");
int choice;
do {
printf("Your choice: ");
} while(!getIntRange(&choice, 0, count));
if(choice == 0) {
freeBiChar(dicos, count);
return NULL;
}

char *value = malloc(sizeof(char) * (strlen(dicos[choice - 1]) + 1));
strcpy(value, dicos[choice - 1]);
freeBiChar(dicos, count);
return value;
}

/**
* \fn void mainMenu()
* \brief Function for guide user into the second menu
Expand Down
1 change: 0 additions & 1 deletion src/gestbib.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ void menu(Dictionary *dico);

void menuCreateDictionary(void);
void menuOpenDictionary(void);
char *menuSelectDictionary(void);
void menuAddDictionaryWord(Dictionary *dico);
void menuCreateDictionaryFromFile(void);
void menuSearchWord(Dictionary *dico);
Expand Down
84 changes: 84 additions & 0 deletions src/gestrech.c
Original file line number Diff line number Diff line change
@@ -1 +1,85 @@
#include "gestrech.h"
#include <string.h>

/**
* \brief Function for guide user into the main menu
*/
void main2Menu(void) {
int choice;
do {
printf("\n\n*** Dictionaries management ***\n\n"
"\t1. Open an existing dictionary\n"
"\t0. Quit\n\n");
do {
printf("Your choice: ");
} while(!getIntRange(&choice, 0, 1));
switch (choice) {
case 1:
menu2OpenDictionary();
break;
case 0:
printf("Good bye!\n");
break;
}
} while(choice != 0);
}
/**
* \brief Help the user to open a Dictionary
*/
void menu2OpenDictionary(void) {
char *dico = menuSelectDictionary();
if(dico != NULL) {
menu2(selectDictionary(dico));
free(dico);
dico = NULL;
}
}

/**
* \brief Function for guide user into the second menu
*/
void menu2(const Dictionary *dico) {
int choice;
do {
printf("\n\n--- Dictionary ---\n\n"
"\t2. Search a similar word\n"
"\t0. Return to Dictionaries management\n\n");
do {
printf("Your choice: ");
} while(!getIntRange(&choice, 0, 1));
switch (choice) {
case 1:
menuSearchSimilarWord(dico);
break;
case 0:
freeDictionary(dico);
clear();
break;
}
} while(choice != 0);
}

/**
* \brief Menu for search similar words in dictionary
* \param dico : Dictionary for search
*
*/
void menuSearchSimilarWord(const Dictionary *dico) {
char word[255] = {'\0'};
printf("Enter a word : ");
if(getString(255, word)) {
if(strlen(word) > 0) {
char **simWords;
const unsigned int nb /*= searchSimilarWords(dico, word, simWords)*/;
if(nb > 0) {
unsigned int i;
printf("Similar words :\n");
for(i=0 ; i < nb ; i++)
printf("\t> %s\n", simWords[i]);
} else
printf("No similar word to %s was found ...\n");
} else
printf("Your word is void ...\n");
} else
fprintf(stderr, "Error while getting user input.\n");
}
5 changes: 5 additions & 0 deletions src/gestrech.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@

#include "dictionary.h"

void main2Menu(void);
void menu2OpenDictionary(void);
void menu2(const Dictionary *dico);
void menuSearchSimilarWord(const Dictionary *dico);

#endif //__GESTRECH_H__
6 changes: 4 additions & 2 deletions src/main2.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <stdio.h>
#include "gestrech.h"

int main(void) {
printf("Hello Word\n");
return 0;
clear();
main2Menu();
return EXIT_SUCCESS;
}