Salome HOME
Merge from BR_V5_DEV 16Feb09
[plugins/ghs3dprlplugin.git] / src / tepal2med / ghs3dprl_msg_parser.cxx
1 // Copyright (C) 2007-2008 OPEN CASCADE, CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // ---
20 //
21 // File   : ghs3dprl_mesh_parser.cxx
22 // Author : Christian VAN WAMBEKE (CEA) (from Hexotic plugin Lioka RAZAFINDRAZAKA)
23 //
24 // ---
25
26 #include <iostream>
27
28 #include "ghs3dprl_msg_parser.h"
29 #include "ghs3dprl_mesh_wrap.h"
30
31 using namespace med_2_2;
32
33 //************************************
34 bool ghs3dprl_msg_parser::startDocument()
35 {
36   indent="";
37   begin="distene_msg";
38   version="version";
39   neighbours="neighbours";
40   count="count";
41   neighbour="neighbour";
42   indice="indice";
43   send="send";
44   vertices="vertices";
45   edges="edges";
46   faces="faces";
47   elements="elements";
48   receive="receive";
49   etat=0;
50   neighbourscount=0;
51   neighbourcourant=0;
52   neighbourindice=0;
53   nbcount=0;
54   verbose=false; //true; //false;
55   return true;
56 }
57
58 //************************************
59 bool ghs3dprl_msg_parser::startElement(const QString &namespaceURI,
60                                        const QString &localName,
61                                        const QString &qName,
62                                        const QXmlAttributes &attrs)
63 {
64   bool ok;
65   if (verbose) std::cout<<indent.toLatin1().constData()<<qName.toLatin1().constData()<<std::endl;
66   typel=qName; shorttypel="";
67   indent += "   ";
68   if (qName==version)
69   {
70     etat=1;
71     return true;
72   }
73   
74   if (qName==neighbours)
75   {
76     etat=2;
77     if (attrs.count()>0 && attrs.localName(0)==count)
78     {
79       neighbourscount=attrs.value(0).toLong(&ok, 10);
80       neighbourcourant=-1;
81       if (verbose) std::cout<<indent.toLatin1().constData()<<attrs.localName(0).toLatin1().constData()<<
82                      "="<<neighbourscount<<std::endl;
83     }
84     return true;
85   }
86   
87   if (qName==neighbour)
88   {
89     etat=3;
90     if (attrs.count()>0 && attrs.localName(0)==indice)
91     {
92       neighbourindice=attrs.value(0).toLong(&ok, 10);
93       neighbourcourant++;
94       //mailw.neighbourindices[mailw.neighbourcourant]=neighbourindice;
95       if (verbose) std::cout<<indent.toLatin1().constData()<<attrs.localName(0).toLatin1().constData()<<
96                      "="<<neighbourindice<<std::endl;
97     }
98     return true;
99   }
100   
101   if (qName==send)
102   {
103     etat=4;
104     sendreceive="s/r=send ";
105     shortsendreceive="SE ";
106     return true;
107   }
108   
109   if (qName==receive)
110   {
111     etat=9;
112     sendreceive="s/r=receive ";
113     shortsendreceive="RE ";
114     return true;
115   }
116   
117   if (qName==vertices)
118   {
119     etat=5;
120     shorttypel="VE ";
121     if (attrs.count()>0 && attrs.localName(0)==count)
122     {
123       nbcount=attrs.value(0).toLong(&ok, 10);
124       if (verbose) std::cout<<indent.toLatin1().constData()<<attrs.localName(0).toLatin1().constData()<<
125                      "="<<nbcount<<std::endl;
126     }
127     return true;
128   }
129   
130   if (qName==edges)
131   {
132     etat=6;
133     shorttypel="ED ";
134     if (attrs.count()>0 && attrs.localName(0)==count)
135     {
136       nbcount=attrs.value(0).toLong(&ok, 10);
137       if (verbose) std::cout<<indent.toLatin1().constData()<<attrs.localName(0).toLatin1().constData()<<
138                      "="<<nbcount<<std::endl;
139     }
140     return true;
141   }
142   
143   if (qName==faces)
144   {
145     etat=7;
146     shorttypel="FA ";
147     if (attrs.count()>0 && attrs.localName(0)==count)
148     {
149       nbcount=attrs.value(0).toLong(&ok, 10);
150       if (verbose) std::cout<<indent.toLatin1().constData()<<attrs.localName(0).toLatin1().constData()<<
151                      "="<<nbcount<<std::endl;
152     }
153     return true;
154   }
155   
156   if (qName==elements)
157   {
158     etat=8;
159     shorttypel="EL ";
160     if (attrs.count()>0 && attrs.localName(0)==count)
161     {
162       nbcount=attrs.value(0).toLong(&ok, 10);
163       if (verbose) std::cout<<indent.toLatin1().constData()<<attrs.localName(0).toLatin1().constData()<<
164                      "="<<nbcount<<std::endl;
165     }
166     return true;
167   }
168   
169   /*for( int i=0; i<attrs.count(); i++ )
170   {
171     std::cout<<indent.toLatin1().constData()<<attrs.localName(i).toLatin1().constData()<<"="<<attrs.value(i)<<std::endl;
172   }*/
173   
174   return true;
175 }
176
177 //************************************
178 bool ghs3dprl_msg_parser::endElement(const QString &namespaceURI,
179                                      const QString &localName,
180                                      const QString &qName)
181 {
182   indent.remove((uint)0,3);
183   if (verbose) std::cout<<indent.toLatin1().constData()<<"\\"<<qName.toLatin1().constData()<<std::endl;
184   nbcount=0; //precaution
185   if (qName==send || qName==receive)
186   {
187     sendreceive="";
188     shortsendreceive="";
189     return true;
190   }
191   if (qName==begin)
192   {
193     mailw->nbfiles++;
194     return true;
195   }
196   return true;
197 }
198
199 //************************************
200 bool ghs3dprl_msg_parser::characters(const QString &strini)
201 {
202   bool ok;
203   med_int *tmint=NULL;
204   long nb=0;
205   //filtre rc,lf,tab et blancs successifs
206   QString str=strini.simplified();
207   //if ( str.length() == 1 && str=="\n" )
208   if (str.length()==0)
209   {
210     //std::cout<<"EMPTY_LINE_CR"<<std::endl;
211     return true;
212   }
213   else
214   {
215     if (etat==1)
216     {
217       double ver=str.toDouble(&ok);
218       if (!ok || ver!=1e0)
219       {
220         std::cout<<"version "<<str.toLatin1().constData()<<" fichier .msg inconnue"<<std::endl;
221         return false;
222       }
223       else return true;
224     }
225     nb=str.count(' ',Qt::CaseSensitive) + 1; //nb chiffres detectes
226     if (nb>1)
227     {
228       //lecture vecteurs d'entiers separateur blanc
229       long i=0;
230       tmint=new med_int[nb];
231       //printf("%staille attendue=%i taille vue=%i\n",(const char *)indent.toLatin1().constData(),nbcount,nb);
232       do
233       {
234         tmint[i]=str.section(' ',i,i).toLong(&ok);
235         //printf("tmint[%i]=%i\n",i,tmint[i]);
236         i++;
237       } while ((i<nb) && ok );
238       if (i<nb)
239       {
240         std::cout<<"pb conversion "<<i<<" eme entier="<<str.section(' ',i-1,i-1).toLatin1().constData()<<std::endl;
241         std::cout<<indent.toLatin1().constData()<<"etat="<<etat<<" nb="<<nb<<std::endl<<str.toLatin1().constData()<<std::endl;
242       }
243     }
244     if (nb==1)
245     {
246       tmint=new med_int[nb];
247       tmint[0]=str.toLong(&ok);
248       if (!ok)
249       {
250         std::cout<<"pb conversion 1er entier="<<str.toLatin1().constData()<<std::endl;
251         std::cout<<indent.toLatin1().constData()<<"etat="<<etat<<" nb="<<nb<<std::endl;
252       }
253     }
254     
255   }
256   //lecture ok stockage de tlong dans mailw.mestab
257   CVWtab *montab=new CVWtab(nb,tmint);
258   QString tmp;
259   /*std::cout<<"InsertKey type="<<typel<<" "<<sendreceive<<
260     " neighbour="<<neighbourindice<<
261     " file="<<mailw->nofile<<std::endl;*/
262   tmp=tmp.sprintf("MS%ld NE%ld ",
263                   mailw->nofile,neighbourindice)+
264     shorttypel+shortsendreceive;
265   tmp=tmp.simplified();
266   ok=mailw->insert_key(tmp,montab);
267   return true;
268 }
269