Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.54 KB | None | 0 0
  1. #include <ctype.h>
  2. #include <dirent.h>
  3. #include <getopt.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <sys/stat.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10.  
  11. #define SHOW 100
  12. #define INNER_FILE 200
  13. #define MAX_STR_LEN 400
  14. #define SIZE 400
  15.  
  16. typedef struct wordlist {
  17. char word[MAX_STR_LEN];
  18. unsigned int total;
  19. struct filelist *headFilelist;
  20. struct wordlist *next;
  21. } wordlist;
  22.  
  23. typedef struct filelist {
  24. char path[MAX_STR_LEN];
  25. unsigned int occorrenze;
  26. struct posix *headPosix;
  27. struct filelist *next;
  28. } filelist;
  29.  
  30. typedef struct posix {
  31. int riga;
  32. int carattere;
  33. struct posix *next;
  34. } posix;
  35.  
  36. typedef struct elem {
  37. char data[MAX_STR_LEN];
  38. struct elem *next;
  39. } cell;
  40.  
  41. typedef cell *list;
  42.  
  43. // Parametri
  44. typedef struct parameter_s {
  45. char word[MAX_STR_LEN];
  46. char input[MAX_STR_LEN];
  47. char output[MAX_STR_LEN];
  48. char report[MAX_STR_LEN];
  49. char toExclude[MAX_STR_LEN];
  50. char wordToShow[MAX_STR_LEN];
  51. char innerFile[MAX_STR_LEN];
  52. int wordOccurences;
  53. int isVerbose;
  54.  
  55. } parameter;
  56.  
  57. //======STRUTTURE=======
  58. struct filelist *append_filelist(struct filelist *list, struct filelist *node);
  59. int compareFilelist(struct filelist *n1, struct filelist *n2);
  60.  
  61. void display_filelist(struct filelist *head);
  62. void display_filelist_file(struct filelist *head, FILE *pFile);
  63. posix *create(int riga, int carattere, posix *next);
  64. posix *append(posix *head, int riga, int carattere);
  65. void delete (posix *head);
  66. void free_list(filelist *head);
  67. void displayPosix(posix *head);
  68. void displayPosix_file(posix *head, FILE *pFile);
  69. struct wordlist *append_wordlist(struct wordlist *list, struct wordlist *node);
  70. int compareWordlist(struct wordlist *n1, struct wordlist *n2);
  71. void display_wordlist(struct wordlist *head);
  72. void display_wordlist_file(struct wordlist *head, char *output);
  73. void free_wordlist(wordlist *head);
  74.  
  75. //=======KMP===========
  76. void computeLPSarray(char *pat, int M, int *lps);
  77. posix *KMPSearch(char *pat, char *txt, int righe, posix *headPosix);
  78. posix *searchWordInFile(char *filename, char *world);
  79. //=========LIST_FILE==============
  80.  
  81. list addToList(list a, char *word);
  82. void printList(list a);
  83. void free_list_scan(list head);
  84. int isDirectory(char *pathname);
  85. list listFiles(char *basePath, int recursively, list a, const char *exclude);
  86. const char *get_filename_ext(const char *filename, const char *exclude);
  87. list take_word_from_file(char *nome_file);
  88. list take_path_from_file(char *path, char *exclude);
  89.  
  90. // Genera report
  91. void find_word(char *file_path, char *file_word, char *exclude_extension,
  92. char *output);
  93.  
  94. // Analisi report
  95. void find_occurences_from_file(char *, char *, char *);
  96. void find_paths_from_report(char *, char *, int);
  97.  
  98. //
  99. //===============PROVE=================
  100. struct wordlist *elaborate_word(char *word, char *pathfile, char *exclude);
  101. struct filelist * listFiles_2(char *basePath, int recursively, filelist * headPathList, const char *exclude, char * word);
  102.  
  103.  
  104.  
  105.  
  106.  
  107. void find_paths_from_report(char *input, char *report, int occurrences) {
  108.  
  109. FILE *pFile;
  110. pFile = fopen(report, "r");
  111. if (pFile == NULL) {
  112. printf("%s\n", report);
  113. perror("Non si apre\n");
  114. }
  115.  
  116. char linea[SIZE];
  117. char temp[SIZE];
  118. char path[SIZE];
  119. int occorrenze;
  120.  
  121. char word[SIZE];
  122. strcpy(word, "WORD ");
  123. strcat(word, input);
  124.  
  125. while (fgets(linea, SIZE, pFile) != NULL) {
  126. if (strstr(linea, word) != NULL) {
  127. printf("%s", linea);
  128. while ((fgets(linea, SIZE, pFile) != NULL) &&
  129. (sscanf(linea, "WORD %s\n", temp) == 0)) {
  130. if (sscanf(linea, "FILE %s\n", path) == 1) {
  131. fgets(linea, SIZE, pFile);
  132. sscanf(linea, "OCCURENCES %d\n", &occorrenze);
  133. if (occorrenze > occurrences) {
  134. printf("FILE %s\n", path);
  135. printf("OCCURRENCES %d\n\n", occorrenze);
  136. }
  137. }
  138. }
  139. }
  140. }
  141.  
  142. fclose(pFile);
  143. }
  144. void find_occurences_from_file(char *input, char *report, char *file) {
  145.  
  146. FILE *pFile;
  147. pFile = fopen(report, "r");
  148. if (pFile == NULL) {
  149. printf("Non si apre.\n");
  150. }
  151.  
  152. char linea[SIZE];
  153. char temp[SIZE];
  154. char path[SIZE];
  155.  
  156. char file2[SIZE] = "FILE ";
  157. strcat(file2, file);
  158. //strcat(file2, "\r\n");
  159.  
  160. unsigned int posizione;
  161. unsigned int riga;
  162.  
  163. char word[200];
  164. strcpy(word, "WORD ");
  165. strcat(word, input);
  166. strcat(file2, "\r\n");
  167.  
  168. while (fgets(linea, SIZE, pFile) != NULL) {
  169. if (strstr(linea, word) != NULL) {
  170.  
  171. printf("%s", linea);
  172. while ((fgets(linea, SIZE, pFile) != NULL) &&
  173. (sscanf(linea, "WORD %s\n", temp) == 0)) {
  174. // se le stringhe sono uguali
  175. if (strcmp(file2, linea) == 0) {
  176. fgets(linea, SIZE, pFile);
  177. // sscanf restituisce numero variabili che riempie
  178. while ((fgets(linea, SIZE, pFile) != NULL) &&
  179. (sscanf(linea, "%i %i", &posizione, &riga) == 2)) {
  180. printf("%i %i\n", posizione, riga);
  181. }
  182. }
  183. }
  184. }
  185. }
  186. fclose(pFile);
  187. }
  188.  
  189. void computeLPSarray(char *pat, int M, int *lps) {
  190. int len = 0; // lenght of previous longest prefix suffix
  191. int i;
  192.  
  193. lps[0] = 0; // lps[0] is always 0
  194. i = 1;
  195.  
  196. while (i < M) // loop calculates lps[i] for i = 1 to M-1
  197. {
  198. if (pat[i] == pat[len]) {
  199. len++;
  200. lps[i] = len;
  201. i++;
  202. } else // pat[i] != pat[len]
  203. {
  204. if (len != 0) {
  205. // this is tricky
  206. // consider the example AAACAAAA and i =7
  207. len = lps[len - 1];
  208. // we donot increment i here
  209. } else // if len == 0
  210. {
  211. lps[i] = 0;
  212. i++;
  213. }
  214. }
  215. }
  216. }
  217. posix *KMPSearch(char *pat, char *txt, int righe, posix *headPosix) {
  218. int M = strlen(pat);
  219. int N = strlen(txt);
  220.  
  221. int *lps = (int *)malloc(M * sizeof(int));
  222. int j = 0;
  223.  
  224. computeLPSarray(pat, M, lps);
  225.  
  226. int i = 0;
  227. while (i < N) {
  228. if (pat[j] == txt[i]) {
  229. j++;
  230. i++;
  231. }
  232. if (j == M) { // Qui andrò ad inserire riga e posizione nella struttura
  233. // posix e la concatenerò alla lista
  234.  
  235. // printf("trovato in riga%d, posizione %d\n", righe, i - j);
  236. headPosix = append(headPosix, righe, i - j);
  237. j = lps[j - 1];
  238. } else if (pat[j] != txt[i]) {
  239. if (j != 0)
  240. j = lps[j - 1];
  241. else
  242. i++;
  243. }
  244. }
  245. free(lps);
  246. return headPosix;
  247. }
  248.  
  249. posix *searchWordInFile(char *filename, char *world) {
  250.  
  251. FILE *fp;
  252. char *line = NULL;
  253. size_t len = 0;
  254. ssize_t read;
  255.  
  256. posix *headPosix = NULL;
  257.  
  258. fp = fopen(filename, "r");
  259. if (fp == NULL)
  260. exit(EXIT_FAILURE);
  261.  
  262. int righe = 1;
  263. while ((read = getline(&line, &len, fp)) != -1) {
  264.  
  265. headPosix = KMPSearch(world, line, righe, headPosix);
  266. righe++;
  267. }
  268.  
  269. fclose(fp);
  270. if (line) {
  271. free(line);
  272. }
  273. return headPosix;
  274. }
  275.  
  276. //=====================================STRUCT===================================
  277.  
  278. struct filelist *append_filelist(struct filelist *list, struct filelist *node) {
  279. struct filelist *prev, *next;
  280. if (!list) {
  281. list = node;
  282. } else {
  283. prev = NULL;
  284. next = list;
  285. while (next && compareFilelist(node, next) > 0) {
  286. prev = next;
  287. next = next->next;
  288. }
  289. if (!next) {
  290. prev->next = node;
  291. } else {
  292. if (prev) {
  293. node->next = prev->next;
  294. prev->next = node;
  295. } else {
  296. node->next = list;
  297. list = node;
  298. }
  299. }
  300. }
  301. return list;
  302. }
  303.  
  304. int compareFilelist(struct filelist *n1, struct filelist *n2) {
  305. if ((n1->occorrenze < n2->occorrenze)) {
  306. return 1;
  307. };
  308. if ((n1->occorrenze == n2->occorrenze) && (strcmp(n1->path, n2->path) > 0)) {
  309. return 1;
  310. };
  311. return -1;
  312. }
  313.  
  314. int compareWordlist(struct wordlist *n1, struct wordlist *n2) {
  315.  
  316. if ((strcmp(n1->word, n2->word) > 0)) {
  317. return 1;
  318. };
  319. return -1;
  320. }
  321.  
  322. void display_filelist(struct filelist *head) {
  323. while (head) {
  324. printf("FILE %s\r\nOCCURENCES %i\r\n", head->path, head->occorrenze);
  325. displayPosix(head->headPosix);
  326. head = head->next;
  327. }
  328. }
  329.  
  330. void display_filelist_file(struct filelist *head, FILE *pFile) {
  331. while (head) {
  332. //TODO: printare dati verbose
  333. fprintf(pFile, "FILE %s\r\nOCCURENCES %i\r\n", head->path,
  334. head->occorrenze);
  335. displayPosix_file(head->headPosix, pFile);
  336. head = head->next;
  337. }
  338. }
  339.  
  340. void display_wordlist(struct wordlist *head) {
  341. while (head) {
  342. printf("WORD %s\r\nTOTAL %i\r\n", head->word, head->total);
  343. display_filelist(head->headFilelist);
  344. head = head->next;
  345. }
  346. }
  347.  
  348. // Stampa a file
  349. void display_wordlist_file(struct wordlist *head, char *output) {
  350.  
  351. FILE *pFile;
  352. if ((pFile = fopen(output, "w")) == NULL) {
  353. puts("Impossibile aprire il file.");
  354. } else {
  355. while (head) {
  356. //TODO: printare dati verbose
  357. fprintf(pFile, "WORD %s\r\nTOTAL %i\r\n", head->word, head->total);
  358. display_filelist_file(head->headFilelist, pFile);
  359. head = head->next;
  360. }
  361. }
  362. fclose(pFile);
  363. }
  364.  
  365. posix *create(int riga, int carattere, posix *next) {
  366. posix *new_posix = (posix *)malloc(sizeof(posix));
  367. if (new_posix == NULL) {
  368. printf("Error creating a new node.\n");
  369. exit(0);
  370. }
  371. new_posix->riga = riga;
  372. new_posix->carattere = carattere;
  373. new_posix->next = next;
  374. return new_posix;
  375. }
  376.  
  377. posix *append(posix *head, int riga, int carattere) {
  378.  
  379. if (!head) {
  380. return create(riga, carattere, NULL);
  381. }
  382.  
  383. /* go to the last node */
  384. posix *cursor = head;
  385.  
  386. while (cursor->next != NULL)
  387. cursor = cursor->next;
  388.  
  389. /* create a new node */
  390. posix *new_posix = create(riga, carattere, NULL);
  391. cursor->next = new_posix;
  392.  
  393. return head;
  394. }
  395.  
  396. void delete (posix *head) {
  397. posix *cursor, *tmp;
  398.  
  399. if (head != NULL) {
  400. cursor = head->next;
  401. head->next = NULL;
  402. while (cursor != NULL) {
  403. tmp = cursor->next;
  404. free(cursor);
  405. cursor = tmp;
  406. }
  407. }
  408. }
  409.  
  410. void free_list(filelist *head) {
  411. filelist *prev = head;
  412. filelist *cur = head;
  413. while (cur) {
  414. prev = cur;
  415. cur = prev->next;
  416. delete (prev->headPosix);
  417. free(prev);
  418. }
  419. }
  420.  
  421. void free_wordlist(wordlist *head) {
  422. wordlist *prev = head;
  423. wordlist *cur = head;
  424. while (cur) {
  425. prev = cur;
  426. cur = prev->next;
  427. free_list(prev->headFilelist);
  428. free(prev);
  429. }
  430. }
  431.  
  432. void displayPosix(posix *head) {
  433.  
  434. posix *cursor = head;
  435. while (cursor != NULL) {
  436. printf("%d %d \n", cursor->riga, cursor->carattere);
  437. cursor = cursor->next;
  438. }
  439. }
  440.  
  441. void displayPosix_file(posix *head, FILE *pFile) {
  442. posix *cursor = head;
  443. while (cursor != NULL) {
  444. //TODO: printare dati verbose
  445. fprintf(pFile, "%d %d \n", cursor->riga, cursor->carattere);
  446. cursor = cursor->next;
  447. }
  448. }
  449.  
  450. struct wordlist *append_wordlist(struct wordlist *list, struct wordlist *node) {
  451. struct wordlist *prev, *next;
  452. if (!list) {
  453. list = node;
  454. } else {
  455. prev = NULL;
  456. next = list;
  457. while (next && compareWordlist(node, next) > 0) {
  458. prev = next;
  459. next = next->next;
  460. }
  461. if (!next) {
  462. prev->next = node;
  463. } else {
  464. if (prev) {
  465. node->next = prev->next;
  466. prev->next = node;
  467. } else {
  468. node->next = list;
  469. list = node;
  470. }
  471. }
  472. }
  473. return list;
  474. }
  475.  
  476. //==========================SCAN==================================
  477.  
  478. void free_list_scan(list head) {
  479. list prev = head;
  480. list cur = head;
  481. while (cur) {
  482. prev = cur;
  483. cur = prev->next;
  484. free(prev);
  485. }
  486. }
  487.  
  488. const char *get_filename_ext(const char *filename, const char *exclude) {
  489. const char *dot = strrchr(filename, '.');
  490. if (strcmp(dot + 1, exclude) == 0)
  491. return 0;
  492. return dot + 1;
  493. }
  494.  
  495. list addToList(list a, char *word) {
  496.  
  497. list iter, temp;
  498. int i = 0;
  499.  
  500. if (a == NULL) {
  501.  
  502. a = (list)malloc(sizeof(cell));
  503. strcpy(a->data, word);
  504. a->next = NULL;
  505. } else {
  506. iter = a;
  507.  
  508. while (iter->next != NULL) {
  509. iter = iter->next;
  510. }
  511.  
  512. temp = (list)malloc(sizeof(cell));
  513. strcpy(temp->data, word);
  514. temp->next = NULL;
  515.  
  516. iter->next = temp;
  517. }
  518. return a;
  519. }
  520.  
  521. void printList(list a) {
  522. list temp = a;
  523.  
  524. printf("List contains following elements : \n");
  525.  
  526. while (temp != NULL) {
  527. printf("%s\n", temp->data);
  528. temp = temp->next;
  529. }
  530. }
  531.  
  532. //=================================================== CHECK IF IT IS A DIRECTORY
  533.  
  534. int isDirectory(char *pathname) {
  535. struct stat sb;
  536.  
  537. if (lstat(pathname, &sb) == 0 && S_ISDIR(sb.st_mode)) {
  538. return 1;
  539. }
  540. return 0;
  541. }
  542.  
  543. //=================================================== SCAN DIRECTORY
  544. list listFiles(char *basePath, int recursively, list a, const char *exclude) {
  545. char path[1000];
  546. struct dirent *dp;
  547. DIR *dir = opendir(basePath);
  548. struct stat sb;
  549.  
  550. // Unable to open directory stream
  551. if (dir) {
  552. if (isDirectory(basePath)) {printf("%s %s \n" , "inizio ad elaborare la cartella", basePath);};
  553.  
  554. while ((dp = readdir(dir)) != NULL) {
  555. if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0) {
  556.  
  557. // Construct new path from our base path
  558. strcpy(path, basePath);
  559. strcat(path, "/");
  560. strcat(path, dp->d_name);
  561.  
  562. if (lstat(path, &sb) == 0 && S_ISREG(sb.st_mode) &&
  563. (exclude ? (get_filename_ext(path, exclude) != 0) : 1)) {
  564. printf("%s %s \n" , "inizio ad elaborare il file", path);
  565.  
  566. a = addToList(a, path);
  567.  
  568. printf("%s %s \n" , "finisco ad elaborare il file", path);
  569. }
  570.  
  571. if (recursively) {
  572. listFiles(path, 1, a, exclude);
  573. }
  574. }
  575. }
  576.  
  577. closedir(dir);
  578. }
  579.  
  580. if (isDirectory(basePath)) {printf("%s %s \n" , "finisco di elaborare la cartella", basePath);};
  581.  
  582. return a;
  583. }
  584.  
  585. //=======================================List
  586. // File===============================================
  587. list take_word_from_file(char *nome_file) {
  588.  
  589. FILE *fp;
  590. char *line = NULL;
  591. size_t len = 0;
  592. ssize_t read;
  593.  
  594. list a = NULL;
  595.  
  596. fp = fopen(nome_file, "r");
  597. if (fp == NULL)
  598. exit(EXIT_FAILURE);
  599.  
  600. while ((read = getline(&line, &len, fp)) != -1) {
  601. if (line[strlen(line) - 1] == '\n') {
  602. line[strlen(line) - 1] = '\0';
  603. }
  604. a = addToList(a, line);
  605. }
  606.  
  607. fclose(fp);
  608. if (line) {
  609. free(line);
  610. }
  611.  
  612. return a;
  613. }
  614.  
  615. list take_path_from_file(char *path, char *exclude) {
  616.  
  617. FILE *fp;
  618. char *line = NULL;
  619. size_t len = 0;
  620. ssize_t read;
  621. list a = NULL;
  622.  
  623. fp = fopen(path, "r");
  624. if (fp == NULL)
  625. exit(EXIT_FAILURE);
  626.  
  627. while ((read = getline(&line, &len, fp)) != -1) {
  628.  
  629. char *ptr;
  630.  
  631. ptr = strtok(line, " ");
  632. // levo il carattere di terminazione nel caso sia presente e verifico se sia
  633. if (ptr[strlen(ptr) - 1] == '\n') {
  634. ptr[strlen(ptr) - 1] = '\0';
  635. };
  636. if (isDirectory(ptr)) {
  637.  
  638. char *tmp = ptr;
  639. ptr = strtok(NULL, " ");
  640. if (ptr != NULL) {
  641.  
  642. a = listFiles(tmp, 1, a, exclude);
  643.  
  644. // inserisce tutti i file validi nella cartella e nelle sotto directory
  645. } else {
  646. a = listFiles(tmp, 0, a, exclude);
  647.  
  648.  
  649. // inserisce tutti i file ma non le sottodirectory
  650. }
  651. } else {
  652. if (get_filename_ext(ptr, exclude) !=
  653. 0) { // aggiungere controllo che esista TODO
  654. printf("%s %s \n" , "inizio ad elaborare il file", ptr);
  655. a = addToList(a, ptr);
  656. printf("%s %s \n" , "finisco ad elaborare il file", ptr);
  657. }
  658. }
  659. }
  660.  
  661. fclose(fp);
  662. if (line) {
  663. free(line);
  664. }
  665.  
  666. return a;
  667. }
  668.  
  669. // INIZIA
  670.  
  671. void find_word(char *file_path, char *file_word, char *exclude_extension,
  672. char *output) {
  673.  
  674. filelist *tmpPathList = NULL;
  675. filelist *headPathList = NULL;
  676.  
  677. wordlist *tmpWordList = NULL;
  678. wordlist *headWordList = NULL;
  679.  
  680. posix *tmpposix;
  681.  
  682. /* char *nome_file_path = {"path.txt"};
  683. char *nome_file_word = {"word.txt"}; */
  684.  
  685. list words = take_word_from_file(file_word);
  686. list paths = take_path_from_file(file_path, exclude_extension);
  687. // FIXME: cambiare nome della variabile
  688. list tmp = paths;
  689. //printList(paths);
  690. while (words) {
  691.  
  692. tmpWordList = (wordlist *)malloc(sizeof(wordlist));
  693. tmpWordList->total = 0;
  694. strcpy(tmpWordList->word, words->data);
  695. paths = tmp;
  696. //Ho creato parola e ciclo per tutti i path
  697. while (paths) {
  698.  
  699. tmpPathList = (filelist *)malloc(sizeof(filelist));
  700. tmpPathList->occorrenze = 0;
  701. strcpy(tmpPathList->path, paths->data);
  702.  
  703. // tmpposix = NULL;
  704. tmpposix = searchWordInFile(paths->data, words->data);
  705.  
  706. tmpPathList->headPosix = tmpposix;
  707.  
  708. while (tmpposix) {
  709. tmpPathList->occorrenze++;
  710. tmpposix = tmpposix->next;
  711. }
  712.  
  713. tmpWordList->total += tmpPathList->occorrenze;
  714.  
  715. headPathList = append_filelist(headPathList, tmpPathList);
  716. tmpPathList = NULL;
  717. paths = paths->next;
  718. }
  719.  
  720. tmpWordList->headFilelist = headPathList;
  721. headWordList = append_wordlist(headWordList, tmpWordList);
  722. headPathList = NULL;
  723. words = words->next;
  724. }
  725.  
  726. // display_wordlist(headWordList);
  727. if (strcmp(output, "NOT_SET") == 0) {
  728. display_wordlist(headWordList);
  729. } else {
  730. display_wordlist_file(headWordList, output);
  731. }
  732.  
  733. free_wordlist(headWordList);
  734. free_list_scan(paths);
  735. free_list_scan(tmp);
  736. free_list_scan(words);
  737. }
  738.  
  739.  
  740. ////============INIZIO METODI NUOVI DI PROVA =====================================================///
  741.  
  742. void find_word_2(char *file_path, char *file_word, char *exclude_extension,
  743. char *output) {
  744.  
  745. filelist *tmpPathList = NULL;
  746. filelist *headPathList = NULL;
  747.  
  748. wordlist *tmpWordList = NULL;
  749. wordlist *headWordList = NULL;
  750.  
  751. posix *tmpposix;
  752.  
  753.  
  754. list words = take_word_from_file(file_word);
  755.  
  756.  
  757. //list paths = take_path_from_file(file_path, exclude_extension);
  758. //list tmp = paths;
  759. //printList(paths);
  760.  
  761.  
  762. while (words) {
  763.  
  764.  
  765. //Ho creato parola e ciclo per tutti i path
  766. tmpWordList = elaborate_word(words->data, file_path, exclude_extension);
  767.  
  768.  
  769. /* while (paths) {
  770.  
  771. tmpPathList = (filelist *)malloc(sizeof(filelist));
  772. tmpPathList->occorrenze = 0;
  773. strcpy(tmpPathList->path, paths->data);
  774.  
  775. // tmpposix = NULL;
  776. tmpposix = searchWordInFile(paths->data, words->data);
  777.  
  778. tmpPathList->headPosix = tmpposix;
  779.  
  780. while (tmpposix) {
  781. tmpPathList->occorrenze++;
  782. tmpposix = tmpposix->next;
  783. }
  784.  
  785. tmpWordList->total += tmpPathList->occorrenze;
  786.  
  787. headPathList = append_filelist(headPathList, tmpPathList);
  788. tmpPathList = NULL;
  789. paths = paths->next;
  790. } */
  791.  
  792. //tmpWordList->headFilelist = headPathList;
  793. headWordList = append_wordlist(headWordList, tmpWordList);
  794. //headPathList = NULL;
  795. words = words->next;
  796. }
  797.  
  798. // display_wordlist(headWordList);
  799. if (strcmp(output, "NOT_SET") == 0) {
  800. display_wordlist(headWordList);
  801. } else {
  802. display_wordlist_file(headWordList, output);
  803. }
  804.  
  805. free_wordlist(headWordList);
  806. free_list_scan(words);
  807. }
  808.  
  809.  
  810.  
  811. struct wordlist *elaborate_word(char* word, char*filepath, char* exclude){
  812. FILE *fp;
  813. char *line = NULL;
  814. size_t len = 0;
  815. ssize_t read;
  816.  
  817. wordlist *tmpWordList = NULL;
  818. filelist *tmpPathList = NULL;
  819. filelist *headPathList = NULL;
  820.  
  821. posix *tmpposix;
  822.  
  823.  
  824. tmpWordList = (wordlist *)malloc(sizeof(wordlist));
  825. tmpWordList->total = 0;
  826. strcpy(tmpWordList->word, word);
  827.  
  828. fp = fopen(filepath, "r");
  829. if (fp == NULL)
  830. exit(EXIT_FAILURE);
  831.  
  832. while ((read = getline(&line, &len, fp)) != -1) {
  833.  
  834. char *ptr;
  835.  
  836. ptr = strtok(line, " ");
  837. // levo il carattere di terminazione nel caso sia presente e verifico se sia
  838. if (ptr[strlen(ptr) - 1] == '\n') {
  839. ptr[strlen(ptr) - 1] = '\0';
  840. };
  841. if (isDirectory(ptr)) {
  842.  
  843. char *tmp = ptr;
  844. ptr = strtok(NULL, " ");
  845. if (ptr != NULL) {
  846. //elabora
  847. headPathList = listFiles_2(tmp, 1, headPathList, exclude, word);
  848.  
  849. // inserisce tutti i file validi nella cartella e nelle sotto directory
  850. } else {
  851.  
  852. //elabora
  853. headPathList = listFiles_2(tmp, 0, headPathList, exclude, word);
  854.  
  855.  
  856. // inserisce tutti i file ma non le sottodirectory
  857. }
  858. } else {
  859. if (get_filename_ext(ptr, exclude) !=
  860. 0) { // aggiungere controllo che esista TODO
  861. printf("%s %s \n" , "inizio ad elaborare il file", ptr);
  862.  
  863.  
  864. tmpPathList = (filelist *)malloc(sizeof(filelist));
  865. tmpPathList->occorrenze = 0;
  866. strcpy(tmpPathList->path, ptr);
  867. tmpposix = searchWordInFile(ptr, word);
  868. tmpPathList->headPosix = tmpposix;
  869. while (tmpposix) {
  870. tmpPathList->occorrenze++;
  871. tmpposix = tmpposix->next;
  872. }
  873.  
  874. headPathList = append_filelist(headPathList, tmpPathList);
  875. tmpPathList = NULL;
  876.  
  877.  
  878.  
  879.  
  880. printf("%s %s \n" , "finisco ad elaborare il file", ptr);
  881. }
  882. }
  883. }
  884.  
  885. fclose(fp);
  886. if (line) {
  887. free(line);
  888. }
  889.  
  890. filelist *countFileList= headPathList;
  891. while(countFileList){
  892. tmpWordList->total += countFileList->occorrenze;
  893. countFileList = countFileList->next;
  894. }
  895. //TODO: Ricordati di eliminare definitavemente poi sto tmp
  896. tmpWordList->headFilelist = headPathList;
  897. return tmpWordList;
  898.  
  899.  
  900. }
  901.  
  902.  
  903. struct filelist * listFiles_2(char *basePath, int recursively, filelist * headPathList, const char *exclude, char * word) {
  904. char path[1000];
  905. struct dirent *dp;
  906. DIR *dir = opendir(basePath);
  907. struct stat sb;
  908. filelist *tmpPathList = NULL;
  909. posix *tmpposix;
  910.  
  911.  
  912. // Unable to open directory stream
  913. if (dir) {
  914. if (isDirectory(basePath)) {printf("%s %s \n" , "inizio ad elaborare la cartella", basePath);};
  915.  
  916. while ((dp = readdir(dir)) != NULL) {
  917. if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0) {
  918.  
  919. // Construct new path from our base path
  920. strcpy(path, basePath);
  921. strcat(path, "/");
  922. strcat(path, dp->d_name);
  923.  
  924. if (lstat(path, &sb) == 0 && S_ISREG(sb.st_mode) &&
  925. (exclude ? (get_filename_ext(path, exclude) != 0) : 1)) {
  926. printf("%s %s \n" , "inizio ad elaborare il file", path);
  927.  
  928. // a = addToList(a, path);
  929.  
  930. tmpPathList = (filelist *)malloc(sizeof(filelist));
  931. tmpPathList->occorrenze = 0;
  932. strcpy(tmpPathList->path, path);
  933. tmpposix = searchWordInFile(path, word);
  934. tmpPathList->headPosix = tmpposix;
  935.  
  936. while (tmpposix) {
  937. tmpPathList->occorrenze++;
  938. tmpposix = tmpposix->next;
  939. }
  940.  
  941. // tmpWordList->total += tmpPathList->occorrenze;
  942. headPathList = append_filelist(headPathList, tmpPathList);
  943. tmpPathList = NULL;
  944.  
  945.  
  946.  
  947. printf("%s %s \n" , "finisco ad elaborare il file", path);
  948. }
  949.  
  950. if (recursively) {
  951. listFiles_2(path, 1, headPathList, exclude, word);
  952. }
  953. }
  954. }
  955.  
  956. closedir(dir);
  957. }
  958.  
  959. if (isDirectory(basePath)) {printf("%s %s \n" , "finisco di elaborare la cartella", basePath);};
  960.  
  961. return headPathList;
  962. }
  963.  
  964.  
  965.  
  966.  
  967.  
  968.  
  969. ////============FINE METODI NUOVI DI PROVA =====================================================///
  970.  
  971.  
  972.  
  973.  
  974.  
  975.  
  976.  
  977. int main(int argc, char **argv) {
  978. parameter param = {"NOT_SET", "NOT_SET", "NOT_SET", "NOT_SET", "",
  979. "NOT_SET", "NOT_SET", 1, 0};
  980.  
  981. // definisce un puntatore al param (descritto sopra)
  982. parameter *param_p = &param;
  983.  
  984. struct option long_options[] = {
  985. //{nome_opzione, richiesta o meno di valori, flag, val}
  986.  
  987. /*
  988. flag specifica come i risultati vengono ritornati per la relativa opzione
  989. -> se nullo, ritorna il val (identifica l'opzione in maniera corta);
  990. -> se non nullo, dovrebbe contenere l'indirizzo di una variabile che
  991. funziona da flag per quella opzione; A quel punto val è il valore da
  992. memorizzare all'interno della variabile che funziona da flag per indicare
  993. che l'opzione è stata vista.
  994. */
  995.  
  996. /* Opzioni che impostano un flag */
  997. {"verbose", no_argument, &(param_p->isVerbose), 1},
  998. /* Opzioni che non impostano un flag.
  999. Si distinguono l'una dall'altra dai propri indici. */
  1000. {"word", required_argument, 0, 'w'},
  1001. {"input", required_argument, 0, 'i'},
  1002. {"output", required_argument, 0, 'o'},
  1003. {"report", required_argument, 0, 'r'},
  1004. {"exclude", required_argument, 0, 'e'},
  1005. {"show", required_argument, 0, SHOW},
  1006. {"file", required_argument, 0, INNER_FILE},
  1007. {0, 0, 0, 0}};
  1008.  
  1009. int c;
  1010. int option_index;
  1011.  
  1012. while ((c = getopt_long(argc, argv, "vw:i:o:r:e:", long_options,
  1013. &option_index)) != -1) {
  1014. switch (c) {
  1015. case 'w':
  1016. strcpy(param_p->word, optarg);
  1017. break;
  1018.  
  1019. case 'i':
  1020. strcpy(param_p->input, optarg);
  1021. break;
  1022.  
  1023. case 'o':
  1024. strcpy(param_p->output, optarg);
  1025. break;
  1026.  
  1027. case 'e':
  1028. strcpy(param_p->toExclude, optarg);
  1029. break;
  1030.  
  1031. case 'r':
  1032. strcpy(param_p->report, optarg);
  1033. break;
  1034.  
  1035. case SHOW:
  1036. strcpy(param_p->wordToShow, optarg);
  1037. for (; optind < argc && *argv[optind] != '-'; optind++) {
  1038. param_p->wordOccurences = atoi(argv[optind]);
  1039. }
  1040. break;
  1041.  
  1042. case INNER_FILE:
  1043. strcpy(param_p->innerFile, optarg);
  1044. break;
  1045.  
  1046. default:
  1047. abort();
  1048. }
  1049. }
  1050.  
  1051. if (param_p->isVerbose) {
  1052. printf("Hai indicato verbose come parametro!\n");
  1053. }
  1054.  
  1055. if (optind < argc) {
  1056. printf("Parametri/elementi rimanenti:\n");
  1057. while (optind < argc) {
  1058. printf("%s\n", argv[optind++]);
  1059. }
  1060. }
  1061.  
  1062. // controlla che --input e --word siano complementari, con --output
  1063. if (strcmp(param_p->word, "NOT_SET") != 0 &&
  1064. strcmp(param_p->input, "NOT_SET") != 0) {
  1065. //printf("Cerco le parole nei file e stampo su file.\n");
  1066. find_word(param_p->input, param_p->word, param_p->toExclude, param_p->output);
  1067. }
  1068.  
  1069.  
  1070. // controlla che --report e --word siano impostati, --file no
  1071. if (strcmp(param_p->report, "NOT_SET") != 0 &&
  1072. strcmp(param_p->wordToShow, "NOT_SET") != 0 &&
  1073. strcmp(param_p->innerFile, "NOT_SET") == 0) {
  1074. find_paths_from_report(param_p->wordToShow, param_p->report, param_p->wordOccurences);
  1075. }
  1076.  
  1077. // controlla che --word, --report e --file siano impostati
  1078. if (strcmp(param_p->report, "NOT_SET") != 0 &&
  1079. strcmp(param_p->wordToShow, "NOT_SET") != 0 &&
  1080. strcmp(param_p->innerFile, "NOT_SET") != 0) {
  1081. //printf("Il report, la word e il file sono specificati: \n");
  1082. find_occurences_from_file( param_p->wordToShow,param_p->report, param_p->innerFile);
  1083. }
  1084. return 0;
  1085. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement