Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int FVAjoutFichier(const char* Info,FICHIERVAR Fich)
- {
- ENREG E;
- int Pos;
- Trace("Entree dans FVAjoutFich() --%s--%d",Info,strlen(Info));
- char* pInter = Info;
- //Pos = PremierLibre;
- // verification si place ==> Remarque du prof, à faire !
- int i = 0, tailleTemp=1, tailleTotale=0, nbEnr;
- //*(Fich.Index +i) = PremierLibre;
- // boucle pour calculer le nombre d'emplacements pris puis comparer à la taille demandée pour l'enregistrement nouveau
- for(i=0; i<Fich.Taille && tailleTemp; i++)
- {
- tailleTemp = *(Fich.Index+1+(2*i));
- tailleTotale += tailleTemp;
- }
- i--; // Pour retomber à l'offset où placer dans l'index
- nbEnr = NB_ENR(strlen(Info), Fich.Taille);
- // Pas assez de place, return 0
- if(PremierLibre==0 || nbEnr > (Fich.Taille-tailleTotale-1)) return 0;
- Trace("NBENR_DEM : %2d ----- NBRESTE : %2d", nbEnr, Fich.Taille-tailleTotale-1);
- // Positionne à partir du premier emplacement libre
- Pos = PremierLibre;
- // Enregistre dans l'index l'indice de début et le nombre d'emplacments pris
- *(Fich.Index + i*2) = Pos;
- // *(Fich.Index + Pos*2 + 1) = nbEnr;
- //i++;
- while (strlen(pInter) > 20)
- {
- // Lecture de l'enregistrement (fichier => E) puis remplacement
- // des caractères par ceux du nouveau message et, enfin, écriture dans le fichier
- // ==> chainage gardé !
- if (lseek(Fich.hdF,Pos * 24,SEEK_SET) == -1) return -1;
- if (read(Fich.hdF,&E,24) != 24) return -1;
- memcpy(E.Donnee,pInter,20);
- if (lseek(Fich.hdF,Pos * 24,SEEK_SET) == -1) return -1;
- if (write(Fich.hdF,&E,24) != 24) return -1;
- Pos = E.Suivant;
- pInter += 20;
- // *(Fich.Index+i) = *(Fich.Index + i) + 1;
- // Incrémente le nombre d'emplacements utilisés
- *(Fich.Index+i*2+1) = *(Fich.Index+i*2+1) + 1;
- }
- // Pareil + Premier libre mis à jour et E.suivant indiquant fin de l'enregistrement complet
- if (lseek(Fich.hdF,Pos * 24,SEEK_SET) == -1) return -1;
- if (read(Fich.hdF,&E,24) != 24) return -1;
- PremierLibre = E.Suivant;
- memcpy(E.Donnee,pInter,20);
- E.Suivant = 0;
- if (lseek(Fich.hdF,Pos * 24,SEEK_SET) == -1) return -1;
- if (write(Fich.hdF,&E,24) != 24) return -1;
- //*(Fich.Index+i) = *(Fich.Index + i) + 1;
- *(Fich.Index+i*2+1) = *(Fich.Index+i*2+1) +1;
- return 1;
- }
Add Comment
Please, Sign In to add comment