Salome HOME
Copyrights update 2015.
[modules/hexablock.git] / src / HEXABLOCK / HexXmlTree.cxx
index 7c28266e2b6b286e0f1d41b221c6d11175741d99..013927af018add9397812614e752373457649567 100755 (executable)
@@ -1,26 +1,49 @@
 
 // C++ : ParserXml
 
+// Copyright (C) 2009-2015  CEA/DEN, EDF R&D
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+
 #include "HexXmlTree.hxx"
 
 BEGIN_NAMESPACE_HEXA
 
 enum { HEND_FILE = -2, HEND_LINE = -1, CRLF = 13 };
 
-// ====================================================== Constructeur 
+// ====================================================== Constructeur
 XmlTree::XmlTree (const string& nom, XmlTree* dad)
 {
-    item_name     = nom;
-    item_vide     = "";
-    xml_parent    = dad;
-    nbr_attributs = 0;
-    nbr_items     = 0;
+   item_name     = nom;
+   item_vide     = "";
+   xml_parent    = dad;
+   nbr_attributs = 0;
+   nbr_items     = 0;
 
    fic_buffer  = "";
    len_buffer  = 0;
-   fic_xml     = NULL;
+   xml_file    = NULL;
    nro_ligne   = 0;
    fic_pos     = 1988;
+
+   xml_flow  = NULL;
+   pos_flow  = 0;
+   xml_ended = true;
 }
 // ====================================================== Destructeur
 XmlTree::~XmlTree ()
@@ -56,6 +79,23 @@ const string& XmlTree::findValue (const string& nom)
        }
    return item_vide;
 }
+// ====================================================== findInteger
+int XmlTree::findInteger (const string& nom)
+{
+   int   val = 0;
+   const string chnum = findValue (nom);
+   if (chnum==item_vide)
+      return val;
+
+   int lg = chnum.size();
+   for (int nc=0 ; nc<lg ; ++nc)
+       {
+       char car = chnum[nc];
+       if (car >= '0' && car <= '9')
+           val  = 10*val + car - '0';
+       }
+   return val;
+}
 // ====================================================== parseFile
 int XmlTree::parseFile (const string& ficnom)
 {
@@ -63,10 +103,40 @@ int XmlTree::parseFile (const string& ficnom)
    len_buffer  = 0;
    nro_ligne   = 0;
    fic_pos     = 1988;
+   xml_flow    = NULL;
+   pos_flow    = 0;
+   xml_ended   = true;
 
-   fic_xml = fopen (ficnom.c_str(), "r");
-   if (fic_xml==NULL)
+   xml_file = fopen (ficnom.c_str(), "r");
+   if (xml_file==NULL)
+      {
+      cout << " **** Fichier XML '" << ficnom << "' inaccessible"
+           << endl;
       return HERR;
+      }
+
+   int ier = parseXml ();
+   return ier;
+}
+// ====================================================== parseStream
+int XmlTree::parseStream (cpchar flux, int& posit)
+{
+   fic_buffer  = "";
+   len_buffer  = 0;
+   nro_ligne   = 0;
+   fic_pos     = 1988;
+
+   xml_flow = flux;
+   pos_flow = posit;
+
+   int ier  = parseXml ();
+   posit    = pos_flow;
+   return ier;
+}
+// ====================================================== parseXml
+int XmlTree::parseXml ()
+{
+   xml_ended  = false;
 
    enum EnumEtat { M_PREMS, M_BALISE_OUVERTE, M_NEUTRE };
    EnumEtat etat =  M_PREMS;
@@ -76,11 +146,11 @@ int XmlTree::parseFile (const string& ficnom)
    string   nom, valeur, foo;
    while ((item_lu=readItem (nom)) != M_NONE)
       {
-      switch (item_lu) 
+      switch (item_lu)
          {
          case M_BEGIN :
               item_lu = getItem (nom, M_IDENT);
-              if (etat==M_PREMS) 
+              if (etat==M_PREMS)
                  setName (nom);
               else
                  node = node -> addChild  (nom);
@@ -88,21 +158,23 @@ int XmlTree::parseFile (const string& ficnom)
               break;
 
          case M_END   :
-              if (etat != M_BALISE_OUVERTE) 
+              if (etat != M_BALISE_OUVERTE)
                  putError (" balise ouverte");
               etat = M_NEUTRE;
               break;
 
          case M_BEGIN_CLOSE :
-              if (etat == M_BALISE_OUVERTE) 
+              if (etat == M_BALISE_OUVERTE)
                  putError (" balise ouverte");
               getItem (nom, M_IDENT);
               getItem (foo, M_END);
               node = node -> getParent ();
+              if (node==NULL)
+                 return HOK;
               break;
 
          case M_CLOSE :
-              if (etat != M_BALISE_OUVERTE) 
+              if (etat != M_BALISE_OUVERTE)
                  putError (" balise deja fermee");
               node = node -> getParent ();
               etat = M_NEUTRE;
@@ -135,7 +207,7 @@ EnumItem XmlTree::getItem (string& value, EnumItem waited)
 {
    EnumItem item = readItem (value);
 
-   if (item == waited) 
+   if (item == waited)
       return item;
    putError ("Erreur de sequence");
    return item;
@@ -150,7 +222,7 @@ EnumItem XmlTree::readItem (string& value)
          if (fic_pos>=len_buffer)
             {
             int ier=readLine ();
-            if (ier==HEND_FILE) 
+            if (ier==HEND_FILE)
                return M_NONE;
             }
          else
@@ -180,17 +252,17 @@ EnumItem XmlTree::readItem (string& value)
                }
             else if (car=='<')
                {
-               if (ncar=='/') 
+               if (ncar=='/')
                   {
                   fic_pos++;
                   return M_BEGIN_CLOSE;
                   }
-               else if (ncar=='?') 
+               else if (ncar=='?')
                   {
                   fic_pos++;
                   return M_PROLOG;
                   }
-               else if (ncar=='!') 
+               else if (ncar=='!')
                   {
                   fic_pos++;
                   return M_COMMENT;
@@ -209,7 +281,7 @@ int XmlTree::getIdent (string& ident)
    for (int nc=fic_pos-1; nc<len_buffer ; nc++)
        {
        char car = fic_buffer[nc];
-       if (isalnum (car))
+       if (isalnum (car) || car =='_')
            {
            ident.push_back (car);
            }
@@ -236,7 +308,7 @@ int XmlTree::goTo (cpchar ouca)
           pos = 0;
        else if (pos<nbc)
           pos++;
-       else 
+       else
           return HOK;
        }
 
@@ -250,9 +322,9 @@ int XmlTree::getString (string& chaine)
 
    while ((car = getChar ()) != EOF)
        {
-       if (car=='"') 
+       if (car=='"')
           return HOK;
-       chaine.push_back (car); 
+       chaine.push_back (car);
        }
 
    return HERR;
@@ -281,27 +353,35 @@ int XmlTree::readLine ()
    fic_buffer = "";
    fic_pos    = 0;
 
-   if (fic_xml==NULL || feof (fic_xml))
+   if (xml_ended)
        return HEND_FILE;
 
    len_buffer = 0;
    int ier = HEND_LINE;
    while (ier==HEND_LINE)
          {
-         int carac = fgetc (fic_xml);
+         int carac = xml_flow != NULL ? xml_flow [pos_flow++]
+                                     : fgetc (xml_file);
+
          if (carac==EOL || carac==CRLF)
             {
             ier = HOK;
             }
-         else if (carac!=EOF)
+         else if (carac!=EOF && carac!=EOS)
             {
-            fic_buffer.push_back (carac); 
+            fic_buffer.push_back (carac);
             len_buffer ++;
             }
          else if (len_buffer > 0)
+            {
+            xml_ended = true;
             ier = HOK;
-         else 
+            }
+         else
+            {
+            xml_ended = true;
             ier = HEND_FILE;
+            }
          }
    return HOK;
 }
@@ -309,7 +389,7 @@ int XmlTree::readLine ()
 void XmlTree::putError (cpchar mess)
 {
      printf (" ***** Erreur : %s\n", mess);
-     printf (" +++ Derniere ligne lue : nro %d, %deme caractere\n", 
+     printf (" +++ Derniere ligne lue : nro %d, %deme caractere\n",
                nro_ligne, fic_pos);
      printf ("%s\n", fic_buffer.c_str());
      //  exit (102);
@@ -341,8 +421,8 @@ void XmlTree::dump (int niveau)
 
    for (int nc=0 ; nc<nbr_attributs ; nc++)
        {
-       cout << marge  << " : " 
-            << tab_attributs [nc] << " = '" << tab_values [nc] 
+       cout << marge  << " : "
+            << tab_attributs [nc] << " = '" << tab_values [nc]
             << "'" << endl;
        }