1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
TypPert *creerSommetsFichierPert(char *fic) {
char ligne_lue[512], /* la ligne qui est lue dans le fichier */
*sommet, /* le sommet en cours de lecture */
*nomTache, /* l intitule de la tache */
*dureeChar, /* la duree de la tache en char */
*pos; /* pour determiner si la ligne lue contient # */
FILE *fichier; /* Pointeur sur le fichier */
int indiceSommet, /* indice du tableau de char 'sommet' */
indiceNomTache, /* indice du tableau de char 'nomTache' */
indiceDureeTache, /* indice du tableau de char 'nomTache' */
i, /* indice de la boucle For */
numSommet, /* numero du sommet en cours */
duree, /* la duree de la tache */
champs; /* le n° du champs qu'on lit dans le fichier (nom, intitule ou duree) */
int caractere,
nb;
char *testeur;
TypPert *pert;
booleen trouve; /* TRUE une fois que l on a lu le sommet sur la ligne */
/* Ouverture du fichier en lecture */
fichier = fopen(fic, "r");
if (fichier != NULL) {
pert = creerPert(nbSommetsFichierPert(fic));
numSommet = 0;
/* Tant qu il y a une ligne a lire */
while (fgets(ligne_lue, 512, fichier) != NULL) {
pos = strchr(ligne_lue, 35); /* 35 = # */
if (pos == NULL) {
/* initialisation des differents tableaux et indices */
sommet = (char *) malloc(sizeof(char));
nomTache = (char *) malloc(sizeof(char));
dureeChar = (char *) malloc(sizeof(char));
indiceSommet = 0;
indiceNomTache = 0;
indiceDureeTache = 0;
numSommet++;
champs = 1;
trouve = FALSE;
nb = 0;
for(i = 0; i < 512 && trouve == FALSE ; i++){
/* si on a traite les 3 premiers champs ou si la fin de ligne est atteinte */
if ((champs > 3) || (ligne_lue[i] == '\0')) {
trouve = TRUE;
} else {
if (ligne_lue[i] != ',') {
switch (champs) {
case 1 :
/* on traite le 1er champs */
caractere = ligne_lue[i];
if(caractere != ' ') {
sommet[indiceSommet] = caractere;
nb++;
indiceSommet++;
if(ligne_lue[i+1] != ',' && ligne_lue[i+1] != ' '){
sommet = realloc(sommet, indiceSommet+1 * sizeof(char));
}
}
break;
case 2 :
/* on traite 2eme champs */
nomTache[indiceNomTache] = ligne_lue[i];
indiceNomTache++;
nomTache = realloc(nomTache, indiceNomTache+1);
break;
case 3 :
/* on traite 3eme champs */
dureeChar[indiceDureeTache] = ligne_lue[i];
indiceDureeTache++;
dureeChar = realloc(dureeChar, indiceDureeTache+1);
break;
default :
break;
}
} else {
/* si on lit une virgule on lit le champs suivant */
champs++;
}
}
}
/* on affecte la valeur des 3 champs au sommet dans le PERT */
char *buffer = strncpy(buffer, sommet, nb);
pert->sommets[numSommet - 1] = buffer;
free(buffer);
pert->intitule[numSommet - 1] = nomTache;
duree = atoi(dureeChar);
insertionSommetPert(pert, numSommet, duree);
free(sommet);
free(nomTache);
free(dureeChar);
}
}
fclose(fichier);
}
return pert;
} |
Partager