Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Grep-Dir v0.2 - grep-dir2.cpp

grep-dir2.cpp

Caricato da:
Scarica il programma completo

  1. /*
  2. Grep Dir v0.2
  3. Descrizione: Grep dir cerca una o due stringhe (in tal caso effettuando una ricerca incrociata)
  4.              nella directory scelta e/o in tutte le sue sub-directory.
  5.              Con il parametro -s infatti è possibile effettuare la ricerca solo ina una determinata dir
  6.  
  7. Autore:     __GiReX__
  8. Linguaggio: C++
  9. Sito:       http://girex.altervista.org/
  10.  
  11. Compile with: $ g++ grep_dir2.cpp -o grep-dir
  12. Or under Windows with Dev-Cpp
  13.  
  14. Thanks to: Style HdS619 for beta-testing and moral support!
  15.  
  16. 30/12/07 02.14
  17. */
  18.  
  19. #define log_file "grep_dir_log.txt"         // Modificare a proprio piacimento    
  20.  
  21. #ifdef WIN32                                // Direttive del pre-processore per cambiare lo slash
  22. #define slash "\\"                          // delle dir in base al SO
  23. #else
  24. #define slash "/"
  25. #endif
  26.  
  27. #include <iostream>
  28. #include <fstream>
  29. #include <dirent.h>
  30. using namespace std;
  31.  
  32. bool single_dir = false;
  33.  
  34. int first_change(char *dir);
  35. int check_dir(char *d, char *match, char *match1);
  36. int check_file(char *file, char *dir, char *match, char *match1);
  37. void display(char *buffer, char *file, char *dir, int riga);
  38. void usage(char *prog);
  39.  
  40. int main(int argc, char *argv[])
  41. {
  42.  int error = 1;  
  43.  
  44.    if(argc == 3)  {                                                             // Controllo dei parametri
  45.        
  46.          if(!first_change(argv[1])) error = check_dir(argv[1], argv[2], NULL);
  47.          
  48.  } else
  49.  
  50.    if(argc == 4 && strcmp(argv[1], "-s"))  {
  51.        
  52.          if(!first_change(argv[1])) error = check_dir(argv[1], argv[2], argv[3]);
  53.          
  54.  } else
  55.  
  56.    if(argc == 4 && !strcmp(argv[1], "-s"))  {
  57.        
  58.          if(!first_change(argv[2]))
  59.     {            
  60.             single_dir = true;
  61.             error = check_dir(argv[2], argv[3], NULL);
  62.     }
  63.  
  64.  } else
  65.  
  66.    if(argc == 5 && !strcmp(argv[1], "-s"))  {
  67.  
  68.          if(!first_change(argv[2]))
  69.     {      
  70.             single_dir = true;
  71.             error = check_dir(argv[2], argv[3], argv[4]);
  72.     }
  73.    
  74.  } else {
  75.        
  76.        usage(argv[0]);
  77.        return -1;
  78.  }
  79.    
  80.    if(!error) cout << "File " << log_file << " creato con successo!\n";
  81.   else cout << "Sono stati riscontrati degli errori durante l'esecuzione del programma!\n";
  82.  
  83.  return 0;
  84. }
  85.    
  86. int first_change(char *dir)
  87. {
  88.     if(chdir(dir) != 0)                                                            // Cambio directory di lavoro
  89.     {                                                                              // in quella passata come argomento
  90.         cout << endl << "Impossibile cambiare la direcory: " << dir << endl;
  91.         return -1;
  92.     }    
  93.  
  94.   return 0;
  95. }  
  96.    
  97. int check_dir(char *d, char *match, char *match1)                                 // Funzione ricorsiva
  98. {                                                                                 // essenziale per trovare le sub-dir
  99.  DIR *dir;
  100.  dirent *dinfo;  
  101.   char current[FILENAME_MAX];
  102.    
  103.     getcwd(current, FILENAME_MAX);                                                // Ottengo la directory corrente
  104.    
  105.     if((dir = opendir(current)) == 0)  {                                          // Apro la directory
  106.         cout << "Impossibile aprire la directory: " << d << endl;
  107.     return -1; }
  108.  
  109.  
  110.      while(dinfo = readdir(dir))                                                  // Scorro la directory
  111.    {
  112.       if(!strcmp(dinfo->d_name, ".") || !strcmp(dinfo->d_name, "..") ||
  113.          !strcmp(dinfo->d_name, "grep_dir_log.txt")) continue;
  114.        
  115.         if(chdir(dinfo->d_name) != 0) check_file(dinfo->d_name, d, match, match1); // Se non è 1 dir, controllo il file
  116.          else {
  117.           if(single_dir == true) { chdir(".."); continue; }                       // Parametro -s, non cambio dir
  118.          
  119.           strcpy(current, d);
  120.           strcat(current, "/");
  121.           strcat(current, dinfo->d_name);                                         // Ottengo una nuova dir
  122.          
  123.           check_dir(current, match, match1);                                      // La funzione richiama se stessa
  124.                                                                                   // con dir differente
  125.            if(chdir("..") != 0) {
  126.                 cout << "Impossibile spostarsi nella cartella superiore\n";       // Torno nella dir superiore
  127.          return -2;  }
  128.        }
  129.      }
  130.    closedir(dir);
  131.  return 0;
  132. }
  133.  
  134.  
  135. int check_file(char *file, char *dir, char *match, char *match1)
  136. {
  137.   bool single_match = true;
  138.    ifstream current(file, ios::in);
  139.    char buffer[1024];
  140.  
  141.     if(!current.is_open()) {
  142.         cout << "Errore nell'apertura del file: " << file << endl;
  143.     return -1;  }  
  144.    
  145.      if(match1 == NULL) single_match = true;
  146.      else single_match = false;
  147.    
  148.       for(int i = 1; current.good(); i++)
  149.       {
  150.           current.getline(buffer, sizeof(buffer));                               // Scorro tutte le righe per trovare il
  151.               if(single_match == true && strstr(buffer, match) ||                // match
  152.                  single_match == false && strstr(buffer, match) && strstr(buffer, match1))
  153.                      display(buffer, file, dir, i);                              // Trovato match, loggo file dir e riga
  154.  
  155.                  memset(buffer, 0, sizeof(buffer));                              // Pulisco buffer
  156.            }
  157.        
  158.       current.clear();
  159.    current.close();
  160.  return 0;
  161. }
  162.    
  163.  
  164.  
  165. void display(char *buffer, char *file, char *dir, int riga)
  166. {
  167.   ofstream log(log_file, ios::out | ios::app);
  168.    
  169.     log  << "---------------------------------------------------------------\n"
  170.          << "Nome file:\t"      << file     << endl
  171.          << "Sub directory:\t"  << dir      << endl
  172.          << "Riga numero:\t"    << riga     << endl
  173.          << endl                << buffer   << endl
  174.          << "---------------------------------------------------------------\n\n";
  175.                                
  176.   log.close();
  177. }
  178.  
  179. void usage(char *prog)
  180. {
  181.    cout << endl
  182.         << "Grep Dir v0.2 by __GiReX__ search string(s) il all sub-directory\n"
  183.         << "and display file, line and sub-dir where it match the string(s)\n\n"
  184.         << "Usage: " << prog << " <directory> <string> [string2] \n"
  185.         << "Usage: " << prog << " -s <directory> <string> [string2]\n\n"
  186.         << "Example: " << prog << " ~/source \"#include <stdio.h>\"\n"
  187.         << "Example: " << prog << " -s  ~/php      WHERE   $id\n\n"
  188.         << "-s : il parametro -s specifica che si intende lavorarare solo\n"
  189.         << "nella directory specificata e non in tutte le sue sub-directory\n\n"
  190.         << "[string2] : specificare la seconda stringa per effettuare una\n"
  191.         << "ricerca incrociata e' opzionale\n\n"
  192.         << "Se tra virgolette \"\" si possono anche passare come argomento intere frasi\n"
  193.         << endl;
  194. }