Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <locale.h>
  5.  
  6. struct Option {
  7.     char* name;
  8.     char* value;
  9. };
  10.  
  11. struct Action {
  12.     char* name;
  13.     char* url;
  14.     struct Option* options;
  15. };
  16.  
  17. struct Task {
  18.     char* name;
  19.     int hours;
  20.     int minutes;
  21.     int seconds;
  22.     char** websites;
  23.     int nbWebsites;
  24.     struct Action* actions;
  25. };
  26.  
  27. /*char* trim(char* string){
  28.     char* newPhrase;
  29.     int i, j = 0;
  30.     int nbCar= strlen(string);
  31.  
  32.     newPhrase = malloc(nbCar*sizeof(char)+1);
  33.  
  34.     for(i=0;i<nbCar;i++){
  35.         if(string[i]!= ' ')
  36.         {
  37.             newPhrase[j++] = string[i];
  38.         }
  39.  
  40.     }
  41.  
  42.     return newPhrase;
  43. }*/
  44.  
  45. char* trim(char* str) {
  46.     char* trimed = str;
  47.    
  48.     if (str[strlen(str)-1] == ' ') {
  49.         str[strlen(str)-1] = '\0';
  50.     }
  51.    
  52.     if (str[0] == ' ') {
  53.         realloc(trimed, sizeof(char) * strlen(trimed) - 1);
  54.         for (int j = 0; j < strlen(trimed); j++) {
  55.             trimed[j] = trimed[j + 1];
  56.         }
  57.     }
  58.    
  59.     return trimed;
  60. }
  61.  
  62. struct Task* getTree(struct Task* tasks, struct Action* actions, int nbActions, int nbTasks) {
  63.     struct Task* tree = malloc(sizeof(tasks) * 255);
  64.     char* actionName, taskActionName;
  65.     actionName = malloc(sizeof(char)*255);
  66.     taskActionName = malloc(sizeof(char)*255);
  67.    
  68.     printf("taille taches : %d", nbTasks);
  69.    
  70.     for (int i = 0; i < nbTasks; i++) {
  71.         for (int k = 0; k < tasks[i].nbWebsites; k++) {
  72.             printf("k : %d\n",tasks[i].nbWebsites);
  73.             taskActionName = tasks[0].websites[1];
  74.             printf("LOL %s\n", tasks[i].websites[k]);
  75.             for (int j = 0; j < nbActions; j++) {
  76.                 actionName = actions[j].name;
  77.                 printf("[Comparer ? %s : %s]\n", tasks[i].websites[k], actions[j].name);
  78.                 if (strncmp(tasks[i].websites[k], actions[j].name, strlen(tasks[i].websites[k]))==0) {
  79.                     printf("Comparaison ok\n");
  80.                     memcpy(&tree[i], &tasks[i], 255);
  81.                     memcpy(&tree[i].actions[j], &actions[j], 255);
  82.                     /*tree[i] = tasks[i];
  83.                     tree[i].actions[j] = actions[j];*/
  84.                 }
  85.             }
  86.         }
  87.     }
  88.    
  89.     return tree;
  90. }
  91.  
  92. /*void simplifyResult(int nbActions, int nbTasks, struct Action* actions, struct Task* tasks){
  93.     for(int i=0; i<nbTasks; i++){
  94.        
  95.     }
  96. }*/
  97.  
  98. int main() {
  99.     setlocale(LC_ALL, "fr_FR");
  100.     FILE* file = fopen("test.txt", "r");
  101.  
  102.     char firstChar;
  103.     char* line = malloc(sizeof(char)*255);
  104.     size_t len = 0;
  105.     int read;
  106.  
  107.     struct Action* actions;
  108.     actions = malloc(sizeof(struct Action) * 255);
  109.    
  110.     struct Task* tasks;
  111.     tasks = malloc(sizeof(struct Task) * 255);
  112.  
  113.     if(!file){
  114.         printf("not found");
  115.         return 1;
  116.     }
  117.  
  118.     int nbActions = 0;
  119.     int nbTasks = 0;
  120.  
  121.     while((fgets(line, 255, file)) != NULL) {
  122.         firstChar = line[0];
  123.  
  124.         switch(firstChar) {
  125.             case '#': break;
  126.             case '=':
  127.                 if (line[1] == '=') {
  128.                     //Début déclaration tâche
  129.                    
  130.                     struct Task task;
  131.                     task.hours = 0;
  132.                     task.minutes = 0;
  133.                     task.seconds = 0;
  134.                    
  135.                     char* newLine = malloc(sizeof(char)*255);
  136.                     char* buffer = malloc(sizeof(char) * 255);
  137.                    
  138.                     fgets(newLine, 255, file);
  139.                    
  140.                     while(1){
  141.                         if(newLine[0]=='\n' || newLine[0]=='#'){
  142.                             fgets(newLine, 255, file);
  143.                         }
  144.                         else {
  145.                             break;
  146.                         }
  147.                     }
  148.                    
  149.                     int depart = 0;
  150.                     while(newLine[depart] != '>') {
  151.                         depart -= -1;
  152.                     }
  153.  
  154.                     int fin = 0;
  155.                     while(newLine[fin] != '}') {
  156.                         fin -= -1;
  157.                     }
  158.  
  159.                     strncat(buffer, &newLine[depart + 1], (fin - depart - 1));
  160.                    
  161.                     task.name = trim(buffer);
  162.                    
  163.                     fgets(newLine, 255, file);
  164.                    
  165.                     while(1){
  166.                         if(newLine[0]=='\n' || newLine[0]=='#'){
  167.                            
  168.                         } else if(newLine[0]=='+'){
  169.                             break;
  170.                         }
  171.                         else {
  172.                            
  173.                             char* newBuffer = malloc(sizeof(char)*255);
  174.                            
  175.                             int depart = 0;
  176.                             while(newLine[depart] != '{') {
  177.                                 depart -= -1;
  178.                             }
  179.          
  180.                             int fin = 0;
  181.                             while(newLine[fin] != '-') {
  182.                                 fin -= -1;
  183.                             }
  184.                            
  185.                             strncat(newBuffer, &newLine[depart + 1], (fin - depart - 1));
  186.                            
  187.                             char* taskTimeName = malloc(sizeof(char)*strlen(trim(newBuffer))-1);
  188.                             taskTimeName = trim(newBuffer);
  189.                            
  190.                             char* timeBuffer = malloc(sizeof(char)*255);
  191.                             int start = 0;
  192.                             int end = 0;
  193.                            
  194.                             while(newLine[start] != '>') {
  195.                                 start -= -1;
  196.                             }
  197.                      
  198.                             while(newLine[end] != '}') {
  199.                                 end -= -1;
  200.                             }
  201.                                    
  202.                             strncat(timeBuffer, &newLine[start + 2], (end - start - 2));
  203.                            
  204.                             if(taskTimeName[0]=='s'){
  205.                                 task.seconds = atoi(timeBuffer);
  206.                             } else if(taskTimeName[0]=='m') {
  207.                                 task.minutes = atoi(timeBuffer);
  208.                             } else if(taskTimeName[0]=='h') {
  209.                                 task.hours = atoi(timeBuffer);
  210.                             }
  211.                            
  212.                         }
  213.                         fgets(newLine, 255, file);
  214.                        
  215.                     }
  216.                    
  217.                     fgets(newLine, 255, file);
  218.                     while(newLine[0]=='\n' || newLine[0]=='#'){
  219.                         fgets(newLine, 255, file);
  220.                     }
  221.                    
  222.                     int multiple = 0;
  223.                     int posComma = 1;
  224.                     char actualCarac;
  225.                     int index=0;
  226.                    
  227.                     for(int i=0; i<strlen(newLine); i++){
  228.                         if(newLine[i] ==','){
  229.                             posComma=i;
  230.                             multiple=1;
  231.                         }
  232.                     }
  233.                    
  234.                     char** websites = malloc(sizeof(char*)*255);
  235.                     int sizeWebsites=0;
  236.                    
  237.                     if(multiple==0){
  238.                         char* websiteBuffer = malloc(sizeof(char)*255);
  239.  
  240.                         strncat(websiteBuffer, &newLine[1], strlen(newLine)-3);
  241.                        
  242.                         websites[sizeWebsites] = websiteBuffer;
  243.                         sizeWebsites++;
  244.                        
  245.                     } else {
  246.                         char* websiteBuffer = malloc(sizeof(char)*255);
  247.                        
  248.                         strncat(websiteBuffer, &newLine[1], posComma-1);
  249.                        
  250.                         websites[sizeWebsites] = trim(websiteBuffer);
  251.                         sizeWebsites++;
  252.                        
  253.                         char* websiteBuffer2 = malloc(sizeof(char)*255);
  254.                        
  255.                         strncat(websiteBuffer2, &newLine[posComma+1], (strlen(newLine)-1)-posComma-2);
  256.  
  257.                         websites[sizeWebsites] = trim(websiteBuffer2);
  258.                         sizeWebsites++;
  259.                     }
  260.                    
  261.                     task.nbWebsites = sizeWebsites;
  262.                     task.websites = websites;
  263.                    
  264.                     /*while(newLine[index] !=',' && newLine[index] != '\n'){
  265.                         posComma++;
  266.                         multiple=1;
  267.                     }*/
  268.                    
  269.                     tasks[nbTasks] = task;
  270.                     nbTasks++;
  271.                    
  272.                 } else {
  273.                     //Début déclaration action
  274.  
  275.                     //Recuperer le nom du site
  276.  
  277.                     char* newLine = malloc(sizeof(char)*255);
  278.                     char* buffer = malloc(sizeof(char) * 255);
  279.                     fgets(newLine, 255, file);
  280.                     while(newLine[0]=='\n' || newLine[0]=='#'){
  281.                         fgets(newLine, 255, file);
  282.                     }
  283.  
  284.                     int depart = 0;
  285.                     while(newLine[depart] != '>') {
  286.                         depart -= -1;
  287.                     }
  288.  
  289.                     int fin = 0;
  290.                     while(newLine[fin] != '}') {
  291.                         fin -= -1;
  292.                     }
  293.  
  294.                     strncat(buffer, &newLine[depart + 1], (fin - depart - 1));
  295.  
  296.                     struct Action action;
  297.                     action.name = trim(buffer);
  298.  
  299.                     //Recuperer l'url du site
  300.  
  301.                     char* newBuffer = malloc(sizeof(char) * 255);
  302.  
  303.                     fgets(newLine, 255, file);
  304.                     while(newLine[0]=='\n' || newLine[0]=='#'){
  305.                         fgets(newLine, 255, file);
  306.                     }
  307.  
  308.                     depart = 0;
  309.                     while(newLine[depart] != '>') {
  310.                         depart -= -1;
  311.                     }
  312.  
  313.                     fin = 0;
  314.                     while(newLine[fin] != '}') {
  315.                         fin -= -1;
  316.                     }
  317.  
  318.                     strncat(newBuffer, &newLine[depart + 1], (fin - depart - 1));
  319.  
  320.                     action.url = trim(newBuffer);
  321.  
  322.                     actions[nbActions] = action;
  323.                    
  324.                     //Récupération des options
  325.                    
  326.                     fgets(newLine, 255, file);
  327.                     while(newLine[0]=='\n' || newLine[0]=='+' || newLine[0]=='#'){
  328.                         fgets(newLine, 255, file);
  329.                     }
  330.                    
  331.                     int nbOptions = 0;
  332.                    
  333.                     actions[nbActions].options = malloc(sizeof(struct Option)*10);
  334.                    
  335.                     while(newLine[0]=='{'){
  336.                         //Début déclaration option
  337.                        
  338.                         char* bufferOption = malloc(sizeof(char) * 255);
  339.                         char* bufferOptionName = malloc(sizeof(char) * 255);
  340.                        
  341.                         struct Option option;
  342.                        
  343.                         int depart2 = 0;
  344.                         while(newLine[depart2] != '{') {
  345.                             depart2 -= -1;
  346.                         }
  347.      
  348.                         int fin2 = 0;
  349.                         while(newLine[fin2] != '-') {
  350.                             fin2 -= -1;
  351.                         }
  352.                        
  353.                         strncat(bufferOptionName, &newLine[depart2 + 1], (fin2 - depart2 - 1));
  354.                        
  355.                         int depart = 0;
  356.                         while(newLine[depart] != '>') {
  357.                             depart -= -1;
  358.                         }
  359.      
  360.                         int fin = 0;
  361.                         while(newLine[fin] != '}') {
  362.                             fin -= -1;
  363.                         }
  364.                        
  365.                         strncat(bufferOption, &newLine[depart + 1], (fin - depart - 1));
  366.                        
  367.                         option.value = trim(bufferOption);
  368.                         option.name = trim(bufferOptionName);
  369.                        
  370.                         fgets(newLine, 255, file);
  371.                        
  372.                         actions[nbActions].options[nbOptions] = option;
  373.                        
  374.                         nbOptions -= -1;
  375.                        
  376.                     }
  377.                    
  378.                     nbActions -= -1;
  379.  
  380.                 }
  381.                 break;
  382.             default: break;
  383.         }
  384.  
  385.     }
  386.    
  387.     //char** websites = malloc(sizeof(char*));
  388.     //websites[0] = trim(actions[0].name);
  389.    
  390.     //tasks[0].websites = websites;
  391.    
  392.     struct Task* tree = getTree(tasks, actions, nbActions, nbTasks);
  393.    
  394.     printf("Action 1 :\n");
  395.     printf("    Nom site : %s\n", actions[0].name);
  396.     printf("    URL site : %s\n", actions[0].url);
  397.     printf("    Option 1 : %s->%s\n", actions[0].options[0].name, actions[0].options[0].value);
  398.     printf("    Option 2 : %s->%s\n", actions[0].options[1].name, actions[0].options[1].value);
  399.     printf("    Option 3 : %s->%s\n", actions[0].options[2].name, actions[0].options[2].value);
  400.  
  401.     printf("Action 2 :\n");
  402.     printf("    Nom site : %s\n", actions[1].name);
  403.     printf("    URL site : %s\n", actions[1].url);
  404.     printf("    Option 1 : %s->%s\n", actions[1].options[0].name, actions[1].options[0].value);
  405.     printf("    Option 2 : %s->%s\n", actions[1].options[1].name, actions[1].options[1].value);
  406.    
  407.     printf("Action 3 :\n");
  408.     printf("    Nom site : %s\n", actions[2].name);
  409.     printf("    URL site : %s\n", actions[2].url);
  410.     printf("    Option 1 : %s->%s\n", actions[2].options[0].name, actions[2].options[0].value);
  411.     printf("    Option 2 : %s->%s\n", actions[2].options[1].name, actions[2].options[1].value);
  412.    
  413.     printf("Tâche 1 :\n");
  414.     printf("    Nom : %s\n", tree[0].name);
  415.     printf("    Temps heures : %d\n", tasks[0].hours);
  416.     printf("    Temps minutes : %d\n", tasks[0].minutes);
  417.     printf("    Temps secondes : %d\n", tasks[0].seconds);
  418.     printf("    Website 1 : %s\n", tree[0].actions[1].name);
  419.     printf("    Website 2 : %s\n", tasks[0].websites[1]);
  420.    
  421.     printf("Tâche 2 :\n");
  422.     printf("    Nom : %s\n", tasks[1].name);
  423.     printf("    Temps heures : %d\n", tasks[1].hours);
  424.     printf("    Temps minutes : %d\n", tasks[1].minutes);
  425.     printf("    Temps seconds : %d\n", tasks[1].seconds);
  426.     printf("    Websites : %s\n", tasks[1].websites[0]);
  427.    
  428.     printf("Tâche 3 :\n");
  429.     printf("    Nom : %s\n", tasks[2].name);
  430.     printf("    Temps heures : %d\n", tasks[2].hours);
  431.     printf("    Temps minutes : %d\n", tasks[2].minutes);
  432.     printf("    Temps seconds : %d\n", tasks[2].seconds);
  433.     printf("    Websites : %s\n", tasks[2].websites[0]);
  434.    
  435.     printf("Nombre d'actions : %d\n", nbActions);
  436.     printf("Nombre de tâches : %d\n", nbTasks);
  437.    
  438.     //simplifyResult(nbActions, nbTasks, actions, tasks);
  439.    
  440.     printf("    Website 1 : %s\n", tasks[0].websites[0]);
  441.    
  442.     fclose(file);
  443.     free(line);
  444.  
  445.     return 0;
  446. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement