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
|
char *str_replace (const char *txt, const char *Avant, const char *Apres)
{
const char *pos; /* Position d'une occurance de Avant dans txt */
char *TxtRetour; /* La chaine retournée */
size_t PosTxtRetour; /* Position du prochain caractère à écrire */
/* dans TxtRetour */
size_t Long; /* Long d'une chaine à écrire dans TxtRetour */
size_t TailleAllouee; /* Taille allouée à TxtRetour */
/* Cherche la première occurence */
pos = strstr (txt, Avant);
/* Aucune occurrences : renvoie simplement une copie de la chaine */
if (pos == NULL)
{
return NULL;
}
/* Alloue une nouvelle chaine */
Long = (size_t)pos - (size_t)txt;
TailleAllouee = Long + strlen (Apres) + 1;
TxtRetour = malloc (TailleAllouee);
PosTxtRetour = 0;
/* Copie la première partie de la chaîne sans occurrence */
strncpy (TxtRetour + PosTxtRetour, txt, Long);
PosTxtRetour += Long;
txt = pos + strlen (Avant);
/* Ajoute la chaîne de remplacement Apres */
Long = strlen (Apres);
strncpy (TxtRetour + PosTxtRetour, Apres, Long);
PosTxtRetour += Long;
/* Cherche la prochaine occurrence */
pos = strstr (txt, Avant);
while (pos != NULL)
{
/* Agrandit la chaine */
Long = (size_t)pos - (size_t)txt;
TailleAllouee += Long + strlen (Apres);
TxtRetour = (char *)realloc (TxtRetour, TailleAllouee);
/* Copie ce qu'il y a entre la dernier occurrence et la nouvelle */
strncpy (TxtRetour + PosTxtRetour, txt, Long);
PosTxtRetour += Long;
/* Passe l'occurrence */
txt = pos + strlen (Avant);
/* Ajoute la chaîne de remplacement */
Long = strlen (Apres);
strncpy (TxtRetour + PosTxtRetour, Apres, Long);
PosTxtRetour += Long;
/* Cherche la prochaine occurrence */
pos = strstr (txt, Avant);
}
/* Ajoute le reste de la chaîne (il reste au moins '\0') */
Long = strlen (txt) + 1;
TailleAllouee += Long;
TxtRetour = realloc (TxtRetour, TailleAllouee);
strncpy (TxtRetour + PosTxtRetour, txt, Long);
return TxtRetour;
}
int copyFileWithModif (char const *fileName, char const *fileNameCreate, char **chaineAChercher, char **chaineARemplacer){
int ret = EXIT_SUCCESS;
FILE *depart = fopen (fileName, "r");
if (depart != NULL){
FILE *arrivee = fopen (fileNameCreate, "w+");
if (arrivee != NULL){
char ligne[TAILLE_MAX] = "";
char *ligne2,*tmp;
while (fgets (ligne, TAILLE_MAX, depart) != NULL){
tmp=malloc(strlen(ligne)*sizeof(char));
strcpy(tmp,ligne);
int i = 0;
while((chaineAChercher[i] != NULL) && (chaineARemplacer[i] != NULL)){
ligne2 = str_replace(tmp,chaineAChercher[i],chaineARemplacer[i]);
if(ligne2 != NULL){
tmp = malloc(strlen(ligne2)*sizeof(char));
strcpy(tmp,ligne2);
}
i++;
}
fputs(tmp,arrivee);
}
fclose (arrivee);
}else{
printf ("ERREUR: Impossible d'ouvrir le fichier: %s.\n",fileNameCreate);
ret = EXIT_FAILURE;
}
fclose (depart);
}else{
perror (fileName);
ret = EXIT_FAILURE;
}
return ret;
} |
Partager