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
| #include <stdlib.h>
#include <string.h>
#include <stdio.h>
char *codePhone(char *source)
{
// Déclaration
int longSource;
int decal;
char *reg_exp;
int spos, epos;
int err;
int maxLine;
int numLin;
phoneComment[0]=0;
maxLine=codePhon[nivPhone].maxlinePhon;
for(numLin=0 ; numLin<maxLine ; numLin++)
{
int match=0;
reg_exp=codePhon[nivPhone].listePhon[numLin].ciblePhon;
/*************************************************/
match=0;
while(match==0)
{
regex_t preg;
err = regcomp(&preg,reg_exp, REG_EXTENDED); // compilation du motif avec recherche d'erreur
if(err == 0) // pas d'erreur dans le motif
{
size_t nmatch=0;
regmatch_t *pmatch = NULL;
nmatch = preg.re_nsub; // Nombre d'occurence du motif trouvé
pmatch = malloc (sizeof(*pmatch) * nmatch);
match = regexec (&preg,source, nmatch, pmatch, 0); // Lancer la recherche
regfree (&preg); // libérer la mémoire
/*****************************/
/* le motif a été trouvé */
/*****************************/
if(match == 0)
{
char *newPhon = NULL;
int k;
spos = pmatch[0].rm_so;
epos = pmatch[0].rm_eo;
spos=spos+codePhon[nivPhone].listePhon[numLin].startCible;
epos=spos+codePhon[nivPhone].listePhon[numLin].longCible;
longSource=strlen(source);
newPhon=codePhon[nivPhone].listePhon[numLin].newPhon;
// Suppression des caractères inutiles par décalage gauche
// remplacer boucle par stcpy (+rapide)??
decal=epos-spos-strlen(newPhon);
for(k=epos ; k<longSource ; k++)
source[k-decal]=source[k];
source[longSource-decal]=0;
// recopie de newPhon
for(k=0 ; k<strlen(newPhon) ; k++)
source[spos+k]=newPhon[k];
// Saut de lignes numLin=numLin+codePhon[nivPhone].listePhon[numLin].sautPhon;
// Commentaires strncat(phoneComment,codePhon[nivPhone].listePhon[numLin].commentPhon,strlen(codePhon[nivPhone].listePhon[numLin].commentPhon));
}
/******************************/
/* le motif n'a pas été trouvé */
/******************************/
else // if match==0
if (match == REG_NOMATCH)
{
}
else
{
/*******************************/
/* erreur dans le motif de recherche */
/*******************************/
char *text;
size_t size;
size = regerror (err, &preg, NULL, 0);
text = malloc (sizeof (*text) * size);
if (text)
{
regerror (err,&preg, text, size);
fprintf (stderr, "%s\n", text);
free (text);
}
else
{
fprintf (stderr, "Memoire insuffisante\n");
exit (EXIT_FAILURE);
}
} // fin test if match==0
} // fin while (match==0)
} // fin test err==0
} // fin boucle numlin
return source;
} |
Partager