Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ctype.h>
- #include <dirent.h>
- #include <getopt.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <errno.h>
- #define SHOW 100
- #define INNER_FILE 200
- #define MAX_STR_LEN 400
- #define SIZE 400
- typedef struct wordlist {
- char word[MAX_STR_LEN];
- unsigned int total;
- struct filelist *headFilelist;
- struct wordlist *next;
- } wordlist;
- typedef struct filelist {
- char path[MAX_STR_LEN];
- unsigned int occorrenze;
- struct posix *headPosix;
- struct filelist *next;
- } filelist;
- typedef struct posix {
- int riga;
- int carattere;
- struct posix *next;
- } posix;
- typedef struct elem {
- char data[MAX_STR_LEN];
- struct elem *next;
- } cell;
- typedef cell *list;
- // Parametri
- typedef struct parameter_s {
- char word[MAX_STR_LEN];
- char input[MAX_STR_LEN];
- char output[MAX_STR_LEN];
- char report[MAX_STR_LEN];
- char toExclude[MAX_STR_LEN];
- char wordToShow[MAX_STR_LEN];
- char innerFile[MAX_STR_LEN];
- int wordOccurences;
- int isVerbose;
- } parameter;
- //======STRUTTURE=======
- struct filelist *append_filelist(struct filelist *list, struct filelist *node);
- int compareFilelist(struct filelist *n1, struct filelist *n2);
- void display_filelist(struct filelist *head);
- void display_filelist_file(struct filelist *head, FILE *pFile);
- posix *create(int riga, int carattere, posix *next);
- posix *append(posix *head, int riga, int carattere);
- void delete (posix *head);
- void free_list(filelist *head);
- void displayPosix(posix *head);
- void displayPosix_file(posix *head, FILE *pFile);
- struct wordlist *append_wordlist(struct wordlist *list, struct wordlist *node);
- int compareWordlist(struct wordlist *n1, struct wordlist *n2);
- void display_wordlist(struct wordlist *head);
- void display_wordlist_file(struct wordlist *head, char *output);
- void free_wordlist(wordlist *head);
- //=======KMP===========
- void computeLPSarray(char *pat, int M, int *lps);
- posix *KMPSearch(char *pat, char *txt, int righe, posix *headPosix);
- posix *searchWordInFile(char *filename, char *world);
- //=========LIST_FILE==============
- list addToList(list a, char *word);
- void printList(list a);
- void free_list_scan(list head);
- int isDirectory(char *pathname);
- list listFiles(char *basePath, int recursively, list a, const char *exclude);
- const char *get_filename_ext(const char *filename, const char *exclude);
- list take_word_from_file(char *nome_file);
- list take_path_from_file(char *path, char *exclude);
- // Genera report
- void find_word(char *file_path, char *file_word, char *exclude_extension,
- char *output);
- // Analisi report
- void find_occurences_from_file(char *, char *, char *);
- void find_paths_from_report(char *, char *, int);
- //
- //===============PROVE=================
- struct wordlist *elaborate_word(char *word, char *pathfile, char *exclude);
- struct filelist * listFiles_2(char *basePath, int recursively, filelist * headPathList, const char *exclude, char * word);
- void find_paths_from_report(char *input, char *report, int occurrences) {
- FILE *pFile;
- pFile = fopen(report, "r");
- if (pFile == NULL) {
- printf("%s\n", report);
- perror("Non si apre\n");
- }
- char linea[SIZE];
- char temp[SIZE];
- char path[SIZE];
- int occorrenze;
- char word[SIZE];
- strcpy(word, "WORD ");
- strcat(word, input);
- while (fgets(linea, SIZE, pFile) != NULL) {
- if (strstr(linea, word) != NULL) {
- printf("%s", linea);
- while ((fgets(linea, SIZE, pFile) != NULL) &&
- (sscanf(linea, "WORD %s\n", temp) == 0)) {
- if (sscanf(linea, "FILE %s\n", path) == 1) {
- fgets(linea, SIZE, pFile);
- sscanf(linea, "OCCURENCES %d\n", &occorrenze);
- if (occorrenze > occurrences) {
- printf("FILE %s\n", path);
- printf("OCCURRENCES %d\n\n", occorrenze);
- }
- }
- }
- }
- }
- fclose(pFile);
- }
- void find_occurences_from_file(char *input, char *report, char *file) {
- FILE *pFile;
- pFile = fopen(report, "r");
- if (pFile == NULL) {
- printf("Non si apre.\n");
- }
- char linea[SIZE];
- char temp[SIZE];
- char path[SIZE];
- char file2[SIZE] = "FILE ";
- strcat(file2, file);
- //strcat(file2, "\r\n");
- unsigned int posizione;
- unsigned int riga;
- char word[200];
- strcpy(word, "WORD ");
- strcat(word, input);
- strcat(file2, "\r\n");
- while (fgets(linea, SIZE, pFile) != NULL) {
- if (strstr(linea, word) != NULL) {
- printf("%s", linea);
- while ((fgets(linea, SIZE, pFile) != NULL) &&
- (sscanf(linea, "WORD %s\n", temp) == 0)) {
- // se le stringhe sono uguali
- if (strcmp(file2, linea) == 0) {
- fgets(linea, SIZE, pFile);
- // sscanf restituisce numero variabili che riempie
- while ((fgets(linea, SIZE, pFile) != NULL) &&
- (sscanf(linea, "%i %i", &posizione, &riga) == 2)) {
- printf("%i %i\n", posizione, riga);
- }
- }
- }
- }
- }
- fclose(pFile);
- }
- void computeLPSarray(char *pat, int M, int *lps) {
- int len = 0; // lenght of previous longest prefix suffix
- int i;
- lps[0] = 0; // lps[0] is always 0
- i = 1;
- while (i < M) // loop calculates lps[i] for i = 1 to M-1
- {
- if (pat[i] == pat[len]) {
- len++;
- lps[i] = len;
- i++;
- } else // pat[i] != pat[len]
- {
- if (len != 0) {
- // this is tricky
- // consider the example AAACAAAA and i =7
- len = lps[len - 1];
- // we donot increment i here
- } else // if len == 0
- {
- lps[i] = 0;
- i++;
- }
- }
- }
- }
- posix *KMPSearch(char *pat, char *txt, int righe, posix *headPosix) {
- int M = strlen(pat);
- int N = strlen(txt);
- int *lps = (int *)malloc(M * sizeof(int));
- int j = 0;
- computeLPSarray(pat, M, lps);
- int i = 0;
- while (i < N) {
- if (pat[j] == txt[i]) {
- j++;
- i++;
- }
- if (j == M) { // Qui andrò ad inserire riga e posizione nella struttura
- // posix e la concatenerò alla lista
- // printf("trovato in riga%d, posizione %d\n", righe, i - j);
- headPosix = append(headPosix, righe, i - j);
- j = lps[j - 1];
- } else if (pat[j] != txt[i]) {
- if (j != 0)
- j = lps[j - 1];
- else
- i++;
- }
- }
- free(lps);
- return headPosix;
- }
- posix *searchWordInFile(char *filename, char *world) {
- FILE *fp;
- char *line = NULL;
- size_t len = 0;
- ssize_t read;
- posix *headPosix = NULL;
- fp = fopen(filename, "r");
- if (fp == NULL)
- exit(EXIT_FAILURE);
- int righe = 1;
- while ((read = getline(&line, &len, fp)) != -1) {
- headPosix = KMPSearch(world, line, righe, headPosix);
- righe++;
- }
- fclose(fp);
- if (line) {
- free(line);
- }
- return headPosix;
- }
- //=====================================STRUCT===================================
- struct filelist *append_filelist(struct filelist *list, struct filelist *node) {
- struct filelist *prev, *next;
- if (!list) {
- list = node;
- } else {
- prev = NULL;
- next = list;
- while (next && compareFilelist(node, next) > 0) {
- prev = next;
- next = next->next;
- }
- if (!next) {
- prev->next = node;
- } else {
- if (prev) {
- node->next = prev->next;
- prev->next = node;
- } else {
- node->next = list;
- list = node;
- }
- }
- }
- return list;
- }
- int compareFilelist(struct filelist *n1, struct filelist *n2) {
- if ((n1->occorrenze < n2->occorrenze)) {
- return 1;
- };
- if ((n1->occorrenze == n2->occorrenze) && (strcmp(n1->path, n2->path) > 0)) {
- return 1;
- };
- return -1;
- }
- int compareWordlist(struct wordlist *n1, struct wordlist *n2) {
- if ((strcmp(n1->word, n2->word) > 0)) {
- return 1;
- };
- return -1;
- }
- void display_filelist(struct filelist *head) {
- while (head) {
- printf("FILE %s\r\nOCCURENCES %i\r\n", head->path, head->occorrenze);
- displayPosix(head->headPosix);
- head = head->next;
- }
- }
- void display_filelist_file(struct filelist *head, FILE *pFile) {
- while (head) {
- //TODO: printare dati verbose
- fprintf(pFile, "FILE %s\r\nOCCURENCES %i\r\n", head->path,
- head->occorrenze);
- displayPosix_file(head->headPosix, pFile);
- head = head->next;
- }
- }
- void display_wordlist(struct wordlist *head) {
- while (head) {
- printf("WORD %s\r\nTOTAL %i\r\n", head->word, head->total);
- display_filelist(head->headFilelist);
- head = head->next;
- }
- }
- // Stampa a file
- void display_wordlist_file(struct wordlist *head, char *output) {
- FILE *pFile;
- if ((pFile = fopen(output, "w")) == NULL) {
- puts("Impossibile aprire il file.");
- } else {
- while (head) {
- //TODO: printare dati verbose
- fprintf(pFile, "WORD %s\r\nTOTAL %i\r\n", head->word, head->total);
- display_filelist_file(head->headFilelist, pFile);
- head = head->next;
- }
- }
- fclose(pFile);
- }
- posix *create(int riga, int carattere, posix *next) {
- posix *new_posix = (posix *)malloc(sizeof(posix));
- if (new_posix == NULL) {
- printf("Error creating a new node.\n");
- exit(0);
- }
- new_posix->riga = riga;
- new_posix->carattere = carattere;
- new_posix->next = next;
- return new_posix;
- }
- posix *append(posix *head, int riga, int carattere) {
- if (!head) {
- return create(riga, carattere, NULL);
- }
- /* go to the last node */
- posix *cursor = head;
- while (cursor->next != NULL)
- cursor = cursor->next;
- /* create a new node */
- posix *new_posix = create(riga, carattere, NULL);
- cursor->next = new_posix;
- return head;
- }
- void delete (posix *head) {
- posix *cursor, *tmp;
- if (head != NULL) {
- cursor = head->next;
- head->next = NULL;
- while (cursor != NULL) {
- tmp = cursor->next;
- free(cursor);
- cursor = tmp;
- }
- }
- }
- void free_list(filelist *head) {
- filelist *prev = head;
- filelist *cur = head;
- while (cur) {
- prev = cur;
- cur = prev->next;
- delete (prev->headPosix);
- free(prev);
- }
- }
- void free_wordlist(wordlist *head) {
- wordlist *prev = head;
- wordlist *cur = head;
- while (cur) {
- prev = cur;
- cur = prev->next;
- free_list(prev->headFilelist);
- free(prev);
- }
- }
- void displayPosix(posix *head) {
- posix *cursor = head;
- while (cursor != NULL) {
- printf("%d %d \n", cursor->riga, cursor->carattere);
- cursor = cursor->next;
- }
- }
- void displayPosix_file(posix *head, FILE *pFile) {
- posix *cursor = head;
- while (cursor != NULL) {
- //TODO: printare dati verbose
- fprintf(pFile, "%d %d \n", cursor->riga, cursor->carattere);
- cursor = cursor->next;
- }
- }
- struct wordlist *append_wordlist(struct wordlist *list, struct wordlist *node) {
- struct wordlist *prev, *next;
- if (!list) {
- list = node;
- } else {
- prev = NULL;
- next = list;
- while (next && compareWordlist(node, next) > 0) {
- prev = next;
- next = next->next;
- }
- if (!next) {
- prev->next = node;
- } else {
- if (prev) {
- node->next = prev->next;
- prev->next = node;
- } else {
- node->next = list;
- list = node;
- }
- }
- }
- return list;
- }
- //==========================SCAN==================================
- void free_list_scan(list head) {
- list prev = head;
- list cur = head;
- while (cur) {
- prev = cur;
- cur = prev->next;
- free(prev);
- }
- }
- const char *get_filename_ext(const char *filename, const char *exclude) {
- const char *dot = strrchr(filename, '.');
- if (strcmp(dot + 1, exclude) == 0)
- return 0;
- return dot + 1;
- }
- list addToList(list a, char *word) {
- list iter, temp;
- int i = 0;
- if (a == NULL) {
- a = (list)malloc(sizeof(cell));
- strcpy(a->data, word);
- a->next = NULL;
- } else {
- iter = a;
- while (iter->next != NULL) {
- iter = iter->next;
- }
- temp = (list)malloc(sizeof(cell));
- strcpy(temp->data, word);
- temp->next = NULL;
- iter->next = temp;
- }
- return a;
- }
- void printList(list a) {
- list temp = a;
- printf("List contains following elements : \n");
- while (temp != NULL) {
- printf("%s\n", temp->data);
- temp = temp->next;
- }
- }
- //=================================================== CHECK IF IT IS A DIRECTORY
- int isDirectory(char *pathname) {
- struct stat sb;
- if (lstat(pathname, &sb) == 0 && S_ISDIR(sb.st_mode)) {
- return 1;
- }
- return 0;
- }
- //=================================================== SCAN DIRECTORY
- list listFiles(char *basePath, int recursively, list a, const char *exclude) {
- char path[1000];
- struct dirent *dp;
- DIR *dir = opendir(basePath);
- struct stat sb;
- // Unable to open directory stream
- if (dir) {
- if (isDirectory(basePath)) {printf("%s %s \n" , "inizio ad elaborare la cartella", basePath);};
- while ((dp = readdir(dir)) != NULL) {
- if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0) {
- // Construct new path from our base path
- strcpy(path, basePath);
- strcat(path, "/");
- strcat(path, dp->d_name);
- if (lstat(path, &sb) == 0 && S_ISREG(sb.st_mode) &&
- (exclude ? (get_filename_ext(path, exclude) != 0) : 1)) {
- printf("%s %s \n" , "inizio ad elaborare il file", path);
- a = addToList(a, path);
- printf("%s %s \n" , "finisco ad elaborare il file", path);
- }
- if (recursively) {
- listFiles(path, 1, a, exclude);
- }
- }
- }
- closedir(dir);
- }
- if (isDirectory(basePath)) {printf("%s %s \n" , "finisco di elaborare la cartella", basePath);};
- return a;
- }
- //=======================================List
- // File===============================================
- list take_word_from_file(char *nome_file) {
- FILE *fp;
- char *line = NULL;
- size_t len = 0;
- ssize_t read;
- list a = NULL;
- fp = fopen(nome_file, "r");
- if (fp == NULL)
- exit(EXIT_FAILURE);
- while ((read = getline(&line, &len, fp)) != -1) {
- if (line[strlen(line) - 1] == '\n') {
- line[strlen(line) - 1] = '\0';
- }
- a = addToList(a, line);
- }
- fclose(fp);
- if (line) {
- free(line);
- }
- return a;
- }
- list take_path_from_file(char *path, char *exclude) {
- FILE *fp;
- char *line = NULL;
- size_t len = 0;
- ssize_t read;
- list a = NULL;
- fp = fopen(path, "r");
- if (fp == NULL)
- exit(EXIT_FAILURE);
- while ((read = getline(&line, &len, fp)) != -1) {
- char *ptr;
- ptr = strtok(line, " ");
- // levo il carattere di terminazione nel caso sia presente e verifico se sia
- if (ptr[strlen(ptr) - 1] == '\n') {
- ptr[strlen(ptr) - 1] = '\0';
- };
- if (isDirectory(ptr)) {
- char *tmp = ptr;
- ptr = strtok(NULL, " ");
- if (ptr != NULL) {
- a = listFiles(tmp, 1, a, exclude);
- // inserisce tutti i file validi nella cartella e nelle sotto directory
- } else {
- a = listFiles(tmp, 0, a, exclude);
- // inserisce tutti i file ma non le sottodirectory
- }
- } else {
- if (get_filename_ext(ptr, exclude) !=
- 0) { // aggiungere controllo che esista TODO
- printf("%s %s \n" , "inizio ad elaborare il file", ptr);
- a = addToList(a, ptr);
- printf("%s %s \n" , "finisco ad elaborare il file", ptr);
- }
- }
- }
- fclose(fp);
- if (line) {
- free(line);
- }
- return a;
- }
- // INIZIA
- void find_word(char *file_path, char *file_word, char *exclude_extension,
- char *output) {
- filelist *tmpPathList = NULL;
- filelist *headPathList = NULL;
- wordlist *tmpWordList = NULL;
- wordlist *headWordList = NULL;
- posix *tmpposix;
- /* char *nome_file_path = {"path.txt"};
- char *nome_file_word = {"word.txt"}; */
- list words = take_word_from_file(file_word);
- list paths = take_path_from_file(file_path, exclude_extension);
- // FIXME: cambiare nome della variabile
- list tmp = paths;
- //printList(paths);
- while (words) {
- tmpWordList = (wordlist *)malloc(sizeof(wordlist));
- tmpWordList->total = 0;
- strcpy(tmpWordList->word, words->data);
- paths = tmp;
- //Ho creato parola e ciclo per tutti i path
- while (paths) {
- tmpPathList = (filelist *)malloc(sizeof(filelist));
- tmpPathList->occorrenze = 0;
- strcpy(tmpPathList->path, paths->data);
- // tmpposix = NULL;
- tmpposix = searchWordInFile(paths->data, words->data);
- tmpPathList->headPosix = tmpposix;
- while (tmpposix) {
- tmpPathList->occorrenze++;
- tmpposix = tmpposix->next;
- }
- tmpWordList->total += tmpPathList->occorrenze;
- headPathList = append_filelist(headPathList, tmpPathList);
- tmpPathList = NULL;
- paths = paths->next;
- }
- tmpWordList->headFilelist = headPathList;
- headWordList = append_wordlist(headWordList, tmpWordList);
- headPathList = NULL;
- words = words->next;
- }
- // display_wordlist(headWordList);
- if (strcmp(output, "NOT_SET") == 0) {
- display_wordlist(headWordList);
- } else {
- display_wordlist_file(headWordList, output);
- }
- free_wordlist(headWordList);
- free_list_scan(paths);
- free_list_scan(tmp);
- free_list_scan(words);
- }
- ////============INIZIO METODI NUOVI DI PROVA =====================================================///
- void find_word_2(char *file_path, char *file_word, char *exclude_extension,
- char *output) {
- filelist *tmpPathList = NULL;
- filelist *headPathList = NULL;
- wordlist *tmpWordList = NULL;
- wordlist *headWordList = NULL;
- posix *tmpposix;
- list words = take_word_from_file(file_word);
- //list paths = take_path_from_file(file_path, exclude_extension);
- //list tmp = paths;
- //printList(paths);
- while (words) {
- //Ho creato parola e ciclo per tutti i path
- tmpWordList = elaborate_word(words->data, file_path, exclude_extension);
- /* while (paths) {
- tmpPathList = (filelist *)malloc(sizeof(filelist));
- tmpPathList->occorrenze = 0;
- strcpy(tmpPathList->path, paths->data);
- // tmpposix = NULL;
- tmpposix = searchWordInFile(paths->data, words->data);
- tmpPathList->headPosix = tmpposix;
- while (tmpposix) {
- tmpPathList->occorrenze++;
- tmpposix = tmpposix->next;
- }
- tmpWordList->total += tmpPathList->occorrenze;
- headPathList = append_filelist(headPathList, tmpPathList);
- tmpPathList = NULL;
- paths = paths->next;
- } */
- //tmpWordList->headFilelist = headPathList;
- headWordList = append_wordlist(headWordList, tmpWordList);
- //headPathList = NULL;
- words = words->next;
- }
- // display_wordlist(headWordList);
- if (strcmp(output, "NOT_SET") == 0) {
- display_wordlist(headWordList);
- } else {
- display_wordlist_file(headWordList, output);
- }
- free_wordlist(headWordList);
- free_list_scan(words);
- }
- struct wordlist *elaborate_word(char* word, char*filepath, char* exclude){
- FILE *fp;
- char *line = NULL;
- size_t len = 0;
- ssize_t read;
- wordlist *tmpWordList = NULL;
- filelist *tmpPathList = NULL;
- filelist *headPathList = NULL;
- posix *tmpposix;
- tmpWordList = (wordlist *)malloc(sizeof(wordlist));
- tmpWordList->total = 0;
- strcpy(tmpWordList->word, word);
- fp = fopen(filepath, "r");
- if (fp == NULL)
- exit(EXIT_FAILURE);
- while ((read = getline(&line, &len, fp)) != -1) {
- char *ptr;
- ptr = strtok(line, " ");
- // levo il carattere di terminazione nel caso sia presente e verifico se sia
- if (ptr[strlen(ptr) - 1] == '\n') {
- ptr[strlen(ptr) - 1] = '\0';
- };
- if (isDirectory(ptr)) {
- char *tmp = ptr;
- ptr = strtok(NULL, " ");
- if (ptr != NULL) {
- //elabora
- headPathList = listFiles_2(tmp, 1, headPathList, exclude, word);
- // inserisce tutti i file validi nella cartella e nelle sotto directory
- } else {
- //elabora
- headPathList = listFiles_2(tmp, 0, headPathList, exclude, word);
- // inserisce tutti i file ma non le sottodirectory
- }
- } else {
- if (get_filename_ext(ptr, exclude) !=
- 0) { // aggiungere controllo che esista TODO
- printf("%s %s \n" , "inizio ad elaborare il file", ptr);
- tmpPathList = (filelist *)malloc(sizeof(filelist));
- tmpPathList->occorrenze = 0;
- strcpy(tmpPathList->path, ptr);
- tmpposix = searchWordInFile(ptr, word);
- tmpPathList->headPosix = tmpposix;
- while (tmpposix) {
- tmpPathList->occorrenze++;
- tmpposix = tmpposix->next;
- }
- headPathList = append_filelist(headPathList, tmpPathList);
- tmpPathList = NULL;
- printf("%s %s \n" , "finisco ad elaborare il file", ptr);
- }
- }
- }
- fclose(fp);
- if (line) {
- free(line);
- }
- filelist *countFileList= headPathList;
- while(countFileList){
- tmpWordList->total += countFileList->occorrenze;
- countFileList = countFileList->next;
- }
- //TODO: Ricordati di eliminare definitavemente poi sto tmp
- tmpWordList->headFilelist = headPathList;
- return tmpWordList;
- }
- struct filelist * listFiles_2(char *basePath, int recursively, filelist * headPathList, const char *exclude, char * word) {
- char path[1000];
- struct dirent *dp;
- DIR *dir = opendir(basePath);
- struct stat sb;
- filelist *tmpPathList = NULL;
- posix *tmpposix;
- // Unable to open directory stream
- if (dir) {
- if (isDirectory(basePath)) {printf("%s %s \n" , "inizio ad elaborare la cartella", basePath);};
- while ((dp = readdir(dir)) != NULL) {
- if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0) {
- // Construct new path from our base path
- strcpy(path, basePath);
- strcat(path, "/");
- strcat(path, dp->d_name);
- if (lstat(path, &sb) == 0 && S_ISREG(sb.st_mode) &&
- (exclude ? (get_filename_ext(path, exclude) != 0) : 1)) {
- printf("%s %s \n" , "inizio ad elaborare il file", path);
- // a = addToList(a, path);
- tmpPathList = (filelist *)malloc(sizeof(filelist));
- tmpPathList->occorrenze = 0;
- strcpy(tmpPathList->path, path);
- tmpposix = searchWordInFile(path, word);
- tmpPathList->headPosix = tmpposix;
- while (tmpposix) {
- tmpPathList->occorrenze++;
- tmpposix = tmpposix->next;
- }
- // tmpWordList->total += tmpPathList->occorrenze;
- headPathList = append_filelist(headPathList, tmpPathList);
- tmpPathList = NULL;
- printf("%s %s \n" , "finisco ad elaborare il file", path);
- }
- if (recursively) {
- listFiles_2(path, 1, headPathList, exclude, word);
- }
- }
- }
- closedir(dir);
- }
- if (isDirectory(basePath)) {printf("%s %s \n" , "finisco di elaborare la cartella", basePath);};
- return headPathList;
- }
- ////============FINE METODI NUOVI DI PROVA =====================================================///
- int main(int argc, char **argv) {
- parameter param = {"NOT_SET", "NOT_SET", "NOT_SET", "NOT_SET", "",
- "NOT_SET", "NOT_SET", 1, 0};
- // definisce un puntatore al param (descritto sopra)
- parameter *param_p = ¶m;
- struct option long_options[] = {
- //{nome_opzione, richiesta o meno di valori, flag, val}
- /*
- flag specifica come i risultati vengono ritornati per la relativa opzione
- -> se nullo, ritorna il val (identifica l'opzione in maniera corta);
- -> se non nullo, dovrebbe contenere l'indirizzo di una variabile che
- funziona da flag per quella opzione; A quel punto val è il valore da
- memorizzare all'interno della variabile che funziona da flag per indicare
- che l'opzione è stata vista.
- */
- /* Opzioni che impostano un flag */
- {"verbose", no_argument, &(param_p->isVerbose), 1},
- /* Opzioni che non impostano un flag.
- Si distinguono l'una dall'altra dai propri indici. */
- {"word", required_argument, 0, 'w'},
- {"input", required_argument, 0, 'i'},
- {"output", required_argument, 0, 'o'},
- {"report", required_argument, 0, 'r'},
- {"exclude", required_argument, 0, 'e'},
- {"show", required_argument, 0, SHOW},
- {"file", required_argument, 0, INNER_FILE},
- {0, 0, 0, 0}};
- int c;
- int option_index;
- while ((c = getopt_long(argc, argv, "vw:i:o:r:e:", long_options,
- &option_index)) != -1) {
- switch (c) {
- case 'w':
- strcpy(param_p->word, optarg);
- break;
- case 'i':
- strcpy(param_p->input, optarg);
- break;
- case 'o':
- strcpy(param_p->output, optarg);
- break;
- case 'e':
- strcpy(param_p->toExclude, optarg);
- break;
- case 'r':
- strcpy(param_p->report, optarg);
- break;
- case SHOW:
- strcpy(param_p->wordToShow, optarg);
- for (; optind < argc && *argv[optind] != '-'; optind++) {
- param_p->wordOccurences = atoi(argv[optind]);
- }
- break;
- case INNER_FILE:
- strcpy(param_p->innerFile, optarg);
- break;
- default:
- abort();
- }
- }
- if (param_p->isVerbose) {
- printf("Hai indicato verbose come parametro!\n");
- }
- if (optind < argc) {
- printf("Parametri/elementi rimanenti:\n");
- while (optind < argc) {
- printf("%s\n", argv[optind++]);
- }
- }
- // controlla che --input e --word siano complementari, con --output
- if (strcmp(param_p->word, "NOT_SET") != 0 &&
- strcmp(param_p->input, "NOT_SET") != 0) {
- //printf("Cerco le parole nei file e stampo su file.\n");
- find_word(param_p->input, param_p->word, param_p->toExclude, param_p->output);
- }
- // controlla che --report e --word siano impostati, --file no
- if (strcmp(param_p->report, "NOT_SET") != 0 &&
- strcmp(param_p->wordToShow, "NOT_SET") != 0 &&
- strcmp(param_p->innerFile, "NOT_SET") == 0) {
- find_paths_from_report(param_p->wordToShow, param_p->report, param_p->wordOccurences);
- }
- // controlla che --word, --report e --file siano impostati
- if (strcmp(param_p->report, "NOT_SET") != 0 &&
- strcmp(param_p->wordToShow, "NOT_SET") != 0 &&
- strcmp(param_p->innerFile, "NOT_SET") != 0) {
- //printf("Il report, la word e il file sono specificati: \n");
- find_occurences_from_file( param_p->wordToShow,param_p->report, param_p->innerFile);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement