void diffBetweenFiles(char * path1, char * path2, int indent_limit) { // Controllo prima se i file sono diversi e poi calcolo le differenze.. if(are_equals(path1, path2) != 0) { str_file * file1; str_file * file2; file1 = (str_file *) malloc(sizeof(str_file)); file2 = (str_file *) malloc(sizeof(str_file)); // Copio i due percorsi passati per parametro nel campo path delle rispettive strutture precedentemente dichiarate.. file1->path = (char*)malloc(sizeof(char) * strlen(path1)); strcpy(file1->path, path1); file2->path = (char*)malloc(sizeof(char) * strlen(path2)); strcpy(file2->path, path2); printf("%s\n", file1->path); printf("%s\n", file2->path); file1->file = fopen(file1->path,"r"); if (file1->file == NULL) { // Impossibile aprire path1 perror(file1->path); exit(EXIT_FAILURE); } file2->file = fopen(file2->path,"r"); if (file2->file == NULL) { // Impossibile aprire path2 perror(file2->path); exit(EXIT_FAILURE); } // Obtain file1 dimension in bytes fseek(file1->file, 0, SEEK_END); file1->size = ftell(file1->file); file1->line = (char *) malloc(file1->size); fseek(file1->file, 0, SEEK_SET); // Return to the top of the file // Obtain file2 dimension in bytes fseek(file2->file, 0, SEEK_END); file2->size = ftell(file2->file); file2->line = (char *) malloc(file2->size); fseek(file2->file, 0, SEEK_SET); // Return to the top of the file /* legge e stampa ogni riga */ char buf_file1[128]; char buf_file2[128]; // File con contenuto diverso.. int indent_tab; for(indent_tab = 0; indent_tab < indent_limit; indent_tab++) { printf(" "); } printf ("Differences between files: %s - %s\n", path1, path2); // Muovo i buffer utilizzati per il controllo in maniera parallela while(fgets(buf_file1, 128, file1->file) != NULL && fgets(buf_file2, 128, file2->file)) { if(strcmp(buf_file1, buf_file2) != 0) { for(indent_tab = 0; indent_tab < indent_limit + 1; indent_tab++) { printf(" "); } printf("+ %s", buf_file1); for(indent_tab = 0; indent_tab < indent_limit + 1; indent_tab++) { printf(" "); } printf("- %s", buf_file2); //printf("Differences in %s - %s", buf_file1, buf_file2); } } // Sarò arrivato alla fine dei uno dei due files.. // Stampo la differenza solo di uno rispetto all'altro.. while(fgets(buf_file1, 128, file1->file) != NULL) { for(indent_tab = 0; indent_tab < indent_limit + 1; indent_tab++) { printf(" "); } printf("+ %s", buf_file1); } if(feof(file1->file)) { printf("finito file 1\n"); } else if(ferror(file1->file)) { printf("error 1\n"); } while(fgets(buf_file2, 128, file2->file) != NULL) { for(indent_tab = 0; indent_tab < indent_limit + 1; indent_tab++) { printf(" "); } printf("- %s", buf_file2); } if(feof(file2->file)) { printf("finito file 2\n"); } else if(ferror(file2->file)) { printf("error 2\n"); } //long int current_seek1 = 0; //long int current_seek2 = 0; //long int prec_seek2 = 0; //printf("size1 %d\n", file1->size); //printf("size2 %d\n", file2->size); // Leggo una riga dal primo file.. /* while(fgets(buf_file1, file1->size, file1->file) != NULL && current_seek2 != file2->size) { // Ottengo la posizione corrente che sto analizzando nel primo file.. //current_seek1 = ftell(file1->file); prec_seek2 = current_seek2; //printf("file 1 leggo %s\n", buf_file1); //fseek(file2->file, 0, SEEK_SET); // Return to the top of the file while(fgets(buf_file2, file2->size, file2->file) != NULL && strcmp(buf_file1, buf_file2) != 0) { // printf("file 2 leggo %s\n", buf_file2); // Do nothing } // Ottengo la posizione corrente che sto analizzando nel secondo file.. current_seek2 = ftell(file2->file); // REMOVE after test.. /* if(strcmp(buf_file1, buf_file2) != 0) { printf("uguali %ssono a %li in file1 e %li in file2", buf_file1, ftell(file1->file), current_seek2); } */ // Se ho raggiunto la fine del secondo file.. /* if (current_seek2 == file2->size) { //printf("fine\n"); printf("+ %s", buf_file1); } else { fseek(file2->file, prec_seek2, SEEK_SET); while(fgets(buf_file2, file2->size, file2->file) != NULL && ftell(file2->file) != current_seek2) { printf("- %s", buf_file2); } } } // Se sono arrivato alla fine del secondo file.. // Leggo tutto il file 1 dalla pos corrente fino alla fine aggiungendo un + while(fgets(buf_file1, file1->size, file1->file) != NULL) { printf("+ %s", buf_file1); } //file1->read = fread(file1->line, file1->size, 1, file1->file); //printf("\nfile1:\n%s\n----------------------------\n\n\nora bello\n\n", file1->line); */ // Read the entire file1 and file2 //file1->read = fread(file1->line, file1->size, 1, file1->file); //file2->read = fread(file2->line, file2->size, 1, file2->file); //printf("\nfile1:\n%s\n----------------------------", file1->line); //printf("\nfile2:\n%s\n----------------------------", file2->line); /* char buffer[1]; while (fread(buffer, sizeof buffer, 1, file1->file) == 1) { printf("%d\n", buffer[0]); } if (feof(file1->file)) { printf("finito\n"); } else { printf("errorreeeeeeeee\n"); } */ /* if(strcmp(file1->line , file2->line) == 0) { printIndented("I files sono uguali!", indent_limit); } else { printIndented("I files sono diversi!", indent_limit); } */ //printf("------------------- fine ------\n"); fclose(file1->file); fclose(file2->file); } }
int main(int argc, char* argv[]){ /*Matrix lucien = CreateNewMatrix(4,7); init_Hamming(lucien); int tmat[1][4] = {1, 0, 0, 1};*/ int hmat[3][7] = { {0,0,0,1,1,1,1}, {0,1,1,0,0,1,1}, {1,0,1,0,1,0,1} }; Matrix hmatrix = convert(3, 7, hmat); /*Matrix puissant = convert(1, 4, tmat); Matrix hmatrix = convert(3, 7, hmat); Matrix result = times_Matrix(puissant, lucien); Matrix decod = decode(result); Matrix error = correct(result, hmatrix);*/ double p = 0.001; int i; int n; for(i = 1; i < 500; i++){ p = (double)i / (double)1000; printf("\n"); printf("Start execution with %.4f\n",p); int success, fail, total; double proba; success = 0; fail = 0; proba = 0; total = 0; for(n = 0; n <= 500; n++){ Matrix randMatrix = random_Matrix(1,4); /*printf("Random message is:\n"); printm(randMatrix);*/ Matrix c = encode (randMatrix); /*printf("Encoded message is:\n"); printm(c);*/ Matrix c2 = noise(c, p); /*printf("Noised message is:\n"); printm(c2);*/ Matrix cor = correct(c2, hmatrix); /*printf("Corrected message is:\n"); printm(cor);*/ Matrix deco = decode(cor); /*printf("Decoded message is:\n"); printm(deco);*/ if(are_equals(randMatrix, deco) == 1){ /*printf(" M == M'\n");*/ success++; } else{ /*printf(" M =\\= M'\n");*/ fail++; } total++; free_matrix(randMatrix); free_matrix(c); free_matrix(c2); free_matrix(cor); free_matrix(deco); } proba = (double)success/(double)total * 100; printf("Proba success is : %.0f\n", proba); proba = (double)fail/(double)total * 100; printf("Proba success is : %.0f\n", proba); printf("\n"); printf("Finish execution\n"); printf("\n"); printf("\n"); } free_matrix(hmatrix); /*printm(lucien); printf("\n"); printm(puissant); printf("\n"); printm(result); printf("\n"); printm(decod); printf("\n"); printm(error); printf("\n"); printm(randMatrix); printf("\n"); free_matrix(lucien); free_matrix(puissant); free_matrix(hmatrix); free_matrix(result); free_matrix(decod); free_matrix(error); free_matrix(randMatrix);*/ return 0; }
int are_equals_directories(char path1[256] , char path2[256]) { struct dirent *dirent1, *dirent2; DIR *dir1, *dir2; struct stat stat1, stat2; int is_dir1, is_dir2; int equal_dir = 0; char completePath1[256]; char completePath2[256]; strcpy(completePath1, path1); strcpy(completePath2, path2); // Verifico se entrambe le directories sono vuote.. if (empty_directories(path1, path2) == 1) { //printf("%s e %s sono vuote\n", path1, path2 ); return 1; } if ((dir1 = opendir(path1)) == NULL) { printf("Cannot open %s\n", path1); syslog(LOG_INFO, "Cannot open %s. Terminated.", path1); exit(EXIT_FAILURE); } while ((dirent1 = readdir(dir1)) != NULL) { if (strcmp(dirent1->d_name, ".") != 0 && strcmp(dirent1->d_name, "..") != 0) { // Concateno il nome del file/directory al path originario per lavorare ricorsivamente sul path completo.. // Serve per tenere traccia del path completo dove si trova il file o la directory che si sta analizzando. //size_t length1 = strlen(path1) + strlen(dirent1->d_name) + 1; //char * completePath1 = (char *) malloc(sizeof(char) * length1); // Se si tratta di una directory aggiungo uno / tra il path e il nome della cartella // if ( is_dir1 ) { snprintf(completePath1, length1 + , "%s%s/", path1, dirent1->d_name); } // else { snprintf(completePath1, length1, "%s%s", path1, dirent1->d_name); } //snprintf(completePath1, length1, "%s%s", path1, dirent1->d_name); strcat(completePath1, dirent1->d_name); // Valuto se si tratta di una sub-directory.. stat(completePath1, &stat1); is_dir1 = ((stat1.st_mode & S_IFMT) == S_IFDIR); /* printf("path1: %s\n", path1); printf("d_name: %s\n", dirent1->d_name); printf("def: %s%s\n", path1, dirent1->d_name); printf("\n"); */ if ((dir2 = opendir(path2)) == NULL) { printf("Cannot open %s\n", path2); syslog(LOG_INFO, "Cannot open %s. Terminated.", path2); exit(EXIT_FAILURE); } equal_dir = 0; // Scorro tutta la seconda directory e controllo se il file è presente while ( !equal_dir && (dirent2 = readdir(dir2)) != NULL) { // Verifico che non siano le entries di default delle directories.. if (strcmp(dirent2->d_name, ".") != 0 && strcmp(dirent2->d_name, "..") != 0) { // Se i files o le directories hanno lo stesso nome.. if(strcmp(dirent1->d_name, dirent2->d_name) == 0) { //printf("analizzando %s\n", dirent2->d_name); // Concateno il nome del file/directory al path originario per lavorare ricorsivamente sul path completo.. // Serve per tenere traccia del path completo dove si trova il file o la directory che si sta analizzando. //size_t length2 = strlen(path2) + strlen(dirent2->d_name) + 1; //char * completePath2 = (char *) malloc(sizeof(char) * length2); // Se si tratta di una directory aggiungo uno / tra il path e il nome della cartella // if ( is_dir1 ) { snprintf(completePath1, length1 + , "%s%s/", path1, dirent1->d_name); } // else { snprintf(completePath1, length1, "%s%s", path1, dirent1->d_name); } //snprintf(completePath2, length2, "%s%s", path2, dirent2->d_name); strcpy(completePath2, path2); // Valuto se si tratta di una sub-directory.. stat(completePath2, &stat2); is_dir2 = ((stat2.st_mode & S_IFMT) == S_IFDIR); // test /* printf("path2: %s\n", path2); printf("d_name2: %s\n", dirent2->d_name); printf("def2: %s%s\n", path2, dirent2->d_name); printf("\n"); */ if (is_dir1 && is_dir2) { strcat(completePath1, "/"); strcat(completePath2, "/"); // Procedo in maniera ricorsiva sulle sub-directory aventi lo stesso nome in entrambi i path.. equal_dir = equal_dir || are_equals_directories(completePath1, completePath2); //printf("equal_dif = %d , dopo equal_directories tra %s e %s\n", equal_dir, completePath1, completePath2 ); } else { equal_dir = equal_dir || are_equals(completePath1, completePath2); } //printf("%d con %s vs %s\n", equal_dir, completePath1, completePath2); //free(completePath2); } } // else { equal_dir = 1; } } if( equal_dir == 0 ) { /* printf("diverse..- %s\n", completePath1); */ return equal_dir; } //free(completePath1); closedir(dir2); } } closedir(dir1); return equal_dir; }
// // tests implementation // void CMathTest::test() { int int_val = -1; long long_val = -1l; float float_val = -1.0f; double double_val = -1.0; #if !defined (_STLP_NO_LONG_DOUBLE) long double long_double_val = -1.0l; #endif CPPUNIT_CHECK( are_equals(std::abs(int_val), -int_val) ); CPPUNIT_CHECK( are_equals(std::abs(long_val), -long_val) ); CPPUNIT_CHECK( are_equals(std::labs(long_val), -long_val) ); CPPUNIT_CHECK( are_equals(std::abs(float_val), -float_val) ); CPPUNIT_CHECK( are_equals(std::abs(double_val), -double_val) ); #if !defined (_STLP_NO_LONG_DOUBLE) CPPUNIT_CHECK( are_equals(std::abs(long_double_val), -long_double_val) ); #endif CPPUNIT_CHECK( are_equals(std::fabs(float_val), -float_val) ); CPPUNIT_CHECK( are_equals(std::fabs(double_val), -double_val) ); #if !defined (_STLP_NO_LONG_DOUBLE) CPPUNIT_CHECK( are_equals(std::fabs(long_double_val), -long_double_val) ); #endif std::div_t div_res = std::div(3, 2); CPPUNIT_CHECK( div_res.quot == 1 ); CPPUNIT_CHECK( div_res.rem == 1 ); std::ldiv_t ldiv_res = std::ldiv(3l, 2l); CPPUNIT_CHECK( ldiv_res.quot == 1l ); CPPUNIT_CHECK( ldiv_res.rem == 1l ); ldiv_res = std::div(3l, 2l); CPPUNIT_CHECK( ldiv_res.quot == 1l ); CPPUNIT_CHECK( ldiv_res.rem == 1l ); std::srand(2); int rand_val = std::rand(); CPPUNIT_CHECK( rand_val >= 0 && rand_val <= RAND_MAX ); CPPUNIT_CHECK( are_equals(std::floor(1.5), 1.0) ); CPPUNIT_CHECK( are_equals(std::ceil(1.5), 2.0) ); CPPUNIT_CHECK( are_equals(std::fmod(1.5, 1.0), 0.5) ); CPPUNIT_CHECK( are_equals(std::sqrt(4.0), 2.0) ); CPPUNIT_CHECK( are_equals(std::pow(2.0, 2), 4.0) ); /* * Uncomment the following to check that it generates an ambiguous call * as there is no Standard pow(int, int) function only pow(double, int), * pow(float, int) and some others... * If it do not generate a compile time error it should at least give * the good result. */ //CPPUNIT_CHECK( are_equals(std::pow(10, -2), 0.01) ); CPPUNIT_CHECK( are_equals(std::pow(10.0, -2), 0.01) ); CPPUNIT_CHECK( are_equals(std::exp(0.0), 1.0) ); CPPUNIT_CHECK( are_equals(std::log(std::exp(1.0)), 1.0) ); CPPUNIT_CHECK( are_equals(std::log10(100.0), 2.0) ); #if !defined (STLPORT) || !defined (_STLP_USING_PLATFORM_SDK_COMPILER) || !defined (_WIN64) CPPUNIT_CHECK( are_equals(std::modf(100.5, &double_val), 0.5) ); CPPUNIT_CHECK( are_equals(double_val, 100.0) ); #endif double_val = std::frexp(8.0, &int_val); CPPUNIT_CHECK( are_equals(double_val * std::pow(2.0, int_val), 8.0) ); CPPUNIT_CHECK( are_equals(std::ldexp(1.0, 2), 4.0) ); CPPUNIT_CHECK( are_equals(std::cos(std::acos(1.0)), 1.0) ); CPPUNIT_CHECK( are_equals(std::sin(std::asin(1.0)), 1.0) ); CPPUNIT_CHECK( are_equals(std::tan(std::atan(1.0)), 1.0) ); CPPUNIT_CHECK( are_equals(std::tan(std::atan2(1.0, 1.0)), 1.0) ); CPPUNIT_CHECK( are_equals(std::cosh(0.0), 1.0) ); CPPUNIT_CHECK( are_equals(std::sinh(0.0), 0.0) ); #if !defined (STLPORT) || !defined (_STLP_USING_PLATFORM_SDK_COMPILER) || !defined (_M_AMD64) CPPUNIT_CHECK( are_equals(std::tanh(0.0), 0.0) ); #endif CPPUNIT_CHECK( are_equals(std::floor(1.5f), 1.0f) ); CPPUNIT_CHECK( are_equals(std::ceil(1.5f), 2.0f) ); CPPUNIT_CHECK( are_equals(std::fmod(1.5f, 1.0f), 0.5f) ); CPPUNIT_CHECK( are_equals(std::sqrt(4.0f), 2.0f) ); CPPUNIT_CHECK( are_equals(std::pow(2.0f, 2), 4.0f) ); CPPUNIT_CHECK( are_equals(std::exp(0.0f), 1.0f) ); CPPUNIT_CHECK( are_equals(std::log(std::exp(1.0f)), 1.0f) ); CPPUNIT_CHECK( are_equals(std::log10(100.0f), 2.0f) ); #if !defined (STLPORT) || !defined (_STLP_USING_PLATFORM_SDK_COMPILER) || !defined (_WIN64) CPPUNIT_CHECK( are_equals(std::modf(100.5f, &float_val), 0.5f) ); CPPUNIT_CHECK( are_equals(float_val, 100.0f) ); #endif float_val = std::frexp(8.0f, &int_val); CPPUNIT_CHECK( are_equals(float_val * std::pow(2.0f, int_val), 8.0f) ); CPPUNIT_CHECK( are_equals(std::ldexp(1.0f, 2), 4.0f) ); CPPUNIT_CHECK( are_equals(std::cos(std::acos(1.0f)), 1.0f) ); CPPUNIT_CHECK( are_equals(std::sin(std::asin(1.0f)), 1.0f) ); CPPUNIT_CHECK( are_equals(std::tan(std::atan(1.0f)), 1.0f) ); CPPUNIT_CHECK( are_equals(std::tan(std::atan2(1.0f, 1.0f)), 1.0f) ); CPPUNIT_CHECK( are_equals(std::cosh(0.0f), 1.0f) ); CPPUNIT_CHECK( are_equals(std::sinh(0.0f), 0.0f) ); #if !defined (STLPORT) || !defined (_STLP_USING_PLATFORM_SDK_COMPILER) || !defined (_M_AMD64) CPPUNIT_CHECK( are_equals(std::tanh(0.0f), 0.0f) ); #endif #if !defined (_STLP_NO_LONG_DOUBLE) CPPUNIT_CHECK( are_equals(std::floor(1.5l), 1.0l) ); CPPUNIT_CHECK( are_equals(std::ceil(1.5l), 2.0l) ); CPPUNIT_CHECK( are_equals(std::fmod(1.5l, 1.0l), 0.5l) ); CPPUNIT_CHECK( are_equals(std::sqrt(4.0l), 2.0l) ); CPPUNIT_CHECK( are_equals(std::pow(2.0l, 2), 4.0l) ); CPPUNIT_CHECK( are_equals(std::exp(0.0l), 1.0l) ); CPPUNIT_CHECK( are_equals(std::log(std::exp(1.0l)), 1.0l) ); CPPUNIT_CHECK( are_equals(std::log10(100.0l), 2.0l) ); # if !defined (STLPORT) || !defined (_STLP_USING_PLATFORM_SDK_COMPILER) || !defined (_WIN64) CPPUNIT_CHECK( are_equals(std::modf(100.5l, &long_double_val), 0.5l) ); CPPUNIT_CHECK( are_equals(long_double_val, 100.0l) ); # endif long_double_val = std::frexp(8.0l, &int_val); CPPUNIT_CHECK( are_equals(long_double_val * std::pow(2.0l, int_val), 8.0l) ); CPPUNIT_CHECK( are_equals(std::ldexp(1.0l, 2), 4.0l) ); CPPUNIT_CHECK( are_equals(std::cos(std::acos(1.0l)), 1.0l) ); CPPUNIT_CHECK( are_equals(std::sin(std::asin(1.0l)), 1.0l) ); CPPUNIT_CHECK( are_equals(std::tan(0.0l), 0.0l) ); CPPUNIT_CHECK( are_equals(std::atan(0.0l), 0.0l) ); CPPUNIT_CHECK( are_equals(std::atan2(0.0l, 1.0l), 0.0l) ); CPPUNIT_CHECK( are_equals(std::cosh(0.0l), 1.0l) ); CPPUNIT_CHECK( are_equals(std::sinh(0.0l), 0.0l) ); # if !defined (STLPORT) || !defined (_STLP_USING_PLATFORM_SDK_COMPILER) || !defined (_M_AMD64) CPPUNIT_CHECK( are_equals(std::tanh(0.0l), 0.0l) ); # endif #endif CPPUNIT_CHECK( are_equals(std::sqrt(std::sqrt(std::sqrt(256.0))), 2.0) ); CPPUNIT_CHECK( are_equals(std::sqrt(std::sqrt(std::sqrt(256.0f))), 2.0f) ); #if !defined (_STLP_NO_LONG_DOUBLE) CPPUNIT_CHECK( are_equals(std::sqrt(std::sqrt(std::sqrt(256.0l))), 2.0l) ); #endif }