1 |
//####//------------------------------------------------------------ |
2 |
//####//------------------------------------------------------------ |
3 |
//####// MAGiC |
4 |
//####// Jean Christophe Cuilliere et Vincent FRANCOIS |
5 |
//####// Departement de Genie Mecanique - UQTR |
6 |
//####//------------------------------------------------------------ |
7 |
//####// MAGIC est un projet de recherche de l equipe ERICCA |
8 |
//####// du departement de genie mecanique de l Universite du Quebec a Trois Rivieres |
9 |
//####// http://www.uqtr.ca/ericca |
10 |
//####// http://www.uqtr.ca/ |
11 |
//####//------------------------------------------------------------ |
12 |
//####//------------------------------------------------------------ |
13 |
//####// |
14 |
//####// parse.cpp |
15 |
//####// |
16 |
//####//------------------------------------------------------------ |
17 |
//####//------------------------------------------------------------ |
18 |
//####// COPYRIGHT 2000-2024 |
19 |
//####// jeu 13 jun 2024 11:54:00 EDT |
20 |
//####//------------------------------------------------------------ |
21 |
//####//------------------------------------------------------------ |
22 |
|
23 |
|
24 |
|
25 |
|
26 |
#include <string> |
27 |
#include <string.h> |
28 |
#include "parse.h" |
29 |
|
30 |
#include "pars_argument.h" |
31 |
|
32 |
#define VRAI 1 |
33 |
#define FAUX 0 |
34 |
#define SIMPLE 103 |
35 |
#define LISTE_VIRGULE 100 |
36 |
#define LISTE_PVIRGULE 101 |
37 |
#define LISTE_ESPACE 102 |
38 |
#define ERREUR -1 |
39 |
|
40 |
|
41 |
PARSE::PARSE() |
42 |
{ |
43 |
} |
44 |
|
45 |
PARSE::~PARSE() |
46 |
{ |
47 |
} |
48 |
|
49 |
|
50 |
std::string PARSE::lire(FILE *in,char fin,int *ierr) |
51 |
{ |
52 |
char c; |
53 |
std::string chainelue=""; |
54 |
*ierr=0; |
55 |
do |
56 |
{ |
57 |
size_t res=fread(&c,sizeof(char),1,in); |
58 |
if (feof(in)) |
59 |
{ |
60 |
*ierr=1; |
61 |
return ""; |
62 |
} |
63 |
if (c>31) chainelue+=c; |
64 |
} |
65 |
while (c!=fin); |
66 |
return chainelue; |
67 |
} |
68 |
|
69 |
|
70 |
|
71 |
void PARSE::decode(char *code,std::string masque,class PARS_ARGUMENT *arg) |
72 |
{ |
73 |
char* mask=(char *)masque.c_str(); |
74 |
decode_old(code,mask,arg); |
75 |
} |
76 |
|
77 |
void PARSE::decode(const char *code,std::string masque,class PARS_ARGUMENT *arg) |
78 |
{ |
79 |
char* mask=(char *)masque.c_str(); |
80 |
decode_old((char*)code,mask,arg); |
81 |
} |
82 |
|
83 |
void PARSE::decode_old(const char *code,char *mask,class PARS_ARGUMENT *arg) |
84 |
{ |
85 |
decode((char*)code,mask,arg); |
86 |
} |
87 |
|
88 |
void PARSE::decode_old(char *code,char *mask2,PARS_ARGUMENT *arg) |
89 |
{ |
90 |
int nb_arg; |
91 |
char **param; |
92 |
|
93 |
|
94 |
char *mask; |
95 |
mask=new char[strlen(mask2)+2]; |
96 |
strcpy(mask,mask2); |
97 |
char *ptrM=mask; |
98 |
int p_arg=0; |
99 |
while (*ptrM) |
100 |
{ |
101 |
if ((*ptrM=='@') || (*ptrM=='?') || (*ptrM=='&') || (*ptrM=='~')) p_arg++; |
102 |
ptrM++; |
103 |
} |
104 |
nb_arg=p_arg; |
105 |
int *type_param=new int[nb_arg]; |
106 |
|
107 |
p_arg=0; |
108 |
ptrM=mask; |
109 |
while (*ptrM) |
110 |
{ |
111 |
if ((*ptrM=='@') || (*ptrM=='&') || (*ptrM=='~') || (*ptrM=='?') ) |
112 |
{ |
113 |
if (*ptrM=='@') type_param[p_arg]=SIMPLE; |
114 |
if (*ptrM=='&') type_param[p_arg]=LISTE_VIRGULE; |
115 |
if (*ptrM=='~') type_param[p_arg]=LISTE_ESPACE; |
116 |
if (*ptrM=='?') type_param[p_arg]=LISTE_PVIRGULE; |
117 |
p_arg++; |
118 |
*ptrM='@'; |
119 |
} |
120 |
ptrM++; |
121 |
} |
122 |
|
123 |
param=new char*[nb_arg]; |
124 |
long lg=strlen(code)+1; |
125 |
for (int i=0;i<nb_arg;i++) |
126 |
{ |
127 |
param[i]=new char[lg]; |
128 |
strcpy(param[i],""); |
129 |
arg[i].argument.clear(); |
130 |
} |
131 |
Match (code,mask,param); |
132 |
for (int i=0;i<nb_arg;i++) |
133 |
arg[i].argument.insert(arg[i].argument.end(),param[i]); |
134 |
for (int i=0;i<nb_arg;i++) |
135 |
if (type_param[i]!=SIMPLE) |
136 |
{ |
137 |
char *param_tmp[2]; |
138 |
param_tmp[0]=new char[lg]; |
139 |
param_tmp[1]=new char[lg]; |
140 |
strcpy(param_tmp[0],arg[i].argument[0].c_str()); |
141 |
arg[i].argument.clear(); |
142 |
int code; |
143 |
char *p=param_tmp[0]; |
144 |
do |
145 |
{ |
146 |
char format[4]={'@',',','@',0}; |
147 |
if (type_param[i]==LISTE_VIRGULE) code=Match(p,format,&(param_tmp[0])); |
148 |
if (type_param[i]==LISTE_ESPACE) code=Match(p,format,&(param_tmp[0])); |
149 |
if (type_param[i]==LISTE_PVIRGULE) code=Match(p,format,&(param_tmp[0])); |
150 |
arg[i].argument.insert(arg[i].argument.end(),param_tmp[0]); |
151 |
strcpy(param_tmp[0],""); |
152 |
p=param_tmp[1]; |
153 |
} |
154 |
while (code==VRAI); |
155 |
delete [] param_tmp[0]; |
156 |
delete [] param_tmp[1]; |
157 |
} |
158 |
|
159 |
for (int i=0;i<nb_arg;i++) |
160 |
delete [] param[i]; |
161 |
delete [] param; |
162 |
delete [] type_param; |
163 |
delete [] mask; |
164 |
} |
165 |
|
166 |
|
167 |
|
168 |
|
169 |
int PARSE::Match(char *code,char *mask,char **param) |
170 |
{ |
171 |
int i=0; |
172 |
char *ptrC,*ptrM,*ptrP; |
173 |
|
174 |
ptrC = code; |
175 |
ptrM = mask; |
176 |
while (*ptrM && *ptrC) |
177 |
{ |
178 |
if (*ptrM == '@') |
179 |
{ |
180 |
++ptrM; |
181 |
ptrP = param[i]; |
182 |
if (!*ptrM) |
183 |
while (*ptrC) *ptrP++ = *ptrC++; |
184 |
else |
185 |
{ |
186 |
if (!MatchC(&ptrC,&ptrP,*ptrM)) return(FAUX); |
187 |
else --ptrP; |
188 |
} |
189 |
*ptrP = 0; |
190 |
++i; |
191 |
} |
192 |
else |
193 |
{ |
194 |
if (*ptrM != *ptrC) return(FAUX); |
195 |
++ptrC; |
196 |
} |
197 |
if (*ptrM) ++ptrM; |
198 |
} |
199 |
if (*ptrM) return(FAUX); |
200 |
return(VRAI); |
201 |
} |
202 |
|
203 |
int PARSE::MatchC(char **ptrC,char **ptrP,char cFin) |
204 |
{ |
205 |
char *lPtrC,*lPtrP; |
206 |
int noOuverture; |
207 |
|
208 |
lPtrC = *ptrC; |
209 |
lPtrP = *ptrP; |
210 |
|
211 |
while (*lPtrC != cFin && *lPtrC) |
212 |
{ |
213 |
if ((noOuverture = Ouverture(*lPtrC)) == ERREUR) |
214 |
*lPtrP++ = *lPtrC++; |
215 |
else |
216 |
{ |
217 |
*lPtrP++ = *lPtrC++; |
218 |
if (!MatchC(&lPtrC,&lPtrP,Fermeture(noOuverture))) return(FAUX); |
219 |
} |
220 |
} |
221 |
if (!*lPtrC) |
222 |
{ |
223 |
*lPtrP++='\0'; |
224 |
return(FAUX); |
225 |
} |
226 |
*lPtrP++ = *lPtrC++; |
227 |
*ptrC = lPtrC; |
228 |
*ptrP = lPtrP; |
229 |
return(VRAI); |
230 |
} |
231 |
|
232 |
int PARSE::Ouverture(char c) |
233 |
{ |
234 |
static char ouverture[4]= {'(','[','{'}; |
235 |
char *ptr; |
236 |
|
237 |
ptr = ouverture; |
238 |
while (*ptr) |
239 |
if (*ptr == c) return(ptr-ouverture); |
240 |
else ++ptr; |
241 |
return(ERREUR); |
242 |
} |
243 |
|
244 |
char PARSE::Fermeture(int i) |
245 |
{ |
246 |
static char fermeture[4] = {')',']','}'}; |
247 |
|
248 |
return(fermeture[i]); |
249 |
} |