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