Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- t_file_lignes* file_ligne_init(char* filename)
- {
- if(isDir(filename)){
- DIR* rep;
- char inter_lignes[100][100];
- struct dirent *fichier = NULL;
- rep = opendir(filename);
- if (rep == NULL)
- {
- printf("The folder '%s' could not be opened", filename);
- exit(-1);
- }
- t_file_lignes* file_ligne = malloc(sizeof(t_file_lignes));
- file_ligne->lignes = malloc(sizeof(char*)*100);
- int j;
- file_ligne->nb_lignes = 0;
- while((fichier = readdir(rep)) != NULL){
- if(strcmp(".",fichier->d_name) == 0 || strcmp("..",fichier->d_name) == 0)
- continue;
- strcpy(inter_lignes[file_ligne->nb_lignes],fichier->d_name);
- file_ligne->nb_lignes++;
- }
- for(j = 0; j < file_ligne->nb_lignes*2; j++){
- file_ligne->lignes[j]= malloc(sizeof(char)*100);
- }
- for(j = 0; j < file_ligne->nb_lignes; j++){
- strcpy(file_ligne->lignes[j],inter_lignes[j]);
- }
- closedir(rep);
- return file_ligne;
- }
- else{
- FILE* file;
- char* readFile = malloc(sizeof(char)*100);
- char inter_lignes[100][100];
- int i = 0;
- ///Initialisation of the struct
- t_file_lignes* file_ligne;
- file_ligne = malloc(sizeof(t_file_lignes));
- file_ligne->lignes = malloc(sizeof(char*)*100);
- for(i = 0; i<100; i++)
- {
- file_ligne->lignes[i]=malloc(sizeof(char)*100);
- }
- file_ligne->max_length = 100;
- file_ligne->nb_lignes = 0;
- ///Initialisation of the lignes value
- file = fopen(filename,"r");
- if(file==NULL)
- {
- printf("Erreur dans l'ouverture des fichiers %s ", filename);
- return NULL;
- }
- readFile = fgets(readFile,100,file);
- while (readFile != NULL)
- {
- readFile = rmv_return(readFile);
- strcpy(inter_lignes[file_ligne->nb_lignes],readFile);
- file_ligne->nb_lignes++;
- readFile = fgets(readFile,100,file);
- }
- for(i=0; i<file_ligne->nb_lignes; i++)
- {
- strcpy(file_ligne->lignes[i],inter_lignes[i]);
- }
- return file_ligne;
- }
- }
- void print_struct(t_file_lignes* lines)
- {
- int i =0;
- for(i=0; i<lines->nb_lignes; i++)
- {
- printf("[%d] --> %s\n",i,lines->lignes[i]);
- }
- }
- char *rmv_return(char *string)
- {
- int i = 0;
- for(i = 0; i<strlen(string); i++)
- {
- if(string[i]=='\n')
- {
- string[i]='\0';
- }
- }
- return string;
- }
- t_file_lignes* rmv_end_space(t_file_lignes* file)
- {
- int i = 0, j = 0, cpt = 0;
- printf("\n\n");
- for(i = 0; i < file->nb_lignes; i++)
- {
- for(j = strlen(file->lignes[i]); j == 0; j--)
- {
- if(file->lignes[i][j]==' ' && cpt == 0)
- {
- file->lignes[i][j] = '\0';
- }
- else cpt = 1;
- }
- }
- print_struct(file);
- return file;
- }
- t_file_lignes* rmv_all_space(t_file_lignes* file)
- {
- int i = 0, j = 0, cpt = 0;
- char** strTemp = NULL;
- strTemp = malloc(sizeof(char*)*100);
- for(i=0;i<100;i++)
- {
- strTemp[i]=malloc(sizeof(char)*100);
- }
- printf("\n\n");
- for(i = 0; i < file->nb_lignes; i++)
- {
- for(j = 0; j < strlen(file->lignes[i]); j++)
- {
- if(file->lignes[i][j+cpt]!=' ')
- {
- strTemp[i][j]=file->lignes[i][j+cpt];
- }
- else
- {
- cpt++;
- strTemp[i][j]=file->lignes[i][j+cpt];
- }
- }
- cpt = 0;
- strTemp[i][j] = '\0';
- }
- file->lignes = strTemp;
- print_struct(file);
- return file;
- }
- /** \brief
- *
- * \param value char*
- * \return t_my_file*
- *
- */
- t_my_file* my_list_create(char* value){
- t_my_file* list = (t_my_file*)malloc(sizeof(t_my_file));
- list->lignes = value;
- list->next = NULL;
- return list;
- }
- t_my_file* list_append(t_my_file* list, char* value){
- t_my_file* temps = list,
- *mode = my_list_create(value);
- if(list->next != NULL){
- while(temps->next != NULL){
- temps = temps->next;
- }
- temps->next = mode;
- }
- else
- temps->next = mode;
- return temps;
- }
- int count_list(t_my_file* list){
- int count = 0;
- if(list)
- {
- while(list->next != NULL){
- list =list->next;
- count++;
- }
- }else
- printf("no list\n");
- return count;
- }
- /** \brief
- *
- * \param s char*
- * \return int
- *
- */
- int isDir(char* s){
- if ((strchr(s, '.')) == NULL) /* Si le nom du chemin n'a pas de point (une extension). */
- return 1;
- else
- return 0;
- }
- /** \brief
- *
- * \param s char*
- * \param rep DIR*
- * \return t_my_file*
- *
- */
- t_my_file* readFolder(char* s, DIR* rep){
- struct dirent *fichier = NULL;
- t_my_file* folder = my_list_create("init");
- while((fichier = readdir(rep)) != NULL){
- if(strcmp(".",fichier->d_name) == 0 || strcmp("..",fichier->d_name) == 0)
- continue;
- printf("%s\n", fichier->d_name);
- folder = list_append(folder,fichier->d_name);
- folder->next = folder;
- }
- /*while ((ent = readdir(rep)) != NULL){
- //printf("%s,\n", ent->d_name);
- fileName = ent->d_name;
- chaine[1] = fileName;
- printf(fileName);
- printf("\n\n");
- folder = list_append(folder,fileName);
- fileName = NULL;
- }*/
- return folder;
- }
- /** \brief
- *
- * \param chemin1 char*
- * \param chemin2 char*
- * \return void
- *
- */
- void browseFolder(char* chemin1, char* chemin2){
- t_my_file* folder1 = (t_my_file*)malloc(sizeof(t_my_file));
- t_my_file* folder2 = (t_my_file*)malloc(sizeof(t_my_file));
- DIR* rep = NULL;
- rep = opendir(chemin1);
- if (rep == NULL)
- {
- printf("The folder '%s' could not be opened", chemin1);
- exit(-1);
- }
- folder1 = readFolder(chemin1, rep);
- closedir(rep);
- rep = opendir(chemin2);
- if (rep == NULL)
- {
- printf("The folder '%s' could not be opened", chemin2);
- exit(-1);
- }
- // t_my_file* folder2 = readFolder(chemin2, rep);
- folder2 = folder1;
- closedir(rep);
- list_display(folder1);
- //diff(folder1,folder2);
- }
- t_my_file* browseFile(char* file1){
- t_my_file* file = my_list_create("init");
- FILE* myFile1 = NULL;
- myFile1 = fopen(file1, "r");
- if(myFile1 == NULL){
- exit(EXIT_FAILURE);
- }
- char line[1024];
- while(fgets(line, 1024, myFile1))
- {
- char* tmp = strdup(line);
- printf("%s///", tmp);
- file = list_append(file, tmp);
- file = list_append(file, "rdrdr");
- free(tmp);
- }
- fclose(myFile1);
- return file;
- }
- /** \brief
- *
- * \param list t_my_folder*
- * \return void
- *
- */
- void list_display(t_my_file* list){
- if (list)
- {
- while(list->next != NULL){
- printf("%s, ", list->lignes);
- list = list->next;
- }
- printf("%s\n", list->lignes);
- }else
- printf("no list\n");
- }
- void diff(t_my_file* list1, t_my_file* list2){
- int length = (count_list(list1)>count_list(list2))?count_list(list1):count_list(list2);
- int lista, listb, i = 0;
- for(; i < length; i++){
- if(list1 && list2){
- if(strcmp(list1->lignes,list2->lignes) == 0){
- continue;
- }
- else if(strcmp(list1->lignes,list2->lignes) == 1){
- printf("%dc%d\n",lista, listb);
- printf("< %s >> %s\n",list1->lignes);
- printf("--- \n");
- printf("> %s >> %s\n",list2->lignes);
- }
- else{
- printf("%dc%d\n",lista, listb);
- printf("> %s >> %s\n",list1->lignes);
- printf("--- \n");
- printf("< %s >> %s\n",list2->lignes);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement