// C++ : ParserXml
-// Copyright (C) 2009-2012 CEA/DEN, EDF R&D
+// Copyright (C) 2009-2022 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.
+// 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
enum { HEND_FILE = -2, HEND_LINE = -1, CRLF = 13 };
-// ====================================================== Constructeur
-XmlTree::XmlTree (const string& nom, XmlTree* dad)
+// ====================================================== Constructeur
+XmlTree::XmlTree (const std::string& nom, XmlTree* dad)
{
item_name = nom;
item_vide = "";
{
}
// ====================================================== findChild
-XmlTree* XmlTree::findChild (const string& nom)
+XmlTree* XmlTree::findChild (const std::string& nom)
{
for (int nro = 0 ; nro<nbr_items ; nro++)
{
return NULL;
}
// ====================================================== findAttribute
-int XmlTree::findAttribute (const string& nom)
+int XmlTree::findAttribute (const std::string& nom)
{
for (int nro = 0 ; nro<nbr_attributs ; nro++)
{
return NOTHING;
}
// ====================================================== findValue
-const string& XmlTree::findValue (const string& nom)
+const std::string& XmlTree::findValue (const std::string& nom)
{
for (int nro = 0 ; nro<nbr_attributs ; nro++)
{
}
return item_vide;
}
+// ====================================================== findInteger
+int XmlTree::findInteger (const std::string& nom)
+{
+ int val = 0;
+ const std::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)
+int XmlTree::parseFile (const std::string& ficnom)
{
fic_buffer = "";
len_buffer = 0;
xml_file = fopen (ficnom.c_str(), "r");
if (xml_file==NULL)
{
- cout << " **** Fichier XML '" << ficnom << "' inaccessible"
- << endl;
+ std::cout << " **** Fichier XML '" << ficnom << "' inaccessible"
+ << std::endl;
return HERR;
}
int ier = parseXml ();
return ier;
}
-// ====================================================== parseFlow
-int XmlTree::parseFlow (cpchar flux)
+// ====================================================== parseStream
+int XmlTree::parseStream (cpchar flux, int& posit)
{
fic_buffer = "";
len_buffer = 0;
nro_ligne = 0;
fic_pos = 1988;
- xml_flow = flux;
- pos_flow = 0;
+ xml_flow = flux;
+ pos_flow = posit;
- int ier = parseXml ();
+ int ier = parseXml ();
+ posit = pos_flow;
return ier;
}
// ====================================================== parseXml
XmlTree* node = this;
EnumItem item_lu;
- string nom, valeur, foo;
+ std::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);
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;
return HOK;
}
// ====================================================== getItem
-EnumItem XmlTree::getItem (string& value, EnumItem waited)
+EnumItem XmlTree::getItem (std::string& value, EnumItem waited)
{
EnumItem item = readItem (value);
- if (item == waited)
+ if (item == waited)
return item;
putError ("Erreur de sequence");
return item;
}
// ====================================================== readItem
-EnumItem XmlTree::readItem (string& value)
+EnumItem XmlTree::readItem (std::string& value)
{
value = "";
if (fic_pos>=len_buffer)
{
int ier=readLine ();
- if (ier==HEND_FILE)
+ if (ier==HEND_FILE)
return M_NONE;
}
else
}
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;
}
}
// ====================================================== getIdent
-int XmlTree::getIdent (string& ident)
+int XmlTree::getIdent (std::string& ident)
{
ident = "";
pos = 0;
else if (pos<nbc)
pos++;
- else
+ else
return HOK;
}
return HERR;
}
// ====================================================== getString
-int XmlTree::getString (string& chaine)
+int XmlTree::getString (std::string& chaine)
{
chaine = "";
int car = ' ';
while ((car = getChar ()) != EOF)
{
- if (car=='"')
+ if (car=='"')
return HOK;
- chaine.push_back (car);
+ chaine.push_back (car);
}
return HERR;
int ier = HEND_LINE;
while (ier==HEND_LINE)
{
- int carac = xml_flow != NULL ? xml_flow [pos_flow++]
+ int carac = xml_flow != NULL ? xml_flow [pos_flow++]
: fgetc (xml_file);
if (carac==EOL || carac==CRLF)
}
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;
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);
}
// ====================================================== addChild
-XmlTree* XmlTree::addChild (const string& nom)
+XmlTree* XmlTree::addChild (const std::string& nom)
{
XmlTree* child = new XmlTree (nom, this);
tab_items.push_back (child);
return child;
}
// ====================================================== addAttribut
-void XmlTree::addAttribut (const string& nom, const string& value)
+void XmlTree::addAttribut (const std::string& nom, const std::string& value)
{
tab_attributs.push_back (nom);
tab_values .push_back (value);
// ====================================================== dump
void XmlTree::dump (int niveau)
{
- string marge = "";
+ std::string marge = "";
for (int niv=0 ; niv<niveau ; niv++)
marge += " | ";
- cout << marge << item_name << endl;
+ std::cout << marge << item_name << std::endl;
for (int nc=0 ; nc<nbr_attributs ; nc++)
{
- cout << marge << " : "
- << tab_attributs [nc] << " = '" << tab_values [nc]
- << "'" << endl;
+ std::cout << marge << " : "
+ << tab_attributs [nc] << " = '" << tab_values [nc]
+ << "'" << std::endl;
}
for (int nc=0 ; nc<nbr_items ; nc++)