Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.94 KB | None | 0 0
  1. char** leggi(char* input) {
  2.     char* ponte;
  3.     ponte = (char*)malloc(9000 * sizeof(char));
  4.     strcpy(ponte, input);
  5.     int l = strlen(ponte);
  6.     int i = 0;
  7.     char* output1;
  8.     char* output2;
  9.     char** output = (char**)malloc(2 * sizeof(char*));
  10.     output1 = (char*)malloc((l+1) * sizeof(char));
  11.     output2 = (char*)malloc((l+1) * sizeof(char)); 
  12.     while (i < l) {    
  13.         output1[i] = ponte[i];
  14.         if (ponte[i] == ' ') {
  15.             output1[i] = '\0';
  16.             int uffa = 0;
  17.             i++;
  18.             while (ponte[i] == ' ') i++;
  19.             while (i < l) {
  20.                 output2[uffa] = ponte[i];
  21.                 uffa++;
  22.                 i++;
  23.             }
  24.             output2[uffa] = '\0';
  25.             output[0] = output1;
  26.             output[1] = output2;
  27.             return output;
  28.         }
  29.         else i++;
  30.     }
  31. }
  32.  
  33. int compara_stringhe(const void* stringa1, const void* stringa2) {  //questa funzione compara due stringhe, mi serve per passarla a qsort
  34.     const char** uno = (const char**)stringa1;
  35.     const char** due = (const char**)stringa2;
  36.     return strcmp(*uno, *due);
  37. }
  38.  
  39.  
  40. void find2(char* nome, nodo* corrente, char** percorsi, char* percorso) {
  41.     char* ponte;
  42.     char* da_passare;
  43.     ponte = (char*)malloc(9000 * sizeof(char));
  44.     da_passare= (char*)malloc(9000 * sizeof(char));
  45.     strcpy(ponte, percorso);
  46.     //strcat(ponte, corrente->nome);
  47.     strcpy(da_passare, ponte);
  48.     //strcat(da_passare, corrente->nome);
  49.     //strcat(da_passare, "/");
  50.     //strcat(ponte, corrente->nome);
  51.     if (strcmp(corrente->nome, nome)==0) {
  52.         //strcat(ponte, "/");
  53.         strcat(ponte, nome);
  54.         percorsi[n_percorsi] = ponte;
  55.         n_percorsi++;
  56.     }
  57.     if (corrente->figlio != NULL) {
  58.         strcat(da_passare, corrente->nome);
  59.         strcat(da_passare, "/");
  60.         find2(nome, corrente->figlio, percorsi, da_passare);
  61.     }
  62.     strcpy(da_passare, percorso);
  63.     if (corrente->fratellodestro != NULL) {
  64.         find2(nome, corrente->fratellodestro, percorsi, da_passare);
  65.     }
  66.    
  67. }
  68.  
  69.  
  70. int main() {
  71.     //int r = 0;
  72.     //char* campo[2];
  73.     //char* token1;
  74.     //char* token2;
  75.     //char* token3;
  76.     char *input;
  77.     nodo* radice;
  78.     radice = (nodo*)malloc(sizeof(nodo));
  79.     radice->nome = "/";
  80.     radice->contenuto = NULL;
  81.     radice->figlio = NULL;
  82.     radice->fratellodestro = NULL;
  83.     radice->f_d = 0;
  84.     input = (char *)malloc(9000 * sizeof(char));
  85.  
  86.    
  87.     while (1) {
  88.         input = fgets(input, 9000, stdin);
  89.         int length = strlen(input) - 1;
  90.         input[length] = '\0';
  91.        
  92.        
  93.         if (strcmp(input, "exit") == 0) break;
  94.        
  95.  
  96.  
  97.        
  98.         char* leggi1 = leggi(input)[0];
  99.         char* leggi2 = leggi(input)[1];
  100.  
  101.  
  102.         if (strcmp(leggi1, "create") == 0) create(leggi2,radice);
  103.         else if (strcmp(leggi1, "create_dir") == 0) create_dir(leggi2,radice);
  104.         else if (strcmp(leggi1, "read") == 0) read(leggi2,radice);
  105.         else if (strcmp(leggi1, "write") == 0) write(leggi(leggi2)[0], leggi(leggi2)[1], radice);
  106.         else if (strcmp(leggi1, "delete") == 0) delet(leggi2,radice);
  107.         else if (strcmp(leggi1, "delete_r") == 0) delete_r(leggi2,radice);
  108.         else if (strcmp(leggi1, "find") == 0) {
  109.             char** percorsi = (char**)malloc(n_risorse * sizeof(char*));
  110.             for (int i = 0; i < n_risorse;i++) {
  111.                 percorsi[i] = (char*)malloc(16 * sizeof(char));
  112.                 strcpy(percorsi[i], "");
  113.             }
  114.             char* percorso;
  115.             percorso = (char*)malloc(6000 * sizeof(char));
  116.             strcpy(percorso, "/");
  117.             if (radice->figlio != NULL) {
  118.                 find2(leggi2, radice->figlio, percorsi, percorso);  //alla find passo il figlio della radice
  119.                 qsort(percorsi, n_percorsi, sizeof(char*), compara_stringhe); //faccio quicksort dell'array dei percorsi
  120.  
  121.                 for (int i = 0; i <= n_risorse-n_percorsi && strcmp(percorsi[i], "") != 0;i++) {                       
  122.                         //printf("sono entrato nel for\n");
  123.                         printf("ok %s\n", percorsi[i]);
  124.                         //free(percorsi[i]);
  125.                 }
  126.                 free(percorsi);
  127.                 if (n_percorsi == 0) printf("no\n");
  128.                 n_percorsi = 0; //rimetto a 0 il numero di percorsi trovati per un futuro nuovo utilizzo di find
  129.                 free(percorso);
  130.             }
  131.             else printf("no\n"); //se la radice non ha figli ovviamente non troverà mai nessuna risorsa la find
  132.         }
  133.         else printf("error\n");
  134.  
  135.         free(leggi1);
  136.         free(leggi2);
  137.     }
  138.    
  139.     free(input);
  140.     return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement