Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <locale.h>
- struct Option {
- char* name;
- char* value;
- };
- struct Action {
- char* name;
- char* url;
- struct Option* options;
- };
- struct Task {
- char* name;
- int hours;
- int minutes;
- int seconds;
- char** websites;
- int nbWebsites;
- struct Action* actions;
- };
- /*char* trim(char* string){
- char* newPhrase;
- int i, j = 0;
- int nbCar= strlen(string);
- newPhrase = malloc(nbCar*sizeof(char)+1);
- for(i=0;i<nbCar;i++){
- if(string[i]!= ' ')
- {
- newPhrase[j++] = string[i];
- }
- }
- return newPhrase;
- }*/
- char* trim(char* str) {
- char* trimed = str;
- if (str[strlen(str)-1] == ' ') {
- str[strlen(str)-1] = '\0';
- }
- if (str[0] == ' ') {
- realloc(trimed, sizeof(char) * strlen(trimed) - 1);
- for (int j = 0; j < strlen(trimed); j++) {
- trimed[j] = trimed[j + 1];
- }
- }
- return trimed;
- }
- struct Task* getTree(struct Task* tasks, struct Action* actions, int nbActions, int nbTasks) {
- struct Task* tree = malloc(sizeof(tasks) * 255);
- char* actionName, taskActionName;
- actionName = malloc(sizeof(char)*255);
- taskActionName = malloc(sizeof(char)*255);
- printf("taille taches : %d", nbTasks);
- for (int i = 0; i < nbTasks; i++) {
- for (int k = 0; k < tasks[i].nbWebsites; k++) {
- printf("k : %d\n",tasks[i].nbWebsites);
- taskActionName = tasks[0].websites[1];
- printf("LOL %s\n", tasks[i].websites[k]);
- for (int j = 0; j < nbActions; j++) {
- actionName = actions[j].name;
- printf("[Comparer ? %s : %s]\n", tasks[i].websites[k], actions[j].name);
- if (strncmp(tasks[i].websites[k], actions[j].name, strlen(tasks[i].websites[k]))==0) {
- printf("Comparaison ok\n");
- memcpy(&tree[i], &tasks[i], 255);
- memcpy(&tree[i].actions[j], &actions[j], 255);
- /*tree[i] = tasks[i];
- tree[i].actions[j] = actions[j];*/
- }
- }
- }
- }
- return tree;
- }
- /*void simplifyResult(int nbActions, int nbTasks, struct Action* actions, struct Task* tasks){
- for(int i=0; i<nbTasks; i++){
- }
- }*/
- int main() {
- setlocale(LC_ALL, "fr_FR");
- FILE* file = fopen("test.txt", "r");
- char firstChar;
- char* line = malloc(sizeof(char)*255);
- size_t len = 0;
- int read;
- struct Action* actions;
- actions = malloc(sizeof(struct Action) * 255);
- struct Task* tasks;
- tasks = malloc(sizeof(struct Task) * 255);
- if(!file){
- printf("not found");
- return 1;
- }
- int nbActions = 0;
- int nbTasks = 0;
- while((fgets(line, 255, file)) != NULL) {
- firstChar = line[0];
- switch(firstChar) {
- case '#': break;
- case '=':
- if (line[1] == '=') {
- //Début déclaration tâche
- struct Task task;
- task.hours = 0;
- task.minutes = 0;
- task.seconds = 0;
- char* newLine = malloc(sizeof(char)*255);
- char* buffer = malloc(sizeof(char) * 255);
- fgets(newLine, 255, file);
- while(1){
- if(newLine[0]=='\n' || newLine[0]=='#'){
- fgets(newLine, 255, file);
- }
- else {
- break;
- }
- }
- int depart = 0;
- while(newLine[depart] != '>') {
- depart -= -1;
- }
- int fin = 0;
- while(newLine[fin] != '}') {
- fin -= -1;
- }
- strncat(buffer, &newLine[depart + 1], (fin - depart - 1));
- task.name = trim(buffer);
- fgets(newLine, 255, file);
- while(1){
- if(newLine[0]=='\n' || newLine[0]=='#'){
- } else if(newLine[0]=='+'){
- break;
- }
- else {
- char* newBuffer = malloc(sizeof(char)*255);
- int depart = 0;
- while(newLine[depart] != '{') {
- depart -= -1;
- }
- int fin = 0;
- while(newLine[fin] != '-') {
- fin -= -1;
- }
- strncat(newBuffer, &newLine[depart + 1], (fin - depart - 1));
- char* taskTimeName = malloc(sizeof(char)*strlen(trim(newBuffer))-1);
- taskTimeName = trim(newBuffer);
- char* timeBuffer = malloc(sizeof(char)*255);
- int start = 0;
- int end = 0;
- while(newLine[start] != '>') {
- start -= -1;
- }
- while(newLine[end] != '}') {
- end -= -1;
- }
- strncat(timeBuffer, &newLine[start + 2], (end - start - 2));
- if(taskTimeName[0]=='s'){
- task.seconds = atoi(timeBuffer);
- } else if(taskTimeName[0]=='m') {
- task.minutes = atoi(timeBuffer);
- } else if(taskTimeName[0]=='h') {
- task.hours = atoi(timeBuffer);
- }
- }
- fgets(newLine, 255, file);
- }
- fgets(newLine, 255, file);
- while(newLine[0]=='\n' || newLine[0]=='#'){
- fgets(newLine, 255, file);
- }
- int multiple = 0;
- int posComma = 1;
- char actualCarac;
- int index=0;
- for(int i=0; i<strlen(newLine); i++){
- if(newLine[i] ==','){
- posComma=i;
- multiple=1;
- }
- }
- char** websites = malloc(sizeof(char*)*255);
- int sizeWebsites=0;
- if(multiple==0){
- char* websiteBuffer = malloc(sizeof(char)*255);
- strncat(websiteBuffer, &newLine[1], strlen(newLine)-3);
- websites[sizeWebsites] = websiteBuffer;
- sizeWebsites++;
- } else {
- char* websiteBuffer = malloc(sizeof(char)*255);
- strncat(websiteBuffer, &newLine[1], posComma-1);
- websites[sizeWebsites] = trim(websiteBuffer);
- sizeWebsites++;
- char* websiteBuffer2 = malloc(sizeof(char)*255);
- strncat(websiteBuffer2, &newLine[posComma+1], (strlen(newLine)-1)-posComma-2);
- websites[sizeWebsites] = trim(websiteBuffer2);
- sizeWebsites++;
- }
- task.nbWebsites = sizeWebsites;
- task.websites = websites;
- /*while(newLine[index] !=',' && newLine[index] != '\n'){
- posComma++;
- multiple=1;
- }*/
- tasks[nbTasks] = task;
- nbTasks++;
- } else {
- //Début déclaration action
- //Recuperer le nom du site
- char* newLine = malloc(sizeof(char)*255);
- char* buffer = malloc(sizeof(char) * 255);
- fgets(newLine, 255, file);
- while(newLine[0]=='\n' || newLine[0]=='#'){
- fgets(newLine, 255, file);
- }
- int depart = 0;
- while(newLine[depart] != '>') {
- depart -= -1;
- }
- int fin = 0;
- while(newLine[fin] != '}') {
- fin -= -1;
- }
- strncat(buffer, &newLine[depart + 1], (fin - depart - 1));
- struct Action action;
- action.name = trim(buffer);
- //Recuperer l'url du site
- char* newBuffer = malloc(sizeof(char) * 255);
- fgets(newLine, 255, file);
- while(newLine[0]=='\n' || newLine[0]=='#'){
- fgets(newLine, 255, file);
- }
- depart = 0;
- while(newLine[depart] != '>') {
- depart -= -1;
- }
- fin = 0;
- while(newLine[fin] != '}') {
- fin -= -1;
- }
- strncat(newBuffer, &newLine[depart + 1], (fin - depart - 1));
- action.url = trim(newBuffer);
- actions[nbActions] = action;
- //Récupération des options
- fgets(newLine, 255, file);
- while(newLine[0]=='\n' || newLine[0]=='+' || newLine[0]=='#'){
- fgets(newLine, 255, file);
- }
- int nbOptions = 0;
- actions[nbActions].options = malloc(sizeof(struct Option)*10);
- while(newLine[0]=='{'){
- //Début déclaration option
- char* bufferOption = malloc(sizeof(char) * 255);
- char* bufferOptionName = malloc(sizeof(char) * 255);
- struct Option option;
- int depart2 = 0;
- while(newLine[depart2] != '{') {
- depart2 -= -1;
- }
- int fin2 = 0;
- while(newLine[fin2] != '-') {
- fin2 -= -1;
- }
- strncat(bufferOptionName, &newLine[depart2 + 1], (fin2 - depart2 - 1));
- int depart = 0;
- while(newLine[depart] != '>') {
- depart -= -1;
- }
- int fin = 0;
- while(newLine[fin] != '}') {
- fin -= -1;
- }
- strncat(bufferOption, &newLine[depart + 1], (fin - depart - 1));
- option.value = trim(bufferOption);
- option.name = trim(bufferOptionName);
- fgets(newLine, 255, file);
- actions[nbActions].options[nbOptions] = option;
- nbOptions -= -1;
- }
- nbActions -= -1;
- }
- break;
- default: break;
- }
- }
- //char** websites = malloc(sizeof(char*));
- //websites[0] = trim(actions[0].name);
- //tasks[0].websites = websites;
- struct Task* tree = getTree(tasks, actions, nbActions, nbTasks);
- printf("Action 1 :\n");
- printf(" Nom site : %s\n", actions[0].name);
- printf(" URL site : %s\n", actions[0].url);
- printf(" Option 1 : %s->%s\n", actions[0].options[0].name, actions[0].options[0].value);
- printf(" Option 2 : %s->%s\n", actions[0].options[1].name, actions[0].options[1].value);
- printf(" Option 3 : %s->%s\n", actions[0].options[2].name, actions[0].options[2].value);
- printf("Action 2 :\n");
- printf(" Nom site : %s\n", actions[1].name);
- printf(" URL site : %s\n", actions[1].url);
- printf(" Option 1 : %s->%s\n", actions[1].options[0].name, actions[1].options[0].value);
- printf(" Option 2 : %s->%s\n", actions[1].options[1].name, actions[1].options[1].value);
- printf("Action 3 :\n");
- printf(" Nom site : %s\n", actions[2].name);
- printf(" URL site : %s\n", actions[2].url);
- printf(" Option 1 : %s->%s\n", actions[2].options[0].name, actions[2].options[0].value);
- printf(" Option 2 : %s->%s\n", actions[2].options[1].name, actions[2].options[1].value);
- printf("Tâche 1 :\n");
- printf(" Nom : %s\n", tree[0].name);
- printf(" Temps heures : %d\n", tasks[0].hours);
- printf(" Temps minutes : %d\n", tasks[0].minutes);
- printf(" Temps secondes : %d\n", tasks[0].seconds);
- printf(" Website 1 : %s\n", tree[0].actions[1].name);
- printf(" Website 2 : %s\n", tasks[0].websites[1]);
- printf("Tâche 2 :\n");
- printf(" Nom : %s\n", tasks[1].name);
- printf(" Temps heures : %d\n", tasks[1].hours);
- printf(" Temps minutes : %d\n", tasks[1].minutes);
- printf(" Temps seconds : %d\n", tasks[1].seconds);
- printf(" Websites : %s\n", tasks[1].websites[0]);
- printf("Tâche 3 :\n");
- printf(" Nom : %s\n", tasks[2].name);
- printf(" Temps heures : %d\n", tasks[2].hours);
- printf(" Temps minutes : %d\n", tasks[2].minutes);
- printf(" Temps seconds : %d\n", tasks[2].seconds);
- printf(" Websites : %s\n", tasks[2].websites[0]);
- printf("Nombre d'actions : %d\n", nbActions);
- printf("Nombre de tâches : %d\n", nbTasks);
- //simplifyResult(nbActions, nbTasks, actions, tasks);
- printf(" Website 1 : %s\n", tasks[0].websites[0]);
- fclose(file);
- free(line);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement