Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- char** leggi(char* input) {
- char* ponte;
- ponte = (char*)malloc(9000 * sizeof(char));
- strcpy(ponte, input);
- int l = strlen(ponte);
- int i = 0;
- char* output1;
- char* output2;
- char** output = (char**)malloc(2 * sizeof(char*));
- output1 = (char*)malloc((l+1) * sizeof(char));
- output2 = (char*)malloc((l+1) * sizeof(char));
- while (i < l) {
- output1[i] = ponte[i];
- if (ponte[i] == ' ') {
- output1[i] = '\0';
- int uffa = 0;
- i++;
- while (ponte[i] == ' ') i++;
- while (i < l) {
- output2[uffa] = ponte[i];
- uffa++;
- i++;
- }
- output2[uffa] = '\0';
- output[0] = output1;
- output[1] = output2;
- return output;
- }
- else i++;
- }
- }
- int compara_stringhe(const void* stringa1, const void* stringa2) { //questa funzione compara due stringhe, mi serve per passarla a qsort
- const char** uno = (const char**)stringa1;
- const char** due = (const char**)stringa2;
- return strcmp(*uno, *due);
- }
- void find2(char* nome, nodo* corrente, char** percorsi, char* percorso) {
- char* ponte;
- char* da_passare;
- ponte = (char*)malloc(9000 * sizeof(char));
- da_passare= (char*)malloc(9000 * sizeof(char));
- strcpy(ponte, percorso);
- //strcat(ponte, corrente->nome);
- strcpy(da_passare, ponte);
- //strcat(da_passare, corrente->nome);
- //strcat(da_passare, "/");
- //strcat(ponte, corrente->nome);
- if (strcmp(corrente->nome, nome)==0) {
- //strcat(ponte, "/");
- strcat(ponte, nome);
- percorsi[n_percorsi] = ponte;
- n_percorsi++;
- }
- if (corrente->figlio != NULL) {
- strcat(da_passare, corrente->nome);
- strcat(da_passare, "/");
- find2(nome, corrente->figlio, percorsi, da_passare);
- }
- strcpy(da_passare, percorso);
- if (corrente->fratellodestro != NULL) {
- find2(nome, corrente->fratellodestro, percorsi, da_passare);
- }
- }
- int main() {
- //int r = 0;
- //char* campo[2];
- //char* token1;
- //char* token2;
- //char* token3;
- char *input;
- nodo* radice;
- radice = (nodo*)malloc(sizeof(nodo));
- radice->nome = "/";
- radice->contenuto = NULL;
- radice->figlio = NULL;
- radice->fratellodestro = NULL;
- radice->f_d = 0;
- input = (char *)malloc(9000 * sizeof(char));
- while (1) {
- input = fgets(input, 9000, stdin);
- int length = strlen(input) - 1;
- input[length] = '\0';
- if (strcmp(input, "exit") == 0) break;
- char* leggi1 = leggi(input)[0];
- char* leggi2 = leggi(input)[1];
- if (strcmp(leggi1, "create") == 0) create(leggi2,radice);
- else if (strcmp(leggi1, "create_dir") == 0) create_dir(leggi2,radice);
- else if (strcmp(leggi1, "read") == 0) read(leggi2,radice);
- else if (strcmp(leggi1, "write") == 0) write(leggi(leggi2)[0], leggi(leggi2)[1], radice);
- else if (strcmp(leggi1, "delete") == 0) delet(leggi2,radice);
- else if (strcmp(leggi1, "delete_r") == 0) delete_r(leggi2,radice);
- else if (strcmp(leggi1, "find") == 0) {
- char** percorsi = (char**)malloc(n_risorse * sizeof(char*));
- for (int i = 0; i < n_risorse;i++) {
- percorsi[i] = (char*)malloc(16 * sizeof(char));
- strcpy(percorsi[i], "");
- }
- char* percorso;
- percorso = (char*)malloc(6000 * sizeof(char));
- strcpy(percorso, "/");
- if (radice->figlio != NULL) {
- find2(leggi2, radice->figlio, percorsi, percorso); //alla find passo il figlio della radice
- qsort(percorsi, n_percorsi, sizeof(char*), compara_stringhe); //faccio quicksort dell'array dei percorsi
- for (int i = 0; i <= n_risorse-n_percorsi && strcmp(percorsi[i], "") != 0;i++) {
- //printf("sono entrato nel for\n");
- printf("ok %s\n", percorsi[i]);
- //free(percorsi[i]);
- }
- free(percorsi);
- if (n_percorsi == 0) printf("no\n");
- n_percorsi = 0; //rimetto a 0 il numero di percorsi trovati per un futuro nuovo utilizzo di find
- free(percorso);
- }
- else printf("no\n"); //se la radice non ha figli ovviamente non troverà mai nessuna risorsa la find
- }
- else printf("error\n");
- free(leggi1);
- free(leggi2);
- }
- free(input);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement