1 |
|
5 |
//------------------------------------------------------------
|
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 "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 |
|
|
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(const char *code,char *mask,class PARS_ARGUMENT *arg)
|
75 |
|
|
{
|
76 |
|
|
decode((char*)code,mask,arg);
|
77 |
|
|
}
|
78 |
|
|
|
79 |
|
|
void PARSE::decode(char *code,char *mask2,PARS_ARGUMENT *arg)
|
80 |
|
|
{
|
81 |
|
|
int nb_arg;
|
82 |
|
|
char **param;
|
83 |
|
|
|
84 |
|
|
|
85 |
|
|
char *mask;
|
86 |
|
|
mask=new char[strlen(mask2)+2];
|
87 |
|
|
strcpy(mask,mask2);
|
88 |
|
|
char *ptrM=mask;
|
89 |
|
|
int p_arg=0;
|
90 |
|
|
while (*ptrM)
|
91 |
|
|
{
|
92 |
|
|
if ((*ptrM=='@') || (*ptrM=='£') || (*ptrM=='&') || (*ptrM=='~')) p_arg++;
|
93 |
|
|
ptrM++;
|
94 |
|
|
}
|
95 |
|
|
nb_arg=p_arg;
|
96 |
|
|
int *type_param=new int[nb_arg];
|
97 |
|
|
|
98 |
|
|
p_arg=0;
|
99 |
|
|
ptrM=mask;
|
100 |
|
|
while (*ptrM)
|
101 |
|
|
{
|
102 |
|
|
if ((*ptrM=='@') || (*ptrM=='&') || (*ptrM=='~') || (*ptrM=='£') )
|
103 |
|
|
{
|
104 |
|
|
if (*ptrM=='@') type_param[p_arg]=SIMPLE;
|
105 |
|
|
if (*ptrM=='&') type_param[p_arg]=LISTE_VIRGULE;
|
106 |
|
|
if (*ptrM=='~') type_param[p_arg]=LISTE_ESPACE;
|
107 |
|
|
if (*ptrM=='£') type_param[p_arg]=LISTE_PVIRGULE;
|
108 |
|
|
p_arg++;
|
109 |
|
|
*ptrM='@';
|
110 |
|
|
}
|
111 |
|
|
ptrM++;
|
112 |
|
|
}
|
113 |
|
|
|
114 |
|
|
param=new char*[nb_arg];
|
115 |
|
|
long lg=strlen(code)+1;
|
116 |
|
|
for (int i=0;i<nb_arg;i++)
|
117 |
|
|
{
|
118 |
|
|
param[i]=new char[lg];
|
119 |
|
|
strcpy(param[i],"");
|
120 |
|
|
arg[i].argument.clear();
|
121 |
|
|
}
|
122 |
|
|
Match (code,mask,param);
|
123 |
|
|
for (int i=0;i<nb_arg;i++)
|
124 |
|
|
arg[i].argument.insert(arg[i].argument.end(),param[i]);
|
125 |
|
|
for (int i=0;i<nb_arg;i++)
|
126 |
|
|
if (type_param[i]!=SIMPLE)
|
127 |
|
|
{
|
128 |
|
|
char *param_tmp[2];
|
129 |
|
|
param_tmp[0]=new char[lg];
|
130 |
|
|
param_tmp[1]=new char[lg];
|
131 |
|
|
strcpy(param_tmp[0],arg[i].argument[0].c_str());
|
132 |
|
|
arg[i].argument.clear();
|
133 |
|
|
int code;
|
134 |
|
|
char *p=param_tmp[0];
|
135 |
|
|
do
|
136 |
|
|
{
|
137 |
|
|
if (type_param[i]==LISTE_VIRGULE) code=Match(p,"@,@",&(param_tmp[0]));
|
138 |
|
|
if (type_param[i]==LISTE_ESPACE) code=Match(p,"@ @",&(param_tmp[0]));
|
139 |
|
|
if (type_param[i]==LISTE_PVIRGULE) code=Match(p,"@;@",&(param_tmp[0]));
|
140 |
|
|
// if (code!=VRAI) continue;
|
141 |
|
|
arg[i].argument.insert(arg[i].argument.end(),param_tmp[0]);
|
142 |
|
|
strcpy(param_tmp[0],"");
|
143 |
|
|
p=param_tmp[1];
|
144 |
|
|
}
|
145 |
|
|
while (code==VRAI);
|
146 |
|
|
delete [] param_tmp[0];
|
147 |
|
|
delete [] param_tmp[1];
|
148 |
|
|
}
|
149 |
|
|
|
150 |
|
|
for (int i=0;i<nb_arg;i++)
|
151 |
|
|
delete [] param[i];
|
152 |
|
|
delete [] param;
|
153 |
|
|
delete [] type_param;
|
154 |
|
|
delete [] mask;
|
155 |
|
|
}
|
156 |
|
|
|
157 |
|
|
|
158 |
|
|
|
159 |
|
|
|
160 |
|
|
int PARSE::Match(char *code,char *mask,char **param)
|
161 |
|
|
{
|
162 |
|
|
int i=0;
|
163 |
|
|
char *ptrC,*ptrM,*ptrP;
|
164 |
|
|
|
165 |
|
|
ptrC = code;
|
166 |
|
|
ptrM = mask;
|
167 |
|
|
while(*ptrM && *ptrC)
|
168 |
|
|
{
|
169 |
|
|
if(*ptrM == '@')
|
170 |
|
|
{
|
171 |
|
|
++ptrM;
|
172 |
|
|
ptrP = param[i];
|
173 |
|
|
if(!*ptrM)
|
174 |
|
|
while(*ptrC) *ptrP++ = *ptrC++;
|
175 |
|
|
else
|
176 |
|
|
{
|
177 |
|
|
if(!MatchC(&ptrC,&ptrP,*ptrM)) return(FAUX);
|
178 |
|
|
else --ptrP;
|
179 |
|
|
}
|
180 |
|
|
*ptrP = 0;
|
181 |
|
|
++i;
|
182 |
|
|
}
|
183 |
|
|
else
|
184 |
|
|
{
|
185 |
|
|
if(*ptrM != *ptrC) return(FAUX);
|
186 |
|
|
++ptrC;
|
187 |
|
|
}
|
188 |
|
|
if(*ptrM) ++ptrM;
|
189 |
|
|
}
|
190 |
|
|
if(*ptrM) return(FAUX);
|
191 |
|
|
return(VRAI);
|
192 |
|
|
}
|
193 |
|
|
|
194 |
|
|
int PARSE::MatchC(char **ptrC,char **ptrP,char cFin)
|
195 |
|
|
{
|
196 |
|
|
char *lPtrC,*lPtrP;
|
197 |
|
|
int noOuverture;
|
198 |
|
|
|
199 |
|
|
lPtrC = *ptrC;
|
200 |
|
|
lPtrP = *ptrP;
|
201 |
|
|
|
202 |
|
|
while(*lPtrC != cFin && *lPtrC)
|
203 |
|
|
{
|
204 |
|
|
if((noOuverture = Ouverture(*lPtrC)) == ERREUR)
|
205 |
|
|
*lPtrP++ = *lPtrC++;
|
206 |
|
|
else
|
207 |
|
|
{
|
208 |
|
|
*lPtrP++ = *lPtrC++;
|
209 |
|
|
if(!MatchC(&lPtrC,&lPtrP,Fermeture(noOuverture))) return(FAUX);
|
210 |
|
|
}
|
211 |
|
|
}
|
212 |
|
|
if (!*lPtrC)
|
213 |
|
|
{
|
214 |
|
|
*lPtrP++='\0';
|
215 |
|
|
return(FAUX);
|
216 |
|
|
}
|
217 |
|
|
*lPtrP++ = *lPtrC++;
|
218 |
|
|
*ptrC = lPtrC;
|
219 |
|
|
*ptrP = lPtrP;
|
220 |
|
|
return(VRAI);
|
221 |
|
|
}
|
222 |
|
|
|
223 |
|
|
int PARSE::Ouverture(char c)
|
224 |
|
|
{
|
225 |
|
|
static char *ouverture = "([{";
|
226 |
|
|
char *ptr;
|
227 |
|
|
|
228 |
|
|
ptr = ouverture;
|
229 |
|
|
while(*ptr)
|
230 |
|
|
if(*ptr == c) return(ptr-ouverture);
|
231 |
|
|
else ++ptr;
|
232 |
|
|
return(ERREUR);
|
233 |
|
|
}
|
234 |
|
|
|
235 |
|
|
char PARSE::Fermeture(int i)
|
236 |
|
|
{
|
237 |
|
|
static char *fermeture = ")]}";
|
238 |
|
|
|
239 |
|
|
return(fermeture[i]);
|
240 |
|
|
}
|