ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/REPOS_ERICCA/magic/lib/parseur/src/parse.cpp
Revision: 252
Committed: Tue Jul 13 18:12:51 2010 UTC (14 years, 10 months ago) by francois
File size: 5449 byte(s)
Log Message:
test nouvelle hiearchie

File Contents

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