Advertisement
Guest User

Untitled

a guest
Nov 26th, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.99 KB | None | 0 0
  1. t_file_lignes* file_ligne_init(char* filename)
  2. {
  3. if(isDir(filename)){
  4. DIR* rep;
  5. char inter_lignes[100][100];
  6. struct dirent *fichier = NULL;
  7. rep = opendir(filename);
  8.  
  9. if (rep == NULL)
  10. {
  11. printf("The folder '%s' could not be opened", filename);
  12. exit(-1);
  13. }
  14. t_file_lignes* file_ligne = malloc(sizeof(t_file_lignes));
  15. file_ligne->lignes = malloc(sizeof(char*)*100);
  16. int j;
  17. file_ligne->nb_lignes = 0;
  18.  
  19. while((fichier = readdir(rep)) != NULL){
  20. if(strcmp(".",fichier->d_name) == 0 || strcmp("..",fichier->d_name) == 0)
  21. continue;
  22. strcpy(inter_lignes[file_ligne->nb_lignes],fichier->d_name);
  23. file_ligne->nb_lignes++;
  24. }
  25.  
  26. for(j = 0; j < file_ligne->nb_lignes*2; j++){
  27. file_ligne->lignes[j]= malloc(sizeof(char)*100);
  28. }
  29. for(j = 0; j < file_ligne->nb_lignes; j++){
  30. strcpy(file_ligne->lignes[j],inter_lignes[j]);
  31. }
  32. closedir(rep);
  33. return file_ligne;
  34. }
  35. else{
  36. FILE* file;
  37. char* readFile = malloc(sizeof(char)*100);
  38. char inter_lignes[100][100];
  39. int i = 0;
  40. ///Initialisation of the struct
  41.  
  42. t_file_lignes* file_ligne;
  43. file_ligne = malloc(sizeof(t_file_lignes));
  44. file_ligne->lignes = malloc(sizeof(char*)*100);
  45. for(i = 0; i<100; i++)
  46. {
  47. file_ligne->lignes[i]=malloc(sizeof(char)*100);
  48. }
  49. file_ligne->max_length = 100;
  50. file_ligne->nb_lignes = 0;
  51.  
  52. ///Initialisation of the lignes value
  53. file = fopen(filename,"r");
  54.  
  55. if(file==NULL)
  56. {
  57. printf("Erreur dans l'ouverture des fichiers %s ", filename);
  58. return NULL;
  59. }
  60.  
  61. readFile = fgets(readFile,100,file);
  62.  
  63. while (readFile != NULL)
  64. {
  65. readFile = rmv_return(readFile);
  66.  
  67. strcpy(inter_lignes[file_ligne->nb_lignes],readFile);
  68.  
  69. file_ligne->nb_lignes++;
  70. readFile = fgets(readFile,100,file);
  71. }
  72. for(i=0; i<file_ligne->nb_lignes; i++)
  73. {
  74. strcpy(file_ligne->lignes[i],inter_lignes[i]);
  75. }
  76. return file_ligne;
  77. }
  78. }
  79.  
  80. void print_struct(t_file_lignes* lines)
  81. {
  82. int i =0;
  83. for(i=0; i<lines->nb_lignes; i++)
  84. {
  85. printf("[%d] --> %s\n",i,lines->lignes[i]);
  86. }
  87.  
  88. }
  89. char *rmv_return(char *string)
  90. {
  91. int i = 0;
  92. for(i = 0; i<strlen(string); i++)
  93. {
  94. if(string[i]=='\n')
  95. {
  96. string[i]='\0';
  97. }
  98. }
  99. return string;
  100. }
  101. t_file_lignes* rmv_end_space(t_file_lignes* file)
  102. {
  103. int i = 0, j = 0, cpt = 0;
  104. printf("\n\n");
  105.  
  106. for(i = 0; i < file->nb_lignes; i++)
  107. {
  108. for(j = strlen(file->lignes[i]); j == 0; j--)
  109. {
  110. if(file->lignes[i][j]==' ' && cpt == 0)
  111. {
  112. file->lignes[i][j] = '\0';
  113. }
  114. else cpt = 1;
  115. }
  116. }
  117. print_struct(file);
  118. return file;
  119. }
  120. t_file_lignes* rmv_all_space(t_file_lignes* file)
  121. {
  122. int i = 0, j = 0, cpt = 0;
  123. char** strTemp = NULL;
  124. strTemp = malloc(sizeof(char*)*100);
  125. for(i=0;i<100;i++)
  126. {
  127. strTemp[i]=malloc(sizeof(char)*100);
  128. }
  129. printf("\n\n");
  130.  
  131. for(i = 0; i < file->nb_lignes; i++)
  132. {
  133. for(j = 0; j < strlen(file->lignes[i]); j++)
  134. {
  135. if(file->lignes[i][j+cpt]!=' ')
  136. {
  137. strTemp[i][j]=file->lignes[i][j+cpt];
  138. }
  139. else
  140. {
  141. cpt++;
  142. strTemp[i][j]=file->lignes[i][j+cpt];
  143. }
  144.  
  145. }
  146. cpt = 0;
  147. strTemp[i][j] = '\0';
  148. }
  149. file->lignes = strTemp;
  150. print_struct(file);
  151. return file;
  152. }
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159. /** \brief
  160. *
  161. * \param value char*
  162. * \return t_my_file*
  163. *
  164. */
  165. t_my_file* my_list_create(char* value){
  166. t_my_file* list = (t_my_file*)malloc(sizeof(t_my_file));
  167. list->lignes = value;
  168. list->next = NULL;
  169. return list;
  170. }
  171.  
  172. t_my_file* list_append(t_my_file* list, char* value){
  173. t_my_file* temps = list,
  174. *mode = my_list_create(value);
  175. if(list->next != NULL){
  176. while(temps->next != NULL){
  177. temps = temps->next;
  178. }
  179. temps->next = mode;
  180. }
  181. else
  182. temps->next = mode;
  183.  
  184. return temps;
  185. }
  186.  
  187. int count_list(t_my_file* list){
  188. int count = 0;
  189. if(list)
  190. {
  191. while(list->next != NULL){
  192. list =list->next;
  193. count++;
  194. }
  195. }else
  196. printf("no list\n");
  197.  
  198. return count;
  199. }
  200. /** \brief
  201. *
  202. * \param s char*
  203. * \return int
  204. *
  205. */
  206. int isDir(char* s){
  207. if ((strchr(s, '.')) == NULL) /* Si le nom du chemin n'a pas de point (une extension). */
  208. return 1;
  209. else
  210. return 0;
  211. }
  212.  
  213. /** \brief
  214. *
  215. * \param s char*
  216. * \param rep DIR*
  217. * \return t_my_file*
  218. *
  219. */
  220. t_my_file* readFolder(char* s, DIR* rep){
  221. struct dirent *fichier = NULL;
  222. t_my_file* folder = my_list_create("init");
  223.  
  224.  
  225. while((fichier = readdir(rep)) != NULL){
  226. if(strcmp(".",fichier->d_name) == 0 || strcmp("..",fichier->d_name) == 0)
  227. continue;
  228. printf("%s\n", fichier->d_name);
  229. folder = list_append(folder,fichier->d_name);
  230. folder->next = folder;
  231. }
  232. /*while ((ent = readdir(rep)) != NULL){
  233. //printf("%s,\n", ent->d_name);
  234. fileName = ent->d_name;
  235. chaine[1] = fileName;
  236. printf(fileName);
  237. printf("\n\n");
  238. folder = list_append(folder,fileName);
  239. fileName = NULL;
  240. }*/
  241. return folder;
  242. }
  243.  
  244. /** \brief
  245. *
  246. * \param chemin1 char*
  247. * \param chemin2 char*
  248. * \return void
  249. *
  250. */
  251. void browseFolder(char* chemin1, char* chemin2){
  252.  
  253. t_my_file* folder1 = (t_my_file*)malloc(sizeof(t_my_file));
  254. t_my_file* folder2 = (t_my_file*)malloc(sizeof(t_my_file));
  255. DIR* rep = NULL;
  256. rep = opendir(chemin1);
  257.  
  258. if (rep == NULL)
  259. {
  260. printf("The folder '%s' could not be opened", chemin1);
  261. exit(-1);
  262. }
  263.  
  264. folder1 = readFolder(chemin1, rep);
  265. closedir(rep);
  266.  
  267. rep = opendir(chemin2);
  268.  
  269. if (rep == NULL)
  270. {
  271. printf("The folder '%s' could not be opened", chemin2);
  272. exit(-1);
  273. }
  274.  
  275. // t_my_file* folder2 = readFolder(chemin2, rep);
  276. folder2 = folder1;
  277. closedir(rep);
  278. list_display(folder1);
  279. //diff(folder1,folder2);
  280. }
  281.  
  282. t_my_file* browseFile(char* file1){
  283. t_my_file* file = my_list_create("init");
  284. FILE* myFile1 = NULL;
  285. myFile1 = fopen(file1, "r");
  286.  
  287. if(myFile1 == NULL){
  288. exit(EXIT_FAILURE);
  289. }
  290.  
  291. char line[1024];
  292. while(fgets(line, 1024, myFile1))
  293. {
  294. char* tmp = strdup(line);
  295. printf("%s///", tmp);
  296. file = list_append(file, tmp);
  297. file = list_append(file, "rdrdr");
  298. free(tmp);
  299. }
  300. fclose(myFile1);
  301. return file;
  302. }
  303.  
  304. /** \brief
  305. *
  306. * \param list t_my_folder*
  307. * \return void
  308. *
  309. */
  310. void list_display(t_my_file* list){
  311. if (list)
  312. {
  313. while(list->next != NULL){
  314. printf("%s, ", list->lignes);
  315. list = list->next;
  316. }
  317. printf("%s\n", list->lignes);
  318. }else
  319. printf("no list\n");
  320. }
  321.  
  322. void diff(t_my_file* list1, t_my_file* list2){
  323. int length = (count_list(list1)>count_list(list2))?count_list(list1):count_list(list2);
  324. int lista, listb, i = 0;
  325. for(; i < length; i++){
  326. if(list1 && list2){
  327. if(strcmp(list1->lignes,list2->lignes) == 0){
  328. continue;
  329. }
  330. else if(strcmp(list1->lignes,list2->lignes) == 1){
  331. printf("%dc%d\n",lista, listb);
  332. printf("< %s >> %s\n",list1->lignes);
  333. printf("--- \n");
  334. printf("> %s >> %s\n",list2->lignes);
  335. }
  336. else{
  337. printf("%dc%d\n",lista, listb);
  338. printf("> %s >> %s\n",list1->lignes);
  339. printf("--- \n");
  340. printf("< %s >> %s\n",list2->lignes);
  341. }
  342. }
  343. }
  344. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement