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